C Program to show different formatting in output using printf
This program shows different output format specifiers present in C programming language. To show a few of its implementation.
- Printing without any formatting
- Printing preceeding 4 white spaces
- Printing with preceeding zeros
- Printing integer format (floating point is removed)
- Printing integer format with white spaces
- Printing value with its exponent
- Printing 3 digits before and 2 digits after decimal
- Printing lower case exponent
- Printing upper case exponent
Program
#include<stdio.h>
void main()
{
int a, b;
float c, d, e;
a = 22;
printf("Printing without formatting:\t%d\n", a);
printf("Preceeding four white spaces:\t%4d\n", a);
printf("Preceeding zeros:\t%04i\n", a);
b = a / 7;
printf("Integer format not printing decimal:\t%d\n", b);
printf("Preceeding three white spaces:\t%3d\n", b);
c = 26.9;
d = c / 3;
printf("Printing value in exponent form:\t%e\n", c);
printf("Printing 3 digits before and 2 digits after decimal:\t%3.2f\n", d);
e = 864591234.985461209573;
printf("Printing exponent in lower case format:\t%g\n", e);
printf("Printing exponent in upper case format:\t%G\n", e);
}
Output
Printing without formatting: 22
Preceeding four white spaces: 22
Preceeding zeros: 0022
Integer format not printing decimal: 3
Preceeding three white spaces: 3
Printing value in exponent form: 2.690000e+01
Printing 3 digits before and 2 digits after decimal: 8.97
Printing exponent in lower case format: 8.64591e+08
Printing exponent in upper case format: 8.64591E+08