Search code examples
c++underflow

C++ underflow example?


I'm currently taking an introductory C++ class and the professor gave us the following example:

unsigned int TEST = -1;
cout << TEST;

Now, on his machine, TEST is returned as the maximum value for an integer, because he forced underflow. But on my machine, it is returned as 0.

Is this behavior compiler dependent or anything? In other words, why did my machine return 0 but his machine return the maximum?


Solution

  • Is this behavior compiler dependent

    No. It is platform dependent in the sense that the maximum value of unsigned int may vary between platforms. But all standard compliant compilers will show the largest value representable by unsigned int - which is never 0.

    why did my machine return 0 but his machine return the maximum?

    Two possible options are: This is not the program that produced the output on your machine, or the compiler on your machine is borked.