Search code examples
cmacrosconditional-compilation

Do #ifdef, #ifndef and #undef work with function-like macros?


There are two types of macros - function-like macros and object-like macros. I can perform various checks/operations on object-like macros:

#ifdef _SOME_OBJECT_LIKE_MACRO    // check if some object-like macro is defined
#undef _SOME_OBJECT_LIKE_MACRO    // undefine some object-like macro
#endif

#define _SOME_OBJECT_LIKE_MACRO 0xff

Do #ifdef, #ifndef and #undef work with function-like macros? For example:

#ifdef _SOME_FUNCTION_LIKE_MACRO()  // check if some function-like macro is defined
#undef _SOME_FUNCTION_LIKE_MACRO()  // undefine some function-like macro
#endif

#define _SOME_FUNCTION_LIKE_MACRO() printf("This is such a useful macro!!!\n");

Does that work? If not, how can I...

  • check the existence of
  • check the nonexistence of
  • undefine

...a function-like macro?


Solution

  • #ifdef, #ifndef, and #undef work with function-like macros but you only need to use the bare macro name. There is no need for the parentheses:

    #ifdef _SOME_FUNCTION_LIKE_MACRO
    #undef _SOME_FUNCTION_LIKE_MACRO
    #endif
    

    Also see http://tigcc.ticalc.org/doc/cpp.html#SEC19.