C if–else chain and statement termination:\nIdentify the compilation error in this program with a missing semicolon.\n\n#include <stdio.h>\nint main()\n{\n int x = 30, y = 40;\n if (x == y)\n printf("x is equal to y\n");\n else if (x > y)\n printf("x is greater than y\n");\n else if (x < y)\n printf("x is less than y\n")\n return 0;\n}

Difficulty: Easy

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 x and y.
  • The final printf lacks 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\n") → no semicolon.Parser therefore merges the next line into the same statement and fails.Add ; to fix: printf("x is less than y\n");


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

More Questions from Control Instructions

Discussion & Comments

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