C structure syntax validation: which of the following structure definitions is correct as written? 1) struct book { char name[10]; float price; int pages; }; 2) struct aa { char name[10]; float price; int pages; } // missing semicolon terminator 3) struct aa { char name[10]; float price; int pages; } // missing semicolon terminator
-
A1
-
B2
-
C3
-
DAll of the above
-
E1 and 2
Answer
Correct Answer: 1
Explanation
Introduction / Context:In C, every struct definition must end with a semicolon after the closing brace. This question tests attention to syntax details and the difference between a definition and a variable declaration that may follow it.
Given Data / Assumptions:
- Three snippets claim to define a structure.
- No trailing variable declarations are present; only the definitions are considered.
- We evaluate exact syntax correctness.
Concept / Approach:The grammar requires a semicolon after a struct or enum declaration list. Omitting it causes a compilation error. Only snippet (1) shows the required semicolon; snippets (2) and (3) omit it.
Step-by-Step Solution:Inspect (1): has closing brace followed by semicolon → valid.Inspect (2) and (3): missing the terminating semicolon → invalid.Therefore, only option (1) is correct.
Verification / Alternative check:Compiling (2) or (3) results in a diagnostic such as “expected ‘;’ after struct declaration.” Adding the semicolon fixes the error.
Why Other Options Are Wrong:2, 3, and “All of the above” include definitions without the required semicolon.1 and 2: includes an invalid definition.
Common Pitfalls:Forgetting the semicolon; confusing a struct definition with a variable declaration that might follow on the same line (e.g., struct T { ... } x;).
Final Answer:1