Question:medium

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

Check where each error is detected: an unclosed quote is caught by the lexer (lexical error), while assigning a char* string literal to an int* variable is caught by type checking (semantic error).
Updated On: Jul 7, 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

The Correct Option is C

Solution and Explanation

Look at what each phase of compilation would catch in these three declarations.

In S1, char *str1 = 'Hello;, the quote that starts the string is never matched by a closing quote before the source line ends. The scanner cannot form a valid STRING token, so this is caught at the lexical analysis stage, a lexical error.

In S2, char *str2 = 'Hello;';, the quotes are balanced: the string constant is 'Hello;', containing the characters H-e-l-l-o-; as its value, followed by the actual statement-terminating semicolon. Both the scanner and the parser accept this line without complaint.

In S3, int *str3 = 'Hello';, tokenization and parsing both succeed since 'Hello' is a well-formed literal. The problem only surfaces when the compiler checks types: a string literal decays to char *, but it is being stored in an int * variable. Pointer types disagree, so this is flagged during semantic analysis, not during scanning or parsing.

Thus S1 fails lexical analysis and S3 fails semantic analysis, matching option C.

Was this answer helpful?
0


Questions Asked in GATE CS exam