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

Discussion

Which of the following statement is correct about the code snippet given below?

#include <stdio.h>

int main()
{
int a = 10, b = 2, c;
a = !( c = c == c) && ++b;
c += ( a + b--);
printf( " %d %d %d", b, c, a);
return 0;

}

 

  • A.The program will print the output 1 3 0
  • B.The program will print the output 0 1 3
  • C.The program will result in an expression syntax error
  • D.The program will print the output 0 3 1

Answer: A

Correct answer: The program will print the output 1 3 0

Take the statements in order.

  1. a = !(c = c == c) && ++b; - c == c is 1, so c becomes 1 and !1 is 0. The && short-circuits, so ++b never runs and b stays 2. a becomes 0.
  2. c += (a + b--); - a + b is \(0 + 2 = 2\), so c becomes \(1 + 2 = 3\), and the post-decrement leaves b at 1.

Printing b, c, a gives 1 3 0.

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

Post your comment

Please log in or register to post a comment.