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?
Official answer and solution
- Official answer
- C
- From
- Official GATE 2026 answer key
Solution
Classify the error in each statement according to the compiler phase (lexical, syntax, or semantic analysis) that detects it.
- Statement S1 (
char *str1 = "Hello;): The opening double quote is never closed before the end of the line, so the scanner cannot form a valid string literal token. This is a lexical error. - Statement S2 (
char *str2 = "Hello;";): The semicolon inside quotes is part of the string literal, and the statement ends with a valid semicolon. S2 has no error. - Statement S3 (
int *str3 = "Hello";): S3 is lexically and syntactically well-formed, but initializes anint *pointer with a string literal of typechar *(char[6]), violating C type compatibility rules during semantic analysis.
Final answer
Option (C)
Common trap
Classifying an unterminated string literal in S1 as a syntactic error instead of a lexical error.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2026 Computer Science and Information Technology - Session 1 (CS1)
- Question
- 27 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.