Using defined(MACRO) inside the C if statement

前端 未结 3 927
伪装坚强ぢ
伪装坚强ぢ 2021-01-11 12:42

I would like to write code in C something like this:

if(defined(MACRO))
  ...
else
  ...

but I could not find any way to do this in

3条回答
  •  太阳男子
    2021-01-11 13:12

    Ok, based on the previous post I got this idea, which seems to work:

    #define DEFINEDX(NAME) ((#NAME)[0] == 0)
    #define DEFINED(NAME) DEFINEDX(NAME)
    

    This will check if NAME is defined and therefore it expands to the empty string with 0 at its first character, or it is undefined in which case it is not the empty string. This works with GCC, so one can write

    if( DEFINED(MACRO) )
      ...
    

提交回复
热议问题