Home > Software engineering >  Is double quote (") a preprocessing-token or an unterminated string literal?
Is double quote (") a preprocessing-token or an unterminated string literal?

Time:04-12

Is double quote (") a preprocessing-token or an unterminated string literal?

C11, 6.4 Lexical elements, Syntax, 1:

preprocessing-token:
    header-name
    identifier
    pp-number
    character-constant
    string-literal
    punctuator
    each non-white-space character that cannot be one of the above

C11, 6.4.5 String literals, Syntax, 1:

string-literal:
    encoding-prefix(opt) " s-char-sequence(opt) "

Note: GCC considers it to be an unterminated string literal:

#if 0
"
#endif

produces:

warning: missing terminating " character

CodePudding user response:

C 2018 6.4.1 3 says one category of preprocessing tokens is “single non-white-space characters that do not lexically match the other preprocessing token categories”, and the next sentence says “If a or a " character matches the last category, the behavior is undefined.” Thus, if an isolated " appears (one that is not paired with another " with an s-char-sequence between them), it fails to match the lexical form of a string literal and is parsed as a single non-white-space character that does not match the other categories, and the behavior is not defined by the standard. This explains the GCC message.

(I note that 6.10.2 3 describes # include directives with “" q-char-sequence " new-line”. However, the earlier grammar in 6.10 1 describes the directives as “# include pp-tokens new-line”. My interpretation of this is that the directive is parsed as having preprocessor token(s), notably a string literal, and 6.10.2 3 says that if that string literal has the form shown as “" q-char-sequence "”, then it is a # include directive of the type that paragraph is discussing.)

  • Related