Check for macro value C

I need to write some code to make sure the macro is defined, but empty (without any values). The test should not be at compile time.

I am trying to write:

#if (funcprototype == "")
MY_WARN("funcprototype is empty");
#endif

the code does not compile because it funcprototypeexpands to empty.

+5
source share
1 answer

If the runtime check is ok, then you can check the length of the string replacement:

#define REAL_STRINGIZE(x) #x
#define STRINGIZE(x) REAL_STRINGIZE(x)

if (STRINGIZE(funcprototype)[0] == '\0') {
    // funcprototype expanded to an empty replacement list
}
else {
    // funcprototype expanded to a non-empty replacement list
}

I don’t think there is a general case, "this macro is replaced by an empty sequence of tokens." This is a similar problem: "is it possible to compare two sequences of tokens for equality", which is impossible to do at compile time.

+5
source

All Articles