Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
#define FOO(a, b, c) bar(a, b, c)
/* ... */
FOO(arg1, arg2, arg3);

Wiki Markup
*PRE01-EX2:* Macro parameters cannot be individually parenthesized when concatenating tokens using the {{##}} operator, converting macro parameters to strings using the {{#}} operator, or concatenating adjacent string literals.  The {{JOIN()}} macro below concatenates both arguments to form a new token. The
SHOW
 {{SHOWA()}} macro converts the single argument into a string literal, which is then concatenated with the adjacent string literal to form the format specification in the call to {{printf()}}. The {{SHOWB()}} macro is an improved version of {{SHOWA()}} that supports invocations of the form {{SHOWB(i%j)}}.  It also eliminates the possibility of a format string vulnerability (see \[[FIO30-C. Exclude user input from format strings]]).

Code Block
#define JOIN(a, b) (a ## b)
#define SHOWSHOWA(a) printf(#a " = %d\n", a) 
#define SHOWB(b) printf("%s = %d\n", #b, b)

See PRE05-A. Understand macro replacement when concatenating tokens for more information on using the ## operator to concatenate tokens.

...