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?
GATE CS - 2026,
Answer: C
The correct option is S1 has a lexical error and S3 has a semantic error.
In S1 the string literal "Hello; is never closed before the end of the line. The lexical analyser cannot form a valid string token, so this is a lexical error.
S2 is valid: the semicolon inside the quotes is part of the string, and the statement ends with its own semicolon.
S3 is well formed syntactically, but it initialises an int * with a string literal, whose type is a char array that decays to char *. That type mismatch is detected during semantic analysis, so it is a semantic error.
Calling S1's error syntactic, or saying S2 has any error, misplaces the phase that catches each fault.
Please log in or register to report this question.