Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   int and float anomalities. Please Help !! (http://www.go4expert.com/forums/int-float-anomalities-help-t27707/)

Pradeep.M 1Feb2012 20:56

int and float anomalities. Please Help !!
Following is the programme:

int main()
int a=2;
float b=2.5;
printf("a: %d %f %d \n",a,a,a);
printf("b: %d %f %d \n",b,b,b);
printf("with f: %f with c:%c\n",150,150);
return 0;
OUTPUT( on gcc compiler)
a: 2 0.000000 148
b: 0 0.000000 1074003968
with f: 0.000000 with c:

the value 2 is printed correctly but 0 is printed with %f is used for int. No problem till here, but after this why 148 is printed. also, in the 2nd line, when %d is used for float variable , 0 is printed , no problem, but then why after that 0.000000 is printed even when %f is used for float...
in the end.. when I want to print the character corresponding to the ANSCI value 150 using %f.. 0 is printed and when using %c , nothing is printed

PLEASE clarify, with an example.

xpi0t0s 3Feb2012 17:20

Re: int and float anomalities. Please Help !!
Before the first printf, add the following line and show us the new complete output:

printf("sizeof(int)=%d; sizeof(float)=%d\n", sizeof(int), sizeof(float) );
But basically, this is because you're not using the correct format codes. Use %d for int AND NOTHING ELSE, and use %f for float AND NOTHING ELSE. They must match, otherwise you get undefined behaviour.

All times are GMT +5.5. The time now is 15:47.