]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/doc/cpp/macros/undefining-and-redefining-macros.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / gcc / doc / cpp / macros / undefining-and-redefining-macros.rst
CommitLineData
c63539ff
ML
1..
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.
5
6.. index:: undefining macros, redefining macros, #undef
7
8.. _undefining-and-redefining-macros:
9
10Undefining and Redefining Macros
11********************************
12
13If a macro ceases to be useful, it may be :dfn:`undefined` with the
14:samp:`#undef` directive. :samp:`#undef` takes a single argument, the
15name of the macro to undefine. You use the bare macro name, even if the
16macro is function-like. It is an error if anything appears on the line
17after the macro name. :samp:`#undef` has no effect if the name is not a
18macro.
19
20.. code-block::
21
22 #define FOO 4
23 x = FOO; → x = 4;
24 #undef FOO
25 x = FOO; → x = FOO;
26
27Once a macro has been undefined, that identifier may be :dfn:`redefined`
28as a macro by a subsequent :samp:`#define` directive. The new definition
29need not have any resemblance to the old definition.
30
31However, if an identifier which is currently a macro is redefined, then
32the new definition must be :dfn:`effectively the same` as the old one.
33Two macro definitions are effectively the same if:
34
35* Both are the same type of macro (object- or function-like).
36
37* All the tokens of the replacement list are the same.
38
39* If there are any parameters, they are the same.
40
41* Whitespace appears in the same places in both. It need not be
42 exactly the same amount of whitespace, though. Remember that comments
43 count as whitespace.
44
45These definitions are effectively the same:
46
47.. code-block:: c++
48
49 #define FOUR (2 + 2)
50 #define FOUR (2 + 2)
51 #define FOUR (2 /* two */ + 2)
52
53but these are not:
54
55.. code-block:: c++
56
57 #define FOUR (2 + 2)
58 #define FOUR ( 2+2 )
59 #define FOUR (2 * 2)
60 #define FOUR(score,and,seven,years,ago) (2 + 2)
61
62If a macro is redefined with a definition that is not effectively the
63same as the old one, the preprocessor issues a warning and changes the
64macro to use the new definition. If the new definition is effectively
65the same, the redefinition is silently ignored. This allows, for
66instance, two different headers to define a common macro. The
3ed1b4ce 67preprocessor will only complain if the definitions do not match.