{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Find it across whitespace characters#define

Info iconThis preview shows pages 39–41. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 ... The presence of two statements—the compound statement and a null statement—in between the if condition and the else makes invalid C code. The definition of the macro SKIP_SPACES can be altered to solve this problem, using a do ... while statement. Here is how: #define SKIP_SPACES(p, limit) \ do { char *lim = (limit); \ while (p < lim) { \ if (*p++ != ’ ’) { \ p--; break; }}} \ Chapter 3: Macros 36 while (0) Now SKIP_SPACES (p, lim); expands into do {...} while (0); which is one statement. The loop executes exactly once; most compilers generate no extra code for it. 3.10.4 Duplication of Side Effects Many C programs define a macro min , for “minimum”, like this: #define min(X, Y) ((X) < (Y) ? (X) : (Y)) When you use this macro with an argument containing a side effect, as shown here, next = min (x + y, foo (z)); it expands as follows: next = ((x + y) < (foo (z)) ? (x + y) : (foo (z))); where x + y has been substituted for X and foo (z) for Y . The function foo is used only once in the statement as it appears in the program, but the expression foo (z) has been substituted twice into the macro expansion. As a result, foo might be called two times when the statement is executed. If it has side effects or if it takes a long time to compute, the results might not be what you intended. We say that min is an unsafe macro. The best solution to this problem is to define min in a way that computes the value of foo (z) only once. The C language offers no standard way to do this, but it can be done with GNU extensions as follows: #define min(X, Y) \ ({ typeof (X) x_ = (X); \ typeof (Y) y_ = (Y); \ (x_ < y_) ? x_ : y_; }) The ‘ ({ ... }) ’ notation produces a compound statement that acts as an expression. Its value is the value of its last statement. This permits us to define local variables and assign each argument to one. The local variables have underscores after their names to reduce the risk of conflict with an identifier of wider scope (it is impossible to avoid this entirely). Now each argument is evaluated exactly once....
View Full Document

{[ snackBarMessage ]}

Page39 / 83

find it across whitespace characters#define SKIP_SPACES(p...

This preview shows document pages 39 - 41. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online