* tree.h (TYPE_IS_SIZETYPE): Add more documentation.
[official-gcc.git] / gcc / cpp.texi
blob8a0bc4fa08972a433eb9422d4df427cc6b4be217
1 \input texinfo
2 @setfilename cpp.info
3 @settitle The C Preprocessor
5 @ifinfo
6 @dircategory Programming
7 @direntry
8 * Cpp: (cpp).                  The GNU C preprocessor.
9 @end direntry
10 @end ifinfo
12 @c @smallbook
13 @c @cropmarks
14 @c @finalout
15 @setchapternewpage odd
16 @ifinfo
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.
26 @ignore
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).
32 @end ignore
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.
40 @end ifinfo
42 @titlepage
43 @c @finalout
44 @title The C Preprocessor
45 @subtitle Last revised July 2000
46 @subtitle for GCC version 2
47 @author Richard M. Stallman
48 @page
49 @vskip 2pc
50 This booklet is eventually intended to form the first chapter of a GNU 
51 C Language manual.
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.
70 @c man end
71 @end titlepage
72 @page
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:
93 @itemize @bullet
94 @item
95 Inclusion of header files.  These are files of declarations that can be
96 substituted into your program.
98 @item
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.
103 @item
104 Conditional compilation.  Using special preprocessing directives, you
105 can include or exclude parts of the program according to various
106 conditions.
108 @item
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
112 from.
113 @end itemize
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
117 Standard C@.
119 In its default mode, the GNU C preprocessor does not do a few things
120 required by the standard.  These are features which are rarely, if ever,
121 used, and may cause surprising changes to the meaning of a program which
122 does not expect them.  To get strict ISO Standard C, you should use the
123 @samp{-std=c89} or @samp{-std=c99} options, depending on which version
124 of the standard you want.  To get all the mandatory diagnostics, you
125 must also use @samp{-pedantic}.  @xref{Invocation}.
127 @c man end
129 @menu
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.
143 @end menu
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 four transformations that the
153 preprocessor always makes on all the input it receives, even in the
154 absence of directives.  These are, in order:
156 @enumerate
157 @item
158 Trigraphs, if enabled, are replaced with the character they represent.
160 @item
161 Backslash-newline sequences are deleted, no matter where.  This
162 feature allows you to break long lines for cosmetic purposes without
163 changing their meaning.
165 Recently, the non-traditional preprocessor has relaxed its treatment of
166 escaped newlines.  Previously, the newline had to immediately follow a
167 backslash.  The current implementation allows whitespace in the form of
168 spaces, horizontal and vertical tabs, and form feeds between the
169 backslash and the subsequent newline.  The preprocessor issues a
170 warning, but treats it as a valid escaped newline and combines the two
171 lines to form a single logical line.  This works within comments and
172 tokens, including multi-line strings, as well as between tokens.
173 Comments are @emph{not} treated as whitespace for the purposes of this
174 relaxation, since they have not yet been replaced with spaces.
176 @item
177 All comments are replaced with single spaces.
179 @item
180 Predefined macro names are replaced with their expansions
181 (@pxref{Predefined}).
182 @end enumerate
184 For end-of-line indicators, any of \n, \r\n, \n\r and \r are recognised,
185 and treated as ending a single line.  As a result, if you mix these in a
186 single file you might get incorrect line numbering, because the
187 preprocessor would interpret the two-character versions as ending just
188 one line.  Previous implementations would only handle UNIX-style \n
189 correctly, so DOS-style \r\n would need to be passed through a filter
190 first.
192 The first three transformations are done @emph{before} all other parsing
193 and before preprocessing directives are recognized.  Thus, for example,
194 you can split a line mechanically with backslash-newline anywhere
195 (except within trigraphs since they are replaced first; see below).
197 @example
199 */ # /*
200 */ defi\
201 ne FO\
202 O 10\
204 @end example
206 @noindent
207 is equivalent into @samp{#define FOO 1020}.
209 There is no way to prevent a backslash at the end of a line from being
210 interpreted as a backslash-newline.  For example,
212 @example
213 "foo\\
214 bar"
215 @end example
217 is equivalent to @code{"foo\bar"}, not to @code{"foo\\bar"}.  To avoid
218 having to worry about this, do not use the GNU extension which permits
219 multi-line strings.  Instead, use string constant concatenation:
221 @example
222    "foo\\"
223    "bar"
224 @end example
226 Your program will be more portable this way, too.
228 There are a few things to note about the above four transformations.
230 @itemize @bullet
231 @item
232 Comments and predefined macro names (or any macro names, for that
233 matter) are not recognized inside the argument of an @samp{#include}
234 directive, when it is delimited with quotes or with @samp{<} and
235 @samp{>}.
237 @item
238 Comments and predefined macro names are never recognized within a
239 character or string constant.
241 @item
242 ISO ``trigraphs'' are converted before backslash-newlines are deleted.
243 If you write what looks like a trigraph with a backslash-newline inside,
244 the backslash-newline is deleted as usual, but it is too late to
245 recognize the trigraph.
247 This is relevant only if you use the @samp{-trigraphs} option to enable
248 trigraph processing.  @xref{Invocation}.
249 @end itemize
251 The preprocessor handles null characters embedded in the input file
252 depending upon the context in which the null appears.  Note that here we
253 are referring not to the two-character escape sequence "\0", but to the
254 single character ASCII NUL.
256 There are three different contexts in which a null character may
257 appear:
259 @itemize @bullet
260 @item
261 Within comments.  Here, null characters are silently ignored.
263 @item
264 Within a string or character constant.  Here the preprocessor emits a
265 warning, but preserves the null character and passes it through to the
266 output file or compiler front-end.
268 @item
269 In any other context, the preprocessor issues a warning, and discards
270 the null character.  The preprocessor treats it like whitespace,
271 combining it with any surrounding whitespace to become a single
272 whitespace block.  Representing the null character by "^@@", this means
273 that code like
275 @example
276 #define X^@@1
277 @end example
279 is equivalent to
281 @example
282 #define X 1
283 @end example
285 and X is defined with replacement text "1".
286 @end itemize
288 @node Directives, Header Files, Global Actions, Top
289 @section Preprocessing Directives
291 @cindex preprocessing directives
292 @cindex directives
293 Most preprocessor features are active only if you use preprocessing
294 directives to request their use.
296 Preprocessing directives are lines in your program that start with
297 @samp{#}.  Whitespace is allowed before and after the @samp{#}.  The
298 @samp{#} is followed by an identifier that is the @dfn{directive name}.
299 For example, @samp{#define} is the directive that defines a macro.
301 Since the @samp{#} must be the first token on the line, it cannot come
302 from a macro expansion if you wish it to begin a directive.  Also, the
303 directive name is not macro expanded.  Thus, if @samp{foo} is defined as
304 a macro expanding to @samp{define}, that does not make @samp{#foo} a
305 valid preprocessing directive.
307 The set of valid directive names is fixed.  Programs cannot define new
308 preprocessing directives.
310 Some directive names require arguments; these make up the rest of the
311 directive line and must be separated from the directive name by
312 whitespace.  For example, @samp{#define} must be followed by a macro
313 name and the intended expansion of the macro.  @xref{Object-like
314 Macros}.
316 A preprocessing directive cannot cover more than one line.  It may be
317 logically extended with backslash-newline, but that has no effect on its
318 meaning.  Comments containing newlines can also divide the directive
319 into multiple lines, but a comment is replaced by a single space before
320 the directive is interpreted.
322 @node Header Files, Macros, Directives, Top
323 @section Header Files
325 @cindex header file
326 A header file is a file containing C declarations and macro definitions
327 (@pxref{Macros}) to be shared between several source files.  You request
328 the use of a header file in your program with the C preprocessing
329 directive @samp{#include}.
331 @menu
332 * Header Uses::         What header files are used for.
333 * Include Syntax::      How to write @samp{#include} directives.
334 * Include Operation::   What @samp{#include} does.
335 * Once-Only::           Preventing multiple inclusion of one header file.
336 * Inheritance::         Including one header file in another header file.
337 * System Headers::      Special treatment for some header files.
338 @end menu
340 @node Header Uses, Include Syntax, Header Files, Header Files
341 @subsection Uses of Header Files
343 Header files serve two kinds of purposes.
345 @itemize @bullet
346 @item
347 @cindex system header files
348 System header files declare the interfaces to parts of the operating
349 system.  You include them in your program to supply the definitions and
350 declarations you need to invoke system calls and libraries.
352 @item
353 Your own header files contain declarations for interfaces between the
354 source files of your program.  Each time you have a group of related
355 declarations and macro definitions all or most of which are needed in
356 several different source files, it is a good idea to create a header
357 file for them.
358 @end itemize
360 Including a header file produces the same results in C compilation as
361 copying the header file into each source file that needs it.  Such
362 copying would be time-consuming and error-prone.  With a header file,
363 the related declarations appear in only one place.  If they need to be
364 changed, they can be changed in one place, and programs that include the
365 header file will automatically use the new version when next recompiled.
366 The header file eliminates the labor of finding and changing all the
367 copies as well as the risk that a failure to find one copy will result
368 in inconsistencies within a program.
370 The usual convention is to give header files names that end with
371 @file{.h}.  Avoid unusual characters in header file names, as they
372 reduce portability.
374 @node Include Syntax, Include Operation, Header Uses, Header Files
375 @subsection The @samp{#include} Directive
377 @findex #include
378 Both user and system header files are included using the preprocessing
379 directive @samp{#include}.  It has three variants:
381 @table @code
382 @item #include <@var{file}>
383 This variant is used for system header files.  It searches for a file
384 named @var{file} in a list of directories specified by you, then in a
385 standard list of system directories.  You specify directories to search
386 for header files with the command option @samp{-I} (@pxref{Invocation}).
387 The option @samp{-nostdinc} inhibits searching the standard system
388 directories; in this case only the directories you specify are searched.
390 The first @samp{>} character terminates the file name.  The file name
391 may contain a @samp{<} character.
393 @item #include "@var{file}"
394 This variant is used for header files of your own program.  It searches
395 for a file named @var{file} first in the current directory, then in the
396 same directories used for system header files.  The current directory is
397 the directory of the current input file.  It is tried first because it
398 is presumed to be the location of the files that the current input file
399 refers to.  (If the @samp{-I-} option is used, the special treatment of
400 the current directory is inhibited. @xref{Invocation}.)
402 The first @samp{"} character terminates the file name.
404 In both these variants, the argument behaves like a string constant in
405 that comments are not recognized, and macro names are not expanded.
406 Thus, in @samp{#include <x/*y>} the @samp{/*} does not start a comment
407 and the directive specifies inclusion of a system header file named
408 @file{x/*y}.
410 However, in either variant, if backslashes occur within @var{file}, they
411 are considered ordinary text characters, not escape characters.  None of
412 the character escape sequences appropriate to string constants in C are
413 processed.  Thus, @samp{#include "x\n\\y"} specifies a filename
414 containing three backslashes.
416 @item #include @var{anything else}
417 @cindex computed @samp{#include}
418 This variant is called a @dfn{computed #include}.  Any @samp{#include}
419 directive whose argument does not fit the above two forms is a computed
420 include.  The text @var{anything else} is checked for macro calls, which
421 are expanded (@pxref{Macros}).  When this is done, the result must match
422 one of the above two variants --- in particular, the expansion must form
423 a string literal token, or a sequence of tokens surrounded by angle
424 braces. @xref{Unreliable Features}.
426 This feature allows you to define a macro which controls the file name
427 to be used at a later point in the program.  One application of this is
428 to allow a site-specific configuration file for your program to specify
429 the names of the system include files to be used.  This can help in
430 porting the program to various operating systems in which the necessary
431 system header files are found in different places.
432 @end table
434 @node Include Operation, Once-Only, Include Syntax, Header Files
435 @subsection How @samp{#include} Works
437 The @samp{#include} directive works by directing the C preprocessor to
438 scan the specified file as input before continuing with the rest of the
439 current file.  The output from the preprocessor contains the output
440 already generated, followed by the output resulting from the included
441 file, followed by the output that comes from the text after the
442 @samp{#include} directive.  For example, given a header file
443 @file{header.h} as follows,
445 @example
446 char *test ();
447 @end example
449 @noindent
450 and a main program called @file{program.c} that uses the header file,
451 like this,
453 @example
454 int x;
455 #include "header.h"
457 main ()
459   printf (test ());
461 @end example
463 @noindent
464 the output generated by the C preprocessor for @file{program.c} as input
465 would be
467 @example
468 int x;
469 char *test ();
471 main ()
473   printf (test ());
475 @end example
477 Included files are not limited to declarations and macro definitions;
478 those are merely the typical uses.  Any fragment of a C program can be
479 included from another file.  The include file could even contain the
480 beginning of a statement that is concluded in the containing file, or
481 the end of a statement that was started in the including file.  However,
482 a comment or a string or character constant may not start in the
483 included file and finish in the including file.  An unterminated
484 comment, string constant or character constant in an included file is
485 considered to end (with an error message) at the end of the file.
487 It is possible for a header file to begin or end a syntactic unit such
488 as a function definition, but that would be very confusing, so don't do
491 The line following the @samp{#include} directive is always treated as a
492 separate line by the C preprocessor, even if the included file lacks a
493 final newline.
495 @node Once-Only, Inheritance, Include Operation, Header Files
496 @subsection Once-Only Include Files
497 @cindex repeated inclusion
498 @cindex including just once
500 Very often, one header file includes another.  It can easily result that
501 a certain header file is included more than once.  This may lead to
502 errors, if the header file defines structure types or typedefs, and is
503 certainly wasteful.  Therefore, we often wish to prevent multiple
504 inclusion of a header file.
506 The standard way to do this is to enclose the entire real contents of the
507 file in a conditional, like this:
509 @example
510 #ifndef FILE_FOO_SEEN
511 #define FILE_FOO_SEEN
513 @var{the entire file}
515 #endif /* FILE_FOO_SEEN */
516 @end example
518 The macro @code{FILE_FOO_SEEN} indicates that the file has been included
519 once already.  In a user header file, the macro name should not begin
520 with @samp{_}.  In a system header file, this name should begin with
521 @samp{__} to avoid conflicts with user programs.  In any kind of header
522 file, the macro name should contain the name of the file and some
523 additional text, to avoid conflicts with other header files.
525 The GNU C preprocessor is programmed to notice when a header file uses
526 this particular construct and handle it efficiently.  If a header file
527 is contained entirely in a @samp{#ifndef} conditional, modulo whitespace
528 and comments, then it remembers that fact.  If a subsequent
529 @samp{#include} specifies the same file, and the macro in the
530 @samp{#ifndef} is already defined, then the directive is skipped without
531 processing the specified file at all.
533 @findex #import
534 In the Objective C language, there is a variant of @samp{#include}
535 called @samp{#import} which includes a file, but does so at most once.
536 If you use @samp{#import} @emph{instead of} @samp{#include}, then you
537 don't need the conditionals inside the header file to prevent multiple
538 execution of the contents.
540 @samp{#import} is obsolete because it is not a well designed feature.
541 It requires the users of a header file --- the applications programmers
542 --- to know that a certain header file should only be included once.  It
543 is much better for the header file's implementor to write the file so
544 that users don't need to know this.  Using @samp{#ifndef} accomplishes
545 this goal.
547 @node Inheritance, System Headers, Once-Only, Header Files
548 @subsection Inheritance and Header Files
549 @cindex inheritance
550 @cindex overriding a header file
552 @dfn{Inheritance} is what happens when one object or file derives some
553 of its contents by virtual copying from another object or file.  In
554 the case of C header files, inheritance means that one header file 
555 includes another header file and then replaces or adds something.
557 If the inheriting header file and the base header file have different
558 names, then inheritance is straightforward: simply write @samp{#include
559 "@var{base}"} in the inheriting file.
561 Sometimes it is necessary to give the inheriting file the same name as
562 the base file.  This is less straightforward.
564 For example, suppose an application program uses the system header
565 @file{sys/signal.h}, but the version of @file{/usr/include/sys/signal.h}
566 on a particular system doesn't do what the application program expects.
567 It might be convenient to define a ``local'' version, perhaps under the
568 name @file{/usr/local/include/sys/signal.h}, to override or add to the
569 one supplied by the system.
571 You can do this by compiling with the option @samp{-I.}, and writing a
572 file @file{sys/signal.h} that does what the application program expects.
573 Making this file include the standard @file{sys/signal.h} is not so easy
574 --- writing @samp{#include <sys/signal.h>} in that file doesn't work,
575 because it includes your own version of the file, not the standard
576 system version.  Used in that file itself, this leads to an infinite
577 recursion and a fatal error in compilation.
579 @samp{#include </usr/include/sys/signal.h>} would find the proper file,
580 but that is not clean, since it makes an assumption about where the
581 system header file is found.  This is bad for maintenance, since it
582 means that any change in where the system's header files are kept
583 requires a change somewhere else.
585 @findex #include_next
586 The clean way to solve this problem is to use 
587 @samp{#include_next}, which means, ``Include the @emph{next} file with
588 this name.''  This directive works like @samp{#include} except in
589 searching for the specified file: it starts searching the list of header
590 file directories @emph{after} the directory in which the current file
591 was found.
593 Suppose you specify @samp{-I /usr/local/include}, and the list of
594 directories to search also includes @file{/usr/include}; and suppose
595 both directories contain @file{sys/signal.h}.  Ordinary @samp{#include
596 <sys/signal.h>} finds the file under @file{/usr/local/include}.  If that
597 file contains @samp{#include_next <sys/signal.h>}, it starts searching
598 after that directory, and finds the file in @file{/usr/include}.
600 @samp{#include_next} is a GCC extension and should not be used in
601 programs intended to be portable to other compilers.
603 @node System Headers,, Inheritance, Header Files
604 @subsection System Headers
605 @cindex system header files
607 The header files declaring interfaces to the operating system and
608 runtime libraries often cannot be written in strictly conforming C.
609 Therefore, GNU C gives code found in @dfn{system headers} special
610 treatment.  Certain categories of warnings are suppressed, notably those
611 enabled by @samp{-pedantic}.
613 Normally, only the headers found in specific directories are considered
614 system headers.  The set of these directories is determined when GCC is
615 compiled.  There are, however, two ways to add to the set.
617 @findex -isystem
618 The @samp{-isystem} command line option adds its argument to the list of
619 directories to search for headers, just like @samp{-I}.  In addition,
620 any headers found in that directory will be considered system headers.
621 Note that unlike @samp{-I}, you must put a space between @samp{-isystem}
622 and its argument.
624 All directories named by @samp{-isystem} are searched @strong{after} all
625 directories named by @samp{-I}, no matter what their order was on the
626 command line.  If the same directory is named by both @samp{-I} and
627 @samp{-isystem}, @samp{-I} wins; it is as if the @samp{-isystem} option
628 had never been specified at all.
630 @findex #pragma GCC system_header
631 There is also a directive, @samp{#pragma GCC system_header}, which tells
632 GCC to consider the rest of the current include file a system header, no
633 matter where it was found.  Code that comes before the @samp{#pragma} in
634 the file will not be affected.
636 @samp{#pragma GCC system_header} has no effect in the primary source file.
638 @node Macros, Conditionals, Header Files, Top
639 @section Macros
641 A macro is a sort of abbreviation which you can define once and then
642 use later.  There are many complicated features associated with macros
643 in the C preprocessor.
645 @menu
646 * Object-like Macros::   Macros that always expand the same way.
647 * Function-like Macros:: Macros that accept arguments that are substituted
648                          into the macro expansion.
649 * Macro Varargs::        Macros with variable number of arguments.
650 * Predefined::           Predefined macros that are always available.
651 * Stringification::      Macro arguments converted into string constants.
652 * Concatenation::        Building tokens from parts taken from macro arguments.
653 * Undefining::           Cancelling a macro's definition.
654 * Redefining::           Changing a macro's definition.
655 * Poisoning::            Ensuring a macro is never defined or used.
656 * Macro Pitfalls::       Macros can confuse the unwary.  Here we explain
657                            several common problems and strange features.
658 @end menu
660 @node Object-like Macros, Function-like Macros, Macros, Macros
661 @subsection Object-like Macros
662 @cindex object-like macro
663 @cindex manifest constant
665 An @dfn{object-like macro} is a kind of abbreviation.  It is a name
666 which stands for a fragment of code.  Some people refer to these as
667 @dfn{manifest constants}.
669 Before you can use a macro, you must @dfn{define} it explicitly with the
670 @samp{#define} directive.  @samp{#define} is followed by the name of the
671 macro and then the token sequence it should be an abbreviation for,
672 which is variously referred to as the macro's @dfn{body},
673 @dfn{expansion} or @dfn{replacement list}.  For example,
675 @example
676 #define BUFFER_SIZE 1020
677 @end example
679 @noindent
680 defines a macro named @samp{BUFFER_SIZE} as an abbreviation for the
681 token @samp{1020}.  If somewhere after this @samp{#define} directive
682 there comes a C statement of the form
684 @example
685 foo = (char *) xmalloc (BUFFER_SIZE);
686 @end example
688 @noindent
689 then the C preprocessor will recognize and @dfn{expand} the macro
690 @samp{BUFFER_SIZE}, resulting in
692 @example
693 foo = (char *) xmalloc (1020);
694 @end example
696 The use of all upper case for macro names is a standard convention.
697 Programs are easier to read when it is possible to tell at a glance
698 which names are macros.
700 Normally, a macro definition can only span a single logical line, like
701 all C preprocessing directives.  Comments within a macro definition may
702 contain newlines, which make no difference since each comment is
703 replaced by a space regardless of its contents.
705 Apart from this, there is no restriction on what can go in a macro body
706 provided it decomposes into valid preprocessing tokens.  In particular,
707 parentheses need not balance, and the body need not resemble valid C
708 code.  (If it does not, you may get error messages from the C
709 compiler when you use the macro.)
711 The C preprocessor scans your program sequentially, so macro definitions
712 take effect at the place you write them.  Therefore, the following input
713 to the C preprocessor
715 @example
716 foo = X;
717 #define X 4
718 bar = X;
719 @end example
721 @noindent
722 produces as output
724 @example
725 foo = X;
727 bar = 4;
728 @end example
730 When the preprocessor expands a macro name, the macro's expansion
731 replaces the macro invocation, and the result is re-scanned for more
732 macros to expand.  For example, after
734 @example
735 #define BUFSIZE 1020
736 #define TABLESIZE BUFSIZE
737 @end example
739 @noindent
740 the name @samp{TABLESIZE} when used in the program would go through two
741 stages of expansion, resulting ultimately in @samp{1020}.
743 This is not the same as defining @samp{TABLESIZE} to be @samp{1020}.
744 The @samp{#define} for @samp{TABLESIZE} uses exactly the expansion you
745 specify --- in this case, @samp{BUFSIZE} --- and does not check to see
746 whether it too contains macro names.  Only when you @emph{use}
747 @samp{TABLESIZE} is the result of its expansion scanned for more macro
748 names.  @xref{Cascaded Macros}.
750 @node Function-like Macros, Macro Varargs, Object-like Macros, Macros
751 @subsection Macros with Arguments
752 @cindex macros with argument
753 @cindex arguments in macro definitions
754 @cindex function-like macro
756 An object-like macro is always replaced by exactly the same tokens each
757 time it is used.  Macros can be made more flexible by taking
758 @dfn{arguments}.  Arguments are fragments of code that you supply each
759 time the macro is used.  These fragments are included in the expansion
760 of the macro according to the directions in the macro definition.  A
761 macro that accepts arguments is called a @dfn{function-like macro}
762 because the syntax for using it looks like a function call.
764 @findex #define
765 To define a macro that uses arguments, you write a @samp{#define}
766 directive with a list of @dfn{parameters} in parentheses after the name
767 of the macro.  The parameters must be valid C identifiers, separated by
768 commas and optionally whitespace.  The @samp{(} must follow the macro
769 name immediately, with no space in between.  If you leave a space, you
770 instead define an object-like macro whose expansion begins with a
771 @samp{(}, and often leads to confusing errors at compile time.
773 As an example, here is a macro that computes the minimum of two numeric
774 values, as it is defined in many C programs:
776 @example
777 #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
778 @end example
780 @noindent
781 (This is not the best way to define a ``minimum'' macro in GNU C@.
782 @xref{Side Effects}, for more information.)
784 To invoke a function-like macro, you write the name of the macro
785 followed by a list of @dfn{arguments} in parentheses, separated by
786 commas.  The invocation of the macro need not be restricted to a single
787 logical line - it can cross as many lines in the source file as you
788 wish.  The number of arguments you give must match the number of
789 parameters in the macro definition; empty arguments are fine.  Examples
790 of use of the macro @samp{min} include @samp{min (1, 2)} and @samp{min
791 (x + 28, *p)}.
793 The expansion text of the macro depends on the arguments you use.  Each
794 macro parameter is replaced throughout the macro expansion with the
795 tokens of the corresponding argument.  Leading and trailing argument
796 whitespace is dropped, and all whitespace between the tokens of an
797 argument is reduced to a single space.  Using the same macro @samp{min}
798 defined above, @samp{min (1, 2)} expands into
800 @example
801 ((1) < (2) ? (1) : (2))
802 @end example
804 @noindent
805 where @samp{1} has been substituted for @samp{X} and @samp{2} for @samp{Y}.
807 Likewise, @samp{min (x + 28, *p)} expands into
809 @example
810 ((x + 28) < (*p) ? (x + 28) : (*p))
811 @end example
813 Parentheses within each argument must balance; a comma within such
814 parentheses does not end the argument.  However, there is no requirement
815 for square brackets or braces to balance, and they do not prevent a
816 comma from separating arguments.  Thus,
818 @example
819 macro (array[x = y, x + 1])
820 @end example
822 @noindent
823 passes two arguments to @code{macro}: @samp{array[x = y} and @samp{x +
824 1]}.  If you want to supply @samp{array[x = y, x + 1]} as an argument,
825 you must write it as @samp{array[(x = y, x + 1)]}, which is equivalent C
826 code.
828 After the arguments have been substituted into the macro body, the
829 resulting expansion replaces the macro invocation, and re-scanned for
830 more macro calls.  Therefore even arguments can contain calls to other
831 macros, either with or without arguments, and even to the same macro.
832 For example, @samp{min (min (a, b), c)} expands into this text:
834 @example
835 ((((a) < (b) ? (a) : (b))) < (c)
836  ? (((a) < (b) ? (a) : (b)))
837  : (c))
838 @end example
840 @noindent
841 (Line breaks shown here for clarity would not actually be generated.)
843 @cindex empty macro arguments
844 If a macro @code{foo} takes one argument, and you want to supply an
845 empty argument, simply supply no preprocessing tokens.  Since whitespace
846 does not form a preprocessing token, it is optional.  For example,
847 @samp{foo ()}, @samp{foo ( )} and @samp{bar (, arg2)}.
849 Previous GNU preprocessor implementations and documentation were
850 incorrect on this point, insisting that a function-like macro that takes
851 a single argument be passed a space if an empty argument was required.
853 If you use a macro name followed by something other than a @samp{(}
854 (after ignoring any whitespace that might follow), it does not form an
855 invocation of the macro, and the preprocessor does not change what you
856 have written.  Therefore, it is possible for the same identifier to be a
857 variable or function in your program as well as a macro, and you can
858 choose in each instance whether to refer to the macro (if an actual
859 argument list follows) or the variable or function (if an argument list
860 does not follow).  For example,
862 @example
863 #define foo(X) X
864 foo bar foo(baz)
865 @end example
867 expands to @samp{foo bar baz}.  Such dual use of one name could be
868 confusing and should be avoided except when the two meanings are
869 effectively synonymous: that is, when the name is both a macro and a
870 function and the two have similar effects.  You can think of the name
871 simply as a function; use of the name for purposes other than calling it
872 (such as, to take the address) will refer to the function, while calls
873 will expand the macro and generate better but equivalent code.
875 For example, you can use a function named @samp{min} in the same source
876 file that defines the macro.  If you write @samp{&min} with no argument
877 list, you refer to the function.  If you write @samp{min (x, bb)}, with
878 an argument list, the macro is expanded.  If you write @samp{(min) (a,
879 bb)}, where the name @samp{min} is not followed by an open-parenthesis,
880 the macro is not expanded, so you wind up with a call to the function
881 @samp{min}.
883 In the definition of a macro with arguments, the list of argument names
884 must follow the macro name immediately with no space in between.  If
885 there is a space after the macro name, the macro is defined as taking no
886 arguments, and all the rest of the line is taken to be the expansion.
887 The reason for this is that it is often useful to define a macro that
888 takes no arguments and whose definition begins with an identifier in
889 parentheses.  This rule makes it possible for you to do either this:
891 @example
892 #define FOO(x) - 1 / (x)
893 @end example
895 @noindent
896 (which defines @samp{FOO} to take an argument and expand into minus the
897 reciprocal of that argument) or this:
899 @example
900 #define BAR (x) - 1 / (x)
901 @end example
903 @noindent
904 (which defines @samp{BAR} to take no argument and always expand into
905 @samp{(x) - 1 / (x)}).
907 Note that the @emph{uses} of a macro with arguments can have spaces
908 before the left parenthesis; it's the @emph{definition} where it matters
909 whether there is a space.
911 @node Macro Varargs, Predefined, Function-like Macros, Macros
912 @subsection Macros with Variable Numbers of Arguments
913 @cindex variable number of arguments
914 @cindex macro with variable arguments
915 @cindex rest argument (in macro)
917 In the ISO C standard of 1999, a macro can be declared to accept a
918 variable number of arguments much as a function can.  The syntax for
919 defining the macro is similar to that of a function.  Here is an
920 example:
922 @example
923 #define eprintf(...) fprintf (stderr, __VA_ARGS__)
924 @end example
926 Here @samp{@dots{}} is a @dfn{variable argument}.  In the invocation of
927 such a macro, it represents the zero or more tokens until the closing
928 parenthesis that ends the invocation, including any commas.  This set of
929 tokens replaces the identifier @code{__VA_ARGS__} in the macro body
930 wherever it appears.  Thus, we have this expansion:
932 @example
933 eprintf ("%s:%d: ", input_file_name, line_number)
934 @expansion{}
935 fprintf (stderr, "%s:%d: " , input_file_name, line_number)
936 @end example
938 Within a @samp{#define} directive, ISO C mandates that the only place
939 the identifier @code{__VA_ARGS__} can appear is in the replacement list
940 of a variable-argument macro.  It may not be used as a macro name, macro
941 argument name, or within a different type of macro.  It may also be
942 forbidden in open text; the standard is ambiguous.  We recommend you
943 avoid using it except for its defined purpose.
945 If your macro is complicated, you may want a more descriptive name for
946 the variable argument than @code{__VA_ARGS__}.  GNU CPP permits this, as
947 an extension.  You may write an argument name immediately before the
948 @samp{@dots{}}; that name is used for the variable argument.  The
949 @code{eprintf} macro above could be written
951 @example
952 #define eprintf(args...) fprintf (stderr, args)
953 @end example
955 @noindent
956 using this extension.  You cannot use @code{__VA_ARGS__} and this
957 extension in the same macro.
959 We might instead have defined eprintf as follows:
961 @example
962 #define eprintf(format, ...) fprintf (stderr, format, __VA_ARGS__)
963 @end example
965 This formulation looks more descriptive, but cannot be used as flexibly.
966 There is no way to produce expanded output of
968 @example
969 fprintf (stderr, "success!\n")
970 @end example
972 @noindent
973 because, in standard C, you are not allowed to leave the variable
974 argument out entirely, and passing an empty argument for the variable
975 arguments will not do what you want.  Writing
977 @example
978 eprintf ("success!\n", )
979 @end example
981 @noindent
982 produces
984 @example
985 fprintf (stderr, "success!\n",)
986 @end example
988 @noindent
989 where the extra comma originates from the replacement list and not from
990 the arguments to eprintf.
992 There is another extension in the GNU C preprocessor which deals with
993 this difficulty.  First, you are allowed to leave the variable argument
994 out entirely:
996 @example
997 eprintf ("success!\n")
998 @end example
1000 Second, the @samp{##} token paste operator has a special meaning when
1001 placed between a comma and a variable argument.  If you write
1003 @example
1004 #define eprintf(format, ...) fprintf (stderr, format, ##__VA_ARGS__)
1005 @end example
1007 and the variable argument is left out when the @samp{eprintf} macro is
1008 used, then the comma before the @samp{##} will be deleted.  This does
1009 @emph{not} happen if you pass an empty argument, nor does it happen if
1010 the token preceding @samp{##} is anything other than a comma.
1012 Previous versions of the preprocessor implemented this extension much
1013 more generally.  We have restricted it in order to minimize the
1014 difference from the C standard.  @xref{Unreliable Features}.
1016 @node Predefined, Stringification, Macro Varargs, Macros
1017 @subsection Predefined Macros
1019 @cindex predefined macros
1020 Several object-like macros are predefined; you use them without
1021 supplying their definitions.  They fall into two classes: standard
1022 macros and system-specific macros.
1024 @menu
1025 * Standard Predefined::     Standard predefined macros.
1026 * Nonstandard Predefined::  Nonstandard predefined macros.
1027 @end menu
1029 @node Standard Predefined, Nonstandard Predefined, Predefined, Predefined
1030 @subsubsection Standard Predefined Macros
1031 @cindex standard predefined macros
1033 The standard predefined macros are available with the same meanings
1034 regardless of the machine or operating system on which you are using GNU
1035 C@.  Their names all start and end with double underscores.  Those
1036 preceding @code{__GNUC__} in this table are standardized by ISO C; the
1037 rest are GNU C extensions.
1039 @table @code
1040 @item __FILE__
1041 @findex __FILE__
1042 This macro expands to the name of the current input file, in the form of
1043 a C string constant.  The precise name returned is the one that was
1044 specified in @samp{#include} or as the input file name argument.  For
1045 example, @samp{"/usr/local/include/myheader.h"} is a possible expansion
1046 of this macro.
1048 @item __LINE__
1049 @findex __LINE__
1050 This macro expands to the current input line number, in the form of a
1051 decimal integer constant.  While we call it a predefined macro, it's
1052 a pretty strange macro, since its ``definition'' changes with each
1053 new line of source code.
1055 This and @samp{__FILE__} are useful in generating an error message to
1056 report an inconsistency detected by the program; the message can state
1057 the source line at which the inconsistency was detected.  For example,
1059 @smallexample
1060 fprintf (stderr, "Internal error: "
1061                  "negative string length "
1062                  "%d at %s, line %d.",
1063          length, __FILE__, __LINE__);
1064 @end smallexample
1066 A @samp{#include} directive changes the expansions of @samp{__FILE__}
1067 and @samp{__LINE__} to correspond to the included file.  At the end of
1068 that file, when processing resumes on the input file that contained
1069 the @samp{#include} directive, the expansions of @samp{__FILE__} and
1070 @samp{__LINE__} revert to the values they had before the
1071 @samp{#include} (but @samp{__LINE__} is then incremented by one as
1072 processing moves to the line after the @samp{#include}).
1074 The expansions of both @samp{__FILE__} and @samp{__LINE__} are altered
1075 if a @samp{#line} directive is used.  @xref{Line Control}.
1077 @item __DATE__
1078 @findex __DATE__
1079 This macro expands to a string constant that describes the date on
1080 which the preprocessor is being run.  The string constant contains
1081 eleven characters and looks like @w{@samp{"Feb  1 1996"}}.
1082 @c After reformatting the above, check that the date remains `Feb  1 1996',
1083 @c all on one line, with two spaces between the `Feb' and the `1'.
1085 @item __TIME__
1086 @findex __TIME__
1087 This macro expands to a string constant that describes the time at
1088 which the preprocessor is being run.  The string constant contains
1089 eight characters and looks like @samp{"23:59:01"}.
1091 @item __STDC__
1092 @findex __STDC__
1093 This macro expands to the constant 1, to signify that this is ISO
1094 Standard C@.  (Whether that is actually true depends on what C compiler
1095 will operate on the output from the preprocessor.)
1097 On some hosts, system include files use a different convention, where
1098 @samp{__STDC__} is normally 0, but is 1 if the user specifies strict
1099 conformance to the C Standard.  The preprocessor follows the host
1100 convention when processing system include files, but when processing
1101 user files it follows the usual GNU C convention.
1103 This macro is not defined if the @samp{-traditional} option is used.
1105 @item __STDC_VERSION__
1106 @findex __STDC_VERSION__
1107 This macro expands to the C Standard's version number, a long integer
1108 constant of the form @samp{@var{yyyy}@var{mm}L} where @var{yyyy} and
1109 @var{mm} are the year and month of the Standard version.  This signifies
1110 which version of the C Standard the preprocessor conforms to.  Like
1111 @samp{__STDC__}, whether this version number is accurate for the entire
1112 implementation depends on what C compiler will operate on the output
1113 from the preprocessor.
1115 This macro is not defined if the @samp{-traditional} option is used.
1117 @item __GNUC__
1118 @findex __GNUC__
1119 This macro is defined if and only if this is GNU C@.  This macro is
1120 defined only when the entire GNU C compiler is in use; if you invoke the
1121 preprocessor directly, @samp{__GNUC__} is undefined.  The value
1122 identifies the major version number of GNU CC (@samp{1} for GNU CC
1123 version 1, which is now obsolete, and @samp{2} for version 2).
1125 @item __GNUC_MINOR__
1126 @findex __GNUC_MINOR__
1127 The macro contains the minor version number of the compiler.  This can
1128 be used to work around differences between different releases of the
1129 compiler (for example, if GCC 2.6.3 is known to support a feature, you
1130 can test for @code{__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 6)}).
1132 @item __GNUC_PATCHLEVEL__
1133 @findex __GNUC_PATCHLEVEL__
1134 This macro contains the patch level of the compiler.  This can be
1135 used to work around differences between different patch level releases
1136 of the compiler (for example, if GCC 2.6.2 is known to contain a bug,
1137 whereas GCC 2.6.3 contains a fix, and you have code which can workaround
1138 the problem depending on whether the bug is fixed or not, you can test for
1139 @code{__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 6) || 
1140 (__GNUC__ == 2 && __GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 3)}).
1142 @item __GNUG__
1143 @findex __GNUG__
1144 The GNU C compiler defines this when the compilation language is
1145 C++; use @samp{__GNUG__} to distinguish between GNU C and GNU
1146 C++.
1148 @item __cplusplus 
1149 @findex __cplusplus 
1150 The ISO standard for C++ requires predefining this variable.  You can
1151 use @samp{__cplusplus} to test whether a header is compiled by a C
1152 compiler or a C++ compiler. The compiler currently uses a value of
1153 @samp{1}, instead of the value @samp{199711L}, which would indicate full
1154 conformance with the standard.
1156 @item __STRICT_ANSI__
1157 @findex __STRICT_ANSI__
1158 GNU C defines this macro if and only if the @samp{-ansi} switch was
1159 specified when GNU C was invoked.  Its definition is the null string.
1160 This macro exists primarily to direct certain GNU header files not to
1161 define certain traditional Unix constructs which are incompatible with
1162 ISO C@.
1164 @item __BASE_FILE__
1165 @findex __BASE_FILE__
1166 This macro expands to the name of the main input file, in the form
1167 of a C string constant.  This is the source file that was specified
1168 on the command line of the preprocessor or C compiler.
1170 @item __INCLUDE_LEVEL__
1171 @findex __INCLUDE_LEVEL_
1172 This macro expands to a decimal integer constant that represents the
1173 depth of nesting in include files.  The value of this macro is
1174 incremented on every @samp{#include} directive and decremented at the
1175 end of every included file.  It starts out at 0, it's value within the
1176 base file specified on the command line.
1178 @item __VERSION__
1179 @findex __VERSION__
1180 This macro expands to a string constant which describes the version
1181 number of GNU C@.  The string is normally a sequence of decimal numbers
1182 separated by periods, such as @samp{"2.6.0"}.
1184 @item __OPTIMIZE__
1185 @findex __OPTIMIZE__
1186 GNU CC defines this macro in optimizing compilations.  It causes certain
1187 GNU header files to define alternative macro definitions for some system
1188 library functions.  You should not refer to or test the definition of
1189 this macro unless you make very sure that programs will execute with the
1190 same effect regardless.
1192 @item __CHAR_UNSIGNED__
1193 @findex __CHAR_UNSIGNED__
1194 GNU C defines this macro if and only if the data type @code{char} is
1195 unsigned on the target machine.  It exists to cause the standard header
1196 file @file{limits.h} to work correctly.  You should not refer to this
1197 macro yourself; instead, refer to the standard macros defined in
1198 @file{limits.h}.  The preprocessor uses this macro to determine whether
1199 or not to sign-extend large character constants written in octal; see
1200 @ref{#if Directive,,The @samp{#if} Directive}.
1202 @item __REGISTER_PREFIX__
1203 @findex __REGISTER_PREFIX__
1204 This macro expands to a string (not a string constant) describing the
1205 prefix applied to CPU registers in assembler code.  You can use it to
1206 write assembler code that is usable in multiple environments.  For
1207 example, in the @samp{m68k-aout} environment it expands to the null
1208 string, but in the @samp{m68k-coff} environment it expands to the string
1209 @samp{%}.
1211 @item __USER_LABEL_PREFIX__
1212 @findex __USER_LABEL_PREFIX__
1213 Similar to @code{__REGISTER_PREFIX__}, but describes the prefix applied
1214 to user generated labels in assembler code.  For example, in the
1215 @samp{m68k-aout} environment it expands to the string @samp{_}, but in
1216 the @samp{m68k-coff} environment it expands to the null string.  This
1217 does not work with the @samp{-mno-underscores} option that the i386
1218 OSF/rose and m88k targets provide nor with the @samp{-mcall*} options of
1219 the rs6000 System V Release 4 target.
1220 @end table
1222 @node Nonstandard Predefined,, Standard Predefined, Predefined
1223 @subsubsection Nonstandard Predefined Macros
1225 The C preprocessor normally has several predefined macros that vary
1226 between machines because their purpose is to indicate what type of
1227 system and machine is in use.  This manual, being for all systems and
1228 machines, cannot tell you exactly what their names are; instead, we
1229 offer a list of some typical ones.  You can use @samp{cpp -dM} to see
1230 the values of predefined macros; see @ref{Invocation}.
1232 Some nonstandard predefined macros describe the operating system in use,
1233 with more or less specificity.  For example,
1235 @table @code
1236 @item unix
1237 @findex unix
1238 @samp{unix} is normally predefined on all Unix systems.
1240 @item BSD
1241 @findex BSD
1242 @samp{BSD} is predefined on recent versions of Berkeley Unix
1243 (perhaps only in version 4.3).
1244 @end table
1246 Other nonstandard predefined macros describe the kind of CPU, with more or
1247 less specificity.  For example,
1249 @table @code
1250 @item vax
1251 @findex vax
1252 @samp{vax} is predefined on Vax computers.
1254 @item mc68000
1255 @findex mc68000
1256 @samp{mc68000} is predefined on most computers whose CPU is a Motorola
1257 68000, 68010 or 68020.
1259 @item m68k
1260 @findex m68k
1261 @samp{m68k} is also predefined on most computers whose CPU is a 68000,
1262 68010 or 68020; however, some makers use @samp{mc68000} and some use
1263 @samp{m68k}.  Some predefine both names.  What happens in GNU C
1264 depends on the system you are using it on.
1266 @item M68020
1267 @findex M68020
1268 @samp{M68020} has been observed to be predefined on some systems that
1269 use 68020 CPUs --- in addition to @samp{mc68000} and @samp{m68k}, which
1270 are less specific.
1272 @item _AM29K
1273 @findex _AM29K
1274 @itemx _AM29000
1275 @findex _AM29000
1276 Both @samp{_AM29K} and @samp{_AM29000} are predefined for the AMD 29000
1277 CPU family.
1279 @item ns32000
1280 @findex ns32000
1281 @samp{ns32000} is predefined on computers which use the National
1282 Semiconductor 32000 series CPU.
1283 @end table
1285 Yet other nonstandard predefined macros describe the manufacturer of
1286 the system.  For example,
1288 @table @code
1289 @item sun
1290 @findex sun
1291 @samp{sun} is predefined on all models of Sun computers.
1293 @item pyr
1294 @findex pyr
1295 @samp{pyr} is predefined on all models of Pyramid computers.
1297 @item sequent
1298 @findex sequent
1299 @samp{sequent} is predefined on all models of Sequent computers.
1300 @end table
1302 These predefined symbols are not only nonstandard, they are contrary to the
1303 ISO standard because their names do not start with underscores.
1304 Therefore, the option @samp{-ansi} inhibits the definition of these
1305 symbols.
1307 This tends to make @samp{-ansi} useless, since many programs depend on
1308 the customary nonstandard predefined symbols.  Even system header files
1309 check them and will generate incorrect declarations if they do not find
1310 the names that are expected.  You might think that the header files
1311 supplied for the Uglix computer would not need to test what machine they
1312 are running on, because they can simply assume it is the Uglix; but
1313 often they do, and they do so using the customary names.  As a result,
1314 very few C programs will compile with @samp{-ansi}.  We intend to avoid
1315 such problems on the GNU system.
1317 What, then, should you do in an ISO C program to test the type of machine
1318 it will run on?
1320 GNU C offers a parallel series of symbols for this purpose, whose names
1321 are made from the customary ones by adding @samp{__} at the beginning
1322 and end.  Thus, the symbol @code{__vax__} would be available on a Vax,
1323 and so on.
1325 The set of nonstandard predefined names in the GNU C preprocessor is
1326 controlled (when @code{cpp} is itself compiled) by the macro
1327 @samp{CPP_PREDEFINES}, which should be a string containing @samp{-D}
1328 options, separated by spaces.  For example, on the Sun 3, we use the
1329 following definition:
1331 @example
1332 #define CPP_PREDEFINES "-Dmc68000 -Dsun -Dunix -Dm68k"
1333 @end example
1335 @noindent 
1336 This macro is usually specified in @file{tm.h}.
1338 @node Stringification, Concatenation, Predefined, Macros
1339 @subsection Stringification
1341 @cindex stringification
1342 @dfn{Stringification} means turning a sequence of preprocessing tokens
1343 into a string literal.  For example, stringifying @samp{foo (z)} results
1344 in @samp{"foo (z)"}.
1346 In the C preprocessor, stringification is possible when macro arguments
1347 are substituted during macro expansion.  When a parameter appears
1348 preceded by a @samp{#} token in the replacement list of a function-like
1349 macro, it indicates that both tokens should be replaced with the
1350 stringification of the corresponding argument during expansion.  The
1351 same argument may be substituted in other places in the definition
1352 without stringification if the argument name appears in those places
1353 with no preceding @samp{#}.
1355 Here is an example of a macro definition that uses stringification:
1357 @smallexample
1358 @group
1359 #define WARN_IF(EXP) \
1360 do @{ if (EXP) \
1361         fprintf (stderr, "Warning: " #EXP "\n"); @} \
1362 while (0)
1363 @end group
1364 @end smallexample
1366 @noindent
1367 Here the argument for @samp{EXP} is substituted once, as-is, into the
1368 @samp{if} statement, and once, stringified, into the argument to
1369 @samp{fprintf}.  The @samp{do} and @samp{while (0)} are a kludge to make
1370 it possible to write @samp{WARN_IF (@var{arg});}, which the resemblance
1371 of @samp{WARN_IF} to a function would make C programmers want to do; see
1372 @ref{Swallow Semicolon}.
1374 The stringification feature is limited to transforming the tokens of a
1375 macro argument into a string constant: there is no way to combine the
1376 argument with surrounding text and stringify it all together.  The
1377 example above shows how an equivalent result can be obtained in ISO
1378 Standard C, using the fact that adjacent string constants are
1379 concatenated by the C compiler to form a single string constant.  The
1380 preprocessor stringifies the actual value of @samp{EXP} into a separate
1381 string constant, resulting in text like
1383 @smallexample
1384 @group
1385 do @{ if (x == 0) \
1386         fprintf (stderr, "Warning: " "x == 0" "\n"); @} \
1387 while (0)
1388 @end group
1389 @end smallexample
1391 @noindent
1392 but the compiler then sees three consecutive string constants and
1393 concatenates them into one, producing effectively
1395 @smallexample
1396 do @{ if (x == 0) \
1397         fprintf (stderr, "Warning: x == 0\n"); @} \
1398 while (0)
1399 @end smallexample
1401 Stringification in C involves more than putting double-quote characters
1402 around the fragment.  The preprocessor backslash-escapes the surrounding
1403 quotes of string literals, and all backslashes within string and
1404 character constants, in order to get a valid C string constant with the
1405 proper contents.  Thus, stringifying @samp{p = "foo\n";} results in
1406 @samp{"p = \"foo\\n\";"}.  However, backslashes that are not inside
1407 string or character constants are not duplicated: @samp{\n} by itself
1408 stringifies to @samp{"\n"}.
1410 Whitespace (including comments) in the text being stringified is handled
1411 according to precise rules.  All leading and trailing whitespace is
1412 ignored.  Any sequence of whitespace in the middle of the text is
1413 converted to a single space in the stringified result.
1415 @node Concatenation, Undefining, Stringification, Macros
1416 @subsection Concatenation
1417 @cindex concatenation
1418 @cindex @samp{##}
1419 @dfn{Concatenation} means joining two strings into one.  In the context
1420 of macro expansion, concatenation refers to joining two preprocessing
1421 tokens to form one.  In particular, a token of a macro argument can be
1422 concatenated with another argument's token or with fixed text to produce
1423 a longer name.  The longer name might be the name of a function,
1424 variable, type, or a C keyword; it might even be the name of another
1425 macro, in which case it will be expanded.
1427 When you define a function-like or object-like macro, you request
1428 concatenation with the special operator @samp{##} in the macro's
1429 replacement list.  When the macro is called, any arguments are
1430 substituted without performing macro expansion, every @samp{##} operator
1431 is deleted, and the two tokens on either side of it are concatenated to
1432 form a single token.
1434 Consider a C program that interprets named commands.  There probably needs
1435 to be a table of commands, perhaps an array of structures declared as
1436 follows:
1438 @example
1439 struct command
1441   char *name;
1442   void (*function) ();
1445 struct command commands[] =
1447   @{ "quit", quit_command@},
1448   @{ "help", help_command@},
1449   @dots{}
1451 @end example
1453 It would be cleaner not to have to give each command name twice, once in
1454 the string constant and once in the function name.  A macro which takes the
1455 name of a command as an argument can make this unnecessary.  The string
1456 constant can be created with stringification, and the function name by
1457 concatenating the argument with @samp{_command}.  Here is how it is done:
1459 @example
1460 #define COMMAND(NAME)  @{ #NAME, NAME ## _command @}
1462 struct command commands[] =
1464   COMMAND (quit),
1465   COMMAND (help),
1466   @dots{}
1468 @end example
1470 The usual case of concatenation is concatenating two names (or a name
1471 and a number) into a longer name.  This isn't the only valid case.
1472 It is also possible to concatenate two numbers (or a number and a name,
1473 such as @samp{1.5} and @samp{e3}) into a number.  Also, multi-character
1474 operators such as @samp{+=} can be formed by concatenation.  However,
1475 two tokens that don't together form a valid token cannot be
1476 concatenated.  For example, concatenation of @samp{x} on one side and
1477 @samp{+} on the other is not meaningful because those two tokens do not
1478 form a valid preprocessing token when concatenated.  UNDEFINED
1480 Keep in mind that the C preprocessor converts comments to whitespace
1481 before macros are even considered.  Therefore, you cannot create a
1482 comment by concatenating @samp{/} and @samp{*}: the @samp{/*} sequence
1483 that starts a comment is not a token, but rather the beginning of a
1484 comment.  You can freely use comments next to @samp{##} in a macro
1485 definition, or in arguments that will be concatenated, because the
1486 comments will be converted to spaces at first sight, and concatenation
1487 operates on tokens and so ignores whitespace.
1489 @node Undefining, Redefining, Concatenation, Macros
1490 @subsection Undefining Macros
1492 @cindex undefining macros
1493 To @dfn{undefine} a macro means to cancel its definition.  This is done
1494 with the @samp{#undef} directive.  @samp{#undef} is followed by the macro
1495 name to be undefined.
1497 Like definition, undefinition occurs at a specific point in the source
1498 file, and it applies starting from that point.  The name ceases to be a
1499 macro name, and from that point on it is treated by the preprocessor as
1500 if it had never been a macro name.
1502 For example,
1504 @example
1505 #define FOO 4
1506 x = FOO;
1507 #undef FOO
1508 x = FOO;
1509 @end example
1511 @noindent
1512 expands into
1514 @example
1515 x = 4;
1517 x = FOO;
1518 @end example
1520 @noindent
1521 In this example, @samp{FOO} had better be a variable or function as well
1522 as (temporarily) a macro, in order for the result of the expansion to be
1523 valid C code.
1525 The same form of @samp{#undef} directive will cancel definitions with
1526 arguments or definitions that don't expect arguments.  The @samp{#undef}
1527 directive has no effect when used on a name not currently defined as a
1528 macro.
1530 @node Redefining, Poisoning, Undefining, Macros
1531 @subsection Redefining Macros
1533 @cindex redefining macros
1534 @dfn{Redefining} a macro means defining (with @samp{#define}) a name that
1535 is already defined as a macro.
1537 A redefinition is trivial if the new definition is transparently
1538 identical to the old one.  You probably wouldn't deliberately write a
1539 trivial redefinition, but they can happen automatically when a header
1540 file is included more than once (@pxref{Header Files}), so they are
1541 accepted silently and without effect.
1543 Nontrivial redefinition is considered likely to be an error, so it
1544 provokes a warning message from the preprocessor.  However, sometimes it
1545 is useful to change the definition of a macro in mid-compilation.  You
1546 can inhibit the warning by undefining the macro with @samp{#undef}
1547 before the second definition.
1549 In order for a redefinition to be trivial, the parameter names must
1550 match and be in the same order, and the new replacement list must
1551 exactly match the one already in effect, with two possible exceptions:
1553 @itemize @bullet
1554 @item
1555 Whitespace may be added or deleted at the beginning or the end of the
1556 replacement list.  In a sense this is vacuous, since strictly such
1557 whitespace doesn't form part of the macro's expansion.
1559 @item
1560 Between tokens in the expansion, any two forms of whitespace are
1561 considered equivalent.  In particular, whitespace may not be eliminated
1562 entirely, nor may it be added where there previously wasn't any.
1563 @end itemize
1565 Recall that a comment counts as whitespace.
1567 As a particular case of the above, you may not redefine an object-like
1568 macro as a function-like macro, and vice-versa.
1570 @node Poisoning, Macro Pitfalls, Redefining, Macros
1571 @subsection Poisoning Macros
1572 @cindex poisoning macros
1573 @findex #pragma GCC poison
1575 Sometimes, there is an identifier that you want to remove completely
1576 from your program, and make sure that it never creeps back in.  To
1577 enforce this, the @samp{#pragma GCC poison} directive can be used.
1578 @samp{#pragma GCC poison} is followed by a list of identifiers to
1579 poison, and takes effect for the rest of the source.  You cannot
1580 @samp{#undef} a poisoned identifier or test to see if it's defined with
1581 @samp{#ifdef}.
1583 For example,
1585 @example
1586 #pragma GCC poison printf sprintf fprintf
1587 sprintf(some_string, "hello");
1588 @end example
1590 @noindent
1591 will produce an error.
1593 @node Macro Pitfalls,, Poisoning, Macros
1594 @subsection Pitfalls and Subtleties of Macros
1595 @cindex problems with macros
1596 @cindex pitfalls of macros
1598 In this section we describe some special rules that apply to macros and
1599 macro expansion, and point out certain cases in which the rules have
1600 counterintuitive consequences that you must watch out for.
1602 @menu
1603 * Misnesting::        Macros can contain unmatched parentheses.
1604 * Macro Parentheses:: Why apparently superfluous parentheses
1605                          may be necessary to avoid incorrect grouping.
1606 * Swallow Semicolon:: Macros that look like functions
1607                          but expand into compound statements.
1608 * Side Effects::      Unsafe macros that cause trouble when
1609                          arguments contain side effects.
1610 * Self-Reference::    Macros whose definitions use the macros' own names.
1611 * Argument Prescan::  Arguments are checked for macro calls before they
1612                          are substituted.
1613 * Cascaded Macros::   Macros whose definitions use other macros.
1614 * Newlines in Args::  Sometimes line numbers get confused.
1615 @end menu
1617 @node Misnesting, Macro Parentheses, Macro Pitfalls, Macro Pitfalls
1618 @subsubsection Improperly Nested Constructs
1620 Recall that when a macro is called with arguments, the arguments are
1621 substituted into the macro body and the result is checked, together with
1622 the rest of the input file, for more macro calls.
1624 It is possible to piece together a macro call coming partially from the
1625 macro body and partially from the arguments.  For example,
1627 @example
1628 #define double(x) (2*(x))
1629 #define call_with_1(x) x(1)
1630 @end example
1632 @noindent
1633 would expand @samp{call_with_1 (double)} into @samp{(2*(1))}.
1635 Macro definitions do not have to have balanced parentheses.  By writing
1636 an unbalanced open parenthesis in a macro body, it is possible to create
1637 a macro call that begins inside the macro body but ends outside of it.
1638 For example,
1640 @example
1641 #define strange(file) fprintf (file, "%s %d",
1642 @dots{}
1643 strange(stderr) p, 35)
1644 @end example
1646 @noindent
1647 This bizarre example expands to @samp{fprintf (stderr, "%s %d", p, 35)}!
1649 @node Macro Parentheses, Swallow Semicolon, Misnesting, Macro Pitfalls
1650 @subsubsection Unintended Grouping of Arithmetic
1651 @cindex parentheses in macro bodies
1653 You may have noticed that in most of the macro definition examples shown
1654 above, each occurrence of a macro argument name had parentheses around
1655 it.  In addition, another pair of parentheses usually surround the
1656 entire macro definition.  Here is why it is best to write macros that
1657 way.
1659 Suppose you define a macro as follows,
1661 @example
1662 #define ceil_div(x, y) (x + y - 1) / y
1663 @end example
1665 @noindent
1666 whose purpose is to divide, rounding up.  (One use for this operation is
1667 to compute how many @samp{int} objects are needed to hold a certain
1668 number of @samp{char} objects.)  Then suppose it is used as follows:
1670 @example
1671 a = ceil_div (b & c, sizeof (int));
1672 @end example
1674 @noindent
1675 This expands into
1677 @example
1678 a = (b & c + sizeof (int) - 1) / sizeof (int);
1679 @end example
1681 @noindent
1682 which does not do what is intended.  The operator-precedence rules of
1683 C make it equivalent to this:
1685 @example
1686 a = (b & (c + sizeof (int) - 1)) / sizeof (int);
1687 @end example
1689 @noindent
1690 What we want is this:
1692 @example
1693 a = ((b & c) + sizeof (int) - 1)) / sizeof (int);
1694 @end example
1696 @noindent
1697 Defining the macro as
1699 @example
1700 #define ceil_div(x, y) ((x) + (y) - 1) / (y)
1701 @end example
1703 @noindent
1704 provides the desired result.
1706 Unintended grouping can result in another way.  Consider @samp{sizeof
1707 ceil_div(1, 2)}.  That has the appearance of a C expression that would
1708 compute the size of the type of @samp{ceil_div (1, 2)}, but in fact it
1709 means something very different.  Here is what it expands to:
1711 @example
1712 sizeof ((1) + (2) - 1) / (2)
1713 @end example
1715 @noindent
1716 This would take the size of an integer and divide it by two.  The
1717 precedence rules have put the division outside the @samp{sizeof} when it
1718 was intended to be inside.
1720 Parentheses around the entire macro definition can prevent such
1721 problems.  Here, then, is the recommended way to define @samp{ceil_div}:
1723 @example
1724 #define ceil_div(x, y) (((x) + (y) - 1) / (y))
1725 @end example
1727 @node Swallow Semicolon, Side Effects, Macro Parentheses, Macro Pitfalls
1728 @subsubsection Swallowing the Semicolon
1730 @cindex semicolons (after macro calls)
1731 Often it is desirable to define a macro that expands into a compound
1732 statement.  Consider, for example, the following macro, that advances a
1733 pointer (the argument @samp{p} says where to find it) across whitespace
1734 characters:
1736 @example
1737 #define SKIP_SPACES(p, limit)  \
1738 @{ register char *lim = (limit); \
1739   while (p != lim) @{            \
1740     if (*p++ != ' ') @{          \
1741       p--; break; @}@}@}
1742 @end example
1744 @noindent
1745 Here backslash-newline is used to split the macro definition, which must
1746 be a single logical line, so that it resembles the way such C code would
1747 be laid out if not part of a macro definition.
1749 A call to this macro might be @samp{SKIP_SPACES (p, lim)}.  Strictly
1750 speaking, the call expands to a compound statement, which is a complete
1751 statement with no need for a semicolon to end it.  However, since it
1752 looks like a function call, it minimizes confusion if you can use it
1753 like a function call, writing a semicolon afterward, as in
1754 @samp{SKIP_SPACES (p, lim);}
1756 This can cause trouble before @samp{else} statements, because the
1757 semicolon is actually a null statement.  Suppose you write
1759 @example
1760 if (*p != 0)
1761   SKIP_SPACES (p, lim);
1762 else @dots{}
1763 @end example
1765 @noindent
1766 The presence of two statements --- the compound statement and a null
1767 statement --- in between the @samp{if} condition and the @samp{else}
1768 makes invalid C code.
1770 The definition of the macro @samp{SKIP_SPACES} can be altered to solve
1771 this problem, using a @samp{do @dots{} while} statement.  Here is how:
1773 @example
1774 #define SKIP_SPACES(p, limit)     \
1775 do @{ register char *lim = (limit); \
1776      while (p != lim) @{            \
1777        if (*p++ != ' ') @{          \
1778          p--; break; @}@}@}           \
1779 while (0)
1780 @end example
1782 Now @samp{SKIP_SPACES (p, lim);} expands into
1784 @example
1785 do @{@dots{}@} while (0);
1786 @end example
1788 @noindent
1789 which is one statement.
1791 @node Side Effects, Self-Reference, Swallow Semicolon, Macro Pitfalls
1792 @subsubsection Duplication of Side Effects
1794 @cindex side effects (in macro arguments)
1795 @cindex unsafe macros
1796 Many C programs define a macro @samp{min}, for ``minimum'', like this:
1798 @example
1799 #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
1800 @end example
1802 When you use this macro with an argument containing a side effect,
1803 as shown here,
1805 @example
1806 next = min (x + y, foo (z));
1807 @end example
1809 @noindent
1810 it expands as follows:
1812 @example
1813 next = ((x + y) < (foo (z)) ? (x + y) : (foo (z)));
1814 @end example
1816 @noindent
1817 where @samp{x + y} has been substituted for @samp{X} and @samp{foo (z)}
1818 for @samp{Y}.
1820 The function @samp{foo} is used only once in the statement as it appears
1821 in the program, but the expression @samp{foo (z)} has been substituted
1822 twice into the macro expansion.  As a result, @samp{foo} might be called
1823 two times when the statement is executed.  If it has side effects or if
1824 it takes a long time to compute, the results might not be what you
1825 intended.  We say that @samp{min} is an @dfn{unsafe} macro.
1827 The best solution to this problem is to define @samp{min} in a way that
1828 computes the value of @samp{foo (z)} only once.  The C language offers
1829 no standard way to do this, but it can be done with GNU C extensions as
1830 follows:
1832 @example
1833 #define min(X, Y)                     \
1834 (@{ typeof (X) __x = (X), __y = (Y);   \
1835    (__x < __y) ? __x : __y; @})
1836 @end example
1838 If you do not wish to use GNU C extensions, the only solution is to be
1839 careful when @emph{using} the macro @samp{min}.  For example, you can
1840 calculate the value of @samp{foo (z)}, save it in a variable, and use
1841 that variable in @samp{min}:
1843 @example
1844 #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
1845 @dots{}
1847   int tem = foo (z);
1848   next = min (x + y, tem);
1850 @end example
1852 @noindent
1853 (where we assume that @samp{foo} returns type @samp{int}).
1855 @node Self-Reference, Argument Prescan, Side Effects, Macro Pitfalls
1856 @subsubsection Self-Referential Macros
1858 @cindex self-reference
1859 A @dfn{self-referential} macro is one whose name appears in its
1860 definition.  A special feature of ISO Standard C is that the
1861 self-reference is not considered a macro call.  It is passed into the
1862 preprocessor output unchanged.
1864 Let's consider an example:
1866 @example
1867 #define foo (4 + foo)
1868 @end example
1870 @noindent
1871 where @samp{foo} is also a variable in your program.
1873 Following the ordinary rules, each reference to @samp{foo} will expand
1874 into @samp{(4 + foo)}; then this will be rescanned and will expand into
1875 @samp{(4 + (4 + foo))}; and so on until it causes a fatal error (memory
1876 full) in the preprocessor.
1878 However, the special rule about self-reference cuts this process short
1879 after one step, at @samp{(4 + foo)}.  Therefore, this macro definition
1880 has the possibly useful effect of causing the program to add 4 to the
1881 value of @samp{foo} wherever @samp{foo} is referred to.
1883 In most cases, it is a bad idea to take advantage of this feature.  A
1884 person reading the program who sees that @samp{foo} is a variable will
1885 not expect that it is a macro as well.  The reader will come across the
1886 identifier @samp{foo} in the program and think its value should be that
1887 of the variable @samp{foo}, whereas in fact the value is four greater.
1889 The special rule for self-reference applies also to @dfn{indirect}
1890 self-reference.  This is the case where a macro @var{x} expands to use a
1891 macro @samp{y}, and the expansion of @samp{y} refers to the macro
1892 @samp{x}.  The resulting reference to @samp{x} comes indirectly from the
1893 expansion of @samp{x}, so it is a self-reference and is not further
1894 expanded.  Thus, after
1896 @example
1897 #define x (4 + y)
1898 #define y (2 * x)
1899 @end example
1901 @noindent
1902 @samp{x} would expand into @samp{(4 + (2 * x))}.  Clear?
1904 Suppose @samp{y} is used elsewhere, not from the definition of @samp{x}.
1905 Then the use of @samp{x} in the expansion of @samp{y} is not a
1906 self-reference because @samp{x} is not ``in progress''.  So it does
1907 expand.  However, the expansion of @samp{x} contains a reference to
1908 @samp{y}, and that is an indirect self-reference now because @samp{y} is
1909 ``in progress''.  The result is that @samp{y} expands to @samp{(2 * (4 +
1910 y))}.
1912 This behavior is specified by the ISO C standard, so you may need to
1913 understand it.
1915 @node Argument Prescan, Cascaded Macros, Self-Reference, Macro Pitfalls
1916 @subsubsection Separate Expansion of Macro Arguments
1917 @cindex expansion of arguments
1918 @cindex macro argument expansion
1919 @cindex prescan of macro arguments
1921 We have explained that the expansion of a macro, including the substituted
1922 arguments, is re-scanned for macro calls to be expanded.
1924 What really happens is more subtle: first each argument is scanned
1925 separately for macro calls.  Then the resulting tokens are substituted
1926 into the macro body to produce the macro expansion, and the macro
1927 expansion is scanned again for macros to expand.
1929 The result is that the arguments are scanned @emph{twice} to expand
1930 macro calls in them.
1932 Most of the time, this has no effect.  If the argument contained any
1933 macro calls, they are expanded during the first scan.  The result
1934 therefore contains no macro calls, so the second scan does not change
1935 it.  If the argument were substituted as given, with no prescan, the
1936 single remaining scan would find the same macro calls and produce the
1937 same results.
1939 You might expect the double scan to change the results when a
1940 self-referential macro is used in an argument of another macro
1941 (@pxref{Self-Reference}): the self-referential macro would be expanded
1942 once in the first scan, and a second time in the second scan.  However,
1943 this is not what happens.  The self-references that do not expand in the
1944 first scan are marked so that they will not expand in the second scan
1945 either.
1947 The prescan is not done when an argument is stringified or concatenated.
1948 Thus,
1950 @example
1951 #define str(s) #s
1952 #define foo 4
1953 str (foo)
1954 @end example
1956 @noindent
1957 expands to @samp{"foo"}.  Once more, prescan has been prevented from
1958 having any noticeable effect.
1960 More precisely, stringification and concatenation use the argument
1961 tokens as given without initially scanning for macros.  The same
1962 argument would be used in expanded form if it is substituted elsewhere
1963 without stringification or concatenation.
1965 @example
1966 #define str(s) #s lose(s)
1967 #define foo 4
1968 str (foo)
1969 @end example
1971 expands to @samp{"foo" lose(4)}.
1973 You might now ask, ``Why mention the prescan, if it makes no difference?
1974 And why not skip it and make the preprocessor faster?''  The answer is
1975 that the prescan does make a difference in three special cases:
1977 @itemize @bullet
1978 @item
1979 Nested calls to a macro.
1981 @item
1982 Macros that call other macros that stringify or concatenate.
1984 @item
1985 Macros whose expansions contain unshielded commas.
1986 @end itemize
1988 We say that @dfn{nested} calls to a macro occur when a macro's argument
1989 contains a call to that very macro.  For example, if @samp{f} is a macro
1990 that expects one argument, @samp{f (f (1))} is a nested pair of calls to
1991 @samp{f}.  The desired expansion is made by expanding @samp{f (1)} and
1992 substituting that into the definition of @samp{f}.  The prescan causes
1993 the expected result to happen.  Without the prescan, @samp{f (1)} itself
1994 would be substituted as an argument, and the inner use of @samp{f} would
1995 appear during the main scan as an indirect self-reference and would not
1996 be expanded.  Here, the prescan cancels an undesirable side effect (in
1997 the medical, not computational, sense of the term) of the special rule
1998 for self-referential macros.
2000 Prescan causes trouble in certain other cases of nested macro calls.
2001 Here is an example:
2003 @example
2004 #define foo  a,b
2005 #define bar(x) lose(x)
2006 #define lose(x) (1 + (x))
2008 bar(foo)
2009 @end example
2011 @noindent
2012 We would like @samp{bar(foo)} to turn into @samp{(1 + (foo))}, which
2013 would then turn into @samp{(1 + (a,b))}.  Instead, @samp{bar(foo)}
2014 expands into @samp{lose(a,b)}, and you get an error because @code{lose}
2015 requires a single argument.  In this case, the problem is easily solved
2016 by the same parentheses that ought to be used to prevent misnesting of
2017 arithmetic operations:
2019 @example
2020 #define foo (a,b)
2021 #define bar(x) lose((x))
2022 @end example
2024 The problem is more serious when the operands of the macro are not
2025 expressions; for example, when they are statements.  Then parentheses
2026 are unacceptable because they would make for invalid C code:
2028 @example
2029 #define foo @{ int a, b; @dots{} @}
2030 @end example
2032 @noindent
2033 In GNU C you can shield the commas using the @samp{(@{@dots{}@})}
2034 construct which turns a compound statement into an expression:
2036 @example
2037 #define foo (@{ int a, b; @dots{} @})
2038 @end example
2040 Or you can rewrite the macro definition to avoid such commas:
2042 @example
2043 #define foo @{ int a; int b; @dots{} @}
2044 @end example
2046 There is also one case where prescan is useful.  It is possible to use
2047 prescan to expand an argument and then stringify it --- if you use two
2048 levels of macros.  Let's add a new macro @samp{xstr} to the example
2049 shown above:
2051 @example
2052 #define xstr(s) str(s)
2053 #define str(s) #s
2054 #define foo 4
2055 xstr (foo)
2056 @end example
2058 This expands into @samp{"4"}, not @samp{"foo"}.  The reason for the
2059 difference is that the argument of @samp{xstr} is expanded at prescan
2060 (because @samp{xstr} does not specify stringification or concatenation
2061 of the argument).  The result of prescan then forms the argument for
2062 @samp{str}.  @samp{str} uses its argument without prescan because it
2063 performs stringification; but it cannot prevent or undo the prescanning
2064 already done by @samp{xstr}.
2066 @node Cascaded Macros, Newlines in Args, Argument Prescan, Macro Pitfalls
2067 @subsubsection Cascaded Use of Macros
2069 @cindex cascaded macros
2070 @cindex macro body uses macro
2071 A @dfn{cascade} of macros is when one macro's body contains a reference
2072 to another macro.  This is very common practice.  For example,
2074 @example
2075 #define BUFSIZE 1020
2076 #define TABLESIZE BUFSIZE
2077 @end example
2079 This is not at all the same as defining @samp{TABLESIZE} to be
2080 @samp{1020}.  The @samp{#define} for @samp{TABLESIZE} uses exactly the
2081 body you specify --- in this case, @samp{BUFSIZE} --- and does not check
2082 to see whether it too is the name of a macro.
2084 It's only when you @emph{use} @samp{TABLESIZE} that the result of its
2085 expansion is checked for more macro names.
2087 This makes a difference if you change the definition of @samp{BUFSIZE}
2088 at some point in the source file.  @samp{TABLESIZE}, defined as shown,
2089 will always expand using the definition of @samp{BUFSIZE} that is
2090 currently in effect:
2092 @example
2093 #define BUFSIZE 1020
2094 #define TABLESIZE BUFSIZE
2095 #undef BUFSIZE
2096 #define BUFSIZE 37
2097 @end example
2099 @noindent
2100 Now @samp{TABLESIZE} expands (in two stages) to @samp{37}.  (The
2101 @samp{#undef} is to prevent any warning about the nontrivial
2102 redefinition of @code{BUFSIZE}.)
2104 @node Newlines in Args,, Cascaded Macros, Macro Pitfalls
2105 @subsection Newlines in Macro Arguments
2106 @cindex newlines in macro arguments
2108 The invocation of a function-like macro can extend over many logical
2109 lines.  The ISO C standard requires that newlines within a macro
2110 invocation be treated as ordinary whitespace.  This means that when the
2111 expansion of a function-like macro replaces its invocation, it appears
2112 on the same line as the macro name did.  Thus line numbers emitted by
2113 the compiler or debugger refer to the line the invocation started on,
2114 which might be different to the line containing the argument causing the
2115 problem.
2117 Here is an example illustrating this:
2119 @example
2120 #define ignore_second_arg(a,b,c) a; c
2122 ignore_second_arg (foo (),
2123                    ignored (),
2124                    syntax error);
2125 @end example
2127 @noindent
2128 The syntax error triggered by the tokens @samp{syntax error} results in
2129 an error message citing line three --- the line of ignore_second_arg ---
2130 even though the problematic code comes from line five.
2132 @node Conditionals, Assertions, Macros, Top
2133 @section Conditionals
2135 @cindex conditionals
2136 In a macro processor, a @dfn{conditional} is a directive that allows a
2137 part of the program to be ignored during compilation, on some
2138 conditions.  In the C preprocessor, a conditional can test either an
2139 arithmetic expression or whether a name is defined as a macro.
2141 A conditional in the C preprocessor resembles in some ways an @samp{if}
2142 statement in C, but it is important to understand the difference between
2143 them.  The condition in an @samp{if} statement is tested during the
2144 execution of your program.  Its purpose is to allow your program to
2145 behave differently from run to run, depending on the data it is
2146 operating on.  The condition in a preprocessing conditional directive is
2147 tested when your program is compiled.  Its purpose is to allow different
2148 code to be included in the program depending on the situation at the
2149 time of compilation.
2151 @menu
2152 * Uses: Conditional Uses.       What conditionals are for.
2153 * Syntax: Conditional Syntax.   How conditionals are written.
2154 * Deletion: Deleted Code.       Making code into a comment.
2155 * Macros: Conditionals-Macros.  Why conditionals are used with macros.
2156 * Errors: #error Directive.     Detecting inconsistent compilation parameters.
2157 @end menu
2159 @node Conditional Uses
2160 @subsection Why Conditionals are Used
2162 Generally there are three kinds of reason to use a conditional.
2164 @itemize @bullet
2165 @item
2166 A program may need to use different code depending on the machine or
2167 operating system it is to run on.  In some cases the code for one
2168 operating system may be erroneous on another operating system; for
2169 example, it might refer to library routines that do not exist on the
2170 other system.  When this happens, it is not enough to avoid executing
2171 the invalid code: merely having it in the program makes it impossible to
2172 link the program and run it.  With a preprocessing conditional, the
2173 offending code can be effectively excised from the program when it is
2174 not valid.
2176 @item
2177 You may want to be able to compile the same source file into two
2178 different programs.  Sometimes the difference between the programs is
2179 that one makes frequent time-consuming consistency checks on its
2180 intermediate data, or prints the values of those data for debugging,
2181 while the other does not.
2183 @item
2184 A conditional whose condition is always false is a good way to exclude
2185 code from the program but keep it as a sort of comment for future
2186 reference.
2187 @end itemize
2189 Most simple programs that are intended to run on only one machine will
2190 not need to use preprocessing conditionals.
2192 @node Conditional Syntax
2193 @subsection Syntax of Conditionals
2195 @findex #if
2196 A conditional in the C preprocessor begins with a @dfn{conditional
2197 directive}: @samp{#if}, @samp{#ifdef} or @samp{#ifndef}.
2198 @xref{Conditionals-Macros}, for information on @samp{#ifdef} and
2199 @samp{#ifndef}; only @samp{#if} is explained here.
2201 @menu
2202 * If: #if Directive.     Basic conditionals using @samp{#if} and @samp{#endif}.
2203 * Else: #else Directive. Including some text if the condition fails.
2204 * Elif: #elif Directive. Testing several alternative possibilities.
2205 @end menu
2207 @node #if Directive
2208 @subsubsection The @samp{#if} Directive
2210 The @samp{#if} directive in its simplest form consists of
2212 @example
2213 #if @var{expression}
2214 @var{controlled text}
2215 #endif /* @var{expression} */
2216 @end example
2218 The comment following the @samp{#endif} is not required, but it is a
2219 good practice because it helps people match the @samp{#endif} to the
2220 corresponding @samp{#if}.  Such comments should always be used, except
2221 in short conditionals that are not nested.  In fact, you can put
2222 anything at all after the @samp{#endif} and it will be ignored by the
2223 GNU C preprocessor, but only comments are acceptable in ISO Standard C@.
2225 @var{expression} is a C expression of integer type, subject to stringent
2226 restrictions.  It may contain
2228 @itemize @bullet
2229 @item
2230 Integer constants, which are all regarded as @code{long} or
2231 @code{unsigned long}.
2233 @item
2234 Character constants, which are interpreted according to the character
2235 set and conventions of the machine and operating system on which the
2236 preprocessor is running.  The GNU C preprocessor uses the C data type
2237 @samp{char} for these character constants; therefore, whether some
2238 character codes are negative is determined by the C compiler used to
2239 compile the preprocessor.  If it treats @samp{char} as signed, then
2240 character codes large enough to set the sign bit will be considered
2241 negative; otherwise, no character code is considered negative.
2243 @item
2244 Arithmetic operators for addition, subtraction, multiplication,
2245 division, bitwise operations, shifts, comparisons, and logical
2246 operations (@samp{&&} and @samp{||}).  The latter two obey the usual
2247 short-circuiting rules of standard C.
2249 @item
2250 Identifiers that are not macros, which are all treated as zero(!).
2252 @item
2253 Macro calls.  All macro calls in the expression are expanded before
2254 actual computation of the expression's value begins.
2255 @end itemize
2257 Note that @samp{sizeof} operators and @code{enum}-type values are not
2258 allowed.  @code{enum}-type values, like all other identifiers that are
2259 not taken as macro calls and expanded, are treated as zero.
2261 The @var{controlled text} inside of a conditional can include
2262 preprocessing directives.  Then the directives inside the conditional
2263 are obeyed only if that branch of the conditional succeeds.  The text
2264 can also contain other conditional groups.  However, the @samp{#if} and
2265 @samp{#endif} directives must balance.
2267 @node #else Directive
2268 @subsubsection The @samp{#else} Directive
2270 @findex #else
2271 The @samp{#else} directive can be added to a conditional to provide
2272 alternative text to be used if the condition is false.  This is what
2273 it looks like:
2275 @example
2276 #if @var{expression}
2277 @var{text-if-true}
2278 #else /* Not @var{expression} */
2279 @var{text-if-false}
2280 #endif /* Not @var{expression} */
2281 @end example
2283 If @var{expression} is nonzero, and thus the @var{text-if-true} is 
2284 active, then @samp{#else} acts like a failing conditional and the
2285 @var{text-if-false} is ignored.  Conversely, if the @samp{#if}
2286 conditional fails, the @var{text-if-false} is considered included.
2288 @node #elif Directive
2289 @subsubsection The @samp{#elif} Directive
2291 @findex #elif
2292 One common case of nested conditionals is used to check for more than two
2293 possible alternatives.  For example, you might have
2295 @example
2296 #if X == 1
2297 @dots{}
2298 #else /* X != 1 */
2299 #if X == 2
2300 @dots{}
2301 #else /* X != 2 */
2302 @dots{}
2303 #endif /* X != 2 */
2304 #endif /* X != 1 */
2305 @end example
2307 Another conditional directive, @samp{#elif}, allows this to be
2308 abbreviated as follows:
2310 @example
2311 #if X == 1
2312 @dots{}
2313 #elif X == 2
2314 @dots{}
2315 #else /* X != 2 and X != 1*/
2316 @dots{}
2317 #endif /* X != 2 and X != 1*/
2318 @end example
2320 @samp{#elif} stands for ``else if''.  Like @samp{#else}, it goes in the
2321 middle of a @samp{#if}-@samp{#endif} pair and subdivides it; it does not
2322 require a matching @samp{#endif} of its own.  Like @samp{#if}, the
2323 @samp{#elif} directive includes an expression to be tested.
2325 The text following the @samp{#elif} is processed only if the original
2326 @samp{#if}-condition failed and the @samp{#elif} condition succeeds.
2327 More than one @samp{#elif} can go in the same @samp{#if}-@samp{#endif}
2328 group.  Then the text after each @samp{#elif} is processed only if the
2329 @samp{#elif} condition succeeds after the original @samp{#if} and any
2330 previous @samp{#elif} directives within it have failed.  @samp{#else} is
2331 equivalent to @samp{#elif 1}, and @samp{#else} is allowed after any
2332 number of @samp{#elif} directives, but @samp{#elif} may not follow
2333 @samp{#else}.
2335 @node Deleted Code
2336 @subsection Keeping Deleted Code for Future Reference
2337 @cindex commenting out code
2339 If you replace or delete a part of the program but want to keep the old
2340 code around as a comment for future reference, the easy way to do this
2341 is to put @samp{#if 0} before it and @samp{#endif} after it.  This is
2342 better than using comment delimiters @samp{/*} and @samp{*/} since those
2343 won't work if the code already contains comments (C comments do not
2344 nest).
2346 This works even if the code being turned off contains conditionals, but
2347 they must be entire conditionals (balanced @samp{#if} and @samp{#endif}).
2349 Conversely, do not use @samp{#if 0} for comments which are not C code.
2350 Use the comment delimiters @samp{/*} and @samp{*/} instead.  The
2351 interior of @samp{#if 0} must consist of complete tokens; in particular,
2352 single-quote characters must balance.  Comments often contain unbalanced
2353 single-quote characters (known in English as apostrophes).  These
2354 confuse @samp{#if 0}.  They do not confuse @samp{/*}.
2356 @node Conditionals-Macros
2357 @subsection Conditionals and Macros
2359 Conditionals are useful in connection with macros or assertions, because
2360 those are the only ways that an expression's value can vary from one
2361 compilation to another.  A @samp{#if} directive whose expression uses no
2362 macros or assertions is equivalent to @samp{#if 1} or @samp{#if 0}; you
2363 might as well determine which one, by computing the value of the
2364 expression yourself, and then simplify the program.
2366 For example, here is a conditional that tests the expression
2367 @samp{BUFSIZE == 1020}, where @samp{BUFSIZE} must be a macro.
2369 @example
2370 #if BUFSIZE == 1020
2371   printf ("Large buffers!\n");
2372 #endif /* BUFSIZE is large */
2373 @end example
2375 (Programmers often wish they could test the size of a variable or data
2376 type in @samp{#if}, but this does not work.  The preprocessor does not
2377 understand @code{sizeof}, or typedef names, or even the type keywords
2378 such as @code{int}.)
2380 @findex defined
2381 The special operator @samp{defined} is used in @samp{#if} expressions to
2382 test whether a certain name is defined as a macro.  Either @samp{defined
2383 @var{name}} or @samp{defined (@var{name})} is an expression whose value
2384 is 1 if @var{name} is defined as macro at the current point in the
2385 program, and 0 otherwise.  For the @samp{defined} operator it makes no
2386 difference what the definition of the macro is; all that matters is
2387 whether there is a definition.  Thus, for example,@refill
2389 @example
2390 #if defined (vax) || defined (ns16000)
2391 @end example
2393 @noindent
2394 would succeed if either of the names @samp{vax} and @samp{ns16000} is
2395 defined as a macro.  You can test the same condition using assertions
2396 (@pxref{Assertions}), like this:
2398 @example
2399 #if #cpu (vax) || #cpu (ns16000)
2400 @end example
2402 If a macro is defined and later undefined with @samp{#undef}, subsequent
2403 use of the @samp{defined} operator returns 0, because the name is no
2404 longer defined.  If the macro is defined again with another
2405 @samp{#define}, @samp{defined} will recommence returning 1.
2407 @findex #ifdef
2408 @findex #ifndef
2409 Conditionals that test whether a single macro is defined are very common,
2410 so there are two special short conditional directives for this case.
2412 @table @code
2413 @item #ifdef @var{name}
2414 is equivalent to @samp{#if defined (@var{name})}.
2416 @item #ifndef @var{name}
2417 is equivalent to @samp{#if ! defined (@var{name})}.
2418 @end table
2420 Macro definitions can vary between compilations for several reasons.
2422 @itemize @bullet
2423 @item
2424 Some macros are predefined on each kind of machine.  For example, on a
2425 Vax, the name @samp{vax} is a predefined macro.  On other machines, it
2426 would not be defined.
2428 @item
2429 Many more macros are defined by system header files.  Different systems
2430 and machines define different macros, or give them different values.  It
2431 is useful to test these macros with conditionals to avoid using a system
2432 feature on a machine where it is not implemented.
2434 @item
2435 Macros are a common way of allowing users to customize a program for
2436 different machines or applications.  For example, the macro
2437 @samp{BUFSIZE} might be defined in a configuration file for your program
2438 that is included as a header file in each source file.  You would use
2439 @samp{BUFSIZE} in a preprocessing conditional in order to generate
2440 different code depending on the chosen configuration.
2442 @item
2443 Macros can be defined or undefined with @samp{-D} and @samp{-U} command
2444 options when you compile the program.  You can arrange to compile the
2445 same source file into two different programs by choosing a macro name to
2446 specify which program you want, writing conditionals to test whether or
2447 how this macro is defined, and then controlling the state of the macro
2448 with compiler command options.  @xref{Invocation}.
2449 @end itemize
2451 @ifinfo
2452 Assertions are usually predefined, but can be defined with preprocessor
2453 directives or command-line options.
2454 @end ifinfo
2456 @node #error Directive
2457 @subsection The @samp{#error} and @samp{#warning} Directives
2459 @findex #error
2460 The directive @samp{#error} causes the preprocessor to report a fatal
2461 error.  The tokens forming the rest of the line following @samp{#error}
2462 are used as the error message, and not macro-expanded.  Internal
2463 whitespace sequences are each replaced with a single space.  The line
2464 must consist of complete tokens.
2466 You would use @samp{#error} inside of a conditional that detects a
2467 combination of parameters which you know the program does not properly
2468 support.  For example, if you know that the program will not run
2469 properly on a Vax, you might write
2471 @smallexample
2472 @group
2473 #ifdef __vax__
2474 #error "Won't work on Vaxen.  See comments at get_last_object."
2475 #endif
2476 @end group
2477 @end smallexample
2479 @noindent
2480 @xref{Nonstandard Predefined}, for why this works.
2482 If you have several configuration parameters that must be set up by
2483 the installation in a consistent way, you can use conditionals to detect
2484 an inconsistency and report it with @samp{#error}.  For example,
2486 @smallexample
2487 #if HASH_TABLE_SIZE % 2 == 0 || HASH_TABLE_SIZE % 3 == 0 \
2488     || HASH_TABLE_SIZE % 5 == 0
2489 #error HASH_TABLE_SIZE should not be divisible by a small prime
2490 #endif
2491 @end smallexample
2493 @findex #warning
2494 The directive @samp{#warning} is like the directive @samp{#error}, but
2495 causes the preprocessor to issue a warning and continue preprocessing.
2496 The tokens following @samp{#warning} are used as the warning message,
2497 and not macro-expanded.
2499 You might use @samp{#warning} in obsolete header files, with a message
2500 directing the user to the header file which should be used instead.
2502 @node Assertions, Line Control, Conditionals, Top
2503 @section Assertions
2504 @cindex assertions
2505 @dfn{Assertions} are a more systematic alternative to macros in writing
2506 conditionals to test what sort of computer or system the compiled
2507 program will run on.  Assertions are usually predefined, but you can
2508 define them with preprocessing directives or command-line options.
2510 @cindex predicates
2511 The macros traditionally used to describe the type of target are not
2512 classified in any way according to which question they answer; they may
2513 indicate a hardware architecture, a particular hardware model, an
2514 operating system, a particular version of an operating system, or
2515 specific configuration options.  These are jumbled together in a single
2516 namespace.  In contrast, each assertion consists of a named question and
2517 an answer.  The question is usually called the @dfn{predicate}.  An
2518 assertion looks like this:
2520 @example
2521 #@var{predicate} (@var{answer})
2522 @end example
2524 @noindent
2525 You must use a properly formed identifier for @var{predicate}.  The
2526 value of @var{answer} can be any sequence of words; all characters are
2527 significant except for leading and trailing whitespace, and differences
2528 in internal whitespace sequences are ignored.  (This is similar to the
2529 rules governing macro redefinition.)  Thus, @samp{x + y} is different
2530 from @samp{x+y} but equivalent to @samp{ x + y }.  @samp{)} is not
2531 allowed in an answer.
2533 @cindex testing predicates
2534 Here is a conditional to test whether the answer @var{answer} is asserted
2535 for the predicate @var{predicate}:
2537 @example
2538 #if #@var{predicate} (@var{answer})
2539 @end example
2541 @noindent
2542 There may be more than one answer asserted for a given predicate.  If
2543 you omit the answer, you can test whether @emph{any} answer is asserted
2544 for @var{predicate}:
2546 @example
2547 #if #@var{predicate}
2548 @end example
2550 @findex #system
2551 @findex #machine
2552 @findex #cpu
2553 Most of the time, the assertions you test will be predefined assertions.
2554 GNU C provides three predefined predicates: @code{system}, @code{cpu},
2555 and @code{machine}.  @code{system} is for assertions about the type of
2556 software, @code{cpu} describes the type of computer architecture, and
2557 @code{machine} gives more information about the computer.  For example,
2558 on a GNU system, the following assertions would be true:
2560 @example
2561 #system (gnu)
2562 #system (mach)
2563 #system (mach 3)
2564 #system (mach 3.@var{subversion})
2565 #system (hurd)
2566 #system (hurd @var{version})
2567 @end example
2569 @noindent
2570 and perhaps others.  The alternatives with
2571 more or less version information let you ask more or less detailed
2572 questions about the type of system software.
2574 On a Unix system, you would find @code{#system (unix)} and perhaps one of:
2575 @code{#system (aix)}, @code{#system (bsd)}, @code{#system (hpux)},
2576 @code{#system (lynx)}, @code{#system (mach)}, @code{#system (posix)},
2577 @code{#system (svr3)}, @code{#system (svr4)}, or @code{#system (xpg4)}
2578 with possible version numbers following.
2580 Other values for @code{system} are @code{#system (mvs)}
2581 and @code{#system (vms)}.
2583 @strong{Portability note:} Many Unix C compilers provide only one answer
2584 for the @code{system} assertion: @code{#system (unix)}, if they support
2585 assertions at all.  This is less than useful.
2587 An assertion with a multi-word answer is completely different from several
2588 assertions with individual single-word answers.  For example, the presence
2589 of @code{system (mach 3.0)} does not mean that @code{system (3.0)} is true.
2590 It also does not directly imply @code{system (mach)}, but in GNU C, that
2591 last will normally be asserted as well.
2593 The current list of possible assertion values for @code{cpu} is:
2594 @code{#cpu (a29k)}, @code{#cpu (alpha)}, @code{#cpu (arm)}, @code{#cpu
2595 (clipper)}, @code{#cpu (convex)}, @code{#cpu (elxsi)}, @code{#cpu
2596 (tron)}, @code{#cpu (h8300)}, @code{#cpu (i370)}, @code{#cpu (i386)},
2597 @code{#cpu (i860)}, @code{#cpu (i960)}, @code{#cpu (m68k)}, @code{#cpu
2598 (m88k)}, @code{#cpu (mips)}, @code{#cpu (ns32k)}, @code{#cpu (hppa)},
2599 @code{#cpu (pyr)}, @code{#cpu (ibm032)}, @code{#cpu (rs6000)},
2600 @code{#cpu (sh)}, @code{#cpu (sparc)}, @code{#cpu (spur)}, @code{#cpu
2601 (tahoe)}, @code{#cpu (vax)}, @code{#cpu (we32000)}.
2603 @findex #assert
2604 You can create assertions within a C program using @samp{#assert}, like
2605 this:
2607 @example
2608 #assert @var{predicate} (@var{answer})
2609 @end example
2611 @noindent
2612 (Note the absence of a @samp{#} before @var{predicate}.)
2614 @cindex unassert
2615 @cindex assertions, undoing
2616 @cindex retracting assertions
2617 @findex #unassert
2618 Each time you do this, you assert a new true answer for @var{predicate}.
2619 Asserting one answer does not invalidate previously asserted answers;
2620 they all remain true.  The only way to remove an answer is with
2621 @samp{#unassert}.  @samp{#unassert} has the same syntax as
2622 @samp{#assert}.  You can also remove all answers to a @var{predicate}
2623 like this:
2625 @example
2626 #unassert @var{predicate}
2627 @end example
2629 You can also add or cancel assertions using command options
2630 when you run @code{gcc} or @code{cpp}.  @xref{Invocation}.
2632 @node Line Control, Other Directives, Assertions, Top
2633 @section Combining Source Files
2635 @cindex line control
2636 One of the jobs of the C preprocessor is to inform the C compiler of where
2637 each line of C code came from: which source file and which line number.
2639 C code can come from multiple source files if you use @samp{#include};
2640 both @samp{#include} and the use of conditionals and macros can cause
2641 the line number of a line in the preprocessor output to be different
2642 from the line's number in the original source file.  You will appreciate
2643 the value of making both the C compiler (in error messages) and symbolic
2644 debuggers such as GDB use the line numbers in your source file.
2646 The C preprocessor builds on this feature by offering a directive by
2647 which you can control the feature explicitly.  This is useful when a
2648 file for input to the C preprocessor is the output from another program
2649 such as the @code{bison} parser generator, which operates on another
2650 file that is the true source file.  Parts of the output from
2651 @code{bison} are generated from scratch, other parts come from a
2652 standard parser file.  The rest are copied nearly verbatim from the
2653 source file, but their line numbers in the @code{bison} output are not
2654 the same as their original line numbers.  Naturally you would like
2655 compiler error messages and symbolic debuggers to know the original
2656 source file and line number of each line in the @code{bison} input.
2658 @findex #line
2659 @code{bison} arranges this by writing @samp{#line} directives into the output
2660 file.  @samp{#line} is a directive that specifies the original line number
2661 and source file name for subsequent input in the current preprocessor input
2662 file.  @samp{#line} has three variants:
2664 @table @code
2665 @item #line @var{linenum}
2666 Here @var{linenum} is a decimal integer constant.  This specifies that
2667 the line number of the following line of input, in its original source file,
2668 was @var{linenum}.
2670 @item #line @var{linenum} @var{filename}
2671 Here @var{linenum} is a decimal integer constant and @var{filename} is a
2672 string constant.  This specifies that the following line of input came
2673 originally from source file @var{filename} and its line number there was
2674 @var{linenum}.  Keep in mind that @var{filename} is not just a file
2675 name; it is surrounded by double-quote characters so that it looks like
2676 a string constant.
2678 @item #line @var{anything else}
2679 @var{anything else} is checked for macro calls, which are expanded.
2680 The result should be a decimal integer constant followed optionally
2681 by a string constant, as described above.
2682 @end table
2684 @samp{#line} directives alter the results of the @samp{__FILE__} and
2685 @samp{__LINE__} predefined macros from that point on.  @xref{Standard
2686 Predefined}.
2688 The output of the preprocessor (which is the input for the rest of the
2689 compiler) contains directives that look much like @samp{#line}
2690 directives.  They start with just @samp{#} instead of @samp{#line}, but
2691 this is followed by a line number and file name as in @samp{#line}.
2692 @xref{Output}.
2694 @node Other Directives, Output, Line Control, Top
2695 @section Miscellaneous Preprocessing Directives
2697 @cindex null directive
2698 This section describes three additional preprocessing directives.  They
2699 are not very useful, but are mentioned for completeness.
2701 The @dfn{null directive} consists of a @samp{#} followed by a newline,
2702 with only whitespace (including comments) in between.  A null directive
2703 is understood as a preprocessing directive but has no effect on the
2704 preprocessor output.  The primary significance of the existence of the
2705 null directive is that an input line consisting of just a @samp{#} will
2706 produce no output, rather than a line of output containing just a
2707 @samp{#}.  Supposedly some old C programs contain such lines.
2709 @findex #pragma
2710 @findex #pragma GCC
2712 The ISO standard specifies that the effect of the @samp{#pragma}
2713 directive is implementation-defined.  The GNU C preprocessor recognizes
2714 some pragmas, and passes unrecognized ones through to the preprocessor
2715 output, so they are available to the compilation pass.
2717 In line with the C99 standard, which introduces a STDC namespace for C99
2718 pragmas, the preprocessor introduces a GCC namespace for GCC pragmas.
2719 Supported GCC preprocessor pragmas are of the form @samp{#pragma GCC
2720 ...}.  For backwards compatibility previously supported pragmas are also
2721 recognized without the @samp{GCC} prefix, however that use is
2722 deprecated.  Pragmas that are already deprecated are not recognized with
2723 a @samp{GCC} prefix.
2725 @findex #ident
2726 The @samp{#ident} directive is supported for compatibility with certain
2727 other systems.  It is followed by a line of text.  On some systems, the
2728 text is copied into a special place in the object file; on most systems,
2729 the text is ignored and this directive has no effect.  Typically
2730 @samp{#ident} is only used in header files supplied with those systems
2731 where it is meaningful.
2733 @findex #pragma GCC dependency
2734 The @samp{#pragma GCC dependency} allows you to check the relative dates
2735 of the current file and another file. If the other file is more recent
2736 than the current file, a warning is issued. This is useful if the
2737 include file is derived from the other file, and should be regenerated.
2738 The other file is searched for using the normal include search path.
2739 Optional trailing text can be used to give more information in the
2740 warning message.
2742 @smallexample
2743 #pragma GCC dependency "parse.y"
2744 #pragma GCC dependency "/usr/include/time.h" rerun /path/to/fixincludes
2745 @end smallexample
2747 @node Output, Unreliable Features, Other Directives, Top
2748 @section C Preprocessor Output
2750 @cindex output format
2751 The output from the C preprocessor looks much like the input, except
2752 that all preprocessing directive lines have been replaced with blank
2753 lines and all comments with spaces.
2755 The ISO standard specifies that it is implementation defined whether a
2756 preprocessor preserves whitespace between tokens, or replaces it with
2757 e.g. a single space.  In the GNU C preprocessor, whitespace between
2758 tokens is collapsed to become a single space, with the exception that
2759 the first token on a non-directive line is preceded with sufficient
2760 spaces that it appears in the same column in the preprocessed output
2761 that it appeared in in the original source file.  This is so the output
2762 is easy to read.  @xref{Unreliable Features}.
2764 Source file name and line number information is conveyed by lines
2765 of the form
2767 @example
2768 # @var{linenum} @var{filename} @var{flags}
2769 @end example
2771 @noindent
2772 which are inserted as needed into the output (but never within a string
2773 or character constant), and in place of long sequences of empty lines.
2774 Such a line means that the following line originated in file
2775 @var{filename} at line @var{linenum}.
2777 After the file name comes zero or more flags, which are @samp{1},
2778 @samp{2}, @samp{3}, or @samp{4}.  If there are multiple flags, spaces
2779 separate them.  Here is what the flags mean:
2781 @table @samp
2782 @item 1
2783 This indicates the start of a new file.
2784 @item 2
2785 This indicates returning to a file (after having included another file).
2786 @item 3
2787 This indicates that the following text comes from a system header file,
2788 so certain warnings should be suppressed.
2789 @item 4
2790 This indicates that the following text should be treated as C@.
2791 @c maybe cross reference NO_IMPLICIT_EXTERN_C
2792 @end table
2794 @node Unreliable Features, Invocation, Output, Top
2795 @section Undefined Behavior and Deprecated Features
2796 @cindex undefined behavior
2797 @cindex deprecated features
2799 This section details GNU C preprocessor behavior that is subject to
2800 change or deprecated.  You are @emph{strongly advised} to write your
2801 software so it does not rely on anything described here; future versions
2802 of the preprocessor may subtly change such behavior or even remove the
2803 feature altogether.
2805 Preservation of the form of whitespace between tokens is unlikely to
2806 change from current behavior (@ref{Output}), but you are advised not
2807 to rely on it.
2809 The following are undocumented and subject to change:-
2811 @itemize @bullet
2813 @item Interpretation of the filename between @samp{<} and @samp{>} tokens
2814  resulting from a macro-expanded filename in a @samp{#include} directive
2816 The text between the @samp{<} and @samp{>} is taken literally if given
2817 directly within a @samp{#include} or similar directive.  If the
2818 angle-bracketed filename is obtained through macro expansion, however,
2819 preservation of whitespace and interpretation of backslashes and quotes
2820 is undefined. @xref{Include Syntax}.
2822 @item Precedence of ## operators with respect to each other
2824 Whether a sequence of ## operators is evaluated left-to-right,
2825 right-to-left or indeed in a consistent direction at all is not
2826 specified.  An example of where this might matter is pasting the
2827 arguments @samp{1}, @samp{e} and @samp{-2}.  This would be fine for
2828 left-to-right pasting, but right-to-left pasting would produce an
2829 invalid token @samp{e-2}.  It is possible to guarantee precedence by
2830 suitable use of nested macros.
2832 @item Precedence of # operator with respect to the ## operator
2834 Which of these two operators is evaluated first is not specified.
2836 @end itemize
2838 The following features are in flux and should not be used in portable
2839 code:
2841 @itemize @bullet
2843 @item Optional argument when invoking rest argument macros
2845 As an extension, GCC permits you to omit the variable arguments entirely
2846 when you use a variable argument macro.  This works whether or not you
2847 give the variable argument a name.  For example, the two macro
2848 invocations in the example below expand to the same thing:
2850 @smallexample
2851 #define debug(format, ...) printf (format, __VA_ARGS__)
2852 debug("string");       /* Not permitted by C standard.  */
2853 debug("string",);      /* OK.  */
2854 @end smallexample
2856 This extension will be preserved, but the special behavior of @samp{##}
2857 in this context has changed in the past and may change again in the
2858 future.
2860 @item ## swallowing preceding text in rest argument macros
2862 Formerly, in a macro expansion, if @samp{##} appeared before a variable
2863 arguments parameter, and the set of tokens specified for that argument in
2864 the macro invocation was empty, previous versions of the GNU C
2865 preprocessor would back up and remove the preceding sequence of
2866 nonwhitespace characters (@strong{not} the preceding token).  This
2867 extension is in direct conflict with the 1999 C standard and has been
2868 drastically pared back.
2870 In the current version of the preprocessor, if @samp{##} appears between
2871 a comma and a variable arguments parameter, and the variable argument is
2872 omitted entirely, the comma will be removed from the expansion.  If the
2873 variable argument is empty, or the token before @samp{##} is not a
2874 comma, then @samp{##} behaves as a normal token paste.  
2876 Portable code should avoid this extension at all costs.
2878 @end itemize
2880 The following features are deprecated and will likely be removed at some
2881 point in the future:-
2883 @itemize @bullet
2885 @item Attempting to paste two tokens which together do not form a valid
2886 preprocessing token
2888 The preprocessor currently warns about this and outputs the two tokens
2889 adjacently, which is probably the behavior the programmer intends.  It
2890 may not work in future, though.
2892 Most of the time, when you get this warning, you will find that @samp{##}
2893 is being used superstitiously, to guard against whitespace appearing
2894 between two tokens.  It is almost always safe to delete the @samp{##}.
2896 @findex #pragma once
2897 @item #pragma once
2899 This pragma was once used to tell the preprocessor that it need not
2900 include a file more than once.  It is now obsolete and should not be
2901 used at all.
2903 @item #pragma poison
2905 This pragma has been superceded by @samp{#pragma GCC poison}.
2906 @xref{Poisoning}.
2908 @item Multi-line string literals in directives
2910 The GNU C preprocessor currently allows newlines in string literals
2911 within a directive.  This is forbidden by the C standard and will
2912 eventually be removed.  (Multi-line string literals in open text are
2913 still supported.)
2915 @item Preprocessing things which are not C
2917 The C preprocessor is intended to be used only with C, C++, and
2918 Objective C source code.  In the past, it has been abused as a general
2919 text processor.  It will choke on input which is not lexically valid C;
2920 for example, apostrophes will be interpreted as the beginning of
2921 character constants, and cause errors.  Also, you cannot rely on it
2922 preserving characteristics of the input which are not significant to
2923 C-family languages.  For instance, if a Makefile is preprocessed, all
2924 the hard tabs will be lost, and the Makefile will not work.
2926 Having said that, you can often get away with using cpp on things which
2927 are not C.  Other Algol-ish programming languages are often safe
2928 (Pascal, Ada, ...)  and so is assembly, with caution. @samp{-traditional}
2929 mode is much more permissive, and can safely be used with e.g. Fortran.
2930 Many of the problems go away if you write C or C++ style comments
2931 instead of native language comments, and if you avoid elaborate macros.
2933 Wherever possible, you should use a preprocessor geared to the language
2934 you are writing in.  Modern versions of the GNU assembler have macro
2935 facilities.  Most high level programming languages have their own
2936 conditional compilation and inclusion mechanism.  If all else fails,
2937 try a true general text processor, such as @xref{Top, M4, , m4, GNU `m4'}.
2939 @end itemize
2941 @node Invocation, Concept Index, Unreliable Features, Top
2942 @section Invoking the C Preprocessor
2943 @cindex invocation of the preprocessor
2945 Most often when you use the C preprocessor you will not have to invoke it
2946 explicitly: the C compiler will do so automatically.  However, the
2947 preprocessor is sometimes useful on its own.
2949 @ignore
2950 @c man begin SYNOPSIS
2951 cpp [@samp{-P}] [@samp{-C}] [@samp{-gcc}] [@samp{-traditional}]
2952     [@samp{-undef}] [@samp{-trigraphs}] [@samp{-pedantic}]
2953     [@samp{-W}@var{warn}...] [@samp{-I}@var{dir}...]
2954     [@samp{-D}@var{macro}[=@var{defn}]...] [@samp{-U}@var{macro}]
2955     [@samp{-A}@var{predicate}(@var{answer})]
2956     [@samp{-M}|@samp{-MM}|@samp{-MD}|@samp{-MMD} [@samp{-MG}]]
2957     [@samp{-x} @var{language}] [@samp{-std=}@var{standard}]
2958     @var{infile} @var{outfile}
2960 Only the most useful options are listed here; see below for the remainder.
2961 @c man end
2962 @c man begin SEEALSO
2963 gcc(1), as(1), ld(1), and the Info entries for @file{cpp}, @file{gcc}, and
2964 @file{binutils}.
2965 @c man end
2966 @end ignore
2968 @c man begin OPTIONS
2969 The C preprocessor expects two file names as arguments, @var{infile} and
2970 @var{outfile}.  The preprocessor reads @var{infile} together with any
2971 other files it specifies with @samp{#include}.  All the output generated
2972 by the combined input files is written in @var{outfile}.
2974 Either @var{infile} or @var{outfile} may be @samp{-}, which as
2975 @var{infile} means to read from standard input and as @var{outfile}
2976 means to write to standard output.  Also, if either file is omitted, it
2977 means the same as if @samp{-} had been specified for that file.
2979 @cindex options
2980 Here is a table of command options accepted by the C preprocessor.
2981 These options can also be given when compiling a C program; they are
2982 passed along automatically to the preprocessor when it is invoked by the
2983 compiler.
2985 @table @samp
2986 @item -P
2987 @findex -P
2988 Inhibit generation of @samp{#}-lines with line-number information in the
2989 output from the preprocessor.  This might be useful when running the
2990 preprocessor on something that is not C code and will be sent to a
2991 program which might be confused by the @samp{#}-lines.  @xref{Output}.
2993 @item -C
2994 @findex -C
2995 Do not discard comments.  All comments are passed through to the output
2996 file, except for comments in processed directives, which are deleted
2997 along with the directive.  Comments appearing in the expansion list of a
2998 macro will be preserved, and appear in place wherever the macro is
2999 invoked.
3001 You should be prepared for side effects when using @samp{-C}; it causes
3002 the preprocessor to treat comments as tokens in their own right.  For
3003 example, macro redefinitions that were trivial when comments were
3004 replaced by a single space might become significant when comments are
3005 retained.  Also, comments appearing at the start of what would be a
3006 directive line have the effect of turning that line into an ordinary
3007 source line, since the first token on the line is no longer a @samp{#}.
3009 @item -traditional
3010 @findex -traditional
3011 Try to imitate the behavior of old-fashioned C, as opposed to ISO C@.
3013 @itemize @bullet
3014 @item
3015 Traditional macro expansion pays no attention to single-quote or
3016 double-quote characters; macro argument symbols are replaced by the
3017 argument values even when they appear within apparent string or
3018 character constants.
3020 @item
3021 Traditionally, it is permissible for a macro expansion to end in the
3022 middle of a string or character constant.  The constant continues into
3023 the text surrounding the macro call.
3025 @item
3026 However, traditionally the end of the line terminates a string or
3027 character constant, with no error.
3029 @item
3030 In traditional C, a comment is equivalent to no text at all.  (In ISO
3031 C, a comment counts as whitespace.)
3033 @item
3034 Traditional C does not have the concept of a ``preprocessing number''.
3035 It considers @samp{1.0e+4} to be three tokens: @samp{1.0e}, @samp{+},
3036 and @samp{4}.
3038 @item
3039 A macro is not suppressed within its own definition, in traditional C@.
3040 Thus, any macro that is used recursively inevitably causes an error.
3042 @item
3043 The character @samp{#} has no special meaning within a macro definition
3044 in traditional C@.
3046 @item
3047 In traditional C, the text at the end of a macro expansion can run
3048 together with the text after the macro call, to produce a single token.
3049 (This is impossible in ISO C@.)
3051 @item
3052 None of the GNU extensions to the preprocessor are available in
3053 @samp{-traditional} mode.
3055 @end itemize
3057 @cindex Fortran
3058 @cindex unterminated
3059 Use the @samp{-traditional} option when preprocessing Fortran code, so
3060 that single-quotes and double-quotes within Fortran comment lines (which
3061 are generally not recognized as such by the preprocessor) do not cause
3062 diagnostics about unterminated character or string constants.
3064 However, this option does not prevent diagnostics about unterminated
3065 comments when a C-style comment appears to start, but not end, within
3066 Fortran-style commentary.
3068 So, the following Fortran comment lines are accepted with
3069 @samp{-traditional}:
3071 @smallexample
3072 C This isn't an unterminated character constant
3073 C Neither is "20000000000, an octal constant
3074 C in some dialects of Fortran
3075 @end smallexample
3077 However, this type of comment line will likely produce a diagnostic, or
3078 at least unexpected output from the preprocessor, due to the
3079 unterminated comment:
3081 @smallexample
3082 C Some Fortran compilers accept /* as starting
3083 C an inline comment.
3084 @end smallexample
3086 @cindex g77
3087 Note that @code{g77} automatically supplies the @samp{-traditional}
3088 option when it invokes the preprocessor.  However, a future version of
3089 @code{g77} might use a different, more-Fortran-aware preprocessor in
3090 place of @code{cpp}.
3092 @item -trigraphs
3093 @findex -trigraphs
3094 Process ISO standard trigraph sequences.  These are three-character
3095 sequences, all starting with @samp{??}, that are defined by ISO C to
3096 stand for single characters.  For example, @samp{??/} stands for
3097 @samp{\}, so @samp{'??/n'} is a character constant for a newline.  By
3098 default, GCC ignores trigraphs, but in standard-conforming modes it
3099 converts them.  See the @samp{-std} option.
3101 The nine trigraph sequences are
3102 @table @samp
3103 @item ??(
3104 -> @samp{[}
3106 @item ??)
3107 -> @samp{]}
3109 @item ??<
3110 -> @samp{@{}
3112 @item ??>
3113 -> @samp{@}}
3115 @item ??=
3116 -> @samp{#}
3118 @item ??/
3119 -> @samp{\}
3121 @item ??'
3122 -> @samp{^}
3124 @item ??!
3125 -> @samp{|}
3127 @item ??-
3128 -> @samp{~}
3130 @end table
3132 Trigraph support is not popular, so many compilers do not implement it
3133 properly.  Portable code should not rely on trigraphs being either
3134 converted or ignored.
3136 @item -pedantic
3137 @findex -pedantic
3138 Issue warnings required by the ISO C standard in certain cases such
3139 as when text other than a comment follows @samp{#else} or @samp{#endif}.
3141 @item -pedantic-errors
3142 @findex -pedantic-errors
3143 Like @samp{-pedantic}, except that errors are produced rather than
3144 warnings.
3146 @item -Wcomment
3147 @findex -Wcomment
3148 @itemx -Wcomments
3149 (Both forms have the same effect).
3150 Warn whenever a comment-start sequence @samp{/*} appears in a @samp{/*}
3151 comment, or whenever a backslash-newline appears in a @samp{//} comment.
3153 @item -Wtrigraphs
3154 @findex -Wtrigraphs
3155 Warn if any trigraphs are encountered.  This option used to take effect
3156 only if @samp{-trigraphs} was also specified, but now works
3157 independently.  Warnings are not given for trigraphs within comments, as
3158 we feel this is obnoxious.
3160 @item -Wwhite-space
3161 @findex -Wwhite-space
3162 Warn about possible white space confusion, e.g. white space between a
3163 backslash and a newline.
3165 @item -Wall
3166 @findex -Wall
3167 Requests @samp{-Wcomment}, @samp{-Wtrigraphs}, and @samp{-Wwhite-space}
3168 (but not @samp{-Wtraditional} or @samp{-Wundef}).
3170 @item -Wtraditional
3171 @findex -Wtraditional
3172 Warn about certain constructs that behave differently in traditional and
3173 ISO C@.
3175 @item -Wundef
3176 @findex -Wundef
3177 Warn if an undefined identifier is evaluated in an @samp{#if} directive.
3179 @item -I @var{directory}
3180 @findex -I
3181 Add the directory @var{directory} to the head of the list of
3182 directories to be searched for header files (@pxref{Include Syntax}).
3183 This can be used to override a system header file, substituting your
3184 own version, since these directories are searched before the system
3185 header file directories.  If you use more than one @samp{-I} option,
3186 the directories are scanned in left-to-right order; the standard
3187 system directories come after.
3189 @item -I-
3190 Any directories specified with @samp{-I} options before the @samp{-I-}
3191 option are searched only for the case of @samp{#include "@var{file}"};
3192 they are not searched for @samp{#include <@var{file}>}.
3194 If additional directories are specified with @samp{-I} options after
3195 the @samp{-I-}, these directories are searched for all @samp{#include}
3196 directives.
3198 In addition, the @samp{-I-} option inhibits the use of the current
3199 directory as the first search directory for @samp{#include "@var{file}"}.
3200 Therefore, the current directory is searched only if it is requested
3201 explicitly with @samp{-I.}.  Specifying both @samp{-I-} and @samp{-I.}
3202 allows you to control precisely which directories are searched before
3203 the current one and which are searched after.
3205 @item -nostdinc
3206 @findex -nostdinc
3207 Do not search the standard system directories for header files.
3208 Only the directories you have specified with @samp{-I} options
3209 (and the current directory, if appropriate) are searched.
3211 @item -nostdinc++
3212 @findex -nostdinc++
3213 Do not search for header files in the C++-specific standard directories,
3214 but do still search the other standard directories.  (This option is
3215 used when building the C++ library.)
3217 @item -remap
3218 @findex -remap
3219 When searching for a header file in a directory, remap file names if a
3220 file named @file{header.gcc} exists in that directory.  This can be used
3221 to work around limitations of file systems with file name restrictions.
3222 The @file{header.gcc} file should contain a series of lines with two
3223 tokens on each line: the first token is the name to map, and the second
3224 token is the actual name to use.
3226 @item -D @var{name}
3227 @findex -D
3228 Predefine @var{name} as a macro, with definition @samp{1}.
3230 @item -D @var{name}=@var{definition}
3231 Predefine @var{name} as a macro, with definition @var{definition}.
3232 There are no restrictions on the contents of @var{definition}, but if
3233 you are invoking the preprocessor from a shell or shell-like program you
3234 may need to use the shell's quoting syntax to protect characters such as
3235 spaces that have a meaning in the shell syntax.  If you use more than
3236 one @samp{-D} for the same @var{name}, the rightmost definition takes
3237 effect.
3239 @item -U @var{name}
3240 @findex -U
3241 Do not predefine @var{name}.  If both @samp{-U} and @samp{-D} are
3242 specified for one name, whichever one appears later on the command line
3243 wins.
3245 @item -undef
3246 @findex -undef
3247 Do not predefine any nonstandard macros.
3249 @item -gcc
3250 @findex -gcc
3251 Define the macros @var{__GNUC__}, @var{__GNUC_MINOR__} and
3252 @var{__GNUC_PATCHLEVEL__}. These are defined automatically when you use
3253 @samp{gcc -E}; you can turn them off in that case with @samp{-no-gcc}.
3255 @item -A @var{predicate}(@var{answer})
3256 @findex -A
3257 Make an assertion with the predicate @var{predicate} and answer
3258 @var{answer}.  @xref{Assertions}.
3260 @item -A -@var{predicate}(@var{answer})
3261 Disable an assertion with the predicate @var{predicate} and answer
3262 @var{answer}.  Specifying no predicate, by @samp{-A-} or @samp{-A -},
3263 disables all predefined assertions and all assertions preceding it on
3264 the command line; and also undefines all predefined macros and all
3265 macros preceding it on the command line.
3267 @item -dM
3268 @findex -dM
3269 Instead of outputting the result of preprocessing, output a list of
3270 @samp{#define} directives for all the macros defined during the
3271 execution of the preprocessor, including predefined macros.  This gives
3272 you a way of finding out what is predefined in your version of the
3273 preprocessor; assuming you have no file @samp{foo.h}, the command
3275 @example
3276 touch foo.h; cpp -dM foo.h
3277 @end example
3279 @noindent 
3280 will show the values of any predefined macros.
3282 @item -dD
3283 @findex -dD
3284 Like @samp{-dM} except in two respects: it does @emph{not} include the
3285 predefined macros, and it outputs @emph{both} the @samp{#define}
3286 directives and the result of preprocessing.  Both kinds of output go to
3287 the standard output file.
3289 @item -dN
3290 @findex -dN
3291 Like @samp{-dD}, but emit only the macro names, not their expansions.
3293 @item -dI
3294 @findex -dI
3295 Output @samp{#include} directives in addition to the result of
3296 preprocessing.
3298 @item -M [-MG]
3299 @findex -M
3300 Instead of outputting the result of preprocessing, output a rule
3301 suitable for @code{make} describing the dependencies of the main source
3302 file.  The preprocessor outputs one @code{make} rule containing the
3303 object file name for that source file, a colon, and the names of all the
3304 included files.  If there are many included files then the rule is split
3305 into several lines using @samp{\}-newline.
3307 @samp{-MG} says to treat missing header files as generated files and
3308 assume they live in the same directory as the source file.  It must be
3309 specified in addition to @samp{-M}.
3311 This feature is used in automatic updating of makefiles.
3313 @item -MM [-MG]
3314 @findex -MM
3315 Like @samp{-M} but mention only the files included with @samp{#include
3316 "@var{file}"}.  System header files included with @samp{#include
3317 <@var{file}>} are omitted.
3319 @item -MD @var{file}
3320 @findex -MD
3321 Like @samp{-M} but the dependency information is written to @var{file}.
3322 This is in addition to compiling the file as specified --- @samp{-MD}
3323 does not inhibit ordinary compilation the way @samp{-M} does.
3325 When invoking @code{gcc}, do not specify the @var{file} argument.
3326 @code{gcc} will create file names made by replacing ".c" with ".d" at
3327 the end of the input file names.
3329 In Mach, you can use the utility @code{md} to merge multiple dependency
3330 files into a single dependency file suitable for using with the
3331 @samp{make} command.
3333 @item -MMD @var{file}
3334 @findex -MMD
3335 Like @samp{-MD} except mention only user header files, not system
3336 header files.
3338 @item -H
3339 @findex -H
3340 Print the name of each header file used, in addition to other normal
3341 activities.
3343 @item -imacros @var{file}
3344 @findex -imacros
3345 Process @var{file} as input, discarding the resulting output, before
3346 processing the regular input file.  Because the output generated from
3347 @var{file} is discarded, the only effect of @samp{-imacros @var{file}}
3348 is to make the macros defined in @var{file} available for use in the
3349 main input.
3351 @item -include @var{file}
3352 @findex -include
3353 Process @var{file} as input, and include all the resulting output,
3354 before processing the regular input file.  
3356 @item -idirafter @var{dir}
3357 @findex -idirafter
3358 @cindex second include path
3359 Add the directory @var{dir} to the second include path.  The directories
3360 on the second include path are searched when a header file is not found
3361 in any of the directories in the main include path (the one that
3362 @samp{-I} adds to).
3364 @item -iprefix @var{prefix}
3365 @findex -iprefix
3366 Specify @var{prefix} as the prefix for subsequent @samp{-iwithprefix}
3367 options.  If the prefix represents a directory, you should include the
3368 final @samp{/}.
3370 @item -iwithprefix @var{dir}
3371 @findex -iwithprefix
3372 Add a directory to the second include path.  The directory's name is
3373 made by concatenating @var{prefix} and @var{dir}, where @var{prefix} was
3374 specified previously with @samp{-iprefix}.
3376 @item -isystem @var{dir}
3377 @findex -isystem
3378 Add a directory to the beginning of the second include path, marking it
3379 as a system directory, so that it gets the same special treatment as
3380 is applied to the standard system directories.  @xref{System Headers}.
3382 @item -x c
3383 @itemx -x c++
3384 @itemx -x objective-c
3385 @itemx -x assembler-with-cpp
3386 @findex -x c
3387 @findex -x objective-c
3388 @findex -x assembler-with-cpp
3389 Specify the source language: C, C++, Objective-C, or assembly.  This has
3390 nothing to do with standards conformance or extensions; it merely
3391 selects which base syntax to expect.  If you give none of these options,
3392 cpp will deduce the language from the extension of the source file:
3393 @samp{.c}, @samp{.cc}, @samp{.m}, or @samp{.S}.  Some other common
3394 extensions for C++ and assembly are also recognized.  If cpp does not
3395 recognize the extension, it will treat the file as C; this is the most
3396 generic mode.
3398 @strong{Note:} Previous versions of cpp accepted a @samp{-lang} option
3399 which selected both the language and the standards conformance level.
3400 This option has been removed, because it conflicts with the @samp{-l}
3401 option.
3403 @item -std=@var{standard}
3404 @itemx -ansi
3405 @findex -std
3406 @findex -ansi
3407 Specify the standard to which the code should conform.  Currently cpp
3408 only knows about the standards for C; other language standards will be
3409 added in the future.
3411 @var{standard}
3412 may be one of:
3413 @table @code
3414 @item iso9899:1990
3415 @itemx c89
3416 The ISO C standard from 1990.  @samp{c89} is the customary shorthand for
3417 this version of the standard.
3419 The @samp{-ansi} option is equivalent to @samp{-std=c89}.
3421 @item iso9899:199409
3422 The 1990 C standard, as amended in 1994.
3424 @item iso9899:1999
3425 @itemx c99
3426 @itemx iso9899:199x
3427 @itemx c9x
3428 The revised ISO C standard, published in December 1999.  Before
3429 publication, this was known as C9X.
3431 @item gnu89
3432 The 1990 C standard plus GNU extensions.  This is the default.
3434 @item gnu99
3435 @itemx gnu9x
3436 The 1999 C standard plus GNU extensions.
3437 @end table
3439 @item -ftabstop=NUMBER
3440 @findex -ftabstop
3441 Set the distance between tabstops.  This helps the preprocessor
3442 report correct column numbers in warnings or errors, even if tabs appear
3443 on the line.  Values less than 1 or greater than 100 are ignored.  The
3444 default is 8.
3446 @item -$
3447 @findex -$
3448 Forbid the use of @samp{$} in identifiers.  The C standard allows
3449 implementations to define extra characters that can appear in
3450 identifiers.  By default the GNU C preprocessor permits @samp{$}, a
3451 common extension.
3452 @end table
3453 @c man end
3455 @node Concept Index, Index, Invocation, Top
3456 @unnumbered Concept Index
3457 @printindex cp
3459 @node Index,, Concept Index, Top
3460 @unnumbered Index of Directives, Macros and Options
3461 @printindex fn
3463 @contents
3464 @bye