C if–else chain and statement termination:
Identify the compilation error in this program with a missing semicolon.
#include
int main()
{
int x = 30, y = 40;
if (x == y)
printf("x is equal to y
");
else if (x > y)
printf("x is greater than y
");
else if (x < y)
printf("x is less than y
")
return 0;
}
-
AError: Statement missing
-
BError: Expression syntax
-
CError: Lvalue required
-
DError: Rvalue required
-
ENone of the above
Answer
Correct Answer: Error: Statement missing
Explanation
Introduction / Context:This item checks attention to C statement syntax. Each expression statement must end with a semicolon, including calls to printf. Missing terminators commonly cause confusing parser errors near subsequent lines.
Given Data / Assumptions:
- Three-branch if–else chain compares
xandy. - The final
printflacks a trailing semicolon.
Concept / Approach:In C, a function call used as a statement is an expression statement and must end with ;. Omitting it results in a syntax error, often reported as “expected ‘;’ before ‘return’” or “statement missing”. The logic of comparisons is otherwise fine.
Step-by-Step Solution:Locate printf("x is less than y") → no semicolon.Parser therefore merges the next line into the same statement and fails.Add ; to fix: printf("x is less than y");
Verification / Alternative check:Recompile after adding the semicolon; the program will run and print exactly one of the three messages based on values.
Why Other Options Are Wrong:Expression syntax is vague; the specific, conventional diagnostic is a missing statement terminator. Lvalue/Rvalue required do not apply to a function call.
Common Pitfalls:Forgetting semicolons on single-line conditionals; misreading the compiler’s error location when it points to return or the line after the offending call.
Final Answer:Error: Statement missing