In C, logical OR produces an integer result: what does this program print?
#include
int main()
{
int a = 100, b = 200, c;
c = (a == 100 || b > 200);
printf("c=%d
", c);
return 0;
}
-
Ac=100
-
Bc=200
-
Cc=1
-
Dc=300
-
Ec=0
Answer
Correct Answer: c=1
Explanation
Introduction / Context:In C, relational and logical operators yield an int result: 1 for true and 0 for false. This question checks that knowledge using a simple disjunction (logical OR).
Given Data / Assumptions:
- a == 100 evaluates to true (1).
- b > 200 evaluates to false (0), because b is exactly 200.
- c stores the result of the logical OR of these two boolean values.
Concept / Approach:The logical OR operator || returns 1 if either operand is nonzero. Since the first operand is true, the overall result is true and short-circuiting applies; the second comparison need not change the outcome.
Step-by-Step Solution:Compute a == 100 → 1.Compute b > 200 → 0.Evaluate 1 || 0 → 1; assign to c.printf prints "c=1".
Verification / Alternative check:Change b to 250 to see that the expression still yields 1. Change a to 99 and b to 200 to see it yields 0.
Why Other Options Are Wrong:(a), (b), and (d) misinterpret logical results as variable values. (e) would only occur if both comparisons were false.
Common Pitfalls:Expecting printf to print the value of true as true instead of 1; forgetting that C uses integers for boolean outcomes.
Final Answer:c=1