2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
6 .. index:: problems with macros, pitfalls of macros
13 In this section we describe some special rules that apply to macros and
14 macro expansion, and point out certain cases in which the rules have
15 counter-intuitive consequences that you must watch out for.
26 When a macro is called with arguments, the arguments are substituted
27 into the macro body and the result is checked, together with the rest of
28 the input file, for more macro calls. It is possible to piece together
29 a macro call coming partially from the macro body and partially from the
30 arguments. For example,
34 #define twice(x) (2*(x))
35 #define call_with_1(x) x(1)
40 Macro definitions do not have to have balanced parentheses. By writing
41 an unbalanced open parenthesis in a macro body, it is possible to create
42 a macro call that begins inside the macro body but ends outside of it.
47 #define strange(file) fprintf (file, "%s %d",
49 strange(stderr) p, 35)
50 → fprintf (stderr, "%s %d", p, 35)
52 The ability to piece together a macro call can be useful, but the use of
53 unbalanced open parentheses in a macro body is just confusing, and
56 .. index:: parentheses in macro bodies
58 .. _operator-precedence-problems:
60 Operator Precedence Problems
61 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
63 You may have noticed that in most of the macro definition examples shown
64 above, each occurrence of a macro argument name had parentheses around
65 it. In addition, another pair of parentheses usually surround the
66 entire macro definition. Here is why it is best to write macros that
69 Suppose you define a macro as follows,
73 #define ceil_div(x, y) (x + y - 1) / y
75 whose purpose is to divide, rounding up. (One use for this operation is
76 to compute how many ``int`` objects are needed to hold a certain
77 number of ``char`` objects.) Then suppose it is used as follows:
81 a = ceil_div (b & c, sizeof (int));
82 → a = (b & c + sizeof (int) - 1) / sizeof (int);
84 This does not do what is intended. The operator-precedence rules of
85 C make it equivalent to this:
89 a = (b & (c + sizeof (int) - 1)) / sizeof (int);
95 a = ((b & c) + sizeof (int) - 1)) / sizeof (int);
101 #define ceil_div(x, y) ((x) + (y) - 1) / (y)
103 provides the desired result.
105 Unintended grouping can result in another way. Consider ``sizeof
106 ceil_div(1, 2)``. That has the appearance of a C expression that would
107 compute the size of the type of ``ceil_div (1, 2)``, but in fact it
108 means something very different. Here is what it expands to:
112 sizeof ((1) + (2) - 1) / (2)
114 This would take the size of an integer and divide it by two. The
115 precedence rules have put the division outside the ``sizeof`` when it
116 was intended to be inside.
118 Parentheses around the entire macro definition prevent such problems.
119 Here, then, is the recommended way to define ``ceil_div`` :
123 #define ceil_div(x, y) (((x) + (y) - 1) / (y))
125 .. index:: semicolons (after macro calls)
127 .. _swallowing-the-semicolon:
129 Swallowing the Semicolon
130 ^^^^^^^^^^^^^^^^^^^^^^^^
132 Often it is desirable to define a macro that expands into a compound
133 statement. Consider, for example, the following macro, that advances a
134 pointer (the argument ``p`` says where to find it) across whitespace
139 #define SKIP_SPACES(p, limit) \
140 { char *lim = (limit); \
145 Here backslash-newline is used to split the macro definition, which must
146 be a single logical line, so that it resembles the way such code would
147 be laid out if not part of a macro definition.
149 A call to this macro might be ``SKIP_SPACES (p, lim)``. Strictly
150 speaking, the call expands to a compound statement, which is a complete
151 statement with no need for a semicolon to end it. However, since it
152 looks like a function call, it minimizes confusion if you can use it
153 like a function call, writing a semicolon afterward, as in
154 ``SKIP_SPACES (p, lim);``
156 This can cause trouble before ``else`` statements, because the
157 semicolon is actually a null statement. Suppose you write
162 SKIP_SPACES (p, lim);
165 The presence of two statements---the compound statement and a null
166 statement---in between the ``if`` condition and the ``else``
167 makes invalid C code.
169 The definition of the macro ``SKIP_SPACES`` can be altered to solve
170 this problem, using a ``do ... while`` statement. Here is how:
174 #define SKIP_SPACES(p, limit) \
175 do { char *lim = (limit); \
181 Now ``SKIP_SPACES (p, lim);`` expands into
187 which is one statement. The loop executes exactly once; most compilers
188 generate no extra code for it.
190 .. index:: side effects (in macro arguments), unsafe macros
192 .. _duplication-of-side-effects:
194 Duplication of Side Effects
195 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
197 Many C programs define a macro ``min``, for 'minimum', like this:
201 #define min(X, Y) ((X) < (Y) ? (X) : (Y))
203 When you use this macro with an argument containing a side effect,
208 next = min (x + y, foo (z));
210 it expands as follows:
214 next = ((x + y) < (foo (z)) ? (x + y) : (foo (z)));
216 where ``x + y`` has been substituted for ``X`` and ``foo (z)``
219 The function ``foo`` is used only once in the statement as it appears
220 in the program, but the expression ``foo (z)`` has been substituted
221 twice into the macro expansion. As a result, ``foo`` might be called
222 two times when the statement is executed. If it has side effects or if
223 it takes a long time to compute, the results might not be what you
224 intended. We say that ``min`` is an :dfn:`unsafe` macro.
226 The best solution to this problem is to define ``min`` in a way that
227 computes the value of ``foo (z)`` only once. The C language offers
228 no standard way to do this, but it can be done with GNU extensions as
234 ({ typeof (X) x_ = (X); \
235 typeof (Y) y_ = (Y); \
236 (x_ < y_) ? x_ : y_; })
238 The :samp:`({ ... })` notation produces a compound statement that
239 acts as an expression. Its value is the value of its last statement.
240 This permits us to define local variables and assign each argument to
241 one. The local variables have underscores after their names to reduce
242 the risk of conflict with an identifier of wider scope (it is impossible
243 to avoid this entirely). Now each argument is evaluated exactly once.
245 If you do not wish to use GNU C extensions, the only solution is to be
246 careful when *using* the macro ``min``. For example, you can
247 calculate the value of ``foo (z)``, save it in a variable, and use
248 that variable in ``min`` :
252 #define min(X, Y) ((X) < (Y) ? (X) : (Y))
256 next = min (x + y, tem);
259 (where we assume that ``foo`` returns type ``int``).
261 .. index:: self-reference
263 .. _self-referential-macros:
265 Self-Referential Macros
266 ^^^^^^^^^^^^^^^^^^^^^^^
268 A :dfn:`self-referential` macro is one whose name appears in its
269 definition. Recall that all macro definitions are rescanned for more
270 macros to replace. If the self-reference were considered a use of the
271 macro, it would produce an infinitely large expansion. To prevent this,
272 the self-reference is not considered a macro call. It is passed into
273 the preprocessor output unchanged. Consider an example:
277 #define foo (4 + foo)
279 where ``foo`` is also a variable in your program.
281 Following the ordinary rules, each reference to ``foo`` will expand
282 into ``(4 + foo)`` ; then this will be rescanned and will expand into
283 ``(4 + (4 + foo))`` ; and so on until the computer runs out of memory.
285 The self-reference rule cuts this process short after one step, at
286 ``(4 + foo)``. Therefore, this macro definition has the possibly
287 useful effect of causing the program to add 4 to the value of ``foo``
288 wherever ``foo`` is referred to.
290 In most cases, it is a bad idea to take advantage of this feature. A
291 person reading the program who sees that ``foo`` is a variable will
292 not expect that it is a macro as well. The reader will come across the
293 identifier ``foo`` in the program and think its value should be that
294 of the variable ``foo``, whereas in fact the value is four greater.
296 One common, useful use of self-reference is to create a macro which
297 expands to itself. If you write
303 then the macro ``EPERM`` expands to ``EPERM``. Effectively, it is
304 left alone by the preprocessor whenever it's used in running text. You
305 can tell that it's a macro with :samp:`#ifdef`. You might do this if you
306 want to define numeric constants with an ``enum``, but have
307 :samp:`#ifdef` be true for each constant.
309 If a macro ``x`` expands to use a macro ``y``, and the expansion of
310 ``y`` refers to the macro ``x``, that is an :dfn:`indirect
311 self-reference` of ``x``. ``x`` is not expanded in this case
312 either. Thus, if we have
319 then ``x`` and ``y`` expand as follows:
329 Each macro is expanded when it appears in the definition of the other
330 macro, but not when it indirectly appears in its own definition.
332 .. index:: expansion of arguments, macro argument expansion, prescan of macro arguments
334 .. _argument-prescan:
339 Macro arguments are completely macro-expanded before they are
340 substituted into a macro body, unless they are stringized or pasted
341 with other tokens. After substitution, the entire macro body, including
342 the substituted arguments, is scanned again for macros to be expanded.
343 The result is that the arguments are scanned *twice* to expand
346 Most of the time, this has no effect. If the argument contained any
347 macro calls, they are expanded during the first scan. The result
348 therefore contains no macro calls, so the second scan does not change
349 it. If the argument were substituted as given, with no prescan, the
350 single remaining scan would find the same macro calls and produce the
353 You might expect the double scan to change the results when a
354 self-referential macro is used in an argument of another macro
355 (see :ref:`self-referential-macros`): the self-referential macro would be
356 expanded once in the first scan, and a second time in the second scan.
357 However, this is not what happens. The self-references that do not
358 expand in the first scan are marked so that they will not expand in the
361 You might wonder, 'Why mention the prescan, if it makes no difference?
362 And why not skip it and make the preprocessor faster?' The answer is
363 that the prescan does make a difference in three special cases:
365 * Nested calls to a macro.
367 We say that :dfn:`nested` calls to a macro occur when a macro's argument
368 contains a call to that very macro. For example, if ``f`` is a macro
369 that expects one argument, ``f (f (1))`` is a nested pair of calls to
370 ``f``. The desired expansion is made by expanding ``f (1)`` and
371 substituting that into the definition of ``f``. The prescan causes
372 the expected result to happen. Without the prescan, ``f (1)`` itself
373 would be substituted as an argument, and the inner use of ``f`` would
374 appear during the main scan as an indirect self-reference and would not
377 * Macros that call other macros that stringize or concatenate.
379 If an argument is stringized or concatenated, the prescan does not
380 occur. If you *want* to expand a macro, then stringize or
381 concatenate its expansion, you can do that by causing one macro to call
382 another macro that does the stringizing or concatenation. For
383 instance, if you have
387 #define AFTERX(x) X_ ## x
388 #define XAFTERX(x) AFTERX(x)
389 #define TABLESIZE 1024
390 #define BUFSIZE TABLESIZE
392 then ``AFTERX(BUFSIZE)`` expands to ``X_BUFSIZE``, and
393 ``XAFTERX(BUFSIZE)`` expands to ``X_1024``. (Not to
394 ``X_TABLESIZE``. Prescan always does a complete expansion.)
396 * Macros used in arguments, whose expansions contain unshielded commas.
398 This can cause a macro expanded on the second scan to be called with the
399 wrong number of arguments. Here is an example:
404 #define bar(x) lose(x)
405 #define lose(x) (1 + (x))
407 We would like ``bar(foo)`` to turn into ``(1 + (foo))``, which
408 would then turn into ``(1 + (a,b))``. Instead, ``bar(foo)``
409 expands into ``lose(a,b)``, and you get an error because ``lose``
410 requires a single argument. In this case, the problem is easily solved
411 by the same parentheses that ought to be used to prevent misnesting of
412 arithmetic operations:
417 or#define bar(x) lose((x))
419 The extra pair of parentheses prevents the comma in ``foo`` 's
420 definition from being interpreted as an argument separator.
422 .. index:: newlines in macro arguments
424 .. _newlines-in-arguments:
426 Newlines in Arguments
427 ^^^^^^^^^^^^^^^^^^^^^
429 The invocation of a function-like macro can extend over many logical
430 lines. However, in the present implementation, the entire expansion
431 comes out on one line. Thus line numbers emitted by the compiler or
432 debugger refer to the line the invocation started on, which might be
433 different to the line containing the argument causing the problem.
435 Here is an example illustrating this:
439 #define ignore_second_arg(a,b,c) a; c
441 ignore_second_arg (foo (),
445 The syntax error triggered by the tokens ``syntax error`` results in
446 an error message citing line three---the line of ignore_second_arg---
447 even though the problematic code comes from line five.
449 We consider this a bug, and intend to fix it in the near future.