Is #ifdef MACRO equivalent to a comment

2019-06-20 16:29发布

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?

8条回答
萌系小妹纸
2楼-- · 2019-06-20 16:33

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 #ifed 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.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-06-20 16:39

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:

  • Directives can be activated/deactivated just modifying 1 line in the code. Block comments require insertion/deletion of 2 elements in different lines of the code.
  • Directives can be nested preserving the IF logic and can also contain block comments. /Block comments/ cannot be nested and this can be problematic when you comment big pieces of code which can contain another comments.
#if 0
...
#if 1
#endif
...
#endif
  • Simple #if 0 directives can be easily converted in define or eval directives which allow a more dynamic conditional code processing.
//Classic verbose code line comment

#if 0
//Directive verbose line or block comment   
#endif

#define verbose 0
#if verbose
//Convenient eval directive to turn on/off this and other verbose blocks
#endif
  • Most IDEs do not highlight syntax of comment blocks but DO highlight syntax of directive code. Same happens with other features like indentation or autocompletion. This makes the readability of /**/ blocks quite poor in contrast to the #if 0 #endif blocks. Also the edition of commented code (e.g. adding a line or fixing something) is easier with directives.
查看更多
仙女界的扛把子
4楼-- · 2019-06-20 16:40

If MACRO is not defined they should be equivalent. A typical way of commenting out large chunks of code is usually:

#if 0
code(); /* possibly with comments. */
#endif

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:

#ifdef __GNUC__
#nonstandardpreprocessordirective
#endif

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.

查看更多
等我变得足够好
5楼-- · 2019-06-20 16:42

No, in your final code, there won't be any trace of the code inside the #ifdef:

// before
#ifdef MACRO
    Not valid C or C++ code
#endif
// after

After precompilations:

// before
// after

There's no remaining code in there.

查看更多
Evening l夕情丶
6楼-- · 2019-06-20 16:47

They are close, but not completely. assuming MACRO is not defined (or assuming you are using #if 0 as recommended in other answers here):

#ifdef MACRO
Not valid C or C++ code
*/  - does no harm
#endif  - oops
more invalid code
#endif

and comments:

/*
    Not valid C or C++ code
    #endif - does no harm
    */ - oops
*/

Comments are for commenting, #ifdef are for disabling legal code. Aarbitrary text should not reside in the source at all.

查看更多
做自己的国王
7楼-- · 2019-06-20 16:48

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.

 /* Comments /* inside */ comments are not allowed. */

BTW, #if 0 is often prefered to #ifdef MACRO in that case.

#if 0
    Invalid C source code
#endif

See this question.

查看更多
登录 后发表回答