Swallowing The Semicolon - Red Hat ENTERPRISE LINUX 3 - USING CPP Using Instructions

Using cpp, the c preprocessor
Hide thumbs Also See for ENTERPRISE LINUX 3 - USING CPP:
Table of Contents

Advertisement

28
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
appearance of a C expression that would compute the size of the type of
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
when it was intended to be inside.
sizeof
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.9.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
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
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
This can cause trouble before
Suppose you write
:
\
\
\
\
SKIP_SPACES (p, lim)
SKIP_SPACES (p, lim);
statements, because the semicolon is actually a null statement.
else
sizeof ceil_div(1, 2)
ceil_div (1, 2)
says where to find it) across whitespace
p
. Strictly speaking, the call expands to a
Chapter 3. Macros
. That has the
, but in

Advertisement

Table of Contents
loading

This manual is also suitable for:

Enterprise linux 3

Table of Contents