Machine-translated by Google. Questions are authored in English.
Top

Discussion

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

Answer: A

Correct answer: Infinite loop

i++ tests the old value and then increments, and the body increments again with ++i. Starting from 65535, the test takes i to 0, the body takes it to 1, and thereafter the value seen by the test is always odd - 1, 3, 5, … Wrapping round at 65536 lands on 1 again, never on 0, so the condition is never false.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.