You create macros with the define directive define is

Info icon This preview shows pages 18–20. Sign up to view the full content.

You create macros with the ‘ #define ’ directive. ‘ #define ’ is followed by the name of the macro and then the token sequence it should be an abbreviation for, which is variously referred to as the macro’s body , expansion or replacement list . For example, #define BUFFER_SIZE 1024 defines a macro named BUFFER_SIZE as an abbreviation for the token 1024 . If somewhere after this ‘ #define ’ directive there comes a C statement of the form foo = (char *) malloc (BUFFER_SIZE); then the C preprocessor will recognize and expand the macro BUFFER_SIZE . The C compiler will see the same tokens as it would if you had written foo = (char *) malloc (1024); By convention, macro names are written in uppercase. Programs are easier to read when it is possible to tell at a glance which names are macros. The macro’s body ends at the end of the ‘ #define ’ line. You may continue the definition onto multiple lines, if necessary, using backslash-newline. When the macro is expanded, however, it will all come out on one line. For example, #define NUMBERS 1, \ 2, \ 3 int x[] = { NUMBERS }; 7→ int x[] = { 1, 2, 3 }; The most common visible consequence of this is surprising line numbers in error messages. There is no restriction on what can go in a macro body provided it decomposes into valid preprocessing tokens. Parentheses need not balance, and the body need not resemble valid C code. (If it does not, you may get error messages from the C compiler when you use the macro.) The C preprocessor scans your program sequentially. Macro definitions take effect at the place you write them. Therefore, the following input to the C preprocessor foo = X; #define X 4 bar = X; produces foo = X; bar = 4; When the preprocessor expands a macro name, the macro’s expansion replaces the macro invocation, then the expansion is examined for more macros to expand. For example, #define TABLESIZE BUFSIZE #define BUFSIZE 1024 TABLESIZE 7→ BUFSIZE 7→ 1024 TABLESIZE is expanded first to produce BUFSIZE , then that macro is expanded to produce the final result, 1024 .
Image of page 18

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

Chapter 3: Macros 15 Notice that BUFSIZE was not defined when TABLESIZE was defined. The ‘ #define ’ for TABLESIZE uses exactly the expansion you specify—in this case, BUFSIZE —and does not check to see whether it too contains macro names. Only when you use TABLESIZE is the result of its expansion scanned for more macro names. This makes a difference if you change the definition of BUFSIZE at some point in the source file. TABLESIZE , defined as shown, will always expand using the definition of BUFSIZE that is currently in effect: #define BUFSIZE 1020 #define TABLESIZE BUFSIZE #undef BUFSIZE #define BUFSIZE 37 Now TABLESIZE expands (in two stages) to 37 . If the expansion of a macro contains its own name, either directly or via intermediate macros, it is not expanded again when the expansion is examined for more macros. This prevents infinite recursion. See Section 3.10.5 [Self-Referential Macros], page 36 , for the precise details.
Image of page 19
Image of page 20
This is the end of the preview. Sign up to access the rest of the document.
  • Summer '13
  • Noone
  • GCC, C preprocessor

{[ 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