edited by
1,865 views
3 votes
3 votes
#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

edited by

1 Answer

Best answer
3 votes
3 votes

Given ,

size of unsigned int = 2 bytes  =  16 bits

Hence magnitude of the variable 'i'  lies between  0  and   216 - 1 which is 65535..

Hence after increment of 65535 , we will get 0 as the next value ..

But we have to see how the increment is going on here..

Here i  =  65535 initially..

So condition 

i++  !=   0

works fine as it is post increment operator..But after evaluating the while condition i is incremented to 0 as explained above.Now in the body of while loop , in printf() statement , i is incremented once again , thereby causing i = 1..

So next time when the condition of while() is checked , i = 1 which is not equal to 0..And effectively in previous step we had 2 increments from 65535 to 1..

Similarly in this iteration we will have increment from 1 to 3.Likewise it will be continued ..Eventually 65533 to 65535 and then the same story continues..:) 

Thus the above program leads to infinite loop..

Hence A) should be the correct answer.. 

selected by

Related questions

0 votes
0 votes
1 answer
1
Debargha Mitra Roy asked 3 days ago
47 views
#include <stdio.h int main() { int a[3] = {1, 3, 5, 7, 9, 11}; int *ptr = a[0]; ptr += sizeof(int); printf("%d", *ptr); return 0; }(Assume size of int to be $2$ bytes.)T...
0 votes
0 votes
2 answers
3
Debargha Mitra Roy asked Apr 10
103 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
1 votes
1 votes
1 answer
4
SSR17 asked Feb 29
252 views
#include <stdio.h int main() { int i = -1; int x = (unsigned char)i; printf("%d", x); return 0; }output is 255 , but please explain how