Home » C Programming » Control Instructions

What will be the output of the program? #include int main() { unsigned int i = 65535; /* Assume 2 byte integer*/ while(i++ != 0) printf("%d",++i); printf("\n"); return 0; }

Correct Answer: Infinite loop

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'.


← Previous Question Next Question→

More Questions from Control Instructions

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion