C Programming - Control Instructions - Discussion

4. 

What will be the output of the program?

#include<stdio.h>
int main()
{
    unsigned int i = 65535; /* Assume 2 byte integer*/
    while(i++ != 0)
        printf("%d",++i);
    printf("\n");
    return 0;
}

[A]. Infinite loop
[B]. 0 1 2 ... 65535
[C]. 0 1 2 ... 32767 - 32766 -32765 -1 0
[D]. No output

Answer: Option A

Explanation:

Here unsigned int size is 2 bytes. It varies from 0,1,2,3, ... to 65535.

Step 1:unsigned int i = 65535;

Step 2:
Loop 1: while(i++ != 0) this statement becomes while(65535 != 0). Hence the while(TRUE) condition is satisfied. Then the printf("%d", ++i); prints '1'(variable 'i' is already incremented by '1' in while statement and now incremented by '1' in printf statement) Loop 2: while(i++ != 0) this statement becomes while(1 != 0). Hence the while(TRUE) condition is satisfied. Then the printf("%d", ++i); prints '3'(variable 'i' is already incremented by '1' in while statement and now incremented by '1' in printf statement)
....
....

The while loop will never stops executing, because variable i will never become '0'(zero). Hence it is an 'Infinite loop'.


Livya said: (Jun 8, 2011)  
Hi

if the program changed to :
#include<stdio.h>
int main()
{

unsigned int i = 65535; /* Assume 2 byte integer*/
while(i++ != 65539)
printf("%d",++i);
printf("\n");
return 0;
}
the output is "6553765539" not 13..... why this happened?

Rupinderjit Singh said: (Oct 4, 2011)  
Because when the upper limit of the unsigned int gets over,it again roll over to 0 and moves onward (that is:0 1 2 3 . . . 65534 65535 0 1 2 3 4 . . . .)

Coder said: (Jun 21, 2013)  
The loop is infinite but the output is strange, and not as expected,

The output I am getting, after running this code is - 655376553965541655436554565547655496555165553655556555765559655

Although, our expectation is that compiler must switch to 0, if we increment 65535 (beyond the upper range of unsigned int). but it's printing beyond the limit like 65537, and then 65539.

What exactly happening ?

Ankits said: (Jul 24, 2013)  
Your compiler might NOT be 16-bit. Means, int is not 16 bits but 32 bits (4 bytes). That is why, it is going beyond 65535.

Aparna.V said: (Sep 13, 2014)  
I can understand that this loop runs infinitely but why is it printing 1 2 3 and so on?

Adarsh said: (Sep 15, 2014)  
@Aparna.

Its because your compiler supports 65535 +, so during program execution, you will have increment of 2 in original value of 65535 (1 due to while condition, and 1 due to printf condition).

Hence output will be 655376553965541.

Abi said: (Sep 23, 2014)  
Unsigned integer value is only 65535. How the loop is execute in infinite?

Sree said: (Nov 24, 2014)  
On loop 2, before the post increment doesn't I value become zero. Hence terminating the while loop?

Vishwambhar said: (Mar 29, 2015)  
What happened if I is signed integer.

Ashish Dev said: (Jul 12, 2015)  
The size of unsigned int is 0 to 65535.

So here:

#include<stdio.h>
int main()
{
unsigned int i = 65535; /* Assume 2 byte integer*/
while(i++ != 0)
printf("%d",++i);
printf("\n");
return 0;
}

When integer gets increment (i++) by 1 then value should be 0.

That does not satisfy the while loop condition. Therefore it must print /n (i.e No output) only once right

So why its infinite loop?

Rachana said: (Jul 16, 2015)  
i=65535; when incremented value should be 0.

Kritika said: (Aug 7, 2015)  
@ Rachna.

Here first the value of I is checked in while loop as 65535 which is not equal o zero so condition is true no as it is post increment value of I is 0 when it passes to printf statement.

As again printf uses pre increment 1 is printed also with this mechanism the value of I never get 0 in while loop so this whole loop gets executed infinite times.

Sapna Kumari said: (Feb 28, 2016)  
Please can you clear how i = 1 in output?

Nilesh said: (Apr 13, 2016)  
In the unsigned int 0 will come at one point ,then condition get false and loops get break then why it is printing infinite times?

Bunny said: (Jul 19, 2016)  
The value of "i" will never be zero in while statement because it will always get odd value like
65535, 1, 3, 5, 7.

Ashish Kunnath said: (Oct 12, 2016)  
So basically, EVEN I THOUGHT THAT IT SHOULD BE NO OUTPUT as after the first increment itself it would become 0.

But it's "post" increment hence, first increment as the reason says is 65535 != 0

Then it GETS INCREMENTED TO 0 ("POST INCREMENT").

THEN increment In printf thus prints 1, and goes on with odd values.

Hope this helped you.

Mohan7575 said: (Jun 28, 2019)  
That's right, the range of unsigned int is 65535. so when increment by 1 it doesn't go -65534 ( because unsigned int can't accept negative numbers accept only positive numbers) hence it goes to first positive number 0.

Then it is further incremented in printf so it shows 1.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.