简体   繁体   中英

How to check in a single line that none of the pre-processor macros are defined?

It's easy to check in a single line if at least one macro is defined:

#if defined(A) || defined(B) || defined(C)
    // do something
#endif

Also checking in a single line if at least one of the macros is not defined:

#if !defined(A) || !defined(B) || !defined(C)
    // do something
#endif

Question: How to check in a single line that none of the macros are defined?

I can do it with ifndefs in three lines as follows:

#ifndef A
#ifndef B
#ifndef C
    // do something
#endif
#endif
#endif

But how to join three ifndefs into a single line?

Emulating your nested #ifndef 's:

#if !defined(A) && !defined(B) && !defined(C)
  // do something
#endif

This checks that none are defined. You say you want "at least one isn't defined", but that's covered by your example with || s.

nested #ifndef can be joined on the same line with just && :

#if !defined(A) && !defined(B) && !defined(C)

#endif

Question: How to check in a single line that none of the macros are defined?

  • To check if one macro is defined: #if defined A .
  • To check if one macro is not defined: #if !defined A . "if not defined A".
  • To check if several macros are not defined: #if !defined A && !defined B && !defined C .
    "if not defined A and not defined B and not defined C"

Common sense usually gets you quite far in boolean algebra. To figure out the boolean equation for more complex cases, define truth tables. Example:

0 = false (not defined) 1 = true (defined)

A B C Output
0 0 0 1
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 0
1 1 0 0
1 1 1 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM