Today one of my friends came to my room to ask a doubt in C++
The he came up with was a very simple one but had a very interesting solution and exhibited the difference in the computations of 32 bit and 64 bit processors and had literally taken us to a stand still
The Code was:
..
…
void main()
{
int i = 0;
i = 400*400/400;
cout<<i;
getch();
}
This was the piece of code, and would like as many people to think about it and give answers with reasons.
What is this ?
i = 400*400/400;
cout<
I think you missed something …
LikeLike
it is cout<<i;
sorry i missed it will edit it thanks
LikeLike
i think the output will be 400
tell me the output
LikeLike
i try it on a 32 bit processor
answer was 72 !!!!!!!!!
hows that possible man
LikeLike
i think ts due to “int i”
try to change it
you may get ur answer
LikeLike
I too got 72 …
someone please explain the answer .
LikeLike
First of all, this is not C++. The function main is supposed to return an int.
Second, if 160000<=INT_MAX the result will be the expected 400. If not, you get implementation-defined behaviour.
Checkout http://home.att.net/~jackklein/c/inttypes.html
LikeLike
If you use gcc, then also a c program requests for a return value, however can you give me the reference where I can find that returning a value is must for C++ I will be greatful and your answer is write
LikeLike
You are right that the main function doesn’t require a “return something;” statement. Still, main needs to be declared with an int as return type, not void. This should be covered in just about any good book about C++ that was published after 1998. When in doubt, check the ISO/IEC 14882:2003 standard.
LikeLike