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 CSE 2026Compiler Design › Lexical Analysis, Compilation Phases & LinkingVerified
(C) S1 \text{S1} S1 has a lexical error and S3 \text{S3} S3 has a semantic error
Classify each C statement by which compiler phase would flag it: a token that cannot even be formed (e.g. an unterminated string) is a lexical error; a well-formed but grammatically illegal construction is a syntactic error; a well-formed, grammatically legal statement that violates type rules is a semantic error. Then match the resulting classification against each option.
Statement S1 contains an unterminated string literal, which the lexical analyzer cannot tokenize, giving a lexical error. Statement S2 is a valid assignment of the string "Hello;" to a char pointer and has no error. Statement S3 is grammatically correct but assigns a char* literal to an int* variable, a type mismatch detected during semantic analysis. Hence S1 has a lexical error and S3 has a semantic error, matching option C.
Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.