Are empty macro definitions allowed in C? How do they behave?

It is simply a macro that expands to, well, nothing. However, now that the macro has been defined you can check with #if defined (or #ifdef) whether it has been defined.

#define FOO

int main(){
    FOO FOO FOO
    printf("Hello world");
}

will expand to

int main(){

    printf("Hello world");
}

There are certain situations where this comes in very handy, for example additional debug information, which you don’t want to show in your release version:

/* Defined only during debug compilations: */
#define CONFIG_USE_DEBUG_MESSAGES

#ifdef CONFIG_USE_DEBUG_MESSAGES
#define DEBUG_MSG(x) print(x)
#else
#define DEBUG_MSG(x) do {} while(0)
#endif

int main(){
    DEBUG_MSG("Entering main");
    /* ... */
}

Since the macro CONFIG_USE_DEBUG_MESSAGES has been defined, DEBUG_MSG(x) will expand to print(x) and you will get Entering main. If you remove the #define, DEBUG_MSG(x) expands to an empty dowhile loop and you won’t see the message.

Leave a Comment