3 @settitle The C Preprocessor
6 @dircategory Programming
8 * Cpp: (cpp). The GNU C preprocessor.
15 @setchapternewpage odd
17 This file documents the GNU C Preprocessor.
19 Copyright 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
20 1999, 2000 Free Software Foundation, Inc.
22 Permission is granted to make and distribute verbatim copies of
23 this manual provided the copyright notice and this permission notice
24 are preserved on all copies.
27 Permission is granted to process this file through Tex and print the
28 results, provided the printed document carries copying permission
29 notice identical to this one except for the removal of this paragraph
30 (this paragraph not being relevant to the printed manual).
33 Permission is granted to copy and distribute modified versions of this
34 manual under the conditions for verbatim copying, provided also that
35 the entire resulting derived work is distributed under the terms of a
36 permission notice identical to this one.
38 Permission is granted to copy and distribute translations of this manual
39 into another language, under the above conditions for modified versions.
44 @title The C Preprocessor
45 @subtitle Last revised July 2000
46 @subtitle for GCC version 2
47 @author Richard M. Stallman
50 This booklet is eventually intended to form the first chapter of a GNU
53 @vskip 0pt plus 1filll
54 @c man begin COPYRIGHT
55 Copyright @copyright{} 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996,
56 1997, 1998, 1999, 2000
57 Free Software Foundation, Inc.
59 Permission is granted to make and distribute verbatim copies of
60 this manual provided the copyright notice and this permission notice
61 are preserved on all copies.
63 Permission is granted to copy and distribute modified versions of this
64 manual under the conditions for verbatim copying, provided also that
65 the entire resulting derived work is distributed under the terms of a
66 permission notice identical to this one.
68 Permission is granted to copy and distribute translations of this manual
69 into another language, under the above conditions for modified versions.
74 @node Top, Global Actions,, (DIR)
75 @chapter The C Preprocessor
76 @c man begin DESCRIPTION
78 The C preprocessor is a @dfn{macro processor} that is used automatically
79 by the C compiler to transform your program before actual compilation.
80 It is called a macro processor because it allows you to define
81 @dfn{macros}, which are brief abbreviations for longer constructs.
83 The C preprocessor is intended only for macro processing of C, C++ and
84 Objective C source files. For macro processing of other files, you are
85 strongly encouraged to use alternatives like M4, which will likely give
86 you better results and avoid many problems. For example, normally the C
87 preprocessor does not preserve arbitrary whitespace verbatim, but
88 instead replaces each sequence with a single space.
90 For use on C-like source files, the C preprocessor provides four
91 separate facilities that you can use as you see fit:
95 Inclusion of header files. These are files of declarations that can be
96 substituted into your program.
99 Macro expansion. You can define @dfn{macros}, which are abbreviations
100 for arbitrary fragments of C code, and then the C preprocessor will
101 replace the macros with their definitions throughout the program.
104 Conditional compilation. Using special preprocessing directives, you
105 can include or exclude parts of the program according to various
109 Line control. If you use a program to combine or rearrange source files
110 into an intermediate file which is then compiled, you can use line
111 control to inform the compiler of where each source line originally came
115 C preprocessors vary in some details. This manual discusses the GNU C
116 preprocessor, which provides a small superset of the features of ISO
119 ISO Standard C requires the rejection of many harmless constructs
120 commonly used by today's C programs. Such incompatibility would be
121 inconvenient for users, so the GNU C preprocessor is configured to
122 accept these constructs by default. Strictly speaking, to get ISO
123 Standard C, you must use the options @samp{-trigraphs}, @samp{-undef}
124 and @samp{-pedantic}, but in practice the consequences of having strict
125 ISO Standard C make it undesirable to do this. @xref{Invocation}.
130 * Global Actions:: Actions made uniformly on all input files.
131 * Directives:: General syntax of preprocessing directives.
132 * Header Files:: How and why to use header files.
133 * Macros:: How and why to use macros.
134 * Conditionals:: How and why to use conditionals.
135 * Assertions:: How and why to use assertions.
136 * Line Control:: Use of line control when you combine source files.
137 * Other Directives:: Miscellaneous preprocessing directives.
138 * Output:: Format of output from the C preprocessor.
139 * Unreliable Features:: Undefined behavior and deprecated features.
140 * Invocation:: How to invoke the preprocessor; command options.
141 * Concept Index:: Index of concepts and terms.
142 * Index:: Index of directives, predefined macros and options.
145 @node Global Actions, Directives, Top, Top
146 @section Transformations Made Globally
147 @cindex ASCII NUL handling
149 Most C preprocessor features are inactive unless you give specific
150 directives to request their use. (Preprocessing directives are lines
151 starting with a @samp{#} token, possibly preceded by whitespace;
152 @pxref{Directives}). However, there are three transformations that the
153 preprocessor always makes on all the input it receives, even in the
154 absence of directives.
158 Trigraphs, if enabled, are replaced with the character they represent.
159 Conceptually, this is the very first action undertaken, just before
160 backslash-newline deletion.
163 Backslash-newline sequences are deleted, no matter where. This
164 feature allows you to break long lines for cosmetic purposes without
165 changing their meaning.
168 All C comments are replaced with single spaces.
171 Predefined macro names are replaced with their expansions
172 (@pxref{Predefined}).
175 The first three transformations are done @emph{before} nearly all other
176 parsing and before preprocessing directives are recognized. Thus, for
177 example, you can split a line cosmetically with backslash-newline
178 anywhere (except within trigraphs since they are replaced first; see
191 is equivalent into @samp{#define FOO 1020}. You can split even an
192 escape sequence with backslash-newline. For example, you can split
193 @code{"foo\bar"} between the @samp{\} and the @samp{b} to get
201 This behavior can be confusing: in all other contexts, a backslash can
202 be inserted in a string constant as an ordinary character by writing a
203 double backslash. This is an exception, but the ISO C standard requires
204 it. (Strict ISO C does not allow string constants to extend to more
205 than one logical line, so they do not consider this a problem.)
207 There are a few exceptions to all three transformations.
211 C comments and predefined macro names are not recognized inside a
212 @samp{#include} directive in which the file name is delimited with
213 @samp{<} and @samp{>}. What lies in-between is read literally.
216 C comments and predefined macro names are never recognized within a
217 character or string constant. (Strictly speaking, this is the rule,
218 not an exception, but it is worth noting here anyway.)
221 Backslash-newline may not safely be used within an ISO ``trigraph'',
222 since trigraphs are converted before backslash-newlines are deleted. If
223 you write what looks like a trigraph with a backslash-newline inside,
224 the backslash-newline is deleted as usual, but it is then too late to
225 recognize the trigraph.
227 This is relevant only if you use the @samp{-trigraphs} option to enable
228 trigraph processing. @xref{Invocation}.
231 The preprocessor handles null characters embedded in the input file
232 depending upon the context in which the null appears. Note that here we
233 are referring not to the two-character escape sequence "\0", but to the
234 single character ASCII NUL.
236 There are three different contexts in which a null character may
241 Within comments. Here, null characters are silently ignored.
244 Within a string or character constant. Here the preprocessor emits a
245 warning, but preserves the null character and passes it through to the
246 output file or compiler front-end.
249 In any other context, the preprocessor issues a warning, and discards
250 the null character. The preprocessor treats it like whitespace,
251 combining it with any surrounding whitespace to become a single
252 whitespace block. Representing the null character by "^@@", this means
265 and X is defined with replacement text "1".
268 @node Directives, Header Files, Global Actions, Top
269 @section Preprocessing Directives
271 @cindex preprocessing directives
273 Most preprocessor features are active only if you use preprocessing
274 directives to request their use.
276 Preprocessing directives are lines in your program that start with
277 @samp{#}. Whitespace is allowed before and after the @samp{#}. The
278 @samp{#} is followed by an identifier that is the @dfn{directive name}.
279 For example, @samp{#define} is the directive that defines a macro.
281 Since the @samp{#} must be the first token on the line, it cannot come
282 from a macro expansion if you wish it to begin a directive. Also, the
283 directive name is not macro expanded. Thus, if @samp{foo} is defined as
284 a macro expanding to @samp{define}, that does not make @samp{#foo} a
285 valid preprocessing directive.
287 The set of valid directive names is fixed. Programs cannot define new
288 preprocessing directives.
290 Some directive names require arguments; these make up the rest of the
291 directive line and must be separated from the directive name by
292 whitespace. For example, @samp{#define} must be followed by a macro
293 name and the intended expansion of the macro. @xref{Object-like
296 A preprocessing directive cannot cover more than one line. It may be
297 logically extended with backslash-newline, but that has no effect on its
298 meaning. Comments containing newlines can also divide the directive
299 into multiple lines, but a comment is replaced by a single space before
300 the directive is interpreted.
302 @node Header Files, Macros, Directives, Top
303 @section Header Files
306 A header file is a file containing C declarations and macro definitions
307 (@pxref{Macros}) to be shared between several source files. You request
308 the use of a header file in your program with the C preprocessing
309 directive @samp{#include}.
312 * Header Uses:: What header files are used for.
313 * Include Syntax:: How to write @samp{#include} directives.
314 * Include Operation:: What @samp{#include} does.
315 * Once-Only:: Preventing multiple inclusion of one header file.
316 * Inheritance:: Including one header file in another header file.
317 * System Headers:: Special treatment for some header files.
320 @node Header Uses, Include Syntax, Header Files, Header Files
321 @subsection Uses of Header Files
323 Header files serve two kinds of purposes.
327 @cindex system header files
328 System header files declare the interfaces to parts of the operating
329 system. You include them in your program to supply the definitions and
330 declarations you need to invoke system calls and libraries.
333 Your own header files contain declarations for interfaces between the
334 source files of your program. Each time you have a group of related
335 declarations and macro definitions all or most of which are needed in
336 several different source files, it is a good idea to create a header
340 Including a header file produces the same results in C compilation as
341 copying the header file into each source file that needs it. Such
342 copying would be time-consuming and error-prone. With a header file,
343 the related declarations appear in only one place. If they need to be
344 changed, they can be changed in one place, and programs that include the
345 header file will automatically use the new version when next recompiled.
346 The header file eliminates the labor of finding and changing all the
347 copies as well as the risk that a failure to find one copy will result
348 in inconsistencies within a program.
350 The usual convention is to give header files names that end with
351 @file{.h}. Avoid unusual characters in header file names, as they
354 @node Include Syntax, Include Operation, Header Uses, Header Files
355 @subsection The @samp{#include} Directive
358 Both user and system header files are included using the preprocessing
359 directive @samp{#include}. It has three variants:
362 @item #include <@var{file}>
363 This variant is used for system header files. It searches for a file
364 named @var{file} in a list of directories specified by you, then in a
365 standard list of system directories. You specify directories to search
366 for header files with the command option @samp{-I} (@pxref{Invocation}).
367 The option @samp{-nostdinc} inhibits searching the standard system
368 directories; in this case only the directories you specify are searched.
370 The parsing of this form of @samp{#include} is slightly special because
371 comments are not recognized within the @samp{<@dots{}>}. Thus, in
372 @samp{#include <x/*y>} the @samp{/*} does not start a comment and the
373 directive specifies inclusion of a system header file named @file{x/*y}.
374 Of course, a header file with such a name is unlikely to exist on Unix,
375 where shell wildcard features would make it hard to manipulate.@refill
377 The first @samp{>} character terminates the file name. The file name
378 may contain a @samp{<} character.
380 @item #include "@var{file}"
381 This variant is used for header files of your own program. It searches
382 for a file named @var{file} first in the current directory, then in the
383 same directories used for system header files. The current directory is
384 the directory of the current input file. It is tried first because it
385 is presumed to be the location of the files that the current input file
386 refers to. (If the @samp{-I-} option is used, the special treatment of
387 the current directory is inhibited.)
389 The first @samp{"} character terminates the file name. If backslashes
390 occur within @var{file}, they are considered ordinary text characters,
391 not escape characters. None of the character escape sequences
392 appropriate to string constants in C are processed. Thus,
393 @samp{#include "x\n\\y"} specifies a filename containing three
396 @item #include @var{anything else}
397 @cindex computed @samp{#include}
398 This variant is called a @dfn{computed #include}. Any @samp{#include}
399 directive whose argument does not fit the above two forms is a computed
400 include. The text @var{anything else} is checked for macro calls, which
401 are expanded (@pxref{Macros}). When this is done, the result must match
402 one of the above two variants --- in particular, the expansion must form
403 a string literal token, or a sequence of tokens surrounded by angle
404 braces. @xref{Unreliable Features}
406 This feature allows you to define a macro which controls the file name
407 to be used at a later point in the program. One application of this is
408 to allow a site-specific configuration file for your program to specify
409 the names of the system include files to be used. This can help in
410 porting the program to various operating systems in which the necessary
411 system header files are found in different places.
414 @node Include Operation, Once-Only, Include Syntax, Header Files
415 @subsection How @samp{#include} Works
417 The @samp{#include} directive works by directing the C preprocessor to
418 scan the specified file as input before continuing with the rest of the
419 current file. The output from the preprocessor contains the output
420 already generated, followed by the output resulting from the included
421 file, followed by the output that comes from the text after the
422 @samp{#include} directive. For example, given a header file
423 @file{header.h} as follows,
430 and a main program called @file{program.c} that uses the header file,
444 the output generated by the C preprocessor for @file{program.c} as input
457 Included files are not limited to declarations and macro definitions;
458 those are merely the typical uses. Any fragment of a C program can be
459 included from another file. The include file could even contain the
460 beginning of a statement that is concluded in the containing file, or
461 the end of a statement that was started in the including file. However,
462 a comment or a string or character constant may not start in the
463 included file and finish in the including file. An unterminated
464 comment, string constant or character constant in an included file is
465 considered to end (with an error message) at the end of the file.
467 It is possible for a header file to begin or end a syntactic unit such
468 as a function definition, but that would be very confusing, so don't do
471 The line following the @samp{#include} directive is always treated as a
472 separate line by the C preprocessor, even if the included file lacks a
475 @node Once-Only, Inheritance, Include Operation, Header Files
476 @subsection Once-Only Include Files
477 @cindex repeated inclusion
478 @cindex including just once
480 Very often, one header file includes another. It can easily result that
481 a certain header file is included more than once. This may lead to
482 errors, if the header file defines structure types or typedefs, and is
483 certainly wasteful. Therefore, we often wish to prevent multiple
484 inclusion of a header file.
486 The standard way to do this is to enclose the entire real contents of the
487 file in a conditional, like this:
490 #ifndef FILE_FOO_SEEN
491 #define FILE_FOO_SEEN
493 @var{the entire file}
495 #endif /* FILE_FOO_SEEN */
498 The macro @code{FILE_FOO_SEEN} indicates that the file has been included
499 once already. In a user header file, the macro name should not begin
500 with @samp{_}. In a system header file, this name should begin with
501 @samp{__} to avoid conflicts with user programs. In any kind of header
502 file, the macro name should contain the name of the file and some
503 additional text, to avoid conflicts with other header files.
505 The GNU C preprocessor is programmed to notice when a header file uses
506 this particular construct and handle it efficiently. If a header file
507 is contained entirely in a @samp{#ifndef} conditional, modulo whitespace
508 and comments, then it remembers that fact. If a subsequent
509 @samp{#include} specifies the same file, and the macro in the
510 @samp{#ifndef} is already defined, then the directive is skipped without
511 processing the specified file at all.
514 In the Objective C language, there is a variant of @samp{#include}
515 called @samp{#import} which includes a file, but does so at most once.
516 If you use @samp{#import} @emph{instead of} @samp{#include}, then you
517 don't need the conditionals inside the header file to prevent multiple
518 execution of the contents.
520 @samp{#import} is obsolete because it is not a well designed feature.
521 It requires the users of a header file --- the applications programmers
522 --- to know that a certain header file should only be included once. It
523 is much better for the header file's implementor to write the file so
524 that users don't need to know this. Using @samp{#ifndef} accomplishes
527 @node Inheritance, System Headers, Once-Only, Header Files
528 @subsection Inheritance and Header Files
530 @cindex overriding a header file
532 @dfn{Inheritance} is what happens when one object or file derives some
533 of its contents by virtual copying from another object or file. In
534 the case of C header files, inheritance means that one header file
535 includes another header file and then replaces or adds something.
537 If the inheriting header file and the base header file have different
538 names, then inheritance is straightforward: simply write @samp{#include
539 "@var{base}"} in the inheriting file.
541 Sometimes it is necessary to give the inheriting file the same name as
542 the base file. This is less straightforward.
544 For example, suppose an application program uses the system header
545 @file{sys/signal.h}, but the version of @file{/usr/include/sys/signal.h}
546 on a particular system doesn't do what the application program expects.
547 It might be convenient to define a ``local'' version, perhaps under the
548 name @file{/usr/local/include/sys/signal.h}, to override or add to the
549 one supplied by the system.
551 You can do this by compiling with the option @samp{-I.}, and writing a
552 file @file{sys/signal.h} that does what the application program expects.
553 Making this file include the standard @file{sys/signal.h} is not so easy
554 --- writing @samp{#include <sys/signal.h>} in that file doesn't work,
555 because it includes your own version of the file, not the standard
556 system version. Used in that file itself, this leads to an infinite
557 recursion and a fatal error in compilation.
559 @samp{#include </usr/include/sys/signal.h>} would find the proper file,
560 but that is not clean, since it makes an assumption about where the
561 system header file is found. This is bad for maintenance, since it
562 means that any change in where the system's header files are kept
563 requires a change somewhere else.
565 @findex #include_next
566 The clean way to solve this problem is to use
567 @samp{#include_next}, which means, ``Include the @emph{next} file with
568 this name.'' This directive works like @samp{#include} except in
569 searching for the specified file: it starts searching the list of header
570 file directories @emph{after} the directory in which the current file
573 Suppose you specify @samp{-I /usr/local/include}, and the list of
574 directories to search also includes @file{/usr/include}; and suppose
575 both directories contain @file{sys/signal.h}. Ordinary @samp{#include
576 <sys/signal.h>} finds the file under @file{/usr/local/include}. If that
577 file contains @samp{#include_next <sys/signal.h>}, it starts searching
578 after that directory, and finds the file in @file{/usr/include}.
580 @samp{#include_next} is a GCC extension and should not be used in
581 programs intended to be portable to other compilers.
583 @node System Headers,, Inheritance, Header Files
584 @subsection System Headers
585 @cindex system header files
587 The header files declaring interfaces to the operating system and
588 runtime libraries often cannot be written in strictly conforming C.
589 Therefore, GNU C gives code found in @dfn{system headers} special
590 treatment. Certain categories of warnings are suppressed, notably those
591 enabled by @samp{-pedantic}.
593 Normally, only the headers found in specific directories are considered
594 system headers. The set of these directories is determined when GCC is
595 compiled. There are, however, two ways to add to the set.
598 The @samp{-isystem} command line option adds its argument to the list of
599 directories to search for headers, just like @samp{-I}. In addition,
600 any headers found in that directory will be considered system headers.
601 Note that unlike @samp{-I}, you must put a space between @samp{-isystem}
604 All directories named by @samp{-isystem} are searched @strong{after} all
605 directories named by @samp{-I}, no matter what their order was on the
606 command line. If the same directory is named by both @samp{-I} and
607 @samp{-isystem}, @samp{-I} wins; it is as if the @samp{-isystem} option
608 had never been specified at all.
610 @findex #pragma GCC system_header
611 There is also a directive, @samp{#pragma GCC system_header}, which tells
612 GCC to consider the rest of the current include file a system header, no
613 matter where it was found. Code that comes before the @samp{#pragma} in
614 the file will not be affected.
616 @samp{#pragma GCC system_header} has no effect in the primary source file.
618 @node Macros, Conditionals, Header Files, Top
621 A macro is a sort of abbreviation which you can define once and then
622 use later. There are many complicated features associated with macros
623 in the C preprocessor.
626 * Object-like Macros:: Macros that always expand the same way.
627 * Function-like Macros:: Macros that accept arguments that are substituted
628 into the macro expansion.
629 * Macro Varargs:: Macros with variable number of arguments.
630 * Predefined:: Predefined macros that are always available.
631 * Stringification:: Macro arguments converted into string constants.
632 * Concatenation:: Building tokens from parts taken from macro arguments.
633 * Undefining:: Cancelling a macro's definition.
634 * Redefining:: Changing a macro's definition.
635 * Poisoning:: Ensuring a macro is never defined or used.
636 * Macro Pitfalls:: Macros can confuse the unwary. Here we explain
637 several common problems and strange features.
640 @node Object-like Macros, Function-like Macros, Macros, Macros
641 @subsection Object-like Macros
642 @cindex object-like macro
643 @cindex manifest constant
645 An @dfn{object-like macro} is a kind of abbreviation. It is a name
646 which stands for a fragment of code. Some people refer to these as
647 @dfn{manifest constants}.
649 Before you can use a macro, you must @dfn{define} it explicitly with the
650 @samp{#define} directive. @samp{#define} is followed by the name of the
651 macro and then the token sequence it should be an abbreviation for,
652 which is variously referred to as the macro's @dfn{body},
653 @dfn{expansion} or @dfn{replacement list}. For example,
656 #define BUFFER_SIZE 1020
660 defines a macro named @samp{BUFFER_SIZE} as an abbreviation for the
661 token @samp{1020}. If somewhere after this @samp{#define} directive
662 there comes a C statement of the form
665 foo = (char *) xmalloc (BUFFER_SIZE);
669 then the C preprocessor will recognize and @dfn{expand} the macro
670 @samp{BUFFER_SIZE}, resulting in
673 foo = (char *) xmalloc (1020);
676 The use of all upper case for macro names is a standard convention.
677 Programs are easier to read when it is possible to tell at a glance
678 which names are macros.
680 Normally, a macro definition can only span a single logical line, like
681 all C preprocessing directives. Comments within a macro definition may
682 contain newlines, which make no difference since each comment is
683 replaced by a space regardless of its contents.
685 Apart from this, there is no restriction on what can go in a macro body
686 provided it decomposes into valid preprocessing tokens. In particular,
687 parentheses need not balance, and the body need not resemble valid C
688 code. (If it does not, you may get error messages from the C
689 compiler when you use the macro.)
691 The C preprocessor scans your program sequentially, so macro definitions
692 take effect at the place you write them. Therefore, the following input
693 to the C preprocessor
710 When the preprocessor expands a macro name, the macro's expansion
711 replaces the macro invocation, and the result is re-scanned for more
712 macros to expand. For example, after
716 #define TABLESIZE BUFSIZE
720 the name @samp{TABLESIZE} when used in the program would go through two
721 stages of expansion, resulting ultimately in @samp{1020}.
723 This is not the same as defining @samp{TABLESIZE} to be @samp{1020}.
724 The @samp{#define} for @samp{TABLESIZE} uses exactly the expansion you
725 specify --- in this case, @samp{BUFSIZE} --- and does not check to see
726 whether it too contains macro names. Only when you @emph{use}
727 @samp{TABLESIZE} is the result of its expansion scanned for more macro
728 names. @xref{Cascaded Macros}.
730 @node Function-like Macros, Macro Varargs, Object-like Macros, Macros
731 @subsection Macros with Arguments
732 @cindex macros with argument
733 @cindex arguments in macro definitions
734 @cindex function-like macro
736 An object-like macro is always replaced by exactly the same tokens each
737 time it is used. Macros can be made more flexible by taking
738 @dfn{arguments}. Arguments are fragments of code that you supply each
739 time the macro is used. These fragments are included in the expansion
740 of the macro according to the directions in the macro definition. A
741 macro that accepts arguments is called a @dfn{function-like macro}
742 because the syntax for using it looks like a function call.
745 To define a macro that uses arguments, you write a @samp{#define}
746 directive with a list of @dfn{parameters} in parentheses after the name
747 of the macro. The parameters must be valid C identifiers, separated by
748 commas and optionally whitespace. The @samp{(} must follow the macro
749 name immediately, with no space in between. If you leave a space, you
750 instead define an object-like macro whose expansion begins with a
751 @samp{(}, and often leads to confusing errors at compile time.
753 As an example, here is a macro that computes the minimum of two numeric
754 values, as it is defined in many C programs:
757 #define min(X, Y) ((X) < (Y) ? (X) : (Y))
761 (This is not the best way to define a ``minimum'' macro in GNU C@.
762 @xref{Side Effects}, for more information.)
764 To invoke a function-like macro, you write the name of the macro
765 followed by a list of @dfn{arguments} in parentheses, separated by
766 commas. The invocation of the macro need not be restricted to a single
767 logical line - it can cross as many lines in the source file as you
768 wish. The number of arguments you give must match the number of
769 parameters in the macro definition; empty arguments are fine. Examples
770 of use of the macro @samp{min} include @samp{min (1, 2)} and @samp{min
773 The expansion text of the macro depends on the arguments you use. Each
774 macro parameter is replaced throughout the macro expansion with the
775 tokens of the corresponding argument. Leading and trailing argument
776 whitespace is dropped, and all whitespace between the tokens of an
777 argument is reduced to a single space. Using the same macro @samp{min}
778 defined above, @samp{min (1, 2)} expands into
781 ((1) < (2) ? (1) : (2))
785 where @samp{1} has been substituted for @samp{X} and @samp{2} for @samp{Y}.
787 Likewise, @samp{min (x + 28, *p)} expands into
790 ((x + 28) < (*p) ? (x + 28) : (*p))
793 Parentheses within each argument must balance; a comma within such
794 parentheses does not end the argument. However, there is no requirement
795 for square brackets or braces to balance, and they do not prevent a
796 comma from separating arguments. Thus,
799 macro (array[x = y, x + 1])
803 passes two arguments to @code{macro}: @samp{array[x = y} and @samp{x +
804 1]}. If you want to supply @samp{array[x = y, x + 1]} as an argument,
805 you must write it as @samp{array[(x = y, x + 1)]}, which is equivalent C
808 After the arguments have been substituted into the macro body, the
809 resulting expansion replaces the macro invocation, and re-scanned for
810 more macro calls. Therefore even arguments can contain calls to other
811 macros, either with or without arguments, and even to the same macro.
812 For example, @samp{min (min (a, b), c)} expands into this text:
815 ((((a) < (b) ? (a) : (b))) < (c)
816 ? (((a) < (b) ? (a) : (b)))
821 (Line breaks shown here for clarity would not actually be generated.)
823 @cindex empty macro arguments
824 If a macro @code{foo} takes one argument, and you want to supply an
825 empty argument, simply supply no preprocessing tokens. Since whitespace
826 does not form a preprocessing token, it is optional. For example,
827 @samp{foo ()}, @samp{foo ( )} and @samp{bar (, arg2)}.
829 Previous GNU preprocessor implementations and documentation were
830 incorrect on this point, insisting that a function-like macro that takes
831 a single argument be passed a space if an empty argument was required.
833 If you use a macro name followed by something other than a @samp{(}
834 (after ignoring any whitespace that might follow), it does not form an
835 invocation of the macro, and the preprocessor does not change what you
836 have written. Therefore, it is possible for the same identifier to be a
837 variable or function in your program as well as a macro, and you can
838 choose in each instance whether to refer to the macro (if an actual
839 argument list follows) or the variable or function (if an argument list
840 does not follow). For example,
847 expands to @samp{foo bar baz}. Such dual use of one name could be
848 confusing and should be avoided except when the two meanings are
849 effectively synonymous: that is, when the name is both a macro and a
850 function and the two have similar effects. You can think of the name
851 simply as a function; use of the name for purposes other than calling it
852 (such as, to take the address) will refer to the function, while calls
853 will expand the macro and generate better but equivalent code.
855 For example, you can use a function named @samp{min} in the same source
856 file that defines the macro. If you write @samp{&min} with no argument
857 list, you refer to the function. If you write @samp{min (x, bb)}, with
858 an argument list, the macro is expanded. If you write @samp{(min) (a,
859 bb)}, where the name @samp{min} is not followed by an open-parenthesis,
860 the macro is not expanded, so you wind up with a call to the function
863 In the definition of a macro with arguments, the list of argument names
864 must follow the macro name immediately with no space in between. If
865 there is a space after the macro name, the macro is defined as taking no
866 arguments, and all the rest of the line is taken to be the expansion.
867 The reason for this is that it is often useful to define a macro that
868 takes no arguments and whose definition begins with an identifier in
869 parentheses. This rule makes it possible for you to do either this:
872 #define FOO(x) - 1 / (x)
876 (which defines @samp{FOO} to take an argument and expand into minus the
877 reciprocal of that argument) or this:
880 #define BAR (x) - 1 / (x)
884 (which defines @samp{BAR} to take no argument and always expand into
885 @samp{(x) - 1 / (x)}).
887 Note that the @emph{uses} of a macro with arguments can have spaces
888 before the left parenthesis; it's the @emph{definition} where it matters
889 whether there is a space.
891 @node Macro Varargs, Predefined, Function-like Macros, Macros
892 @subsection Macros with Variable Numbers of Arguments
893 @cindex variable number of arguments
894 @cindex macro with variable arguments
895 @cindex rest argument (in macro)
897 In the ISO C standard of 1999, a macro can be declared to accept a
898 variable number of arguments much as a function can. The syntax for
899 defining the macro is similar to that of a function. Here is an
903 #define eprintf(...) fprintf (stderr, __VA_ARGS__)
906 Here @samp{@dots{}} is a @dfn{variable argument}. In the invocation of
907 such a macro, it represents the zero or more tokens until the closing
908 parenthesis that ends the invocation, including any commas. This set of
909 tokens replaces the identifier @code{__VA_ARGS__} in the macro body
910 wherever it appears. Thus, we have this expansion:
913 eprintf ("%s:%d: ", input_file_name, line_number)
915 fprintf (stderr, "%s:%d: " , input_file_name, line_number)
918 We might instead have defined eprintf as follows:-
921 #define eprintf(format, ...) fprintf (stderr, format, __VA_ARGS__)
924 This formulation looks more descriptive, but unfortunately causes
925 problems if fprintf wants no arguments the format. There is no way to
926 produce expanded output of
929 fprintf (stderr, "success!\n")
933 since passing an empty argument for the variable arguments part like this
936 eprintf ("success!\n", )
943 fprintf (stderr, "success!\n",)
947 where the extra comma originates from the replacement list and not from
948 the arguments to eprintf.
950 Within a @samp{#define} directive, ISO C mandates that the only place
951 the identifier @code{__VA_ARGS__} can appear is in the replacement list
952 of a variable-argument macro. Using it as a macro name, macro argument
953 or within a different type of macro is illegal.
955 Before standardization, previous GNU preprocessors implemented a
956 slightly different syntax for defining variable-argument macros. The
957 macros were called ``rest args macros''. You could assign a name to the
958 variable arguments, by contrast the standardized method leaves them
959 anonymous. For example, the eprintf macro could have been defined like
963 #define eprintf(format...) fprintf (stderr, format)
966 Now that there is a standardized construct, you are encouraged to use
967 that instead. It is unlikely that support for named variable arguments
968 will be removed in future revisions of CPP, since being able to assign a
969 name is descriptive, and there is a wide base of legacy code. However,
970 two obscure features of the GNU style are deprecated and likely to be
971 dropped in future. @xref{Unreliable Features}.
973 @node Predefined, Stringification, Macro Varargs, Macros
974 @subsection Predefined Macros
976 @cindex predefined macros
977 Several object-like macros are predefined; you use them without
978 supplying their definitions. They fall into two classes: standard
979 macros and system-specific macros.
982 * Standard Predefined:: Standard predefined macros.
983 * Nonstandard Predefined:: Nonstandard predefined macros.
986 @node Standard Predefined, Nonstandard Predefined, Predefined, Predefined
987 @subsubsection Standard Predefined Macros
988 @cindex standard predefined macros
990 The standard predefined macros are available with the same meanings
991 regardless of the machine or operating system on which you are using GNU
992 C@. Their names all start and end with double underscores. Those
993 preceding @code{__GNUC__} in this table are standardized by ISO C; the
994 rest are GNU C extensions.
999 This macro expands to the name of the current input file, in the form of
1000 a C string constant. The precise name returned is the one that was
1001 specified in @samp{#include} or as the input file name argument. For
1002 example, @samp{"/usr/local/include/myheader.h"} is a possible expansion
1007 This macro expands to the current input line number, in the form of a
1008 decimal integer constant. While we call it a predefined macro, it's
1009 a pretty strange macro, since its ``definition'' changes with each
1010 new line of source code.
1012 This and @samp{__FILE__} are useful in generating an error message to
1013 report an inconsistency detected by the program; the message can state
1014 the source line at which the inconsistency was detected. For example,
1017 fprintf (stderr, "Internal error: "
1018 "negative string length "
1019 "%d at %s, line %d.",
1020 length, __FILE__, __LINE__);
1023 A @samp{#include} directive changes the expansions of @samp{__FILE__}
1024 and @samp{__LINE__} to correspond to the included file. At the end of
1025 that file, when processing resumes on the input file that contained
1026 the @samp{#include} directive, the expansions of @samp{__FILE__} and
1027 @samp{__LINE__} revert to the values they had before the
1028 @samp{#include} (but @samp{__LINE__} is then incremented by one as
1029 processing moves to the line after the @samp{#include}).
1031 The expansions of both @samp{__FILE__} and @samp{__LINE__} are altered
1032 if a @samp{#line} directive is used. @xref{Line Control}.
1036 This macro expands to a string constant that describes the date on
1037 which the preprocessor is being run. The string constant contains
1038 eleven characters and looks like @w{@samp{"Feb 1 1996"}}.
1039 @c After reformatting the above, check that the date remains `Feb 1 1996',
1040 @c all on one line, with two spaces between the `Feb' and the `1'.
1044 This macro expands to a string constant that describes the time at
1045 which the preprocessor is being run. The string constant contains
1046 eight characters and looks like @samp{"23:59:01"}.
1050 This macro expands to the constant 1, to signify that this is ISO
1051 Standard C@. (Whether that is actually true depends on what C compiler
1052 will operate on the output from the preprocessor.)
1054 On some hosts, system include files use a different convention, where
1055 @samp{__STDC__} is normally 0, but is 1 if the user specifies strict
1056 conformance to the C Standard. The preprocessor follows the host
1057 convention when processing system include files, but when processing
1058 user files it follows the usual GNU C convention.
1060 This macro is not defined if the @samp{-traditional} option is used.
1062 @item __STDC_VERSION__
1063 @findex __STDC_VERSION__
1064 This macro expands to the C Standard's version number, a long integer
1065 constant of the form @samp{@var{yyyy}@var{mm}L} where @var{yyyy} and
1066 @var{mm} are the year and month of the Standard version. This signifies
1067 which version of the C Standard the preprocessor conforms to. Like
1068 @samp{__STDC__}, whether this version number is accurate for the entire
1069 implementation depends on what C compiler will operate on the output
1070 from the preprocessor.
1072 This macro is not defined if the @samp{-traditional} option is used.
1076 This macro is defined if and only if this is GNU C@. This macro is
1077 defined only when the entire GNU C compiler is in use; if you invoke the
1078 preprocessor directly, @samp{__GNUC__} is undefined. The value
1079 identifies the major version number of GNU CC (@samp{1} for GNU CC
1080 version 1, which is now obsolete, and @samp{2} for version 2).
1082 @item __GNUC_MINOR__
1083 @findex __GNUC_MINOR__
1084 The macro contains the minor version number of the compiler. This can
1085 be used to work around differences between different releases of the
1086 compiler (for example, if GCC 2.6.3 is known to support a feature, you
1087 can test for @code{__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 6)}).
1089 @item __GNUC_PATCHLEVEL__
1090 @findex __GNUC_PATCHLEVEL__
1091 This macro contains the patch level of the compiler. This can be
1092 used to work around differences between different patch level releases
1093 of the compiler (for example, if GCC 2.6.2 is known to contain a bug,
1094 whereas GCC 2.6.3 contains a fix, and you have code which can workaround
1095 the problem depending on whether the bug is fixed or not, you can test for
1096 @code{__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 6) ||
1097 (__GNUC__ == 2 && __GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 3)}).
1101 The GNU C compiler defines this when the compilation language is
1102 C++; use @samp{__GNUG__} to distinguish between GNU C and GNU
1107 The ISO standard for C++ requires predefining this variable. You can
1108 use @samp{__cplusplus} to test whether a header is compiled by a C
1109 compiler or a C++ compiler. The compiler currently uses a value of
1110 @samp{1}, instead of the value @samp{199711L}, which would indicate full
1111 conformance with the standard.
1113 @item __STRICT_ANSI__
1114 @findex __STRICT_ANSI__
1115 GNU C defines this macro if and only if the @samp{-ansi} switch was
1116 specified when GNU C was invoked. Its definition is the null string.
1117 This macro exists primarily to direct certain GNU header files not to
1118 define certain traditional Unix constructs which are incompatible with
1122 @findex __BASE_FILE__
1123 This macro expands to the name of the main input file, in the form
1124 of a C string constant. This is the source file that was specified
1125 on the command line of the preprocessor or C compiler.
1127 @item __INCLUDE_LEVEL__
1128 @findex __INCLUDE_LEVEL_
1129 This macro expands to a decimal integer constant that represents the
1130 depth of nesting in include files. The value of this macro is
1131 incremented on every @samp{#include} directive and decremented at the
1132 end of every included file. It starts out at 0, it's value within the
1133 base file specified on the command line.
1137 This macro expands to a string constant which describes the version
1138 number of GNU C@. The string is normally a sequence of decimal numbers
1139 separated by periods, such as @samp{"2.6.0"}.
1142 @findex __OPTIMIZE__
1143 GNU CC defines this macro in optimizing compilations. It causes certain
1144 GNU header files to define alternative macro definitions for some system
1145 library functions. You should not refer to or test the definition of
1146 this macro unless you make very sure that programs will execute with the
1147 same effect regardless.
1149 @item __CHAR_UNSIGNED__
1150 @findex __CHAR_UNSIGNED__
1151 GNU C defines this macro if and only if the data type @code{char} is
1152 unsigned on the target machine. It exists to cause the standard header
1153 file @file{limits.h} to work correctly. You should not refer to this
1154 macro yourself; instead, refer to the standard macros defined in
1155 @file{limits.h}. The preprocessor uses this macro to determine whether
1156 or not to sign-extend large character constants written in octal; see
1157 @ref{#if Directive,,The @samp{#if} Directive}.
1159 @item __REGISTER_PREFIX__
1160 @findex __REGISTER_PREFIX__
1161 This macro expands to a string (not a string constant) describing the
1162 prefix applied to CPU registers in assembler code. You can use it to
1163 write assembler code that is usable in multiple environments. For
1164 example, in the @samp{m68k-aout} environment it expands to the null
1165 string, but in the @samp{m68k-coff} environment it expands to the string
1168 @item __USER_LABEL_PREFIX__
1169 @findex __USER_LABEL_PREFIX__
1170 Similar to @code{__REGISTER_PREFIX__}, but describes the prefix applied
1171 to user generated labels in assembler code. For example, in the
1172 @samp{m68k-aout} environment it expands to the string @samp{_}, but in
1173 the @samp{m68k-coff} environment it expands to the null string. This
1174 does not work with the @samp{-mno-underscores} option that the i386
1175 OSF/rose and m88k targets provide nor with the @samp{-mcall*} options of
1176 the rs6000 System V Release 4 target.
1179 @node Nonstandard Predefined,, Standard Predefined, Predefined
1180 @subsubsection Nonstandard Predefined Macros
1182 The C preprocessor normally has several predefined macros that vary
1183 between machines because their purpose is to indicate what type of
1184 system and machine is in use. This manual, being for all systems and
1185 machines, cannot tell you exactly what their names are; instead, we
1186 offer a list of some typical ones. You can use @samp{cpp -dM} to see
1187 the values of predefined macros; see @ref{Invocation}.
1189 Some nonstandard predefined macros describe the operating system in use,
1190 with more or less specificity. For example,
1195 @samp{unix} is normally predefined on all Unix systems.
1199 @samp{BSD} is predefined on recent versions of Berkeley Unix
1200 (perhaps only in version 4.3).
1203 Other nonstandard predefined macros describe the kind of CPU, with more or
1204 less specificity. For example,
1209 @samp{vax} is predefined on Vax computers.
1213 @samp{mc68000} is predefined on most computers whose CPU is a Motorola
1214 68000, 68010 or 68020.
1218 @samp{m68k} is also predefined on most computers whose CPU is a 68000,
1219 68010 or 68020; however, some makers use @samp{mc68000} and some use
1220 @samp{m68k}. Some predefine both names. What happens in GNU C
1221 depends on the system you are using it on.
1225 @samp{M68020} has been observed to be predefined on some systems that
1226 use 68020 CPUs --- in addition to @samp{mc68000} and @samp{m68k}, which
1233 Both @samp{_AM29K} and @samp{_AM29000} are predefined for the AMD 29000
1238 @samp{ns32000} is predefined on computers which use the National
1239 Semiconductor 32000 series CPU.
1242 Yet other nonstandard predefined macros describe the manufacturer of
1243 the system. For example,
1248 @samp{sun} is predefined on all models of Sun computers.
1252 @samp{pyr} is predefined on all models of Pyramid computers.
1256 @samp{sequent} is predefined on all models of Sequent computers.
1259 These predefined symbols are not only nonstandard, they are contrary to the
1260 ISO standard because their names do not start with underscores.
1261 Therefore, the option @samp{-ansi} inhibits the definition of these
1264 This tends to make @samp{-ansi} useless, since many programs depend on
1265 the customary nonstandard predefined symbols. Even system header files
1266 check them and will generate incorrect declarations if they do not find
1267 the names that are expected. You might think that the header files
1268 supplied for the Uglix computer would not need to test what machine they
1269 are running on, because they can simply assume it is the Uglix; but
1270 often they do, and they do so using the customary names. As a result,
1271 very few C programs will compile with @samp{-ansi}. We intend to avoid
1272 such problems on the GNU system.
1274 What, then, should you do in an ISO C program to test the type of machine
1277 GNU C offers a parallel series of symbols for this purpose, whose names
1278 are made from the customary ones by adding @samp{__} at the beginning
1279 and end. Thus, the symbol @code{__vax__} would be available on a Vax,
1282 The set of nonstandard predefined names in the GNU C preprocessor is
1283 controlled (when @code{cpp} is itself compiled) by the macro
1284 @samp{CPP_PREDEFINES}, which should be a string containing @samp{-D}
1285 options, separated by spaces. For example, on the Sun 3, we use the
1286 following definition:
1289 #define CPP_PREDEFINES "-Dmc68000 -Dsun -Dunix -Dm68k"
1293 This macro is usually specified in @file{tm.h}.
1295 @node Stringification, Concatenation, Predefined, Macros
1296 @subsection Stringification
1298 @cindex stringification
1299 @dfn{Stringification} means turning a sequence of preprocessing tokens
1300 into a string literal. For example, stringifying @samp{foo (z)} results
1301 in @samp{"foo (z)"}.
1303 In the C preprocessor, stringification is possible when macro arguments
1304 are substituted during macro expansion. When a parameter appears
1305 preceded by a @samp{#} token in the replacement list of a function-like
1306 macro, it indicates that both tokens should be replaced with the
1307 stringification of the corresponding argument during expansion. The
1308 same argument may be substituted in other places in the definition
1309 without stringification if the argument name appears in those places
1310 with no preceding @samp{#}.
1312 Here is an example of a macro definition that uses stringification:
1316 #define WARN_IF(EXP) \
1318 fprintf (stderr, "Warning: " #EXP "\n"); @} \
1324 Here the argument for @samp{EXP} is substituted once, as-is, into the
1325 @samp{if} statement, and once, stringified, into the argument to
1326 @samp{fprintf}. The @samp{do} and @samp{while (0)} are a kludge to make
1327 it possible to write @samp{WARN_IF (@var{arg});}, which the resemblance
1328 of @samp{WARN_IF} to a function would make C programmers want to do; see
1329 @ref{Swallow Semicolon}.
1331 The stringification feature is limited to transforming the tokens of a
1332 macro argument into a string constant: there is no way to combine the
1333 argument with surrounding text and stringify it all together. The
1334 example above shows how an equivalent result can be obtained in ISO
1335 Standard C, using the fact that adjacent string constants are
1336 concatenated by the C compiler to form a single string constant. The
1337 preprocessor stringifies the actual value of @samp{EXP} into a separate
1338 string constant, resulting in text like
1343 fprintf (stderr, "Warning: " "x == 0" "\n"); @} \
1349 but the compiler then sees three consecutive string constants and
1350 concatenates them into one, producing effectively
1354 fprintf (stderr, "Warning: x == 0\n"); @} \
1358 Stringification in C involves more than putting double-quote characters
1359 around the fragment. The preprocessor backslash-escapes the surrounding
1360 quotes of string literals, and all backslashes within string and
1361 character constants, in order to get a valid C string constant with the
1362 proper contents. Thus, stringifying @samp{p = "foo\n";} results in
1363 @samp{"p = \"foo\\n\";"}. However, backslashes that are not inside
1364 string or character constants are not duplicated: @samp{\n} by itself
1365 stringifies to @samp{"\n"}.
1367 Whitespace (including comments) in the text being stringified is handled
1368 according to precise rules. All leading and trailing whitespace is
1369 ignored. Any sequence of whitespace in the middle of the text is
1370 converted to a single space in the stringified result.
1372 @node Concatenation, Undefining, Stringification, Macros
1373 @subsection Concatenation
1374 @cindex concatenation
1376 @dfn{Concatenation} means joining two strings into one. In the context
1377 of macro expansion, concatenation refers to joining two preprocessing
1378 tokens to form one. In particular, a token of a macro argument can be
1379 concatenated with another argument's token or with fixed text to produce
1380 a longer name. The longer name might be the name of a function,
1381 variable, type, or a C keyword; it might even be the name of another
1382 macro, in which case it will be expanded.
1384 When you define a function-like or object-like macro, you request
1385 concatenation with the special operator @samp{##} in the macro's
1386 replacement list. When the macro is called, any arguments are
1387 substituted without performing macro expansion, every @samp{##} operator
1388 is deleted, and the two tokens on either side of it are concatenated to
1389 form a single token.
1391 Consider a C program that interprets named commands. There probably needs
1392 to be a table of commands, perhaps an array of structures declared as
1399 void (*function) ();
1402 struct command commands[] =
1404 @{ "quit", quit_command@},
1405 @{ "help", help_command@},
1410 It would be cleaner not to have to give each command name twice, once in
1411 the string constant and once in the function name. A macro which takes the
1412 name of a command as an argument can make this unnecessary. The string
1413 constant can be created with stringification, and the function name by
1414 concatenating the argument with @samp{_command}. Here is how it is done:
1417 #define COMMAND(NAME) @{ #NAME, NAME ## _command @}
1419 struct command commands[] =
1427 The usual case of concatenation is concatenating two names (or a name
1428 and a number) into a longer name. This isn't the only valid case.
1429 It is also possible to concatenate two numbers (or a number and a name,
1430 such as @samp{1.5} and @samp{e3}) into a number. Also, multi-character
1431 operators such as @samp{+=} can be formed by concatenation. However,
1432 two tokens that don't together form a valid token cannot be
1433 concatenated. For example, concatenation of @samp{x} on one side and
1434 @samp{+} on the other is not meaningful because those two tokens do not
1435 form a valid preprocessing token when concatenated. UNDEFINED
1437 Keep in mind that the C preprocessor converts comments to whitespace
1438 before macros are even considered. Therefore, you cannot create a
1439 comment by concatenating @samp{/} and @samp{*}: the @samp{/*} sequence
1440 that starts a comment is not a token, but rather the beginning of a
1441 comment. You can freely use comments next to @samp{##} in a macro
1442 definition, or in arguments that will be concatenated, because the
1443 comments will be converted to spaces at first sight, and concatenation
1444 operates on tokens and so ignores whitespace.
1446 @node Undefining, Redefining, Concatenation, Macros
1447 @subsection Undefining Macros
1449 @cindex undefining macros
1450 To @dfn{undefine} a macro means to cancel its definition. This is done
1451 with the @samp{#undef} directive. @samp{#undef} is followed by the macro
1452 name to be undefined.
1454 Like definition, undefinition occurs at a specific point in the source
1455 file, and it applies starting from that point. The name ceases to be a
1456 macro name, and from that point on it is treated by the preprocessor as
1457 if it had never been a macro name.
1478 In this example, @samp{FOO} had better be a variable or function as well
1479 as (temporarily) a macro, in order for the result of the expansion to be
1482 The same form of @samp{#undef} directive will cancel definitions with
1483 arguments or definitions that don't expect arguments. The @samp{#undef}
1484 directive has no effect when used on a name not currently defined as a
1487 @node Redefining, Poisoning, Undefining, Macros
1488 @subsection Redefining Macros
1490 @cindex redefining macros
1491 @dfn{Redefining} a macro means defining (with @samp{#define}) a name that
1492 is already defined as a macro.
1494 A redefinition is trivial if the new definition is transparently
1495 identical to the old one. You probably wouldn't deliberately write a
1496 trivial redefinition, but they can happen automatically when a header
1497 file is included more than once (@pxref{Header Files}), so they are
1498 accepted silently and without effect.
1500 Nontrivial redefinition is considered likely to be an error, so it
1501 provokes a warning message from the preprocessor. However, sometimes it
1502 is useful to change the definition of a macro in mid-compilation. You
1503 can inhibit the warning by undefining the macro with @samp{#undef}
1504 before the second definition.
1506 In order for a redefinition to be trivial, the parameter names must
1507 match and be in the same order, and the new replacement list must
1508 exactly match the one already in effect, with two possible exceptions:
1512 Whitespace may be added or deleted at the beginning or the end of the
1513 replacement list. In a sense this is vacuous, since strictly such
1514 whitespace doesn't form part of the macro's expansion.
1517 Between tokens in the expansion, any two forms of whitespace are
1518 considered equivalent. In particular, whitespace may not be eliminated
1519 entirely, nor may it be added where there previously wasn't any.
1522 Recall that a comment counts as whitespace.
1524 As a particular case of the above, you may not redefine an object-like
1525 macro as a function-like macro, and vice-versa.
1527 @node Poisoning, Macro Pitfalls, Redefining, Macros
1528 @subsection Poisoning Macros
1529 @cindex poisoning macros
1530 @findex #pragma GCC poison
1532 Sometimes, there is an identifier that you want to remove completely
1533 from your program, and make sure that it never creeps back in. To
1534 enforce this, the @samp{#pragma GCC poison} directive can be used.
1535 @samp{#pragma GCC poison} is followed by a list of identifiers to
1536 poison, and takes effect for the rest of the source. You cannot
1537 @samp{#undef} a poisoned identifier or test to see if it's defined with
1543 #pragma GCC poison printf sprintf fprintf
1544 sprintf(some_string, "hello");
1548 will produce an error.
1550 @node Macro Pitfalls,, Poisoning, Macros
1551 @subsection Pitfalls and Subtleties of Macros
1552 @cindex problems with macros
1553 @cindex pitfalls of macros
1555 In this section we describe some special rules that apply to macros and
1556 macro expansion, and point out certain cases in which the rules have
1557 counterintuitive consequences that you must watch out for.
1560 * Misnesting:: Macros can contain unmatched parentheses.
1561 * Macro Parentheses:: Why apparently superfluous parentheses
1562 may be necessary to avoid incorrect grouping.
1563 * Swallow Semicolon:: Macros that look like functions
1564 but expand into compound statements.
1565 * Side Effects:: Unsafe macros that cause trouble when
1566 arguments contain side effects.
1567 * Self-Reference:: Macros whose definitions use the macros' own names.
1568 * Argument Prescan:: Arguments are checked for macro calls before they
1570 * Cascaded Macros:: Macros whose definitions use other macros.
1571 * Newlines in Args:: Sometimes line numbers get confused.
1574 @node Misnesting, Macro Parentheses, Macro Pitfalls, Macro Pitfalls
1575 @subsubsection Improperly Nested Constructs
1577 Recall that when a macro is called with arguments, the arguments are
1578 substituted into the macro body and the result is checked, together with
1579 the rest of the input file, for more macro calls.
1581 It is possible to piece together a macro call coming partially from the
1582 macro body and partially from the arguments. For example,
1585 #define double(x) (2*(x))
1586 #define call_with_1(x) x(1)
1590 would expand @samp{call_with_1 (double)} into @samp{(2*(1))}.
1592 Macro definitions do not have to have balanced parentheses. By writing
1593 an unbalanced open parenthesis in a macro body, it is possible to create
1594 a macro call that begins inside the macro body but ends outside of it.
1598 #define strange(file) fprintf (file, "%s %d",
1600 strange(stderr) p, 35)
1604 This bizarre example expands to @samp{fprintf (stderr, "%s %d", p, 35)}!
1606 @node Macro Parentheses, Swallow Semicolon, Misnesting, Macro Pitfalls
1607 @subsubsection Unintended Grouping of Arithmetic
1608 @cindex parentheses in macro bodies
1610 You may have noticed that in most of the macro definition examples shown
1611 above, each occurrence of a macro argument name had parentheses around
1612 it. In addition, another pair of parentheses usually surround the
1613 entire macro definition. Here is why it is best to write macros that
1616 Suppose you define a macro as follows,
1619 #define ceil_div(x, y) (x + y - 1) / y
1623 whose purpose is to divide, rounding up. (One use for this operation is
1624 to compute how many @samp{int} objects are needed to hold a certain
1625 number of @samp{char} objects.) Then suppose it is used as follows:
1628 a = ceil_div (b & c, sizeof (int));
1635 a = (b & c + sizeof (int) - 1) / sizeof (int);
1639 which does not do what is intended. The operator-precedence rules of
1640 C make it equivalent to this:
1643 a = (b & (c + sizeof (int) - 1)) / sizeof (int);
1647 What we want is this:
1650 a = ((b & c) + sizeof (int) - 1)) / sizeof (int);
1654 Defining the macro as
1657 #define ceil_div(x, y) ((x) + (y) - 1) / (y)
1661 provides the desired result.
1663 Unintended grouping can result in another way. Consider @samp{sizeof
1664 ceil_div(1, 2)}. That has the appearance of a C expression that would
1665 compute the size of the type of @samp{ceil_div (1, 2)}, but in fact it
1666 means something very different. Here is what it expands to:
1669 sizeof ((1) + (2) - 1) / (2)
1673 This would take the size of an integer and divide it by two. The
1674 precedence rules have put the division outside the @samp{sizeof} when it
1675 was intended to be inside.
1677 Parentheses around the entire macro definition can prevent such
1678 problems. Here, then, is the recommended way to define @samp{ceil_div}:
1681 #define ceil_div(x, y) (((x) + (y) - 1) / (y))
1684 @node Swallow Semicolon, Side Effects, Macro Parentheses, Macro Pitfalls
1685 @subsubsection Swallowing the Semicolon
1687 @cindex semicolons (after macro calls)
1688 Often it is desirable to define a macro that expands into a compound
1689 statement. Consider, for example, the following macro, that advances a
1690 pointer (the argument @samp{p} says where to find it) across whitespace
1694 #define SKIP_SPACES(p, limit) \
1695 @{ register char *lim = (limit); \
1696 while (p != lim) @{ \
1697 if (*p++ != ' ') @{ \
1702 Here backslash-newline is used to split the macro definition, which must
1703 be a single logical line, so that it resembles the way such C code would
1704 be laid out if not part of a macro definition.
1706 A call to this macro might be @samp{SKIP_SPACES (p, lim)}. Strictly
1707 speaking, the call expands to a compound statement, which is a complete
1708 statement with no need for a semicolon to end it. However, since it
1709 looks like a function call, it minimizes confusion if you can use it
1710 like a function call, writing a semicolon afterward, as in
1711 @samp{SKIP_SPACES (p, lim);}
1713 This can cause trouble before @samp{else} statements, because the
1714 semicolon is actually a null statement. Suppose you write
1718 SKIP_SPACES (p, lim);
1723 The presence of two statements --- the compound statement and a null
1724 statement --- in between the @samp{if} condition and the @samp{else}
1725 makes invalid C code.
1727 The definition of the macro @samp{SKIP_SPACES} can be altered to solve
1728 this problem, using a @samp{do @dots{} while} statement. Here is how:
1731 #define SKIP_SPACES(p, limit) \
1732 do @{ register char *lim = (limit); \
1733 while (p != lim) @{ \
1734 if (*p++ != ' ') @{ \
1735 p--; break; @}@}@} \
1739 Now @samp{SKIP_SPACES (p, lim);} expands into
1742 do @{@dots{}@} while (0);
1746 which is one statement.
1748 @node Side Effects, Self-Reference, Swallow Semicolon, Macro Pitfalls
1749 @subsubsection Duplication of Side Effects
1751 @cindex side effects (in macro arguments)
1752 @cindex unsafe macros
1753 Many C programs define a macro @samp{min}, for ``minimum'', like this:
1756 #define min(X, Y) ((X) < (Y) ? (X) : (Y))
1759 When you use this macro with an argument containing a side effect,
1763 next = min (x + y, foo (z));
1767 it expands as follows:
1770 next = ((x + y) < (foo (z)) ? (x + y) : (foo (z)));
1774 where @samp{x + y} has been substituted for @samp{X} and @samp{foo (z)}
1777 The function @samp{foo} is used only once in the statement as it appears
1778 in the program, but the expression @samp{foo (z)} has been substituted
1779 twice into the macro expansion. As a result, @samp{foo} might be called
1780 two times when the statement is executed. If it has side effects or if
1781 it takes a long time to compute, the results might not be what you
1782 intended. We say that @samp{min} is an @dfn{unsafe} macro.
1784 The best solution to this problem is to define @samp{min} in a way that
1785 computes the value of @samp{foo (z)} only once. The C language offers
1786 no standard way to do this, but it can be done with GNU C extensions as
1791 (@{ typeof (X) __x = (X), __y = (Y); \
1792 (__x < __y) ? __x : __y; @})
1795 If you do not wish to use GNU C extensions, the only solution is to be
1796 careful when @emph{using} the macro @samp{min}. For example, you can
1797 calculate the value of @samp{foo (z)}, save it in a variable, and use
1798 that variable in @samp{min}:
1801 #define min(X, Y) ((X) < (Y) ? (X) : (Y))
1805 next = min (x + y, tem);
1810 (where we assume that @samp{foo} returns type @samp{int}).
1812 @node Self-Reference, Argument Prescan, Side Effects, Macro Pitfalls
1813 @subsubsection Self-Referential Macros
1815 @cindex self-reference
1816 A @dfn{self-referential} macro is one whose name appears in its
1817 definition. A special feature of ISO Standard C is that the
1818 self-reference is not considered a macro call. It is passed into the
1819 preprocessor output unchanged.
1821 Let's consider an example:
1824 #define foo (4 + foo)
1828 where @samp{foo} is also a variable in your program.
1830 Following the ordinary rules, each reference to @samp{foo} will expand
1831 into @samp{(4 + foo)}; then this will be rescanned and will expand into
1832 @samp{(4 + (4 + foo))}; and so on until it causes a fatal error (memory
1833 full) in the preprocessor.
1835 However, the special rule about self-reference cuts this process short
1836 after one step, at @samp{(4 + foo)}. Therefore, this macro definition
1837 has the possibly useful effect of causing the program to add 4 to the
1838 value of @samp{foo} wherever @samp{foo} is referred to.
1840 In most cases, it is a bad idea to take advantage of this feature. A
1841 person reading the program who sees that @samp{foo} is a variable will
1842 not expect that it is a macro as well. The reader will come across the
1843 identifier @samp{foo} in the program and think its value should be that
1844 of the variable @samp{foo}, whereas in fact the value is four greater.
1846 The special rule for self-reference applies also to @dfn{indirect}
1847 self-reference. This is the case where a macro @var{x} expands to use a
1848 macro @samp{y}, and the expansion of @samp{y} refers to the macro
1849 @samp{x}. The resulting reference to @samp{x} comes indirectly from the
1850 expansion of @samp{x}, so it is a self-reference and is not further
1851 expanded. Thus, after
1859 @samp{x} would expand into @samp{(4 + (2 * x))}. Clear?
1861 Suppose @samp{y} is used elsewhere, not from the definition of @samp{x}.
1862 Then the use of @samp{x} in the expansion of @samp{y} is not a
1863 self-reference because @samp{x} is not ``in progress''. So it does
1864 expand. However, the expansion of @samp{x} contains a reference to
1865 @samp{y}, and that is an indirect self-reference now because @samp{y} is
1866 ``in progress''. The result is that @samp{y} expands to @samp{(2 * (4 +
1869 This behavior is specified by the ISO C standard, so you may need to
1872 @node Argument Prescan, Cascaded Macros, Self-Reference, Macro Pitfalls
1873 @subsubsection Separate Expansion of Macro Arguments
1874 @cindex expansion of arguments
1875 @cindex macro argument expansion
1876 @cindex prescan of macro arguments
1878 We have explained that the expansion of a macro, including the substituted
1879 arguments, is re-scanned for macro calls to be expanded.
1881 What really happens is more subtle: first each argument is scanned
1882 separately for macro calls. Then the resulting tokens are substituted
1883 into the macro body to produce the macro expansion, and the macro
1884 expansion is scanned again for macros to expand.
1886 The result is that the arguments are scanned @emph{twice} to expand
1887 macro calls in them.
1889 Most of the time, this has no effect. If the argument contained any
1890 macro calls, they are expanded during the first scan. The result
1891 therefore contains no macro calls, so the second scan does not change
1892 it. If the argument were substituted as given, with no prescan, the
1893 single remaining scan would find the same macro calls and produce the
1896 You might expect the double scan to change the results when a
1897 self-referential macro is used in an argument of another macro
1898 (@pxref{Self-Reference}): the self-referential macro would be expanded
1899 once in the first scan, and a second time in the second scan. However,
1900 this is not what happens. The self-references that do not expand in the
1901 first scan are marked so that they will not expand in the second scan
1904 The prescan is not done when an argument is stringified or concatenated.
1914 expands to @samp{"foo"}. Once more, prescan has been prevented from
1915 having any noticeable effect.
1917 More precisely, stringification and concatenation use the argument
1918 tokens as given without initially scanning for macros. The same
1919 argument would be used in expanded form if it is substituted elsewhere
1920 without stringification or concatenation.
1923 #define str(s) #s lose(s)
1928 expands to @samp{"foo" lose(4)}.
1930 You might now ask, ``Why mention the prescan, if it makes no difference?
1931 And why not skip it and make the preprocessor faster?'' The answer is
1932 that the prescan does make a difference in three special cases:
1936 Nested calls to a macro.
1939 Macros that call other macros that stringify or concatenate.
1942 Macros whose expansions contain unshielded commas.
1945 We say that @dfn{nested} calls to a macro occur when a macro's argument
1946 contains a call to that very macro. For example, if @samp{f} is a macro
1947 that expects one argument, @samp{f (f (1))} is a nested pair of calls to
1948 @samp{f}. The desired expansion is made by expanding @samp{f (1)} and
1949 substituting that into the definition of @samp{f}. The prescan causes
1950 the expected result to happen. Without the prescan, @samp{f (1)} itself
1951 would be substituted as an argument, and the inner use of @samp{f} would
1952 appear during the main scan as an indirect self-reference and would not
1953 be expanded. Here, the prescan cancels an undesirable side effect (in
1954 the medical, not computational, sense of the term) of the special rule
1955 for self-referential macros.
1957 Prescan causes trouble in certain other cases of nested macro calls.
1962 #define bar(x) lose(x)
1963 #define lose(x) (1 + (x))
1969 We would like @samp{bar(foo)} to turn into @samp{(1 + (foo))}, which
1970 would then turn into @samp{(1 + (a,b))}. Instead, @samp{bar(foo)}
1971 expands into @samp{lose(a,b)}, and you get an error because @code{lose}
1972 requires a single argument. In this case, the problem is easily solved
1973 by the same parentheses that ought to be used to prevent misnesting of
1974 arithmetic operations:
1978 #define bar(x) lose((x))
1981 The problem is more serious when the operands of the macro are not
1982 expressions; for example, when they are statements. Then parentheses
1983 are unacceptable because they would make for invalid C code:
1986 #define foo @{ int a, b; @dots{} @}
1990 In GNU C you can shield the commas using the @samp{(@{@dots{}@})}
1991 construct which turns a compound statement into an expression:
1994 #define foo (@{ int a, b; @dots{} @})
1997 Or you can rewrite the macro definition to avoid such commas:
2000 #define foo @{ int a; int b; @dots{} @}
2003 There is also one case where prescan is useful. It is possible to use
2004 prescan to expand an argument and then stringify it --- if you use two
2005 levels of macros. Let's add a new macro @samp{xstr} to the example
2009 #define xstr(s) str(s)
2015 This expands into @samp{"4"}, not @samp{"foo"}. The reason for the
2016 difference is that the argument of @samp{xstr} is expanded at prescan
2017 (because @samp{xstr} does not specify stringification or concatenation
2018 of the argument). The result of prescan then forms the argument for
2019 @samp{str}. @samp{str} uses its argument without prescan because it
2020 performs stringification; but it cannot prevent or undo the prescanning
2021 already done by @samp{xstr}.
2023 @node Cascaded Macros, Newlines in Args, Argument Prescan, Macro Pitfalls
2024 @subsubsection Cascaded Use of Macros
2026 @cindex cascaded macros
2027 @cindex macro body uses macro
2028 A @dfn{cascade} of macros is when one macro's body contains a reference
2029 to another macro. This is very common practice. For example,
2032 #define BUFSIZE 1020
2033 #define TABLESIZE BUFSIZE
2036 This is not at all the same as defining @samp{TABLESIZE} to be
2037 @samp{1020}. The @samp{#define} for @samp{TABLESIZE} uses exactly the
2038 body you specify --- in this case, @samp{BUFSIZE} --- and does not check
2039 to see whether it too is the name of a macro.
2041 It's only when you @emph{use} @samp{TABLESIZE} that the result of its
2042 expansion is checked for more macro names.
2044 This makes a difference if you change the definition of @samp{BUFSIZE}
2045 at some point in the source file. @samp{TABLESIZE}, defined as shown,
2046 will always expand using the definition of @samp{BUFSIZE} that is
2047 currently in effect:
2050 #define BUFSIZE 1020
2051 #define TABLESIZE BUFSIZE
2057 Now @samp{TABLESIZE} expands (in two stages) to @samp{37}. (The
2058 @samp{#undef} is to prevent any warning about the nontrivial
2059 redefinition of @code{BUFSIZE}.)
2061 @node Newlines in Args,, Cascaded Macros, Macro Pitfalls
2062 @subsection Newlines in Macro Arguments
2063 @cindex newlines in macro arguments
2065 The invocation of a function-like macro can extend over many logical
2066 lines. The ISO C standard requires that newlines within a macro
2067 invocation be treated as ordinary whitespace. This means that when the
2068 expansion of a function-like macro replaces its invocation, it appears
2069 on the same line as the macro name did. Thus line numbers emitted by
2070 the compiler or debugger refer to the line the invocation started on,
2071 which might be different to the line containing the argument causing the
2074 Here is an example illustrating this:
2077 #define ignore_second_arg(a,b,c) a; c
2079 ignore_second_arg (foo (),
2085 The syntax error triggered by the tokens @samp{syntax error} results in
2086 an error message citing line three --- the line of ignore_second_arg ---
2087 even though the problematic code comes from line five.
2089 @node Conditionals, Assertions, Macros, Top
2090 @section Conditionals
2092 @cindex conditionals
2093 In a macro processor, a @dfn{conditional} is a directive that allows a
2094 part of the program to be ignored during compilation, on some
2095 conditions. In the C preprocessor, a conditional can test either an
2096 arithmetic expression or whether a name is defined as a macro.
2098 A conditional in the C preprocessor resembles in some ways an @samp{if}
2099 statement in C, but it is important to understand the difference between
2100 them. The condition in an @samp{if} statement is tested during the
2101 execution of your program. Its purpose is to allow your program to
2102 behave differently from run to run, depending on the data it is
2103 operating on. The condition in a preprocessing conditional directive is
2104 tested when your program is compiled. Its purpose is to allow different
2105 code to be included in the program depending on the situation at the
2106 time of compilation.
2109 * Uses: Conditional Uses. What conditionals are for.
2110 * Syntax: Conditional Syntax. How conditionals are written.
2111 * Deletion: Deleted Code. Making code into a comment.
2112 * Macros: Conditionals-Macros. Why conditionals are used with macros.
2113 * Errors: #error Directive. Detecting inconsistent compilation parameters.
2116 @node Conditional Uses
2117 @subsection Why Conditionals are Used
2119 Generally there are three kinds of reason to use a conditional.
2123 A program may need to use different code depending on the machine or
2124 operating system it is to run on. In some cases the code for one
2125 operating system may be erroneous on another operating system; for
2126 example, it might refer to library routines that do not exist on the
2127 other system. When this happens, it is not enough to avoid executing
2128 the invalid code: merely having it in the program makes it impossible to
2129 link the program and run it. With a preprocessing conditional, the
2130 offending code can be effectively excised from the program when it is
2134 You may want to be able to compile the same source file into two
2135 different programs. Sometimes the difference between the programs is
2136 that one makes frequent time-consuming consistency checks on its
2137 intermediate data, or prints the values of those data for debugging,
2138 while the other does not.
2141 A conditional whose condition is always false is a good way to exclude
2142 code from the program but keep it as a sort of comment for future
2146 Most simple programs that are intended to run on only one machine will
2147 not need to use preprocessing conditionals.
2149 @node Conditional Syntax
2150 @subsection Syntax of Conditionals
2153 A conditional in the C preprocessor begins with a @dfn{conditional
2154 directive}: @samp{#if}, @samp{#ifdef} or @samp{#ifndef}.
2155 @xref{Conditionals-Macros}, for information on @samp{#ifdef} and
2156 @samp{#ifndef}; only @samp{#if} is explained here.
2159 * If: #if Directive. Basic conditionals using @samp{#if} and @samp{#endif}.
2160 * Else: #else Directive. Including some text if the condition fails.
2161 * Elif: #elif Directive. Testing several alternative possibilities.
2165 @subsubsection The @samp{#if} Directive
2167 The @samp{#if} directive in its simplest form consists of
2170 #if @var{expression}
2171 @var{controlled text}
2172 #endif /* @var{expression} */
2175 The comment following the @samp{#endif} is not required, but it is a
2176 good practice because it helps people match the @samp{#endif} to the
2177 corresponding @samp{#if}. Such comments should always be used, except
2178 in short conditionals that are not nested. In fact, you can put
2179 anything at all after the @samp{#endif} and it will be ignored by the
2180 GNU C preprocessor, but only comments are acceptable in ISO Standard C@.
2182 @var{expression} is a C expression of integer type, subject to stringent
2183 restrictions. It may contain
2187 Integer constants, which are all regarded as @code{long} or
2188 @code{unsigned long}.
2191 Character constants, which are interpreted according to the character
2192 set and conventions of the machine and operating system on which the
2193 preprocessor is running. The GNU C preprocessor uses the C data type
2194 @samp{char} for these character constants; therefore, whether some
2195 character codes are negative is determined by the C compiler used to
2196 compile the preprocessor. If it treats @samp{char} as signed, then
2197 character codes large enough to set the sign bit will be considered
2198 negative; otherwise, no character code is considered negative.
2201 Arithmetic operators for addition, subtraction, multiplication,
2202 division, bitwise operations, shifts, comparisons, and logical
2203 operations (@samp{&&} and @samp{||}). The latter two obey the usual
2204 short-circuiting rules of standard C.
2207 Identifiers that are not macros, which are all treated as zero(!).
2210 Macro calls. All macro calls in the expression are expanded before
2211 actual computation of the expression's value begins.
2214 Note that @samp{sizeof} operators and @code{enum}-type values are not
2215 allowed. @code{enum}-type values, like all other identifiers that are
2216 not taken as macro calls and expanded, are treated as zero.
2218 The @var{controlled text} inside of a conditional can include
2219 preprocessing directives. Then the directives inside the conditional
2220 are obeyed only if that branch of the conditional succeeds. The text
2221 can also contain other conditional groups. However, the @samp{#if} and
2222 @samp{#endif} directives must balance.
2224 @node #else Directive
2225 @subsubsection The @samp{#else} Directive
2228 The @samp{#else} directive can be added to a conditional to provide
2229 alternative text to be used if the condition is false. This is what
2233 #if @var{expression}
2235 #else /* Not @var{expression} */
2237 #endif /* Not @var{expression} */
2240 If @var{expression} is nonzero, and thus the @var{text-if-true} is
2241 active, then @samp{#else} acts like a failing conditional and the
2242 @var{text-if-false} is ignored. Conversely, if the @samp{#if}
2243 conditional fails, the @var{text-if-false} is considered included.
2245 @node #elif Directive
2246 @subsubsection The @samp{#elif} Directive
2249 One common case of nested conditionals is used to check for more than two
2250 possible alternatives. For example, you might have
2264 Another conditional directive, @samp{#elif}, allows this to be
2265 abbreviated as follows:
2272 #else /* X != 2 and X != 1*/
2274 #endif /* X != 2 and X != 1*/
2277 @samp{#elif} stands for ``else if''. Like @samp{#else}, it goes in the
2278 middle of a @samp{#if}-@samp{#endif} pair and subdivides it; it does not
2279 require a matching @samp{#endif} of its own. Like @samp{#if}, the
2280 @samp{#elif} directive includes an expression to be tested.
2282 The text following the @samp{#elif} is processed only if the original
2283 @samp{#if}-condition failed and the @samp{#elif} condition succeeds.
2284 More than one @samp{#elif} can go in the same @samp{#if}-@samp{#endif}
2285 group. Then the text after each @samp{#elif} is processed only if the
2286 @samp{#elif} condition succeeds after the original @samp{#if} and any
2287 previous @samp{#elif} directives within it have failed. @samp{#else} is
2288 equivalent to @samp{#elif 1}, and @samp{#else} is allowed after any
2289 number of @samp{#elif} directives, but @samp{#elif} may not follow
2293 @subsection Keeping Deleted Code for Future Reference
2294 @cindex commenting out code
2296 If you replace or delete a part of the program but want to keep the old
2297 code around as a comment for future reference, the easy way to do this
2298 is to put @samp{#if 0} before it and @samp{#endif} after it. This is
2299 better than using comment delimiters @samp{/*} and @samp{*/} since those
2300 won't work if the code already contains comments (C comments do not
2303 This works even if the code being turned off contains conditionals, but
2304 they must be entire conditionals (balanced @samp{#if} and @samp{#endif}).
2306 Conversely, do not use @samp{#if 0} for comments which are not C code.
2307 Use the comment delimiters @samp{/*} and @samp{*/} instead. The
2308 interior of @samp{#if 0} must consist of complete tokens; in particular,
2309 single-quote characters must balance. Comments often contain unbalanced
2310 single-quote characters (known in English as apostrophes). These
2311 confuse @samp{#if 0}. They do not confuse @samp{/*}.
2313 @node Conditionals-Macros
2314 @subsection Conditionals and Macros
2316 Conditionals are useful in connection with macros or assertions, because
2317 those are the only ways that an expression's value can vary from one
2318 compilation to another. A @samp{#if} directive whose expression uses no
2319 macros or assertions is equivalent to @samp{#if 1} or @samp{#if 0}; you
2320 might as well determine which one, by computing the value of the
2321 expression yourself, and then simplify the program.
2323 For example, here is a conditional that tests the expression
2324 @samp{BUFSIZE == 1020}, where @samp{BUFSIZE} must be a macro.
2328 printf ("Large buffers!\n");
2329 #endif /* BUFSIZE is large */
2332 (Programmers often wish they could test the size of a variable or data
2333 type in @samp{#if}, but this does not work. The preprocessor does not
2334 understand @code{sizeof}, or typedef names, or even the type keywords
2335 such as @code{int}.)
2338 The special operator @samp{defined} is used in @samp{#if} expressions to
2339 test whether a certain name is defined as a macro. Either @samp{defined
2340 @var{name}} or @samp{defined (@var{name})} is an expression whose value
2341 is 1 if @var{name} is defined as macro at the current point in the
2342 program, and 0 otherwise. For the @samp{defined} operator it makes no
2343 difference what the definition of the macro is; all that matters is
2344 whether there is a definition. Thus, for example,@refill
2347 #if defined (vax) || defined (ns16000)
2351 would succeed if either of the names @samp{vax} and @samp{ns16000} is
2352 defined as a macro. You can test the same condition using assertions
2353 (@pxref{Assertions}), like this:
2356 #if #cpu (vax) || #cpu (ns16000)
2359 If a macro is defined and later undefined with @samp{#undef}, subsequent
2360 use of the @samp{defined} operator returns 0, because the name is no
2361 longer defined. If the macro is defined again with another
2362 @samp{#define}, @samp{defined} will recommence returning 1.
2366 Conditionals that test whether a single macro is defined are very common,
2367 so there are two special short conditional directives for this case.
2370 @item #ifdef @var{name}
2371 is equivalent to @samp{#if defined (@var{name})}.
2373 @item #ifndef @var{name}
2374 is equivalent to @samp{#if ! defined (@var{name})}.
2377 Macro definitions can vary between compilations for several reasons.
2381 Some macros are predefined on each kind of machine. For example, on a
2382 Vax, the name @samp{vax} is a predefined macro. On other machines, it
2383 would not be defined.
2386 Many more macros are defined by system header files. Different systems
2387 and machines define different macros, or give them different values. It
2388 is useful to test these macros with conditionals to avoid using a system
2389 feature on a machine where it is not implemented.
2392 Macros are a common way of allowing users to customize a program for
2393 different machines or applications. For example, the macro
2394 @samp{BUFSIZE} might be defined in a configuration file for your program
2395 that is included as a header file in each source file. You would use
2396 @samp{BUFSIZE} in a preprocessing conditional in order to generate
2397 different code depending on the chosen configuration.
2400 Macros can be defined or undefined with @samp{-D} and @samp{-U} command
2401 options when you compile the program. You can arrange to compile the
2402 same source file into two different programs by choosing a macro name to
2403 specify which program you want, writing conditionals to test whether or
2404 how this macro is defined, and then controlling the state of the macro
2405 with compiler command options. @xref{Invocation}.
2409 Assertions are usually predefined, but can be defined with preprocessor
2410 directives or command-line options.
2413 @node #error Directive
2414 @subsection The @samp{#error} and @samp{#warning} Directives
2417 The directive @samp{#error} causes the preprocessor to report a fatal
2418 error. The tokens forming the rest of the line following @samp{#error}
2419 are used as the error message, and not macro-expanded. Internal
2420 whitespace sequences are each replaced with a single space. The line
2421 must consist of complete tokens.
2423 You would use @samp{#error} inside of a conditional that detects a
2424 combination of parameters which you know the program does not properly
2425 support. For example, if you know that the program will not run
2426 properly on a Vax, you might write
2431 #error "Won't work on Vaxen. See comments at get_last_object."
2437 @xref{Nonstandard Predefined}, for why this works.
2439 If you have several configuration parameters that must be set up by
2440 the installation in a consistent way, you can use conditionals to detect
2441 an inconsistency and report it with @samp{#error}. For example,
2444 #if HASH_TABLE_SIZE % 2 == 0 || HASH_TABLE_SIZE % 3 == 0 \
2445 || HASH_TABLE_SIZE % 5 == 0
2446 #error HASH_TABLE_SIZE should not be divisible by a small prime
2451 The directive @samp{#warning} is like the directive @samp{#error}, but
2452 causes the preprocessor to issue a warning and continue preprocessing.
2453 The tokens following @samp{#warning} are used as the warning message,
2454 and not macro-expanded.
2456 You might use @samp{#warning} in obsolete header files, with a message
2457 directing the user to the header file which should be used instead.
2459 @node Assertions, Line Control, Conditionals, Top
2462 @dfn{Assertions} are a more systematic alternative to macros in writing
2463 conditionals to test what sort of computer or system the compiled
2464 program will run on. Assertions are usually predefined, but you can
2465 define them with preprocessing directives or command-line options.
2468 The macros traditionally used to describe the type of target are not
2469 classified in any way according to which question they answer; they may
2470 indicate a hardware architecture, a particular hardware model, an
2471 operating system, a particular version of an operating system, or
2472 specific configuration options. These are jumbled together in a single
2473 namespace. In contrast, each assertion consists of a named question and
2474 an answer. The question is usually called the @dfn{predicate}. An
2475 assertion looks like this:
2478 #@var{predicate} (@var{answer})
2482 You must use a properly formed identifier for @var{predicate}. The
2483 value of @var{answer} can be any sequence of words; all characters are
2484 significant except for leading and trailing whitespace, and differences
2485 in internal whitespace sequences are ignored. (This is similar to the
2486 rules governing macro redefinition.) Thus, @samp{x + y} is different
2487 from @samp{x+y} but equivalent to @samp{ x + y }. @samp{)} is not
2488 allowed in an answer.
2490 @cindex testing predicates
2491 Here is a conditional to test whether the answer @var{answer} is asserted
2492 for the predicate @var{predicate}:
2495 #if #@var{predicate} (@var{answer})
2499 There may be more than one answer asserted for a given predicate. If
2500 you omit the answer, you can test whether @emph{any} answer is asserted
2501 for @var{predicate}:
2504 #if #@var{predicate}
2510 Most of the time, the assertions you test will be predefined assertions.
2511 GNU C provides three predefined predicates: @code{system}, @code{cpu},
2512 and @code{machine}. @code{system} is for assertions about the type of
2513 software, @code{cpu} describes the type of computer architecture, and
2514 @code{machine} gives more information about the computer. For example,
2515 on a GNU system, the following assertions would be true:
2521 #system (mach 3.@var{subversion})
2523 #system (hurd @var{version})
2527 and perhaps others. The alternatives with
2528 more or less version information let you ask more or less detailed
2529 questions about the type of system software.
2531 On a Unix system, you would find @code{#system (unix)} and perhaps one of:
2532 @code{#system (aix)}, @code{#system (bsd)}, @code{#system (hpux)},
2533 @code{#system (lynx)}, @code{#system (mach)}, @code{#system (posix)},
2534 @code{#system (svr3)}, @code{#system (svr4)}, or @code{#system (xpg4)}
2535 with possible version numbers following.
2537 Other values for @code{system} are @code{#system (mvs)}
2538 and @code{#system (vms)}.
2540 @strong{Portability note:} Many Unix C compilers provide only one answer
2541 for the @code{system} assertion: @code{#system (unix)}, if they support
2542 assertions at all. This is less than useful.
2544 An assertion with a multi-word answer is completely different from several
2545 assertions with individual single-word answers. For example, the presence
2546 of @code{system (mach 3.0)} does not mean that @code{system (3.0)} is true.
2547 It also does not directly imply @code{system (mach)}, but in GNU C, that
2548 last will normally be asserted as well.
2550 The current list of possible assertion values for @code{cpu} is:
2551 @code{#cpu (a29k)}, @code{#cpu (alpha)}, @code{#cpu (arm)}, @code{#cpu
2552 (clipper)}, @code{#cpu (convex)}, @code{#cpu (elxsi)}, @code{#cpu
2553 (tron)}, @code{#cpu (h8300)}, @code{#cpu (i370)}, @code{#cpu (i386)},
2554 @code{#cpu (i860)}, @code{#cpu (i960)}, @code{#cpu (m68k)}, @code{#cpu
2555 (m88k)}, @code{#cpu (mips)}, @code{#cpu (ns32k)}, @code{#cpu (hppa)},
2556 @code{#cpu (pyr)}, @code{#cpu (ibm032)}, @code{#cpu (rs6000)},
2557 @code{#cpu (sh)}, @code{#cpu (sparc)}, @code{#cpu (spur)}, @code{#cpu
2558 (tahoe)}, @code{#cpu (vax)}, @code{#cpu (we32000)}.
2561 You can create assertions within a C program using @samp{#assert}, like
2565 #assert @var{predicate} (@var{answer})
2569 (Note the absence of a @samp{#} before @var{predicate}.)
2572 @cindex assertions, undoing
2573 @cindex retracting assertions
2575 Each time you do this, you assert a new true answer for @var{predicate}.
2576 Asserting one answer does not invalidate previously asserted answers;
2577 they all remain true. The only way to remove an answer is with
2578 @samp{#unassert}. @samp{#unassert} has the same syntax as
2579 @samp{#assert}. You can also remove all answers to a @var{predicate}
2583 #unassert @var{predicate}
2586 You can also add or cancel assertions using command options
2587 when you run @code{gcc} or @code{cpp}. @xref{Invocation}.
2589 @node Line Control, Other Directives, Assertions, Top
2590 @section Combining Source Files
2592 @cindex line control
2593 One of the jobs of the C preprocessor is to inform the C compiler of where
2594 each line of C code came from: which source file and which line number.
2596 C code can come from multiple source files if you use @samp{#include};
2597 both @samp{#include} and the use of conditionals and macros can cause
2598 the line number of a line in the preprocessor output to be different
2599 from the line's number in the original source file. You will appreciate
2600 the value of making both the C compiler (in error messages) and symbolic
2601 debuggers such as GDB use the line numbers in your source file.
2603 The C preprocessor builds on this feature by offering a directive by
2604 which you can control the feature explicitly. This is useful when a
2605 file for input to the C preprocessor is the output from another program
2606 such as the @code{bison} parser generator, which operates on another
2607 file that is the true source file. Parts of the output from
2608 @code{bison} are generated from scratch, other parts come from a
2609 standard parser file. The rest are copied nearly verbatim from the
2610 source file, but their line numbers in the @code{bison} output are not
2611 the same as their original line numbers. Naturally you would like
2612 compiler error messages and symbolic debuggers to know the original
2613 source file and line number of each line in the @code{bison} input.
2616 @code{bison} arranges this by writing @samp{#line} directives into the output
2617 file. @samp{#line} is a directive that specifies the original line number
2618 and source file name for subsequent input in the current preprocessor input
2619 file. @samp{#line} has three variants:
2622 @item #line @var{linenum}
2623 Here @var{linenum} is a decimal integer constant. This specifies that
2624 the line number of the following line of input, in its original source file,
2627 @item #line @var{linenum} @var{filename}
2628 Here @var{linenum} is a decimal integer constant and @var{filename} is a
2629 string constant. This specifies that the following line of input came
2630 originally from source file @var{filename} and its line number there was
2631 @var{linenum}. Keep in mind that @var{filename} is not just a file
2632 name; it is surrounded by double-quote characters so that it looks like
2635 @item #line @var{anything else}
2636 @var{anything else} is checked for macro calls, which are expanded.
2637 The result should be a decimal integer constant followed optionally
2638 by a string constant, as described above.
2641 @samp{#line} directives alter the results of the @samp{__FILE__} and
2642 @samp{__LINE__} predefined macros from that point on. @xref{Standard
2645 The output of the preprocessor (which is the input for the rest of the
2646 compiler) contains directives that look much like @samp{#line}
2647 directives. They start with just @samp{#} instead of @samp{#line}, but
2648 this is followed by a line number and file name as in @samp{#line}.
2651 @node Other Directives, Output, Line Control, Top
2652 @section Miscellaneous Preprocessing Directives
2654 @cindex null directive
2655 This section describes three additional preprocessing directives. They
2656 are not very useful, but are mentioned for completeness.
2658 The @dfn{null directive} consists of a @samp{#} followed by a newline,
2659 with only whitespace (including comments) in between. A null directive
2660 is understood as a preprocessing directive but has no effect on the
2661 preprocessor output. The primary significance of the existence of the
2662 null directive is that an input line consisting of just a @samp{#} will
2663 produce no output, rather than a line of output containing just a
2664 @samp{#}. Supposedly some old C programs contain such lines.
2669 The ISO standard specifies that the effect of the @samp{#pragma}
2670 directive is implementation-defined. The GNU C preprocessor recognizes
2671 some pragmas, and passes unrecognized ones through to the preprocessor
2672 output, so they are available to the compilation pass.
2674 In line with the C99 standard, which introduces a STDC namespace for C99
2675 pragmas, the preprocessor introduces a GCC namespace for GCC pragmas.
2676 Supported GCC preprocessor pragmas are of the form @samp{#pragma GCC
2677 ...}. For backwards compatibility previously supported pragmas are also
2678 recognized without the @samp{GCC} prefix, however that use is
2679 deprecated. Pragmas that are already deprecated are not recognized with
2680 a @samp{GCC} prefix.
2683 The @samp{#ident} directive is supported for compatibility with certain
2684 other systems. It is followed by a line of text. On some systems, the
2685 text is copied into a special place in the object file; on most systems,
2686 the text is ignored and this directive has no effect. Typically
2687 @samp{#ident} is only used in header files supplied with those systems
2688 where it is meaningful.
2690 @findex #pragma GCC dependency
2691 The @samp{#pragma GCC dependency} allows you to check the relative dates
2692 of the current file and another file. If the other file is more recent
2693 than the current file, a warning is issued. This is useful if the
2694 include file is derived from the other file, and should be regenerated.
2695 The other file is searched for using the normal include search path.
2696 Optional trailing text can be used to give more information in the
2700 #pragma GCC dependency "parse.y"
2701 #pragma GCC dependency "/usr/include/time.h" rerun /path/to/fixincludes
2704 @node Output, Unreliable Features, Other Directives, Top
2705 @section C Preprocessor Output
2707 @cindex output format
2708 The output from the C preprocessor looks much like the input, except
2709 that all preprocessing directive lines have been replaced with blank
2710 lines and all comments with spaces.
2712 The ISO standard specifies that it is implementation defined whether a
2713 preprocessor preserves whitespace between tokens, or replaces it with
2714 e.g. a single space. In the GNU C preprocessor, whitespace between
2715 tokens is collapsed to become a single space, with the exception that
2716 the first token on a non-directive line is preceded with sufficient
2717 spaces that it appears in the same column in the preprocessed output
2718 that it appeared in in the original source file. This is so the output
2719 is easy to read. @xref{Unreliable Features}.
2721 Source file name and line number information is conveyed by lines
2725 # @var{linenum} @var{filename} @var{flags}
2729 which are inserted as needed into the output (but never within a string
2730 or character constant), and in place of long sequences of empty lines.
2731 Such a line means that the following line originated in file
2732 @var{filename} at line @var{linenum}.
2734 After the file name comes zero or more flags, which are @samp{1},
2735 @samp{2}, @samp{3}, or @samp{4}. If there are multiple flags, spaces
2736 separate them. Here is what the flags mean:
2740 This indicates the start of a new file.
2742 This indicates returning to a file (after having included another file).
2744 This indicates that the following text comes from a system header file,
2745 so certain warnings should be suppressed.
2747 This indicates that the following text should be treated as C@.
2748 @c maybe cross reference NO_IMPLICIT_EXTERN_C
2751 @node Unreliable Features, Invocation, Output, Top
2752 @section Undefined Behavior and Deprecated Features
2753 @cindex undefined behavior
2754 @cindex deprecated features
2756 This section details GNU C preprocessor behavior that is subject to
2757 change or deprecated. You are @emph{strongly advised} to write your
2758 software so it does not rely on anything described here; future versions
2759 of the preprocessor may subtly change such behavior or even remove the
2762 Preservation of the form of whitespace between tokens is unlikely to
2763 change from current behavior (see @ref{Output}), but you are advised not
2766 The following are undocumented and subject to change:-
2770 @item Interpretation of the filename between @samp{<} and @samp{>} tokens
2771 resulting from a macro-expanded @samp{#include} directive
2773 The text between the @samp{<} and @samp{>} is taken literally if given
2774 directly within a @samp{#include} or similar directive. If a directive
2775 of this form is obtained through macro expansion, however, behavior like
2776 preservation of whitespace, and interpretation of backslashes and quotes
2777 is undefined. @xref{Include Syntax}
2779 @item Precedence of ## operators with respect to each other
2781 It is not defined whether a sequence of ## operators are evaluated
2782 left-to-right, right-to-left or indeed in a consistent direction at all.
2783 An example of where this might matter is pasting the arguments @samp{1},
2784 @samp{e} and @samp{-2}. This would be fine for left-to-right pasting,
2785 but right-to-left pasting would produce an invalid token @samp{e-2}.
2787 @item Precedence of # operator with respect to the ## operator
2789 It is undefined which of these two operators is evaluated first.
2793 The following features are deprecated and will likely be removed at some
2794 point in the future:-
2798 @item ## swallowing the previous token in GNU rest argument macros
2800 In a macro expansion, if ## appeared before a GNU named variable arguments
2801 parameter, and the set of tokens specified for that argument in the
2802 macro invocation was empty, previous versions of the GNU C preprocessor
2803 would back up and remove the token appearing before the ##. This
2804 behavior was not well-defined, and alternative ways of achieving its
2805 intended use are available. Since the ISO C standard now provides for
2806 variable-argument macros, and since this old behavior potentially
2807 conflicts with behavior mandated by the standard, this feature is now
2808 deprecated and will be removed in future.
2810 The current preprocessor still supports it for reasons of code
2811 migration, and warns at each use of the feature.
2813 @item Optional argument when invoking GNU rest argument macros
2815 In the invocation of a GNU named variable arguments macro, the variable
2816 arguments were optional. For example, the following two invocations are
2817 both legal for GNU rest args. The first is illegal in the equivalent
2818 formulation using ISO C anonymous variable arguments and
2819 @code{__VA_ARGS__}:-
2822 #define debug(format, args...) printf (format, args)
2823 debug("string"); /* Illegal in ISO C equivalent. */
2824 debug("string",); /* OK for both. */
2827 The current preprocessor still supports it for reasons of code
2828 migration, and warns at each use of the feature.
2830 @item Attempting to paste two tokens which together do not form a valid
2833 The preprocessor currently warns about this and outputs the two tokens
2834 adjacently, which is probably the behavior the programmer intends. It
2835 may not work in future, though.
2837 @findex #pragma once
2840 This pragma was once used to tell the preprocessor that it need not
2841 include a file more than once. It is now obsolete and should not be
2844 @item #pragma poison
2846 This pragma has been superceded by @samp{#pragma GCC poison}.
2849 @item Multi-line string literals in directives
2851 The GNU C preprocessor currently allows newlines in string literals
2856 @node Invocation, Concept Index, Unreliable Features, Top
2857 @section Invoking the C Preprocessor
2858 @cindex invocation of the preprocessor
2860 Most often when you use the C preprocessor you will not have to invoke it
2861 explicitly: the C compiler will do so automatically. However, the
2862 preprocessor is sometimes useful on its own.
2865 @c man begin SYNOPSIS
2866 cpp [@samp{-P}] [@samp{-C}] [@samp{-gcc}] [@samp{-traditional}]
2867 [@samp{-undef}] [@samp{-trigraphs}] [@samp{-pedantic}]
2868 [@samp{-W}@var{warn}...] [@samp{-I}@var{dir}...]
2869 [@samp{-D}@var{macro}[=@var{defn}]...] [@samp{-U}@var{macro}]
2870 [@samp{-A}@var{predicate}(@var{answer})]
2871 [@samp{-M}|@samp{-MM}|@samp{-MD}|@samp{-MMD} [@samp{-MG}]]
2872 [@samp{-x} @var{language}] [@samp{-std=}@var{standard}]
2873 @var{infile} @var{outfile}
2875 Only the most useful options are listed here; see below for the remainder.
2877 @c man begin SEEALSO
2878 gcc(1), as(1), ld(1), and the Info entries for @file{cpp}, @file{gcc}, and
2883 @c man begin OPTIONS
2884 The C preprocessor expects two file names as arguments, @var{infile} and
2885 @var{outfile}. The preprocessor reads @var{infile} together with any
2886 other files it specifies with @samp{#include}. All the output generated
2887 by the combined input files is written in @var{outfile}.
2889 Either @var{infile} or @var{outfile} may be @samp{-}, which as
2890 @var{infile} means to read from standard input and as @var{outfile}
2891 means to write to standard output. Also, if either file is omitted, it
2892 means the same as if @samp{-} had been specified for that file.
2895 Here is a table of command options accepted by the C preprocessor.
2896 These options can also be given when compiling a C program; they are
2897 passed along automatically to the preprocessor when it is invoked by the
2903 Inhibit generation of @samp{#}-lines with line-number information in
2904 the output from the preprocessor (@pxref{Output}). This might be
2905 useful when running the preprocessor on something that is not C code
2906 and will be sent to a program which might be confused by the
2911 Do not discard comments. All comments are passed through to the output
2912 file, except for comments in processed directives, which are deleted
2913 along with the directive. Comments appearing in the expansion list of a
2914 macro will be preserved, and appear in place wherever the macro is
2917 You should be prepared for side effects when using -C; it causes the
2918 preprocessor to treat comments as tokens in their own right. For
2919 example, macro redefinitions that were trivial when comments were
2920 replaced by a single space might become significant when comments are
2921 retained. Also, comments appearing at the start of what would be a
2922 directive line have the effect of turning that line into an ordinary
2923 source line, since the first token on the line is no longer a @samp{#}.
2926 @findex -traditional
2927 Try to imitate the behavior of old-fashioned C, as opposed to ISO C@.
2928 Note: support for this option is currently fairly broken.
2932 Traditional macro expansion pays no attention to single-quote or
2933 double-quote characters; macro argument symbols are replaced by the
2934 argument values even when they appear within apparent string or
2935 character constants.
2938 Traditionally, it is permissible for a macro expansion to end in the
2939 middle of a string or character constant. The constant continues into
2940 the text surrounding the macro call.
2943 However, traditionally the end of the line terminates a string or
2944 character constant, with no error.
2947 In traditional C, a comment is equivalent to no text at all. (In ISO
2948 C, a comment counts as whitespace.)
2951 Traditional C does not have the concept of a ``preprocessing number''.
2952 It considers @samp{1.0e+4} to be three tokens: @samp{1.0e}, @samp{+},
2956 A macro is not suppressed within its own definition, in traditional C@.
2957 Thus, any macro that is used recursively inevitably causes an error.
2960 The character @samp{#} has no special meaning within a macro definition
2964 In traditional C, the text at the end of a macro expansion can run
2965 together with the text after the macro call, to produce a single token.
2966 (This is impossible in ISO C@.)
2969 Traditionally, @samp{\} inside a macro argument suppresses the syntactic
2970 significance of the following character.
2974 @cindex unterminated
2975 Use the @samp{-traditional} option when preprocessing Fortran code, so
2976 that single-quotes and double-quotes within Fortran comment lines (which
2977 are generally not recognized as such by the preprocessor) do not cause
2978 diagnostics about unterminated character or string constants.
2980 However, this option does not prevent diagnostics about unterminated
2981 comments when a C-style comment appears to start, but not end, within
2982 Fortran-style commentary.
2984 So, the following Fortran comment lines are accepted with
2985 @samp{-traditional}:
2988 C This isn't an unterminated character constant
2989 C Neither is "20000000000, an octal constant
2990 C in some dialects of Fortran
2993 However, this type of comment line will likely produce a diagnostic, or
2994 at least unexpected output from the preprocessor, due to the
2995 unterminated comment:
2998 C Some Fortran compilers accept /* as starting
2999 C an inline comment.
3003 Note that @code{g77} automatically supplies the @samp{-traditional}
3004 option when it invokes the preprocessor. However, a future version of
3005 @code{g77} might use a different, more-Fortran-aware preprocessor in
3006 place of @code{cpp}.
3010 Process ISO standard trigraph sequences. These are three-character
3011 sequences, all starting with @samp{??}, that are defined by ISO C to
3012 stand for single characters. For example, @samp{??/} stands for
3013 @samp{\}, so @samp{'??/n'} is a character constant for a newline.
3014 Strictly speaking, the GNU C preprocessor does not conform to ISO
3015 Standard C unless @samp{-trigraphs} is used, but if you ever notice the
3016 difference it will be with relief.
3018 The nine trigraph sequences are
3019 @samp{??(} -> @samp{[},
3020 @samp{??)} -> @samp{]},
3021 @samp{??<} -> @samp{@{},
3022 @samp{??>} -> @samp{@}},
3023 @samp{??=} -> @samp{#},
3024 @samp{??/} -> @samp{\},
3025 @samp{??'} -> @samp{^},
3026 @samp{??!} -> @samp{|},
3027 @samp{??-} -> @samp{~}
3031 Issue warnings required by the ISO C standard in certain cases such
3032 as when text other than a comment follows @samp{#else} or @samp{#endif}.
3034 @item -pedantic-errors
3035 @findex -pedantic-errors
3036 Like @samp{-pedantic}, except that errors are produced rather than
3042 @c "Not worth documenting" both singular and plural forms of this
3043 @c option, per RMS. Also unclear which is better; hence may need to
3044 @c switch this at some future date. pesch@cygnus.com, 2jan92.
3046 (Both forms have the same effect).
3048 Warn whenever a comment-start sequence @samp{/*} appears in a @samp{/*}
3049 comment, or whenever a backslash-newline appears in a @samp{//} comment.
3053 Warn if any trigraphs are encountered.
3056 @findex -Wwhite-space
3057 Warn about possible white space confusion, e.g. white space between a
3058 backslash and a newline.
3062 Requests @samp{-Wcomment}, @samp{-Wtrigraphs}, and @samp{-Wwhite-space}
3063 (but not @samp{-Wtraditional} or @samp{-Wundef}).
3066 @findex -Wtraditional
3067 Warn about certain constructs that behave differently in traditional and
3072 Warn if an undefined identifier is evaluated in an @samp{#if} directive.
3074 @item -I @var{directory}
3076 Add the directory @var{directory} to the head of the list of
3077 directories to be searched for header files (@pxref{Include Syntax}).
3078 This can be used to override a system header file, substituting your
3079 own version, since these directories are searched before the system
3080 header file directories. If you use more than one @samp{-I} option,
3081 the directories are scanned in left-to-right order; the standard
3082 system directories come after.
3085 Any directories specified with @samp{-I} options before the @samp{-I-}
3086 option are searched only for the case of @samp{#include "@var{file}"};
3087 they are not searched for @samp{#include <@var{file}>}.
3089 If additional directories are specified with @samp{-I} options after
3090 the @samp{-I-}, these directories are searched for all @samp{#include}
3093 In addition, the @samp{-I-} option inhibits the use of the current
3094 directory as the first search directory for @samp{#include "@var{file}"}.
3095 Therefore, the current directory is searched only if it is requested
3096 explicitly with @samp{-I.}. Specifying both @samp{-I-} and @samp{-I.}
3097 allows you to control precisely which directories are searched before
3098 the current one and which are searched after.
3102 Do not search the standard system directories for header files.
3103 Only the directories you have specified with @samp{-I} options
3104 (and the current directory, if appropriate) are searched.
3108 Do not search for header files in the C++-specific standard directories,
3109 but do still search the other standard directories. (This option is
3110 used when building the C++ library.)
3114 When searching for a header file in a directory, remap file names if a
3115 file named @file{header.gcc} exists in that directory. This can be used
3116 to work around limitations of file systems with file name restrictions.
3117 The @file{header.gcc} file should contain a series of lines with two
3118 tokens on each line: the first token is the name to map, and the second
3119 token is the actual name to use.
3123 Predefine @var{name} as a macro, with definition @samp{1}.
3125 @item -D @var{name}=@var{definition}
3126 Predefine @var{name} as a macro, with definition @var{definition}.
3127 There are no restrictions on the contents of @var{definition}, but if
3128 you are invoking the preprocessor from a shell or shell-like program you
3129 may need to use the shell's quoting syntax to protect characters such as
3130 spaces that have a meaning in the shell syntax. If you use more than
3131 one @samp{-D} for the same @var{name}, the rightmost definition takes
3136 Do not predefine @var{name}. If both @samp{-U} and @samp{-D} are
3137 specified for one name, whichever one appears later on the command line
3142 Do not predefine any nonstandard macros.
3146 Define the macros @var{__GNUC__}, @var{__GNUC_MINOR__} and
3147 @var{__GNUC_PATCHLEVEL__}. These are defined automatically when you use
3148 @samp{gcc -E}; you can turn them off in that case with @samp{-no-gcc}.
3150 @item -A @var{predicate}(@var{answer})
3152 Make an assertion with the predicate @var{predicate} and answer
3153 @var{answer}. @xref{Assertions}.
3155 @item -A -@var{predicate}(@var{answer})
3156 Disable an assertion with the predicate @var{predicate} and answer
3157 @var{answer}. Specifying no predicate, by @samp{-A-} or @samp{-A -},
3158 disables all predefined assertions and all assertions preceding it on
3159 the command line; and also undefines all predefined macros and all
3160 macros preceding it on the command line.
3164 Instead of outputting the result of preprocessing, output a list of
3165 @samp{#define} directives for all the macros defined during the
3166 execution of the preprocessor, including predefined macros. This gives
3167 you a way of finding out what is predefined in your version of the
3168 preprocessor; assuming you have no file @samp{foo.h}, the command
3171 touch foo.h; cpp -dM foo.h
3175 will show the values of any predefined macros.
3179 Like @samp{-dM} except in two respects: it does @emph{not} include the
3180 predefined macros, and it outputs @emph{both} the @samp{#define}
3181 directives and the result of preprocessing. Both kinds of output go to
3182 the standard output file.
3186 Output @samp{#include} directives in addition to the result of
3191 Instead of outputting the result of preprocessing, output a rule
3192 suitable for @code{make} describing the dependencies of the main source
3193 file. The preprocessor outputs one @code{make} rule containing the
3194 object file name for that source file, a colon, and the names of all the
3195 included files. If there are many included files then the rule is split
3196 into several lines using @samp{\}-newline.
3198 @samp{-MG} says to treat missing header files as generated files and
3199 assume they live in the same directory as the source file. It must be
3200 specified in addition to @samp{-M}.
3202 This feature is used in automatic updating of makefiles.
3206 Like @samp{-M} but mention only the files included with @samp{#include
3207 "@var{file}"}. System header files included with @samp{#include
3208 <@var{file}>} are omitted.
3210 @item -MD @var{file}
3212 Like @samp{-M} but the dependency information is written to @var{file}.
3213 This is in addition to compiling the file as specified --- @samp{-MD}
3214 does not inhibit ordinary compilation the way @samp{-M} does.
3216 When invoking @code{gcc}, do not specify the @var{file} argument.
3217 @code{gcc} will create file names made by replacing ".c" with ".d" at
3218 the end of the input file names.
3220 In Mach, you can use the utility @code{md} to merge multiple dependency
3221 files into a single dependency file suitable for using with the
3222 @samp{make} command.
3224 @item -MMD @var{file}
3226 Like @samp{-MD} except mention only user header files, not system
3231 Print the name of each header file used, in addition to other normal
3234 @item -imacros @var{file}
3236 Process @var{file} as input, discarding the resulting output, before
3237 processing the regular input file. Because the output generated from
3238 @var{file} is discarded, the only effect of @samp{-imacros @var{file}}
3239 is to make the macros defined in @var{file} available for use in the
3242 @item -include @var{file}
3244 Process @var{file} as input, and include all the resulting output,
3245 before processing the regular input file.
3247 @item -idirafter @var{dir}
3249 @cindex second include path
3250 Add the directory @var{dir} to the second include path. The directories
3251 on the second include path are searched when a header file is not found
3252 in any of the directories in the main include path (the one that
3255 @item -iprefix @var{prefix}
3257 Specify @var{prefix} as the prefix for subsequent @samp{-iwithprefix}
3258 options. If the prefix represents a directory, you should include the
3261 @item -iwithprefix @var{dir}
3262 @findex -iwithprefix
3263 Add a directory to the second include path. The directory's name is
3264 made by concatenating @var{prefix} and @var{dir}, where @var{prefix} was
3265 specified previously with @samp{-iprefix}.
3267 @item -isystem @var{dir}
3269 Add a directory to the beginning of the second include path, marking it
3270 as a system directory, so that it gets the same special treatment as
3271 is applied to the standard system directories. @xref{System Headers}.
3275 @itemx -x objective-c
3276 @itemx -x assembler-with-cpp
3278 @findex -x objective-c
3279 @findex -x assembler-with-cpp
3280 Specify the source language: C, C++, Objective-C, or assembly. This has
3281 nothing to do with standards conformance or extensions; it merely
3282 selects which base syntax to expect. If you give none of these options,
3283 cpp will deduce the language from the extension of the source file:
3284 @samp{.c}, @samp{.cc}, @samp{.m}, or @samp{.S}. Some other common
3285 extensions for C++ and assembly are also recognized. If cpp does not
3286 recognize the extension, it will treat the file as C; this is the most
3289 @strong{Note:} Previous versions of cpp accepted a @samp{-lang} option
3290 which selected both the language and the standards conformance level.
3291 This option has been removed, because it conflicts with the @samp{-l}
3294 @item -std=@var{standard}
3298 Specify the standard to which the code should conform. Currently cpp
3299 only knows about the standards for C; other language standards will be
3300 added in the future.
3307 The ISO C standard from 1990. @samp{c89} is the customary shorthand for
3308 this version of the standard.
3310 The @samp{-ansi} option is equivalent to @samp{-std=c89}.
3312 @item iso9899:199409
3313 The 1990 C standard, as amended in 1994.
3319 The revised ISO C standard, published in December 1999. Before
3320 publication, this was known as C9X.
3323 The 1990 C standard plus GNU extensions. This is the default.
3327 The 1999 C standard plus GNU extensions.
3332 Look for commands to the program checker @code{lint} embedded in
3333 comments, and emit them preceded by @samp{#pragma lint}. For example,
3334 the comment @samp{/* NOTREACHED */} becomes @samp{#pragma lint
3337 Because of the clash with @samp{-l}, you must use the awkward syntax
3338 above. In a future release, this option will be replaced by
3339 @samp{-flint} or @samp{-Wlint}; we are not sure which yet.
3341 @item -ftabstop=NUMBER
3343 Indicates the distance between tabstops. This helps the preprocessor
3344 report correct column numbers in warnings or errors, even if tabs appear
3345 on the line. Values less than 1 or greater than 100 are ignored. The
3350 Forbid the use of @samp{$} in identifiers. The C standard allows
3351 implementations to define extra characters that can appear in
3352 identifiers. By default the GNU C preprocessor permits @samp{$}, a
3357 @node Concept Index, Index, Invocation, Top
3358 @unnumbered Concept Index
3361 @node Index,, Concept Index, Top
3362 @unnumbered Index of Directives, Macros and Options