An interesting one which is when i got stuck during my college days and found the reason during my training days at honeywell.
main()
{
float dA=25.0;
int nI=46,nJ=56;
printf("%d %d %d",dA,nI,nJ);
return 0;
}
Guess the output and try it in VC++ compliler(where i tested this code). You will be surprised. You may not expect this output.
Hint for the reason in post title itself.
Post your reasons as comments
My explanation (may not be sufficient) on this Behavior is
printf function is implemented using variable number of arguments method. In this method each and every argument value is popped out from the stack. The dA is a double and so it will occupy 8 bytes in the stack. But from the format specifier it is mentioned as integer(%d).
So when retrieving the value of dA, it will pop out only 4 bytes from the stack i.e. MSB of the DA. So now the stack pointer will point to the LSB of the dA.
When retrieving the value of nI, it will pop out next 4 bytes which is the LSB of dA.
Likewise when retrieving the value of nJ, it will pop out the value of nI instead of nJ since the stack pointer is now pointed to nI.
So the output will be
0 1077477376 46
If you are not clear please mail me or post comments.
main()
{
float dA=25.0;
int nI=46,nJ=56;
printf("%d %d %d",dA,nI,nJ);
return 0;
}
Guess the output and try it in VC++ compliler(where i tested this code). You will be surprised. You may not expect this output.
Hint for the reason in post title itself.
Post your reasons as comments
My explanation (may not be sufficient) on this Behavior is
printf function is implemented using variable number of arguments method. In this method each and every argument value is popped out from the stack. The dA is a double and so it will occupy 8 bytes in the stack. But from the format specifier it is mentioned as integer(%d).
So when retrieving the value of dA, it will pop out only 4 bytes from the stack i.e. MSB of the DA. So now the stack pointer will point to the LSB of the dA.
When retrieving the value of nI, it will pop out next 4 bytes which is the LSB of dA.
Likewise when retrieving the value of nJ, it will pop out the value of nI instead of nJ since the stack pointer is now pointed to nI.
So the output will be
0 1077477376 46
If you are not clear please mail me or post comments.
4 comments:
This is my wild guess.
For the first %d(left most), the printf code tries to read the 4 bytes at the location dA and print as int. And this collapses the following parameters.
Eager to know the apt reason.
Yep. I remember the pblm we faced. i haven’t tried it out in any VC++ compiler as of now, but I'll provide the possible reason
Printf uses a Variable parameter methodology, where in the variables are popped from the stack based on the format specifiers we had specified in teh beginning (%d, %f, %s etc…). Now we have given %d for a float and hence it pops out 4 bytes considering the no. to be an integer, where as the actual size of the value was 8bytes. This left out 4bytes is taken as a part of the 2nd no. in the argument list. Hence the 2nd one would print an unexpcted value, so on and on with following variables
You will get junk values for all the 3 elements. Here dA is not type casted to int and printf will try to interpret the address of dA as integer. The storage format of float is not same as integer, so you will not even get val 25 as output for dA. Not sure why nI and nJ are becoming junk.
You will get junk values for all 3 elements. Here dA is not type casted to integer and printf function will try to interpret the dA's address as integer. Floating point values are not stored in a different format and reading that address as integer will not get you the whole value (25). Printf function takes multiple arguments, where all arguments are pushed to stack. Printf will try to get the values from the stack based on the format specifier (%d). In this case when printf first tries to read dA as integer it will read only the first 4 bytes... but actually the value of dA is of type float which takes more number of byte to store... this makes reading of other elements as junk.
Post a Comment