cc-preprocessorpreprocessorpreprocessor-directive

Is there a C preprocessor which can replace contiguous else and ifdef directives?


I have a piece of code which looks like this,

#ifdef A
  printf("A");
#else
  #ifdef B
    printf("B");
  #endif
#endif

else and ifedef B can be replaced with elif defined B as per this. But, isn't there elifdef directive in C language?


NOTE: elif B is not suitable for this, it needs an expression; where as A, B are compile time switches with no value defined for them.


Solution

  • The directives are #elifdef and #elifndef, starting from C23. They are implemented in GCC 12, seem to be already implemented in Clang 13 according to cppreference, were proposed in N2645 and it are also part of the working draft N3096 in section 6.10.1 Conditional inclusion.

    The conditional expression inclusion preprocessing directives, #ifdef, #ifndef, #elifdef, and #elifndef directives are collectively known as the conditional inclusion preprocessing directives.

    #ifdef A
      printf("A");
    #elifdef B
      printf("B");
    #endif