Control Instructions

Q1:
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

ANS:A - Infinite loop

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



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.