The arguments to a macro should not include preprocessor directives such as #define
, #ifdef
, and #include
. Doing so is undefined behavior. This includes using preprocessor directives in arguments to a function where it is unknown whether or not the function is implemented using a macro. Examples include standard library functions such as memcpy()
, printf()
, and assert()
.
...
Wiki Markup |
---|
In this noncompliant code example \[[Non-bugs in GCC C|http://gcc.gnu.org/bugs.html#nonbugs_c]\], the author is attempting to specify an argument to {{memcpy()}} depending on the current platform by using preprocessor directives within the function call. However, if {{memcpy()}} is implemented using a macro, the code will result in undefined behavior. |
Code Block |
---|
|
memcpy(dest, src,
#ifdef PLATFORM1
12
#else
24
#endif
);
|
...
Wiki Markup |
---|
In this compliant solution \[[Non-bugs in GCC C|http://gcc.gnu.org/bugs.html#nonbugs_c]\], the appropriate call to {{memcpy()}} is determined outside the function call. |
Code Block |
---|
|
#ifdef PLATFORM1
memcpy(dest, src, 12);
#else
memcpy(dest, src, 24);
#endif
|
...