{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Passes two arguments to macro arrayx y and x 1 if you

Info iconThis preview shows pages 20–22. Sign up to view the full content.

View Full Document Right Arrow Icon
passes two arguments to macro : array[x = y and x + 1] . If you want to supply array[x = y, x + 1] as an argument, you can write it as array[(x = y, x + 1)] , which is equivalent C code. All arguments to a macro are completely macro-expanded before they are substituted into the macro body. After substitution, the complete text is scanned again for macros to expand, including the arguments. This rule may seem strange, but it is carefully designed so you need not worry about whether any function call is actually a macro invocation. You can run into trouble if you try to be too clever, though. See Section 3.10.6 [Argument Prescan], page 37 , for detailed discussion. For example, min (min (a, b), c) is first expanded to min (((a) < (b) ? (a) : (b)), (c)) and then to ((((a) < (b) ? (a) : (b))) < (c) ? (((a) < (b) ? (a) : (b))) : (c)) (Line breaks shown here for clarity would not actually be generated.) You can leave macro arguments empty; this is not an error to the preprocessor (but many macros will then expand to invalid code). You cannot leave out arguments entirely; if a macro takes two arguments, there must be exactly one comma at the top level of its argument list. Here are some silly examples using min :
Background image of page 20

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 3: Macros 17 min(, b) 7→ (( ) < (b) ? ( ) : (b)) min(a, ) 7→ ((a ) < ( ) ? (a ) : ( )) min(,) 7→ (( ) < ( ) ? ( ) : ( )) min((,),) 7→ (((,)) < ( ) ? ((,)) : ( )) min() error macro "min" requires 2 arguments, but only 1 given min(,,) error macro "min" passed 3 arguments, but takes just 2 Whitespace is not a preprocessing token, so if a macro foo takes one argument, foo () and foo ( ) both supply it an empty argument. Previous GNU preprocessor implementa- tions and documentation were incorrect on this point, insisting that a function-like macro that takes a single argument be passed a space if an empty argument was required. Macro parameters appearing inside string literals are not replaced by their corresponding actual arguments. #define foo(x) x, "x" foo(bar) 7→ bar, "x" 3.4 Stringification Sometimes you may want to convert a macro argument into a string constant. Parameters are not replaced inside string constants, but you can use the ‘ # ’ preprocessing operator instead. When a macro parameter is used with a leading ‘ # ’, the preprocessor replaces it with the literal text of the actual argument, converted to a string constant. Unlike normal parameter replacement, the argument is not macro-expanded first. This is called stringification . There is no way to combine an argument with surrounding text and stringify it all together. Instead, you can write a series of adjacent string constants and stringified argu- ments. The preprocessor will replace the stringified arguments with string constants. The C compiler will then combine all the adjacent string constants into one long string. Here is an example of a macro definition that uses stringification: #define WARN_IF(EXP) \ do { if (EXP) \ fprintf (stderr, "Warning: " #EXP "\n"); } \ while (0) WARN_IF (x == 0); 7→ do { if (x == 0) fprintf (stderr, "Warning: " "x == 0" "\n"); } while (0); The argument for EXP is substituted once, as-is, into the if statement, and once, stringified, into the argument to fprintf . If x were a macro, it would be expanded in the if statement, but not in the string.
Background image of page 21
Image of page 22
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}