Note that // not a valid C90 comment. It was introduced in C99, so make sure your compiler and preprocessor know that they must use the C99 standard. In many cases, -std=c99 . (The question has been edited to make it clear)
Further, I don’t think the preprocessor cares about the comments. From the 6.10 specification of the C99, the grammar of the preprocessor directives is shown, and nowhere does it mention comments ...
The ANSI C standard clearly states that comments should be replaced in 2.1.1.2 phase 3 “Translation Phases” (5.1.1.2 to C99). (Figure from this other answer ).
- The source file is split into preprocessing tokens and a sequence of space characters (including comments). The source file should not end with a partial preprocessing marker or partial comment. Each comment is replaced by a single space character. Newline characters are saved. Regardless of whether each non-empty sequence of space characters other than a new line is preserved, or replaced by a single space, is determined by the implementation.
Older tools may not have been respected either because they preceded any C standard, or they had errors, or they interpreted the standard in different ways. They probably saved these bugs / quirks for backward compatibility. Testing with clang -E -std=c99 vs /usr/bin/cpp -std=c99 confirms this. They behave differently, despite the fact that the same compiler is under the hood.
$ /usr/bin/cpp --version Apple LLVM version 8.0.0 (clang-800.0.42.1) Target: x86_64-apple-darwin16.3.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin $ clang --version Apple LLVM version 8.0.0 (clang-800.0.42.1) Target: x86_64-apple-darwin16.3.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin $ ls -l /usr/bin/cpp -rwxr-xr-x 1 root wheel 18240 Dec 10 01:04 /usr/bin/cpp $ ls -l /usr/bin/clang -rwxr-xr-x 1 root wheel 18240 Dec 10 01:04 /usr/bin/clang $ /usr/bin/cpp -std=c99 test.c # 1 "test.c" # 1 "<built-in>" 1 # 1 "<built-in>" 3 # 330 "<built-in>" 3 # 1 "<command line>" 1 # 1 "<built-in>" 2 # 1 "test.c" 2 int foo[1 // hello there]; $ /usr/bin/clang -E -std=c99 test.c # 1 "test.c" # 1 "<built-in>" 1 # 1 "<built-in>" 3 # 331 "<built-in>" 3 # 1 "<command line>" 1 # 1 "<built-in>" 2 # 1 "test.c" 2 int foo[1];
I suspect you are calling clang as /usr/bin/cpp , causing compatibility with the / quirk error with the original cpp behavior set back when the behavior is unclear.
I think the lesson is to use cc -E rather than cpp to ensure consistent behavior.
source share