{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

32 function like macros you can also define macros

Info icon This preview shows pages 19–21. Sign up to view the full content.

View Full Document Right Arrow Icon
3.2 Function-like Macros You can also define macros whose use looks like a function call. These are called function- like macros . To define a function-like macro, you use the same ‘ #define ’ directive, but you put a pair of parentheses immediately after the macro name. For example, #define lang_init() c_init() lang_init() 7→ c_init() A function-like macro is only expanded if its name appears with a pair of parentheses after it. If you write just the name, it is left alone. This can be useful when you have a function and a macro of the same name, and you wish to use the function sometimes. extern void foo(void); #define foo() /* optimized inline version */ ... foo(); funcptr = foo; Here the call to foo() will use the macro, but the function pointer will get the address of the real function. If the macro were to be expanded, it would cause a syntax error. If you put spaces between the macro name and the parentheses in the macro definition, that does not define a function-like macro, it defines an object-like macro whose expansion happens to begin with a pair of parentheses. #define lang_init () c_init() lang_init() 7→ () c_init()() The first two pairs of parentheses in this expansion come from the macro. The third is the pair that was originally after the macro invocation. Since lang_init is an object-like macro, it does not consume those parentheses.
Image of page 19

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

View Full Document Right Arrow Icon
Chapter 3: Macros 16 3.3 Macro Arguments Function-like macros can take arguments , just like true functions. To define a macro that uses arguments, you insert parameters between the pair of parentheses in the macro def- inition that make the macro function-like. The parameters must be valid C identifiers, separated by commas and optionally whitespace. To invoke a macro that takes arguments, you write the name of the macro followed by a list of actual arguments in parentheses, separated by commas. The invocation of the macro need not be restricted to a single logical line—it can cross as many lines in the source file as you wish. The number of arguments you give must match the number of parameters in the macro definition. When the macro is expanded, each use of a parameter in its body is replaced by the tokens of the corresponding argument. (You need not use all of the parameters in the macro body.) As an example, here is a macro that computes the minimum of two numeric values, as it is defined in many C programs, and some uses. #define min(X, Y) ((X) < (Y) ? (X) : (Y)) x = min(a, b); 7→ x = ((a) < (b) ? (a) : (b)); y = min(1, 2); 7→ y = ((1) < (2) ? (1) : (2)); z = min(a + 28, *p); 7→ z = ((a + 28) < (*p) ? (a + 28) : (*p)); (In this small example you can already see several of the dangers of macro arguments. See Section 3.10 [Macro Pitfalls], page 34 , for detailed explanations.) Leading and trailing whitespace in each argument is dropped, and all whitespace between the tokens of an argument is reduced to a single space. Parentheses within each argument must balance; a comma within such parentheses does not end the argument. However, there is no requirement for square brackets or braces to balance, and they do not prevent a comma from separating arguments. Thus, macro (array[x = y, x + 1])
Image of page 20
Image of page 21
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