0

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.

dandan78
  • 13,328
  • 13
  • 64
  • 78
  • 2
    There is no "exception" for `unsigned int`. Other than that it works as you described. If the variable was of type `int` you would invoke Undefined Behaviour (not necessarily an "exception") when adding `1` the the int maximum value. – pmg Jan 11 '20 at 09:10
  • The term "terminate" is broad. It could mean that the program crashes, or just exits normally (with e.g. the `return 0` from the `main` function). First you need to specify what you mean by "terminate", or if you don't know yourself then ask your teacher what he or her mean by that. – Some programmer dude Jan 11 '20 at 09:15
  • 1
    On the iteration after `for (i=UINT_MAX; i>0; i++);`, `i` is reduced modulo `UINT_MAX` to zero and the loop terminates. [C11 Standard - 6.2.5 Types(p9)](http://port70.net/~nsz/c/c11/n1570.html#6.2.5p9) (second sentence) – David C. Rankin Jan 11 '20 at 10:00
  • I have removed my incorrect my answer in which I wrote that the behavior was undefined. Thanks for pointing it out to me. – Andreas Wenzel Jan 11 '20 at 10:01
  • @AndreasWenzel - you can edit and just correct it and just include that reference in it `:)` – David C. Rankin Jan 11 '20 at 10:03
  • @DavidC.Rankin: I have now created a new answer. My old answer was not salvagable and had to be rewritten completely, because it was based on a false premise. I have credited you in the comments section of my answer for providing the link. – Andreas Wenzel Jan 11 '20 at 10:36

2 Answers2

2

Basically, you are correct in everything you say. Once the loop counter goes past UINT_MAX (usually 2^32-1), it will wrap to 0, which causes the loop to terminate due to the loop condition no longer being satisfied.

The only thing wrong with what you say is that you used the word "exception". There is nothing wrong with the result of an unsigned integer arithmetic operation being larger than UINT_MAX. According to C11 Standard - 6.2.5 Types(p9), the result is well-defined. It will just be subjected to modulo UINT_MAX + 1 so that it fits into an unsigned int.

However, beware that with signed integers, an overflow causes undefined behavior. See the following StackOverflow question for further information:

Why is unsigned integer overflow defined behavior but signed integer overflow isn't?.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • Credit goes to [David C. Rankin](https://stackoverflow.com/users/3422102/david-c-rankin) for providing the link to the appropriate section of the C standard. I just took it over from his comment (with his permission). – Andreas Wenzel Jan 11 '20 at 10:33
0

The program terminates because the counter i will wrap to 0 when it reaches UINT_MAX.

chmike
  • 20,922
  • 21
  • 83
  • 106