We say that nested calls to a macro occur when a

Info icon This preview shows pages 42–44. Sign up to view the full content.

View Full Document Right Arrow Icon
We say that nested calls to a macro occur when a macro’s argument contains a call to that very macro. For example, if f is a macro that expects one argument, f (f (1)) is a nested pair of calls to f . The desired expansion is made by expanding f (1) and substituting that into the definition of f . The prescan causes the expected result to happen. Without the prescan, f (1) itself would be substituted as an argument, and the inner use of f would appear during the main scan as an indirect self-reference and would not be expanded. Macros that call other macros that stringify or concatenate. If an argument is stringified or concatenated, the prescan does not occur. If you want to expand a macro, then stringify or concatenate its expansion, you can do that by causing one macro to call another macro that does the stringification or concatenation. For instance, if you have #define AFTERX(x) X_ ## x #define XAFTERX(x) AFTERX(x) #define TABLESIZE 1024 #define BUFSIZE TABLESIZE then AFTERX(BUFSIZE) expands to X_BUFSIZE , and XAFTERX(BUFSIZE) expands to X_ 1024 . (Not to X_TABLESIZE . Prescan always does a complete expansion.) Macros used in arguments, whose expansions contain unshielded commas. This can cause a macro expanded on the second scan to be called with the wrong number of arguments. Here is an example: #define foo a,b #define bar(x) lose(x) #define lose(x) (1 + (x)) We would like bar(foo) to turn into (1 + (foo)) , which would then turn into (1 + (a,b)) . Instead, bar(foo) expands into lose(a,b) , and you get an error because lose requires a single argument. In this case, the problem is easily solved by the same parentheses that ought to be used to prevent misnesting of arithmetic operations: #define foo (a,b) or #define bar(x) lose((x)) The extra pair of parentheses prevents the comma in foo ’s definition from being inter- preted as an argument separator. 3.10.7 Newlines in Arguments The invocation of a function-like macro can extend over many logical lines. However, in the present implementation, the entire expansion comes out on one line. Thus line numbers emitted by the compiler or debugger refer to the line the invocation started on, which might be different to the line containing the argument causing the problem.
Image of page 42

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

View Full Document Right Arrow Icon
Chapter 4: Conditionals 39 Here is an example illustrating this: #define ignore_second_arg(a,b,c) a; c ignore_second_arg (foo (), ignored (), syntax error); The syntax error triggered by the tokens syntax error results in an error message citing line three—the line of ignore second arg— even though the problematic code comes from line five. We consider this a bug, and intend to fix it in the near future. 4 Conditionals A conditional is a directive that instructs the preprocessor to select whether or not to include a chunk of code in the final token stream passed to the compiler. Preprocessor conditionals can test arithmetic expressions, or whether a name is defined as a macro, or both simultaneously using the special defined operator.
Image of page 43
Image of page 44
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