Home / C Programming / Control Instructions :: Discussion

Discussion :: Control Instructions

  1. What will be the output of the program?

     #include
     int main() 
     {  
        unsigned int i = 65536; /* 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 D

    Explanation :

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

    Step 1:unsigned int i = 65536; here variable i becomes '0'(zero). because unsigned int varies from 0 to 65535.

    Step 2: while(i != 0) this statement becomes while(0 != 0). Hence the while(FALSE) condition is not satisfied. So, the inside the statements of while loop will not get executed.

    Hence there is no output.

    Note: Don't forget that the size of int should be 2 bytes. If you run the above program in GCC it may run infinite loop, because in Linux platform the size of the integer is 4 bytes.


Be The First To Comment