can any one explain plz urgently
code was this
#include<stdio.h>
#include<conio.h>
void main()
{
double a=3.1428571;
int b=4347;
printf("%e\t%5d\t%5.4f\t%.5f\n",a,b,9,9);
getch();
}
--------OUTPUT-------------
3.142857e+000 4347 0.0000 0.00000
plz explain this why is the output of this function is this
it is all related to the format specifiers you are giving in the printf string, these are
%e
%5d
%5.4f
%.5f
and you pass in for these specifiers
double a=3.1428571;
int b=4347;
integer constant 9
integer constant 9
respectively. Taking the triples (format string, value, output)
3.142857e+000, %e, double a=3.1428571;
a is a floating point variable of type double with the value 3.1428571, %e indicates the printf to expect a floating point value and to print it out in exponential format therefore printf outputs 3.142857e+000
4347, %5d, int b=4347;
b is and integer variable with the value 4347, %5d indicates to printf to expect an int and to output it in a field of size 5 (i.e. 5 characters wide), therefore printf outputs ' 4347'.
0.0000, %5.4f, integer constant 9
integer constant 9 is an integer constant value 9, unfortunately %5.4f indicates to printf to expect a double value since int is normally 2 or 4 bytes and double is 8 bytes printf f consumes both the first and second integer constant 9 and interprets them as a double (this appears to have value 0 or alternatively is not representable) there printf outputs '0.0000' note it has followed the specification of 4 decimal places but ignore the specification of field with 5, it is impossible to fit 4 decimal places into a field width of 5.
0.00000, %.5f, integer constant 9
actually this integer constant has already been consumed by printf, at this point it is just reading random stack data it is just sheer fluke that the value is 0. %.5f indicates to printf to expect a double and print it using 5 decimal places so it outputs 0.00000