Machine-translated by Google. Questions are authored in English.
Top

Discussion

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

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.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.