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?

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
collegedunia
Verified By Collegedunia

The Correct Option is C

Solution and Explanation

Step 1: Examine Statement S1: char *str1 = 'Hello; The string literal is opened with a double quote but never closed before the line ends. An unterminated string literal is a lexical error because the scanner cannot recognize a complete string-literal token.

Step 2: Examine Statement S2: char *str2 = 'Hello;'; Here the string literal is 'Hello;' (the semicolon inside the quotes is just a character of the string), properly closed with a quote, followed by the real terminating semicolon. This line has no lexical or syntactic problem; it simply initializes str2 with the text Hello;.

Step 3: Examine Statement S3: int *str3 = 'Hello'; The literal 'Hello' is a valid, well-formed string constant, so lexically and syntactically the statement is fine. However, a string literal has type char *, and it is being assigned to a variable of type int *. This type mismatch is a semantic error (most compilers issue a warning for the incompatible pointer type, and strict compilers reject it).

Step 4: Putting these together: S1 has a lexical error and S3 has a semantic error, which is exactly option C.

\[ \boxed{\text{Option C is correct}} \]

Was this answer helpful?
0
0

Top GATE CS Computer Science and IT Engineering Questions

View More Questions

Top GATE CS Programming in C Questions