Operator precedence with logical NOT (!) and relational >= : evaluate the output\n\n#include<stdio.h>\nint main()\n{\n int a = 500, b = 100, c;\n if(!a >= 400)\n b = 300;\n c = 200;\n printf("b = %d c = %d\n", b, c);\n return 0;\n}

Difficulty: Medium

Correct Answer: b = 100 c = 200

Explanation:


Introduction / Context:
This question probes operator precedence and associativity between logical NOT (!) and relational operators (>=). It tests whether you know that unary ! is applied before the comparison.



Given Data / Assumptions:

  • a = 500, b = 100 initially.
  • c is assigned 200 unconditionally after the if.
  • Prints b and c.


Concept / Approach:
In C, unary operators like ! have higher precedence than relational operators like >=. Therefore the expression is parsed as (!a) >= 400, not as !(a >= 400). Since a is 500 (nonzero), !a is 0. Then 0 >= 400 is false, so the body of the if does not execute.



Step-by-Step Solution:
Compute !a: !500 → 0.Compare 0 >= 400 → false.Therefore b remains 100.c is set to 200.printf prints “b = 100 c = 200”.


Verification / Alternative check:
Parenthesize as !(a >= 400) (which evaluates to false) to see explicitly the same result. Change a to 0 to test the true branch: !0 is 1, and 1 >= 400 is false; still no assignment to b.



Why Other Options Are Wrong:
Claims that b becomes 300 ignore precedence; “garbage” for c is invalid because c is definitely assigned before printing.



Common Pitfalls:
Misreading the expression as !(a >= 400); overlooking that !a yields 0 or 1 only; assuming side effects on a.



Final Answer:
b = 100 c = 200

More Questions from Control Instructions

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion