Is the @ character permitted in source C?

Obviously, having a compiler with the @ symbol will result in a syntax error (unless it is a comment or a string literal). However, if the character is found, for example. inside the #if 0 block, is the program technically?

I tried this:

 #define NOTHING(x) int main() { NOTHING(@@@@); return 0; } 

with -pedantic -Wall -Wextra , on both gcc and clang, and it did not give any warnings. I am not sure if this is guaranteed, or if they simply do not have a special warning.

I did not find anything in the standard, saying one way or another, which is disturbing. I donโ€™t want to base the tool on this just to find out that a compiler compatible with the standard clamps it.

+7
c c-preprocessor
source share
1 answer

You can include almost any character in a C program if it is interrupted or eliminated or compressed by a preprocessor. @ is a valid preprocessor token, so it will not be flagged as an error during compilation preprocessing stages.

The standard does not prevent the compiler from issuing warnings about anything, and it is possible that the compiler will issue a warning in this case. But I donโ€™t know what itโ€™s doing, and I consider it a problem of implementation quality.

Relevant standard sections:

  • The definition of the preprocessing token in section 6.4 is: โ€œeach character of a non-white space that cannot be one of the above.โ€ Please note that @@@@ is therefore four pre-processing tokens.

  • & sect; 6.4 / 2, emphasis added: "Each pre-processing token that is converted to a token must have the lexical form of a keyword, identifier, constant, string literal or punctuation." Until @ is converted to a token, an error does not occur. Conversion to a token occurs at step 7 of the compilation process (see Section 5.1.1.2/7). This happens after the macro expansion, which includes the structure, in step 4.

+9
source share

All Articles