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

Info icon This 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 :
Image of page 20

Info icon This 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.
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern