Home / C Programming / Control Instructions :: Discussion

Discussion :: Control Instructions

  1. 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; } 
    

  2. A.

    Infinite loop

    B.

    0 1 2 ... 65535

    C.

    0 1 2 ... 32767 - 32766 -32765 -1 0

    D.

    No output

    View Answer

    Workspace

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


Be The First To Comment