The self reference rule cuts this process short after

Info icon This preview shows pages 41–43. Sign up to view the full content.

View Full Document Right Arrow Icon
The self-reference rule cuts this process short after one step, at (4 + foo) . Therefore, this macro definition has the possibly useful effect of causing the program to add 4 to the value of foo wherever foo is referred to. In most cases, it is a bad idea to take advantage of this feature. A person reading the program who sees that foo is a variable will not expect that it is a macro as well. The reader will come across the identifier foo in the program and think its value should be that of the variable foo , whereas in fact the value is four greater. One common, useful use of self-reference is to create a macro which expands to itself. If you write #define EPERM EPERM then the macro EPERM expands to EPERM . Effectively, it is left alone by the preprocessor whenever it’s used in running text. You can tell that it’s a macro with ‘ #ifdef ’. You might do this if you want to define numeric constants with an enum , but have ‘ #ifdef ’ be true for each constant. If a macro x expands to use a macro y , and the expansion of y refers to the macro x , that is an indirect self-reference of x . x is not expanded in this case either. Thus, if we have #define x (4 + y) #define y (2 * x) then x and y expand as follows: x 7→ (4 + y) 7→ (4 + (2 * x)) y 7→ (2 * x) 7→ (2 * (4 + y)) Each macro is expanded when it appears in the definition of the other macro, but not when it indirectly appears in its own definition. 3.10.6 Argument Prescan Macro arguments are completely macro-expanded before they are substituted into a macro body, unless they are stringified or pasted with other tokens. After substitution, the en- tire macro body, including the substituted arguments, is scanned again for macros to be expanded. The result is that the arguments are scanned twice to expand macro calls in them. Most of the time, this has no effect. If the argument contained any macro calls, they are expanded during the first scan. The result therefore contains no macro calls, so the second scan does not change it. If the argument were substituted as given, with no prescan, the single remaining scan would find the same macro calls and produce the same results. You might expect the double scan to change the results when a self-referential macro is used in an argument of another macro (see Section 3.10.5 [Self-Referential Macros], page 36 ):
Image of page 41

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

View Full Document Right Arrow Icon
Chapter 3: Macros 38 the self-referential macro would be expanded once in the first scan, and a second time in the second scan. However, this is not what happens. The self-references that do not expand in the first scan are marked so that they will not expand in the second scan either. You might wonder, “Why mention the prescan, if it makes no difference? And why not skip it and make the preprocessor faster?” The answer is that the prescan does make a difference in three special cases: Nested calls to a macro.
Image of page 42
Image of page 43
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