0

I have this little piece of code and I am having trouble comprehending what could possibly be the output?

int main()
{
    unsigned int i=65000;
    while ( i++ != 0 );
    printf("%d",i);
    return 0;
}

I can sense post-increment is playing a major role in the loop but I am not able to visualise it. Do unsigned ints behave differently in while loops after reaching highest positive value? Lastly what is the purpose of a semicolon after the while loop?

f__society
  • 494
  • 6
  • 12
  • 3
    Unsigned integer overflow [is defined in C](http://stackoverflow.com/questions/18195715/why-is-unsigned-integer-overflow-defined-behavior-but-signed-integer-overflow-is), and essentially wraps around. –  Apr 29 '17 at 11:26
  • 3
    Compile and run it? – Daniel Jour Apr 29 '17 at 11:26
  • The loop will exit once `i` has reached its maximum value (wrapping to 0), though it is very likely (certainly with compiler optimisations turned on), the loop will be discarded, and `i` set to 1 directly (you'd have to check the assembler output for that). –  Apr 29 '17 at 11:29
  • Ran on an online IDE it says : 'Time Limit of 5 seconds exceeded' does that indicate Infinite loop @DanielJour ? – f__society Apr 29 '17 at 11:29
  • That just indicates a loop longer than 5 seconds. Why would 5 seconds be infinite? –  Apr 29 '17 at 11:29
  • It doesnt depend on while loop, unsigned means left most bit which is holding i value is not set to 1. Lefmost bit is known as signed bit. – Inder R Singh Apr 29 '17 at 11:30
  • Learn more about signed bit in linux that will be your answer – Inder R Singh Apr 29 '17 at 11:31
  • 4
    @InderRSingh unsigned types don't have sign bits – harold Apr 29 '17 at 11:33
  • @harlod pls read my 2 comments clearly – Inder R Singh Apr 29 '17 at 11:36
  • 2
    @InderRSingh you're getting my name wrong even though it auto-completes, and anyway I thought I read them clearly - maybe your comments aren't clear. There is no sign bit, so it's unrelated to what's going on here. Of course, the most significant bit of an unsigned int *can* be 1. – harold Apr 29 '17 at 11:38
  • Calling a function accepting a variable number of arguments (`printf()`) without a prototype in scope invokes **Undefined Behaviour** (anything can happen: program can get stuck, program can print 42, compiler can stop without producing an executable, ...). You need to `#include ` for a conforming program. – pmg Apr 29 '17 at 11:49
  • 1
    @InderRSingh: Your comments don't make sense. As harold wrote, unsigned integers don't have a sign bit. You might confuse them with positive signed integers which have a signe bit, but that is not necessarily the uppermost bit in the storage unit. Also signed integer overrflow is one of the classical ways to invoke undefined behaviour. And that has absolutely **nothing** to do with the Linux OS. – too honest for this site Apr 29 '17 at 12:18

1 Answers1

1

Unsigned integer overflow is defined (see Why is unsigned integer overflow defined behavior but signed integer overflow isn't?), and this code will probably terminate (it may take time: see below).

However, the size of int varies between platforms. If it was 16-bit (some old compilers), it will soon reach the maximum value 65535 and terminate on the wrap. However, if it was 32-bit (GCC and many), it's going to take a while to overflow it. It can also be optimized, since it has no side effects: see below.

The semicolon after the while statement terminates the expression, which means the loop does nothing and allows while to be optimized out.

At last, the output would be 1.

Tatsuyuki Ishi
  • 3,883
  • 3
  • 29
  • 41
  • I think the output is 1 (try it), since there's a postfix increment to `i`. –  Apr 29 '17 at 11:31
  • Corrected the things. – Tatsuyuki Ishi Apr 29 '17 at 11:32
  • http://www.geeksforgeeks.org/output-of-c-programs-set-6/ See here they say output would be 1. Tried running on their IDE but it says : 'Time Limit of 5 seconds exceeded' – f__society Apr 29 '17 at 11:33
  • Of course if you are allowing programs to be ran by the public, you would put a time limit on those programs. You do not want people to be able to crash your system. Therefore the program has not completed as it ran out of time – Ed Heal Apr 29 '17 at 11:39
  • @f__society your program takes about 7 seconds to run on my PC using 32-bit `int`. – Weather Vane Apr 29 '17 at 11:41