I was following a discussion on a forum where a person has asked whether the following generates lexical errors or not,

(1) char p="gate;

(2) int /* n;

(3) printf("%d, n);

When I tried to run them on my CPP compiler I got these errors respectively for the following 1, 2 and 3

enter image description here

enter image description here

enter image description here

When I looked them up on the internet they all are appearing to be Syntax errors.. Am I right? or are they lexical errors?


1 Answer 1


The C standard does not bother to distinguish between different kinds of errors encountered during parsing, but it is possible to categorise errors according to which clause of the standard applies.

The second line in your query (int /* n; -- assuming that there is no */ in the remaining program text) is covered by a requirement in the description of phases of translation, §; in particular, in phase 3: (emphasis added)

  1. The source file is decomposed into preprocessing tokens and sequences of white-space characters (including comments). A source file shall not end in a partial preprocessing token or in a partial comment. Each comment is replaced by one space character. New-line characters are retained. Whether each nonempty sequence of white-space characters other than new-line is retained or replaced by one space character is implementation-defined.

The other two errors are also produced in phase 3, because the unmatched quote cannot be decomposed into a preprocessing token. The error is mentioned specifically in §6.4/3:

  1. A token is the minimal lexical element of the language in translation phases 7 and 8. The categories of tokens are: keywords, identifiers, constants, string literals, and punctuators. A preprocessing token is the minimal lexical element of the language in translation phases 3 through 6. The categories of preprocessing tokens are: header names, identifiers, preprocessing numbers, character constants, string literals, punctuators, and single non-white-space characters that do not lexically match the other preprocessing token categories. If a ' or a " character matches the last category, the behavior is undefined.

That probably justifies the use of the phrase "lexical error", at least as an informal description.

  • $\begingroup$ Thanks a lot.. :) $\endgroup$
    – Turing101
    Oct 3, 2019 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.