GATE 2026 CS (CS1) – Question 27
Consider the following C statements:
`char *str1 = "Hello; /* Statement S1 */`
`char *str2 = "Hello;"; /* Statement S2 */`
`int *str3 = "Hello"; /* Statement S3 */`
Which of the following statements is/are true?
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (C) S1 has a lexical error and S3 has a semantic error
Explanation
- In **S1**: The string literal `"Hello;` is missing its closing double quote before the end of the line. The lexical analyzer detects an unterminated string literal, which is a **lexical error**.
- In **S2**: `char *str2 = "Hello;";` is valid C syntax and semantics (string containing semicolon). It has no errors.
- In **S3**: `int *str3 = "Hello";` is syntactically valid according to the C grammar for declarations with initializers, but violates type compatibility rules (assigning `char *` / array of char to `int *` without an explicit cast). Type mismatch is checked during semantic analysis and constitutes a **semantic error** (type error).
Therefore, option (C) correctly identifies S1 as having a lexical error and S3 as having a semantic error.