How to comment #if, #else, #endif preprocessor design?

Imagine a C preprocessor block:

#if defined( NAME )
...
#else  // comment-else
...
#endif  // comment-endif

Such blocks can be quite large and confusing.

To clarify intent and behavior, how could you write comment-else and comment-endif as a NAME expression?

Note. I must add that I am particularly interested in more complex cases with combined expressions and nesting.

+3
source share
5 answers
#if defined(NAME)
#else // defined(NAME)
#endif // defined(NAME)

This, if used in your source, is very clear.

+12
source

Em..

#if defined( NAME )
...
#else  // NAME
...
#endif  // NAME

cannot become more obvious than that ...

+6
source
#if defined(NAME)
    ...
#else
    // !defined(NAME)
    ...
#endif // defined(NAME)

, #else // condition , #elif condition. , , ( , , SO), , , . .

+2

If you reorganize the "large and complex" code to separate functions, you can easily see the entire section at once.

#if defined NAME
    do_something_for_name();
#else
    do_something_without_name();
#endif

Really no special comment conventions are needed, right?

+2
source
#if defined( NAME )
...
#else  /* Else part of NAME */
...
#endif  /* End of NAME */

This is how I do it.

When reading / viewing code (provided that you use the VIM editor on Linux), % is (Shift + 5) easy for us to switch from #ifto #elseand to #endif.

+1
source

Source: https://habr.com/ru/post/1795463/


All Articles