Assuming that MACRO is not defined, are these equivalent
#ifdef MACRO
Not valid C or C++ code
#endif
/*
Not valid C or C++ code
*/
In GCC 4.7.1, it seems to be equivalent but are there preprocessors that do more?
Assuming that MACRO is not defined, are these equivalent
#ifdef MACRO
Not valid C or C++ code
#endif
/*
Not valid C or C++ code
*/
In GCC 4.7.1, it seems to be equivalent but are there preprocessors that do more?
Yes, they are equivalent, the preprocessing stage will eliminate
Not valid C or C++ code
before the compiler proper sees the code.Preprocessing involves the removal of comments, and code that is
#if
ed out.But if someone compiles the code with
-DMACRO
, the#ifdef
version gets you in trouble, better use#if 0
to remove code via the preprocessor.Yes, most pre-processors (if not all) will remove both, comments and directives evaluated to 0. The differences between the two are mostly functional.
My advice is to use directives to "comment" code (#if 0 {} #endif) and use comments just for commenting (quite logic right?). Main reasons are:
If MACRO is not defined they should be equivalent. A typical way of commenting out large chunks of code is usually:
This allows you to disable large parts of code even when they contain comments. So it's better than normal comments for disabling parts of code.
There's a caveat though. I've run into a compiler that choked on something like this:
Where "nonstandardpreprocessordirective" was a preprocessor directive that only works on GCC. I'm not exactly sure what the standard says about this, but it has caused problems in reality in the past. I do not remember which compiler though.
No, in your final code, there won't be any trace of the code inside the
#ifdef
:After precompilations:
There's no remaining code in there.
They are close, but not completely. assuming MACRO is not defined (or assuming you are using
#if 0
as recommended in other answers here):and comments:
Comments are for commenting,
#ifdef
are for disabling legal code. Aarbitrary text should not reside in the source at all.In the general case, both are equivalent.
However, if your "not valid C or C++ code" contains comments, the first form will work, whereas the second won't. That's because C standard forbids imbricated comments.
BTW,
#if 0
is often prefered to#ifdef MACRO
in that case.See this question.