ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
int a = 1, b = 2;
int result = !a == !b;
printf("%d", result);
  1. A. Compile-time error: ! cannot be applied this way
  2. B. 0
  3. C. 3
  4. D. 1

Correct answer: D. 1

! has higher precedence than ==, so !a == !b parses as (!a) == (!b), never '!(a == b)'. !a is !1 = 0 (since a is nonzero/true). !b is !2 = 0 (b is also nonzero/true, so its logical negation is also 0). 0 == 0 is true, so result = 1.

More C Programming questions

Exams that ask this topic