Thursday, 15 August 2013

c - Reason for the output of the following code : -



c - Reason for the output of the following code : -

i have few lines of code could't understand reason output..

int main() { int a=5; float b=10.5,c=11.0; printf("%d",b); printf("\n%d",c); printf("\n%f",a); homecoming 0; }

o/p in visual c++ :- 0 ,0 ,0.000000

gcc compiler :- 0,0, 11.000000

when phone call variadic function printf, floats undergo promotion double. ints passed as-is. printf hence expects double when write %f, , int when write %d.

not giving double, int, hence undefined behaviour. similarly, passing double when function expects int undefined.

as usual, undefined behaviour means "anything happen". never, ever rely on undefined behaviour.

c

No comments:

Post a Comment