Question:

Consider the following C statements:
char *str1 = "Hello;      /* Statement S1 */
char *str2 = "Hello;";    /* Statement S2 */
int  *str3 = "Hello";     /* Statement S3 */
Which of the following options is/are correct?

Show Hint

Ask at which compiler phase each line first fails: can the scanner even form valid tokens (lexical), does the token sequence match the grammar (syntactic), or is the shape fine but the types clash (semantic)?
Updated On: Jul 22, 2026
  • S1 and S2 have syntactic errors
  • S2 has a lexical error and S3 has a syntactic error
  • S1 has a lexical error and S3 has a semantic error
  • S1 has a syntactic error and S3 has a semantic error
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is C

Solution and Explanation

Step 1: Recall the three error categories a compiler can report.
A lexical error occurs when the scanner cannot form a valid token from the input characters (for example an unterminated string or character literal). A syntactic error occurs when a valid sequence of tokens does not match any rule of the language's grammar. A semantic error occurs when the code is lexically and syntactically valid but violates a type or meaning rule, such as assigning an incompatible type without a cast.

Step 2: Analyze statement S1: char *str1 = "Hello;.
The string literal that begins at the first double quote is never closed with a matching double quote before the end of the physical line. A C string literal cannot span a line break without an explicit continuation, so the scanner fails to tokenize this string at all -- it runs off the end of the line looking for a closing quote. This is a lexical error, because the failure happens while trying to form a token, before any grammar rule is even checked.

Step 3: Analyze statement S2: char *str2 = "Hello;";.
Here the string literal is properly opened and closed: it consists of the six characters Hello; (the semicolon is just an ordinary character sitting inside the string, not a statement terminator). After this closed string literal comes the real statement-terminating semicolon. The declaration char *str2 = "..."; is completely well-formed C: a character pointer initialized to point at a string literal. S2 has no error of any kind.

Step 4: Analyze statement S3: int *str3 = "Hello";.
This line is syntactically a perfectly legal pointer declaration with an initializer, so the parser accepts it without complaint. The problem is only revealed during type checking: "Hello" has type char *, but it is being assigned to a variable of type int *, and pointer types to different base types are incompatible without an explicit cast. This is a semantic (type) error, not a syntax error, because the grammar shape of the statement is fine; only the meaning/typing is wrong.

Step 5: Evaluate each option using S1 = lexical error, S2 = no error, S3 = semantic error.
(A) claims S1 and S2 both have syntactic errors -- wrong on both counts, since S1's error is lexical and S2 has no error at all.
(B) claims S2 has a lexical error and S3 has a syntactic error -- wrong on both counts, since S2 has no error and S3's error is semantic, not syntactic.
(C) claims S1 has a lexical error and S3 has a semantic error -- this matches the analysis exactly.
(D) claims S1 has a syntactic error and S3 has a semantic error -- half right, but S1's error is lexical (a tokenization failure), not a grammar-rule violation, so this option is not fully correct.

Step 6: Conclusion.
Only option (C) correctly classifies both errors.
\[ \boxed{\text{Correct option: (C)}} \]
Was this answer helpful?
0
0

Top GATE CS Compiler Design Questions

View More Questions