Consider the following C statements: char str1 = "Hello; / Statement S1 / char…
2026
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?
- A.
S1 and S2 have syntactic errors
- B.
S2 has a lexical error and S3 has a syntactic error
- C.
S1 has a lexical error and S3 has a semantic error
- D.
S1 has a syntactic error and S3 has a semantic error
Attempted by 116 students.
Show answer & explanation
Correct answer: C
Statement S1 contains an unclosed string literal, which causes a lexical error because the tokenizer cannot identify the end of the token. Statement S3 attempts to assign a string literal to an integer variable, which is a type mismatch classified as a semantic error. Option 2 correctly identifies S1's lexical error and S3's semantic error, matching standard compiler behavior for these specific code snippets.