The arguments to a macro should not include preprocessor directives, such as {{\ Wiki Markup #define
}}, {{\#ifdef
}}, and {{\#include
}}. Doing so is [undefined behavior|BB. Definitions#undefined behavior], according to Section 6.10.3, Paragraph 11 of the C99 Standard \[ [ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\].
The sequence of preprocessing tokens bounded by the outside-most matching parentheses forms the list of arguments for the function-like macro. The individual arguments within the list are separated by comma preprocessing tokens, but comma preprocessing tokens between matching inner parentheses do not separate arguments. If there are sequences of preprocessing tokens within the list of arguments that would otherwise act as preprocessing directives, the behavior is undefined.
...
Noncompliant Code Example
...
In this noncompliant code example \[ [GCC Bugs|http://gcc.gnu.org/bugs.html#nonbugs_c]\], the author uses preprocessor directives to specify platform-specific arguments to {{memcpy()
}}. However, if {{memcpy()
}} is implemented using a macro, the code results in undefined behavior.
Code Block | ||||
---|---|---|---|---|
| ||||
memcpy(dest, src, #ifdef PLATFORM1 12 #else 24 #endif ); |
Compliant Code Example
In this compliant solution \ [[GCC Bugs|http://gcc.gnu.org/bugs.html#nonbugs_c]\], the appropriate call to {{ Wiki Markup memcpy()
}} is determined outside the function call.
Code Block | ||||
---|---|---|---|---|
| ||||
#ifdef PLATFORM1 memcpy(dest, src, 12); #else memcpy(dest, src, 24); #endif |
...
ISO/IEC 9899:1999 Section 6.10.3.1, "Argument substitution," paragraph 11
Bibliography
...
\[[GCC Bugs|http://gcc.gnu.org/bugs.html#nonbugs_c]\] "Non-bugs"
...
PRE31-C. Avoid side-effects in arguments to unsafe macros 01. Preprocessor (PRE) 02. Declarations and Initialization (DCL)