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 a=0, b=1, c=3;
    *((a) ? &b : &a) = a ? b : c;
    printf("%d, %d, %d\n", a, b, c);
    return 0;
}

 

  • A.0, 1, 3
  • B.1, 2, 3
  • C.3, 1, 3
  • D.1, 3, 1

Answer: C

Correct answer: 3, 1, 3

Both conditionals test a, which is 0 and therefore false. The left-hand side selects &a, so the assignment targets a; the right-hand side selects c, which is 3. So a becomes 3 while b and c are untouched, giving 3, 1, 3. Note that a conditional expression can be dereferenced to form an lvalue like this only because both branches are pointers.

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

Post your comment

Please log in or register to post a comment.