Let's assume we have the following loop:
for (int i = 1; i < 2; i--)
{
cout << i << endl;
}
Without a doubt, this is an infinite for-loop. However, my question is that for how long will it run? Because my prof said that given that an int is 4 bytes = 32 bits
, it can store a number from -2147483648 to 2147483647
. Given this, he went on to say that in a for-loop, if it ever reach one of these numbers, and the loop is still programmed to continue (like this one is), then the number will become positive, and the condition will be met, terminating the loop.
My question is exactly is: how does this happen? Why does the number -2147483648 become +2147483648 in the for-loop, and why does +2147483648 turn to -2147483648 in the for-loop? Because I used to think that a segmentation fault would occur as soon as the counter hit 2147483649. Thanks for your responses!