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

Discussion

What will be the output of the program?

#include <stdio.h>
int main()
{
    int x=1, y=1;
    for(; y; printf("%d %d\n", x, y))
    {
        y = x++ <= 5;
    }
    printf("\n");
    return 0;
}

 

  • A.2 1
    3 1
    4 1
    5 1
    6 1
    7 0
  • B.2 1
    3 1
    4 1
    5 1
    6 1
  • C.2 1
    3 1
    4 1
    5 1
  • D.2 2
    3 3
    4 4
    5 5

Answer: A

Correct answer: 2 1 3 1 4 1 5 1 6 1 7 0

The third clause of the for is the printf, so the values are printed after each pass through the body. The body sets y to the result of x++ <= 5, which is 1 while the old x is at most 5. So the pairs printed are 2 1, 3 1, 4 1, 5 1, 6 1 - and on the pass where x is 6 the comparison fails, giving 7 0, after which the condition y is false and the loop ends.

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

Post your comment

Please log in or register to post a comment.