For example define strangefile fprintf file s d

Info icon This preview shows pages 38–40. Sign up to view the full content.

View Full Document Right Arrow Icon
the macro body but ends outside of it. For example, #define strange(file) fprintf (file, "%s %d", ... strange(stderr) p, 35) 7→ fprintf (stderr, "%s %d", p, 35) The ability to piece together a macro call can be useful, but the use of unbalanced open parentheses in a macro body is just confusing, and should be avoided. 3.10.2 Operator Precedence Problems You may have noticed that in most of the macro definition examples shown above, each occurrence of a macro argument name had parentheses around it. In addition, another pair of parentheses usually surround the entire macro definition. Here is why it is best to write macros that way. Suppose you define a macro as follows, #define ceil_div(x, y) (x + y - 1) / y whose purpose is to divide, rounding up. (One use for this operation is to compute how many int objects are needed to hold a certain number of char objects.) Then suppose it is used as follows: a = ceil_div (b & c, sizeof (int)); 7→ a = (b & c + sizeof (int) - 1) / sizeof (int); This does not do what is intended. The operator-precedence rules of C make it equivalent to this:
Image of page 38

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

View Full Document Right Arrow Icon
Chapter 3: Macros 35 a = (b & (c + sizeof (int) - 1)) / sizeof (int); What we want is this: a = ((b & c) + sizeof (int) - 1)) / sizeof (int); Defining the macro as #define ceil_div(x, y) ((x) + (y) - 1) / (y) provides the desired result. Unintended grouping can result in another way. Consider sizeof ceil_div(1, 2) . That has the appearance of a C expression that would compute the size of the type of ceil_div (1, 2) , but in fact it means something very different. Here is what it expands to: sizeof ((1) + (2) - 1) / (2) This would take the size of an integer and divide it by two. The precedence rules have put the division outside the sizeof when it was intended to be inside. Parentheses around the entire macro definition prevent such problems. Here, then, is the recommended way to define ceil_div : #define ceil_div(x, y) (((x) + (y) - 1) / (y)) 3.10.3 Swallowing the Semicolon Often it is desirable to define a macro that expands into a compound statement. Consider, for example, the following macro, that advances a pointer (the argument p says where to find it) across whitespace characters: #define SKIP_SPACES(p, limit) \ { char *lim = (limit); \ while (p < lim) { \ if (*p++ != ’ ’) { \ p--; break; }}} Here backslash-newline is used to split the macro definition, which must be a single logical line, so that it resembles the way such code would be laid out if not part of a macro definition. A call to this macro might be SKIP_SPACES (p, lim) . Strictly speaking, the call expands to a compound statement, which is a complete statement with no need for a semicolon to end it. However, since it looks like a function call, it minimizes confusion if you can use it like a function call, writing a semicolon afterward, as in SKIP_SPACES (p, lim); This can cause trouble before else statements, because the semicolon is actually a null statement. Suppose you write if (*p != 0) SKIP_SPACES (p, lim); else ...
Image of page 39
Image of page 40
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