In C programming, consider the following code (normalize line breaks exactly as shown):\n#include<stdio.h>\nint main()\n{\n int i = 10, j = 15;\n if(i % 2 = j % 3)\n printf("CuriousTab\n");\n return 0;\n}\n\nWhich compiler diagnosis best applies to this program and why?

Difficulty: Easy

Correct Answer: Error: Lvalue required

Explanation:


Introduction / Context:
This question tests understanding of assignment versus comparison in C, and the requirement that the left-hand side of an assignment be an lvalue (a modifiable storage location). Placing an assignment inside an if condition is legal only when the left-hand side is assignable; otherwise the compiler must reject it.



Given Data / Assumptions:

  • Code fragment uses i % 2 and j % 3 inside an if condition.
  • The operator used is = (assignment), not == (equality comparison).
  • Standard C compiler semantics apply.


Concept / Approach:
In C, the assignment operator requires its left operand to be an lvalue. Expressions like i % 2 produce rvalues (temporary results) and cannot be assigned into. Therefore writing i % 2 = j % 3 is illegal. If the intent was to compare, the correct operator is == and both sides may be rvalues.



Step-by-Step Solution:
Identify the operator: code uses =, not ==.Check assignability: i % 2 is not a modifiable object (not an lvalue).Conclusion: assignment to a non-lvalue → compile-time error.Fix: replace with if ((i % 2) == (j % 3)) { ... }



Verification / Alternative check:
Try compiling: the diagnostic typically says “lvalue required” or “expression is not assignable,” confirming the rule.



Why Other Options Are Wrong:
“Expression syntax” is generic and less accurate; the syntax is valid but semantically invalid for assignment. “Rvalue required” is the inverse of the real rule. “Runs successfully” is impossible due to the constraint on the left operand. “Warning only” understates the mandatory error.



Common Pitfalls:
Accidentally using = in conditions; not parenthesizing modular expressions; assuming compilers auto-correct to ==.



Final Answer:
Error: Lvalue required

More Questions from Control Instructions

Discussion & Comments

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