The C preprocessor (cpp
) seems like it should handle this code correctly:
#define A 1 // hello there
int foo[A];
I would expect to replace A
with 1
.
What happens is that A
is replaced with 1 // hello there
, which results in the following output from cpp -std=c99 test.c
:
# 1 "test.c"
int foo[1 // hello there];
Which is not valid C and fails to compile.
How can I get cpp
to perform the proper replacement?
Note on compiler: Using cpp
from the latest (8.2.1, Dec 2016) Xcode on mac, so I doubt it's due to an outdated compiler.
According to MSDN, comments are replaced with a single space in the tokenization phase, which happens before the preprocessing phase where macros are expanded.
Removing comments : It removes all the comments. A comment is written only for the humans to understand the code. So, it is obvious that they are of no use to a machine. So, preprocessor removes all of them as they are not required in the execution and won't be executed as well.
You're most likely used to using the preprocessor to include files directly into other files, or #define constants, but the preprocessor can also be used to create "inlined" code using macros expanded at compile time and to prevent code from being compiled twice.
It makes no provision for either the library or the preprocessor."). The C99 specification handles this explicity, though. The comments are replaced with a single space in the "translation phase", which happens prior to the Preprocessing directive parsing. (Section 6.10 for details).
This is particularly true because preprocessor directives are rarely indented, so it can be hard to follow the flow of execution. On many compilers, the #pragma once directive can be used intead of include guards. The other major use of the preprocessor is to define macros.
// C++ program to remove comments from a C/C++ program #include <iostream> usingnamespacestd; string removeComments(string prgm) intn = prgm.length(); string res; // Flags to indicate that single line and multiple line comments // have started or not. bools_cmt = false; boolm_cmt = false; // Traverse the given program for(inti=0; i<n; i++)
Somewhat to my surprise, I can reproduce the problem on my Mac (macOS Sierra 10.12.2; Apple LLVM version 8.0.0 (clang-800.0.42.1)
) using /usr/bin/cpp
which is the XCode cpp
— but not using GNU cpp
(which I invoke using just cpp
).
Workarounds include:
/usr/bin/gcc -E -std=c99 test.c
This uses the clang
wrapper gcc
to run the C preprocessor and correctly handles the version. You could add a -v
option and see what it runs; I didn't see it running cpp
per se (it runs clang -cc1 -E
with lots of other information).
You can also use:
clang -E -std=c99 test.c
It's effectively the same thing.
You could also install GCC and use that instead of XCode. There are questions with answers about how to get that done (but it isn't for the faint of heart).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With