Code:#include <stdio.h> void main() { printf("%d",59.00); //prints out 2147483619 }
I can't understand the output of this program! Please help!
Code:#include <stdio.h> void main() { printf("%d",59.00); //prints out 2147483619 }
I can't understand the output of this program! Please help!
Code:printf("%f",59.00);
"without goto we would be wtf'd"
Do you want to know WHY 2147483619 was printed or the answer @Structure gave you is enough?
Well since it began with "void main", printing "I'm a banana" would have been no less undefined than any other answer.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Why was it wrong on my part to use "void main"? And can you please tell me why the result came out to be 2146483619 , i still don't understand even after reading answers on stack overflow.
It's wrong by definition: the return type of main is int.Originally Posted by ranadas
The format specifier you used is %d, which expects a corresponding int argument. You passed 59.00 as the corresponding argument, which is a double. Since the format specifier and the corresponding argument do not match, the behaviour is undefined. So, don't do this. Your compiler will typically be able to warn you about such mistakes.Originally Posted by ranadas
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Then why only print 2147483619? it could have printed 728 or 753 or 283728983 something like this.. there must be some logic , it is not garbage value
Although there is some "logic" to it, it is not necessarily interesting. It is basically a garbage value. The exact value depends on your compiler/CPU/OS. For example, when I ran your original program I got the value -914973672.
One possibility is that it is interpretting the first half of the binary representation of the 64-bit double floating point representation as a 32-bit integer. But that is not necessarily what is happening, since it's possible that ints and floating point values are passed in different ways. They are not always passed on the stack in modern systems. For example, look at my results for the following:
Code:#include <stdio.h> int main() { printf("%d\n", 123); // prints 123 printf("%d\n", 59.00); // prints 123 return 0; }
Last edited by john.c; 01-28-2020 at 11:01 PM.
A little inaccuracy saves tons of explanation. - H.H. Munro