-1

Here is the program using a while loop,

#include <stdio.h>

int main()
{
   int i;
   i = 1;
   while (i <= 32767)
   {
       printf("%d", i);
       i = i + 1;
   }
}

do you think the loop would execute indefinitely?

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Simon
  • 33
  • 3
  • Very likely yes -- and if you have **compiler warnings enabled** -- the compiler will tell you why... – David C. Rankin Jan 20 '18 at 09:44
  • 2
    "My book": [edit] its title, authors, and year of publication into your question. If it's something like "Turbo-C For Kids, (c) 1982" then you need a more recent tutorial. – Jongware Jan 20 '18 at 11:45
  • usr2564301 don't mock me dude its my text book thats not bearing any standard commercial author or edition or is renowned, I'm a kid though – Simon Jan 20 '18 at 14:38
  • 1
    We're not mocking you; we're trying to find out whether it is sensible for you to be using that text book. It may not be if it thinks `sizeof(int) == 2`. It is no longer a common compiler configuration for the non-embedded systems you are probably using while you learn C. (Further, it hasn't been common since the mid-90s, if not earlier — which is long before you were born if you're still a kid.) – Jonathan Leffler Jan 20 '18 at 16:38
  • @JonathanLeffler sadly just because of the comment you made I just stumbled upon the sizeof unary operator on wiki and realized that I forgot over 15 pages of chapter 1 of my book and now I'm back to square 1....:( I study chapters and then I realize that I've forgotten the past pages and go back and so on this process has halted my learning quite seriously. – Simon Jan 20 '18 at 16:49
  • There are, sadly, a lot of bad books on C which should not be used for learning modern C. They were usually of indifferent quality when written, but the passage of time and changes to the C language make them counter-productive now. There's a [Definitive C Book Guide and List](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) that covers most of the better books — there are bound to be omissions, but it is still some help. If your book is not on the list, then be cautious. This question indicates that you need to be cautious with it. – Jonathan Leffler Jan 20 '18 at 16:53
  • 1
    Johnathanleffler my book is listed as a keep away from – Simon Jan 20 '18 at 17:26
  • Just to mention, many answers/comments made here are solely for the purpose of gaining reputation points, since no further responses are given – Simon Jan 21 '18 at 07:03
  • Do you mean you need more answers? It seems to me all bases are covered, and there is enough choice for you to pick one and [accept](https://stackoverflow.com/help/accepted-answer) it. – Jongware Jan 21 '18 at 15:36
  • My account has already been slaughtered by the SO bots...nvm... Btw usr2564301 dude you talk too much but don't even put a name on...not a standard practice...just saying – Simon Jan 21 '18 at 18:23

3 Answers3

5

Well it's signed integer. Considering that if int is of 16 bits, it will overflow at one point precisely when the value is INT_MAX or 32767. At that point it is undefined behavior.

It is undefined behavior - when int is of 16 bits. As the behavior is undefined we can't say it will always run infinitely etc in that case.

In your system if int is of 32 bits or higher then the behavior of this program is not undefined.

From standard

.... Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

In your case if sizeof(int) = 4 or higher, then the loop will stop. The only way to know whether the behavior is undefined or not is to know what the size of int is.

To summarise

  • If int is of 32 bits or higher then this will stop.
  • If int is of 16 bits then this will be undefined behavior. It may loop indefinitely or it may not. It's not defined by the standard.
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
user2736738
  • 30,591
  • 5
  • 42
  • 56
  • why would it overflow when the while loop has demanded the loop to stop right at 32767? – Simon Jan 20 '18 at 08:58
  • @Simon.: Check the answer it might help – user2736738 Jan 20 '18 at 09:12
  • `7fff -> 8000 -> ... -> 7fff -> 8000 -> ...` yes it may be implementation defined, but it will likely run forever given `(i <= 32767) -- "is always true due to limited range of data type"`. (test it with `char i` and `(i <= 127)` and a counter to break after `127` reached twice) – David C. Rankin Jan 20 '18 at 09:35
  • @DavidC.Rankin.: Yes if it becomes negative then yes it will run for ever (and follows the same policy each time 32767 is incremented on that variable type)//but yes if it is `8000` or something then it will stop. (Both are covered by that undefined behavior that the standard says about) – user2736738 Jan 20 '18 at 09:37
  • No argument over the undefined nature of adding 1 to `INT_MAX`, but it was kindof a neat question. – David C. Rankin Jan 20 '18 at 09:39
  • @DavidC.Rankin.: My earlier edit was missing the size argument. ( Yes it is a quick edit and all that ) and I mentioend teacher instead of book. But well that fetched me a DV. – user2736738 Jan 20 '18 at 09:40
  • @DavidC.Rankin.: Thanks for the comment and stopping by. – user2736738 Jan 20 '18 at 09:41
  • 1
    @coderredoc I don't know who the dv voter was, but obviously they didn't have the integrity to back up their downvote with a comment. Those all come out in the wash anyway... – David C. Rankin Jan 20 '18 at 09:41
  • 3
    @Simon: The code does not say “Stop at 32767.” It says “Continue if `i` <= 32767.” In order for that test to be false, `i` must first be incremented to be more than 32767. If `int` cannot represent 32768, that is not possible, and incrementing `i` beyond 32767 has undefined behavior. – Eric Postpischil Jan 20 '18 at 09:51
2

If you disregard the specific numbers and instead write

while (i <= MAX_INT)

the compiler sees this as "loop while i is less than or equal to the largest value it can ever have".

As i - by definition - can never be larger than the largest value, this condition will always be true and the loop would be infinite.

However, as the code tries to compute i + 1 even when i cannot possibly become any larger, there is an error in the program. The language standard explicltly states that if the program tries this - overflow on a signed variable - the result is undefined.

Undefined behavior can have any result according to the language standard. This includes getting some other value for i (perhaps a negative one despite trying to add 1), having the OS trap and terminate the program, or possibly even terminate a loop that would otherwise be infinite. We just don't know.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • why is the initialization always by 1 and not zero? it always starts with i = 1; but not i = 0; why so ?? – Simon Jan 20 '18 at 15:05
  • 1
    @Simon - The 1 is just an example, it could be *any* value. But you have to give it *some* value, otherwise all uses of `i` would be undefined. – Bo Persson Jan 21 '18 at 09:15
1

I don't know your book. And in fact the book is correct if you assume that the type int is a 16 bit signed integer. The range of a 16 bit integer goes from -32768 to +32767. So in this case the condition i<=32767 will always be true.

But in your programm I think the type of int is a 32 bit integer which range goes from -2147483648 to +2147483647.

If you replace int i with short i the loop should be an infinity loop.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Benjamin J.
  • 1,239
  • 1
  • 15
  • 28
  • even if it goes upto 32767, with a 16bit int should'nt it just stop right there and not go beyond the int limit of 32767 as mentioned in the while loop ? why would it be indefinite? – Simon Jan 20 '18 at 08:56
  • 1
    @Simon: Your condition is that the loop should run if `i` is smaller or equal to 32767. The max value for `i` (if it's a 16 bit signed integer) is 32767. So the condition is true. The next value (after `i = i + 1`) is -32768 (because of a integer overflow) and the condiition is still true. – Benjamin J. Jan 20 '18 at 08:58
  • why the next value after 32767 is -32768? why the negative sign? the range is -32768 to 32767.... – Simon Jan 20 '18 at 09:01
  • After the variable 'i' reaches 32767, a new iteration of the loop is executed. Then, 'i' will be incremented and overflow. So this leads to undefined behavior, not necessarily infinite loop. – machine_1 Jan 20 '18 at 09:03
  • 32767 is the same as 0x7FFF. The next value is 0x8000 which represents -32768 (for a singed integer). And this is called a integer overflow. – Benjamin J. Jan 20 '18 at 09:03
  • 2
    The C standard does not define the behavior on integer overflow. Arithmetic cannot be relied upon to wrap. A program may wrap, abort, get wrong results, or behave in any other way. – Eric Postpischil Jan 20 '18 at 09:43