Search code examples
cpreprocessor

C preprocessor parameter bracket covering the 'defined' keyword


    #if (defined AAAA)       //rather than-  #if defined (AAAA)
    #define BBB (0)
    #else
    #define BBB (1)
    #endif

In the first line above, the parameter bracket includes the "defined" keyword, is there a scenario where this can cause any problem? I know the usual way would be-

#if defined (AAAA)

Solution

  • defined may be used as an ordinary operator inside #if directives.

    C 2018 6.10.1 1 says:

    The expression that controls conditional inclusion shall be an integer constant expression except that: identifiers (including those lexically identical to keywords) are interpreted as described below and it may contain unary operator expressions of the form

    defined identifier

       or
    

    defined ( identifier )

    which evaluate to 1 if the identifier is currently defined as a macro name (that is, if it is predefined or if it has been the subject of a #define preprocessing directive without an intervening #undef directive with the same subject identifier), 0 if it is not.

    Thus, there is no requirement that a #if directive using defined have defined as the sole or top-level operator. It may appear anywhere in an expression, including inside parentheses or even as:

    #if defined x + defined y + defined z == 2
    

    which would test whether exactly two of x, y, and z are defined.

    Also note that parentheses are not required; using defined (AAAA) instead of defined AAAA just adds clutter.