I am studying for my C exam and I've been asked this question:
"Given this code, does it terminate? If so, why?"
int main() {
unsigned int i;
for (i=1; i>0; i++);
return 0;
}
I've been thinking that it does terminate due to the nature of the unsigned int
: in the for loop i
goes from 1 to the max value (which I believe is 2^32-1) freely, then the loop encounters an overflow exception and it goes back to 0, the first value for an unsigned int. This is in conflict with the condition i>0 of the loop and it terminates it, going to "return 0" and terminating the program.
Is my hypothesis correct? We have no solution given by the professor so while it does make sense to me it might be horribly incorrect, that's why I need your help.