c-tree.texi, [...]: Replace . at end of sentences preceded by a capital letter with @..
[platform/upstream/gcc.git] / gcc / doc / cpp.texi
1 \input texinfo
2 @setfilename cpp.info
3 @settitle The C Preprocessor
4 @setchapternewpage off
5 @c @smallbook
6 @c @cropmarks
7 @c @finalout
8
9 @macro copyrightnotice
10 @c man begin COPYRIGHT
11 Copyright @copyright{} 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996,
12 1997, 1998, 1999, 2000, 2001
13 Free Software Foundation, Inc.
14
15 Permission is granted to copy, distribute and/or modify this document
16 under the terms of the GNU Free Documentation License, Version 1.1 or
17 any later version published by the Free Software Foundation.  A copy of
18 the license is included in the accompanying manual for GCC, in the
19 section ``GNU Free Documentation License''.
20 @c man end
21 @end macro
22
23 @c The manpage doesn't have Front-Cover and Back-Cover Texts, but the
24 @c complete manual does.  -zw
25
26 @ignore
27 @c man begin COPYRIGHT
28 This manual contains no Invariant Sections, and has no Front-Cover Texts
29 or Back-Cover Texts.
30 @c man end
31 @end ignore
32
33 @macro covertexts
34 This manual contains no Invariant Sections.  The Front-Cover Texts are
35 (a) (see below), and the the Back-Cover Texts are (b) (see below).
36
37 (a) The FSF's Front-Cover Text is:
38
39      A GNU Manual
40
41 (b) The FSF's Back-Cover Text is:
42
43      You have freedom to copy and modify this GNU Manual, like GNU
44      software.  Copies published by the Free Software Foundation raise
45      funds for GNU development.
46 @end macro
47
48 @macro gcctabopt{body}
49 @code{\body\}
50 @end macro
51
52 @ifinfo
53 @dircategory Programming
54 @direntry
55 * Cpp: (cpp).                  The GNU C preprocessor.
56 @end direntry
57 @end ifinfo
58
59 @titlepage
60 @title The C Preprocessor
61 @subtitle Last revised April 2001
62 @subtitle for GCC version 3
63 @author Richard M. Stallman
64 @author Zachary Weinberg
65 @page
66 @c There is a fill at the bottom of the page, so we need a filll to
67 @c override it.
68 @vskip 0pt plus 1filll
69 @copyrightnotice{}
70 @covertexts{}
71 @end titlepage
72 @contents
73 @page
74
75 @ifinfo
76 @node Top
77 @top
78 The C preprocessor implements the macro language used to transform C,
79 C++, and Objective-C programs before they are compiled.  It can also be
80 useful on its own.
81
82 @menu
83 * Overview::
84 * Header Files::
85 * Macros::
86 * Conditionals::
87 * Diagnostics::
88 * Line Control::
89 * Pragmas::
90 * Other Directives::
91 * Preprocessor Output::
92 * Traditional Mode::
93 * Implementation Details::
94 * Invocation::
95 * Index of Directives::
96 * Concept Index::
97
98 @detailmenu
99  --- The Detailed Node Listing ---
100
101 Overview
102
103 * Initial processing::
104 * Tokenization::
105 * The preprocessing language::
106
107 Header Files
108
109 * Include Syntax::
110 * Include Operation::
111 * Search Path::
112 * Once-Only Headers::
113 * Computed Includes::
114 * Wrapper Headers::
115 * System Headers::
116
117 Macros
118
119 * Object-like Macros::
120 * Function-like Macros::
121 * Macro Arguments::
122 * Stringification::
123 * Concatenation::
124 * Variadic Macros::
125 * Predefined Macros::
126 * Undefining and Redefining Macros::
127 * Macro Pitfalls::
128
129 Predefined Macros
130
131 * Standard Predefined Macros::
132 * Common Predefined Macros::
133 * System-specific Predefined Macros::
134 * C++ Named Operators::
135
136 Macro Pitfalls
137
138 * Misnesting::
139 * Operator Precedence Problems::
140 * Swallowing the Semicolon::
141 * Duplication of Side Effects::
142 * Self-Referential Macros::
143 * Argument Prescan::
144 * Newlines in Arguments::
145
146 Conditionals
147
148 * Conditional Uses::
149 * Conditional Syntax::
150 * Deleted Code::
151
152 Conditional Syntax
153
154 * Ifdef::
155 * If::
156 * Defined::
157 * Else::
158 * Elif::
159
160 Implementation Details
161
162 * Implementation-defined behavior::
163 * Implementation limits::
164 * Obsolete Features::
165 * Differences from previous versions::
166
167 Obsolete Features
168
169 * Assertions::
170 * Obsolete once-only headers::
171 * Miscellaneous obsolete features::
172
173 @end detailmenu
174 @end menu
175
176 @copyrightnotice{}
177 @covertexts{}
178 @end ifinfo
179
180 @node Overview
181 @chapter Overview
182 @c man begin DESCRIPTION
183 The C preprocessor, often known as @dfn{cpp}, is a @dfn{macro processor}
184 that is used automatically by the C compiler to transform your program
185 before compilation.  It is called a macro processor because it allows
186 you to define @dfn{macros}, which are brief abbreviations for longer
187 constructs.
188
189 The C preprocessor is intended to be used only with C, C++, and
190 Objective-C source code.  In the past, it has been abused as a general
191 text processor.  It will choke on input which does not obey C's lexical
192 rules.  For example, apostrophes will be interpreted as the beginning of
193 character constants, and cause errors.  Also, you cannot rely on it
194 preserving characteristics of the input which are not significant to
195 C-family languages.  If a Makefile is preprocessed, all the hard tabs
196 will be removed, and the Makefile will not work.
197
198 Having said that, you can often get away with using cpp on things which
199 are not C@.  Other Algol-ish programming languages are often safe
200 (Pascal, Ada, etc.) So is assembly, with caution.  @option{-traditional}
201 mode preserves more white space, and is otherwise more permissive.  Many
202 of the problems can be avoided by writing C or C++ style comments
203 instead of native language comments, and keeping macros simple.
204
205 Wherever possible, you should use a preprocessor geared to the language
206 you are writing in.  Modern versions of the GNU assembler have macro
207 facilities.  Most high level programming languages have their own
208 conditional compilation and inclusion mechanism.  If all else fails,
209 try a true general text processor, such as GNU M4.
210
211 C preprocessors vary in some details.  This manual discusses the GNU C
212 preprocessor, which provides a small superset of the features of ISO
213 Standard C@.  In its default mode, the GNU C preprocessor does not do a
214 few things required by the standard.  These are features which are
215 rarely, if ever, used, and may cause surprising changes to the meaning
216 of a program which does not expect them.  To get strict ISO Standard C,
217 you should use the @option{-std=c89} or @option{-std=c99} options, depending
218 on which version of the standard you want.  To get all the mandatory
219 diagnostics, you must also use @option{-pedantic}.  @xref{Invocation}.
220 @c man end
221
222 @menu
223 * Initial processing::
224 * Tokenization::
225 * The preprocessing language::
226 @end menu
227
228 @node Initial processing
229 @section Initial processing
230
231 The preprocessor performs a series of textual transformations on its
232 input.  These happen before all other processing.  Conceptually, they
233 happen in a rigid order, and the entire file is run through each
234 transformation before the next one begins.  GNU CPP actually does them
235 all at once, for performance reasons.  These transformations correspond
236 roughly to the first three ``phases of translation'' described in the C
237 standard.
238
239 @enumerate
240 @item
241 @cindex character sets
242 @cindex line endings
243 The input file is read into memory and broken into lines.
244
245 GNU CPP expects its input to be a text file, that is, an unstructured
246 stream of ASCII characters, with some characters indicating the end of a
247 line of text.  Extended ASCII character sets, such as ISO Latin-1 or
248 Unicode encoded in UTF-8, are also acceptable.  Character sets that are
249 not strict supersets of seven-bit ASCII will not work.  We plan to add
250 complete support for international character sets in a future release.
251
252 Different systems use different conventions to indicate the end of a
253 line.  GCC accepts the ASCII control sequences @kbd{LF}, @kbd{@w{CR
254 LF}}, @kbd{CR}, and @kbd{@w{LF CR}} as end-of-line markers.  The first
255 three are the canonical sequences used by Unix, DOS and VMS, and the
256 classic Mac OS (before OSX) respectively.  You may therefore safely copy
257 source code written on any of those systems to a different one and use
258 it without conversion.  (GCC may lose track of the current line number
259 if a file doesn't consistently use one convention, as sometimes happens
260 when it is edited on computers with different conventions that share a
261 network file system.)  @kbd{@w{LF CR}} is included because it has been
262 reported as an end-of-line marker under exotic conditions.
263
264 If the last line of any input file lacks an end-of-line marker, the end
265 of the file is considered to implicitly supply one.  The C standard says
266 that this condition provokes undefined behavior, so GCC will emit a
267 warning message.
268
269 @item
270 @cindex trigraphs
271 If trigraphs are enabled, they are replaced by their corresponding
272 single characters.
273
274 These are nine three-character sequences, all starting with @samp{??},
275 that are defined by ISO C to stand for single characters.  They permit
276 obsolete systems that lack some of C's punctuation to use C@.  For
277 example, @samp{??/} stands for @samp{\}, so @t{'??/n'} is a character
278 constant for a newline.  By default, GCC ignores trigraphs, but if you
279 request a strictly conforming mode with the @option{-std} option, then
280 it converts them.
281
282 Trigraphs are not popular and many compilers implement them incorrectly.
283 Portable code should not rely on trigraphs being either converted or
284 ignored.  If you use the @option{-Wall} or @option{-Wtrigraphs} options,
285 GCC will warn you when a trigraph would change the meaning of your
286 program if it were converted.
287
288 In a string constant, you can prevent a sequence of question marks from
289 being confused with a trigraph by inserting a backslash between the
290 question marks.  @t{"(??\?)"} is the string @samp{(???)}, not
291 @samp{(?]}.  Traditional C compilers do not recognize this idiom.
292
293 The nine trigraphs and their replacements are
294
295 @example
296 Trigraph:       ??(  ??)  ??<  ??>  ??=  ??/  ??'  ??!  ??-
297 Replacement:      [    ]    @{    @}    #    \    ^    |    ~
298 @end example
299
300 @item
301 @cindex continued lines
302 @cindex backslash-newline
303 Continued lines are merged into one long line.
304
305 A continued line is a line which ends with a backslash, @samp{\}.  The
306 backslash is removed and the following line is joined with the current
307 one.  No space is inserted, so you may split a line anywhere, even in
308 the middle of a word.  (It is generally more readable to split lines
309 only at white space.)
310
311 The trailing backslash on a continued line is commonly referred to as a
312 @dfn{backslash-newline}.
313
314 If there is white space between a backslash and the end of a line, that
315 is still a continued line.  However, as this is usually the result of an
316 editing mistake, and many compilers will not accept it as a continued
317 line, GCC will warn you about it.
318
319 @item
320 @cindex comments
321 @cindex line comments
322 @cindex block comments
323 All comments are replaced with single spaces.
324
325 There are two kinds of comments.  @dfn{Block comments} begin with
326 @samp{/*} and continue until the next @samp{*/}.  Block comments do not
327 nest:
328
329 @example
330 /* @r{this is} /* @r{one comment} */ @r{text outside comment}
331 @end example
332
333 @dfn{Line comments} begin with @samp{//} and continue to the end of the
334 current line.  Line comments do not nest either, but it does not matter,
335 because they would end in the same place anyway.
336
337 @example
338 // @r{this is} // @r{one comment}
339 @r{text outside comment}
340 @end example
341 @end enumerate
342
343 It is safe to put line comments inside block comments, or vice versa.
344
345 @example
346 @group
347 /* @r{block comment}
348    // @r{contains line comment}
349    @r{yet more comment}
350  */ @r{outside comment}
351
352 // @r{line comment} /* @r{contains block comment} */
353 @end group
354 @end example
355
356 But beware of commenting out one end of a block comment with a line
357 comment.
358
359 @example
360 @group
361  // @r{l.c.}  /* @r{block comment begins}
362     @r{oops! this isn't a comment anymore} */
363 @end group
364 @end example
365
366 Comments are not recognized within string literals.  @t{@w{"/* blah
367 */"}} is the string constant @samp{@w{/* blah */}}, not an empty string.
368
369 Line comments are not in the 1989 edition of the C standard, but they
370 are recognized by GCC as an extension.  In C++ and in the 1999 edition
371 of the C standard, they are an official part of the language.
372
373 Since these transformations happen before all other processing, you can
374 split a line mechanically with backslash-newline anywhere.  You can
375 comment out the end of a line.  You can continue a line comment onto the
376 next line with backslash-newline.  You can even split @samp{/*},
377 @samp{*/}, and @samp{//} onto multiple lines with backslash-newline.
378 For example:
379
380 @example
381 @group
382 /\
383 *
384 */ # /*
385 */ defi\
386 ne FO\
387 O 10\
388 20
389 @end group
390 @end example
391
392 @noindent
393 is equivalent to @code{@w{#define FOO 1020}}.  All these tricks are
394 extremely confusing and should not be used in code intended to be
395 readable.
396
397 There is no way to prevent a backslash at the end of a line from being
398 interpreted as a backslash-newline.
399
400 @example
401 "foo\\
402 bar"
403 @end example
404
405 @noindent
406 is equivalent to @code{"foo\bar"}, not to @code{"foo\\bar"}.  To avoid
407 having to worry about this, do not use the deprecated GNU extension
408 which permits multi-line strings.  Instead, use string literal
409 concatenation:
410
411 @example
412    "foo\\"
413    "bar"
414 @end example
415
416 @noindent
417 Your program will be more portable this way, too.
418
419 @node Tokenization
420 @section Tokenization
421
422 @cindex tokens
423 @cindex preprocessing tokens
424 After the textual transformations are finished, the input file is
425 converted into a sequence of @dfn{preprocessing tokens}.  These mostly
426 correspond to the syntactic tokens used by the C compiler, but there are
427 a few differences.  White space separates tokens; it is not itself a
428 token of any kind.  Tokens do not have to be separated by white space,
429 but it is often necessary to avoid ambiguities.
430
431 When faced with a sequence of characters that has more than one possible
432 tokenization, the preprocessor is greedy.  It always makes each token,
433 starting from the left, as big as possible before moving on to the next
434 token.  For instance, @code{a+++++b} is interpreted as
435 @code{@w{a ++ ++ + b}}, not as @code{@w{a ++ + ++ b}}, even though the
436 latter tokenization could be part of a valid C program and the former
437 could not.
438
439 Once the input file is broken into tokens, the token boundaries never
440 change, except when the @samp{##} preprocessing operator is used to paste
441 tokens together.  @xref{Concatenation}.  For example,
442
443 @example
444 @group
445 #define foo() bar
446 foo()baz
447      @expansion{} bar baz
448 @emph{not}
449      @expansion{} barbaz
450 @end group
451 @end example
452
453 The compiler does not re-tokenize the preprocessor's output.  Each
454 preprocessing token becomes one compiler token.
455
456 @cindex identifiers
457 Preprocessing tokens fall into five broad classes: identifiers,
458 preprocessing numbers, string literals, punctuators, and other.  An
459 @dfn{identifier} is the same as an identifier in C: any sequence of
460 letters, digits, or underscores, which begins with a letter or
461 underscore.  Keywords of C have no significance to the preprocessor;
462 they are ordinary identifiers.  You can define a macro whose name is a
463 keyword, for instance.  The only identifier which can be considered a
464 preprocessing keyword is @code{defined}.  @xref{Defined}.
465
466 This is mostly true of other languages which use the C preprocessor.
467 However, a few of the keywords of C++ are significant even in the
468 preprocessor.  @xref{C++ Named Operators}.
469
470 In the 1999 C standard, identifiers may contain letters which are not
471 part of the ``basic source character set,'' at the implementation's
472 discretion (such as accented Latin letters, Greek letters, or Chinese
473 ideograms).  This may be done with an extended character set, or the
474 @samp{\u} and @samp{\U} escape sequences.  GCC does not presently
475 implement either feature in the preprocessor or the compiler.
476
477 As an extension, GCC treats @samp{$} as a letter.  This is for
478 compatibility with some systems, such as VMS, where @samp{$} is commonly
479 used in system-defined function and object names.  @samp{$} is not a
480 letter in strictly conforming mode, or if you specify the @option{-$}
481 option.  @xref{Invocation}.
482
483 @cindex numbers
484 @cindex preprocessing numbers
485 A @dfn{preprocessing number} has a rather bizarre definition.  The
486 category includes all the normal integer and floating point constants
487 one expects of C, but also a number of other things one might not
488 initially recognize as a number.  Formally, preprocessing numbers begin
489 with an optional period, a required decimal digit, and then continue
490 with any sequence of letters, digits, underscores, periods, and
491 exponents.  Exponents are the two-character sequences @samp{e+},
492 @samp{e-}, @samp{E+}, @samp{E-}, @samp{p+}, @samp{p-}, @samp{P+}, and
493 @samp{P-}.  (The exponents that begin with @samp{p} or @samp{P} are new
494 to C99.  They are used for hexadecimal floating-point constants.)
495
496 The purpose of this unusual definition is to isolate the preprocessor
497 from the full complexity of numeric constants.  It does not have to
498 distinguish between lexically valid and invalid floating-point numbers,
499 which is complicated.  The definition also permits you to split an
500 identifier at any position and get exactly two tokens, which can then be
501 pasted back together with the @samp{##} operator.
502
503 It's possible for preprocessing numbers to cause programs to be
504 misinterpreted.  For example, @code{0xE+12} is a preprocessing number
505 which does not translate to any valid numeric constant, therefore a
506 syntax error.  It does not mean @code{@w{0xE + 12}}, which is what you
507 might have intended.
508
509 @cindex string literals
510 @cindex string constants
511 @cindex character constants
512 @cindex header file names
513 @c the @: prevents makeinfo from turning '' into ".
514 @dfn{String literals} are string constants, character constants, and
515 header file names (the argument of @samp{#include}).@footnote{The C
516 standard uses the term @dfn{string literal} to refer only to what we are
517 calling @dfn{string constants}.}  String constants and character
518 constants are straightforward: @t{"@dots{}"} or @t{'@dots{}'}.  In
519 either case embedded quotes should be escaped with a backslash:
520 @t{'\'@:'} is the character constant for @samp{'}.  There is no limit on
521 the length of a character constant, but the value of a character
522 constant that contains more than one character is
523 implementation-defined.  @xref{Implementation Details}.
524
525 Header file names either look like string constants, @t{"@dots{}"}, or are
526 written with angle brackets instead, @t{<@dots{}>}.  In either case,
527 backslash is an ordinary character.  There is no way to escape the
528 closing quote or angle bracket.  The preprocessor looks for the header
529 file in different places depending on which form you use.  @xref{Include
530 Operation}.
531
532 In standard C, no string literal may extend past the end of a line.  GNU
533 CPP accepts multi-line string constants, but not multi-line character
534 constants or header file names.  This extension is deprecated and will
535 be removed in GCC 3.1.  You may use continued lines instead, or string
536 constant concatenation.  @xref{Differences from previous versions}.
537
538 @cindex punctuators
539 @dfn{Punctuators} are all the usual bits of punctuation which are
540 meaningful to C and C++.  All but three of the punctuation characters in
541 ASCII are C punctuators.  The exceptions are @samp{@@}, @samp{$}, and
542 @samp{`}.  In addition, all the two- and three-character operators are
543 punctuators.  There are also six @dfn{digraphs}, which are merely
544 alternate ways to spell other punctuators.  This is a second attempt to
545 work around missing punctuation in obsolete systems.  It has no negative
546 side effects, unlike trigraphs, but does not cover as much ground.  The
547 digraphs and their corresponding normal punctuators are:
548
549 @example
550 Digraph:        <%  %>  <:  :>  %:  %:%:
551 Punctuator:      @{   @}   [   ]   #    ##
552 @end example
553
554 @cindex other tokens
555 Any other single character is considered ``other.'' It is passed on to
556 the preprocessor's output unmolested.  The C compiler will almost
557 certainly reject source code containing ``other'' tokens.  In ASCII, the
558 only other characters are @samp{@@}, @samp{$}, @samp{`}, and control
559 characters other than NUL (all bits zero).  (Note that @samp{$} is
560 normally considered a letter.)  All characters with the high bit set
561 (numeric range 0x7F--0xFF) are also ``other'' in the present
562 implementation.  This will change when proper support for international
563 character sets is added to GCC@.
564
565 NUL is a special case because of the high probability that its
566 appearance is accidental, and because it may be invisible to the user
567 (many terminals do not display NUL at all).  Within comments, NULs are
568 silently ignored, just as any other character would be.  In running
569 text, NUL is considered white space.  For example, these two directives
570 have the same meaning.
571
572 @example
573 #define X^@@1
574 #define X 1
575 @end example
576
577 @noindent
578 (where @samp{^@@} is ASCII NUL)@.  Within string or character constants,
579 NULs are preserved.  In the latter two cases the preprocessor emits a
580 warning message.
581
582 @node The preprocessing language
583 @section The preprocessing language
584 @cindex directives
585 @cindex preprocessing directives
586 @cindex directive line
587 @cindex directive name
588
589 After tokenization, the stream of tokens may simply be passed straight
590 to the compiler's parser.  However, if it contains any operations in the
591 @dfn{preprocessing language}, it will be transformed first.  This stage
592 corresponds roughly to the standard's ``translation phase 4'' and is
593 what most people think of as the preprocessor's job.
594
595 The preprocessing language consists of @dfn{directives} to be executed
596 and @dfn{macros} to be expanded.  Its primary capabilities are:
597
598 @itemize @bullet
599 @item
600 Inclusion of header files.  These are files of declarations that can be
601 substituted into your program.
602
603 @item
604 Macro expansion.  You can define @dfn{macros}, which are abbreviations
605 for arbitrary fragments of C code.  The preprocessor will replace the
606 macros with their definitions throughout the program.  Some macros are
607 automatically defined for you.
608
609 @item
610 Conditional compilation.  You can include or exclude parts of the
611 program according to various conditions.
612
613 @item
614 Line control.  If you use a program to combine or rearrange source files
615 into an intermediate file which is then compiled, you can use line
616 control to inform the compiler where each source line originally came
617 from.
618
619 @item
620 Diagnostics.  You can detect problems at compile time and issue errors
621 or warnings.
622 @end itemize
623
624 There are a few more, less useful, features.
625
626 Except for expansion of predefined macros, all these operations are
627 triggered with @dfn{preprocessing directives}.  Preprocessing directives
628 are lines in your program that start with @samp{#}.  Whitespace is
629 allowed before and after the @samp{#}.  The @samp{#} is followed by an
630 identifier, the @dfn{directive name}.  It specifies the operation to
631 perform.  Directives are commonly referred to as @samp{#@var{name}}
632 where @var{name} is the directive name.  For example, @samp{#define} is
633 the directive that defines a macro.
634
635 The @samp{#} which begins a directive cannot come from a macro
636 expansion.  Also, the directive name is not macro expanded.  Thus, if
637 @code{foo} is defined as a macro expanding to @code{define}, that does
638 not make @samp{#foo} a valid preprocessing directive.
639
640 The set of valid directive names is fixed.  Programs cannot define new
641 preprocessing directives.
642
643 Some directives require arguments; these make up the rest of the
644 directive line and must be separated from the directive name by
645 whitespace.  For example, @samp{#define} must be followed by a macro
646 name and the intended expansion of the macro.
647
648 A preprocessing directive cannot cover more than one line.  The line
649 may, however, be continued with backslash-newline, or by a block comment
650 which extends past the end of the line.  In either case, when the
651 directive is processed, the continuations have already been merged with
652 the first line to make one long line.
653
654 @node Header Files
655 @chapter Header Files
656
657 @cindex header file
658 A header file is a file containing C declarations and macro definitions
659 (@pxref{Macros}) to be shared between several source files.  You request
660 the use of a header file in your program by @dfn{including} it, with the
661 C preprocessing directive @samp{#include}.
662
663 Header files serve two purposes.
664
665 @itemize @bullet
666 @item
667 @cindex system header files
668 System header files declare the interfaces to parts of the operating
669 system.  You include them in your program to supply the definitions and
670 declarations you need to invoke system calls and libraries.
671
672 @item
673 Your own header files contain declarations for interfaces between the
674 source files of your program.  Each time you have a group of related
675 declarations and macro definitions all or most of which are needed in
676 several different source files, it is a good idea to create a header
677 file for them.
678 @end itemize
679
680 Including a header file produces the same results as copying the header
681 file into each source file that needs it.  Such copying would be
682 time-consuming and error-prone.  With a header file, the related
683 declarations appear in only one place.  If they need to be changed, they
684 can be changed in one place, and programs that include the header file
685 will automatically use the new version when next recompiled.  The header
686 file eliminates the labor of finding and changing all the copies as well
687 as the risk that a failure to find one copy will result in
688 inconsistencies within a program.
689
690 In C, the usual convention is to give header files names that end with
691 @file{.h}.  It is most portable to use only letters, digits, dashes, and
692 underscores in header file names, and at most one dot.
693
694 @menu
695 * Include Syntax::
696 * Include Operation::
697 * Search Path::
698 * Once-Only Headers::
699 * Computed Includes::
700 * Wrapper Headers::
701 * System Headers::
702 @end menu
703
704 @node Include Syntax
705 @section Include Syntax
706
707 @findex #include
708 Both user and system header files are included using the preprocessing
709 directive @samp{#include}.  It has two variants:
710
711 @table @code
712 @item #include <@var{file}>
713 This variant is used for system header files.  It searches for a file
714 named @var{file} in a standard list of system directories.  You can prepend
715 directories to this list with the @option{-I} option (@pxref{Invocation}).
716
717 @item #include "@var{file}"
718 This variant is used for header files of your own program.  It searches
719 for a file named @var{file} first in the directory containing the
720 current file, then in the same directories used for @code{<@var{file}>}.
721 @end table
722
723 The argument of @samp{#include}, whether delimited with quote marks or
724 angle brackets, behaves like a string constant in that comments are not
725 recognized, and macro names are not expanded.  Thus, @code{@w{#include
726 <x/*y>}} specifies inclusion of a system header file named @file{x/*y}.
727
728 However, if backslashes occur within @var{file}, they are considered
729 ordinary text characters, not escape characters.  None of the character
730 escape sequences appropriate to string constants in C are processed.
731 Thus, @code{@w{#include "x\n\\y"}} specifies a filename containing three
732 backslashes.  (Some systems interpret @samp{\} as a pathname separator.
733 All of these also interpret @samp{/} the same way.  It is most portable
734 to use only @samp{/}.)
735
736 It is an error if there is anything (other than comments) on the line
737 after the file name.
738
739 @node Include Operation
740 @section Include Operation
741
742 The @samp{#include} directive works by directing the C preprocessor to
743 scan the specified file as input before continuing with the rest of the
744 current file.  The output from the preprocessor contains the output
745 already generated, followed by the output resulting from the included
746 file, followed by the output that comes from the text after the
747 @samp{#include} directive.  For example, if you have a header file
748 @file{header.h} as follows,
749
750 @example
751 char *test (void);
752 @end example
753
754 @noindent
755 and a main program called @file{program.c} that uses the header file,
756 like this,
757
758 @example
759 int x;
760 #include "header.h"
761
762 int
763 main (void)
764 @{
765   puts (test ());
766 @}
767 @end example
768
769 @noindent
770 the compiler will see the same token stream as it would if
771 @file{program.c} read
772
773 @example
774 int x;
775 char *test (void);
776
777 int
778 main (void)
779 @{
780   puts (test ());
781 @}
782 @end example
783
784 Included files are not limited to declarations and macro definitions;
785 those are merely the typical uses.  Any fragment of a C program can be
786 included from another file.  The include file could even contain the
787 beginning of a statement that is concluded in the containing file, or
788 the end of a statement that was started in the including file.  However,
789 a comment or a string or character constant may not start in the
790 included file and finish in the including file.  An unterminated
791 comment, string constant or character constant in an included file is
792 considered to end (with an error message) at the end of the file.
793
794 To avoid confusion, it is best if header files contain only complete
795 syntactic units---function declarations or definitions, type
796 declarations, etc.
797
798 The line following the @samp{#include} directive is always treated as a
799 separate line by the C preprocessor, even if the included file lacks a
800 final newline.
801
802 @node Search Path
803 @section Search Path
804
805 GCC looks in several different places for headers.  On a normal Unix
806 system, if you do not instruct it otherwise, it will look for headers
807 requested with @code{@w{#include <@var{file}>}} in:
808
809 @example
810 /usr/local/include
811 /usr/lib/gcc-lib/@var{target}/@var{version}/include
812 /usr/@var{target}/include
813 /usr/include
814 @end example
815
816 For C++ programs, it will also look in @file{/usr/include/g++-v3},
817 first.  In the above, @var{target} is the canonical name of the system
818 GCC was configured to compile code for; often but not always the same as
819 the canonical name of the system it runs on.  @var{version} is the
820 version of GCC in use.
821
822 You can add to this list with the @option{-I@var{dir}} command line
823 option.  All the directories named by @option{-I} are searched, in
824 left-to-right order, @emph{before} the default directories.  You can
825 also prevent GCC from searching any of the default directories with the
826 @option{-nostdinc} option.  This is useful when you are compiling an
827 operating system kernel or some other program that does not use the
828 standard C library facilities, or the standard C library itself.
829
830 GCC looks for headers requested with @code{@w{#include "@var{file}"}}
831 first in the directory containing the current file, then in the same
832 places it would have looked for a header requested with angle brackets.
833 For example, if @file{/usr/include/sys/stat.h} contains
834 @code{@w{#include "types.h"}}, GCC looks for @file{types.h} first in
835 @file{/usr/include/sys}, then in its usual search path.
836
837 @samp{#line} (@pxref{Line Control}) does not change GCC's idea of the
838 directory containing the current file.
839
840 You may put @option{-I-} at any point in your list of @option{-I} options.
841 This has two effects.  First, directories appearing before the
842 @option{-I-} in the list are searched only for headers requested with
843 quote marks.  Directories after @option{-I-} are searched for all
844 headers.  Second, the directory containing the current file is not
845 searched for anything, unless it happens to be one of the directories
846 named by an @option{-I} switch.
847
848 @option{-I. -I-} is not the same as no @option{-I} options at all, and does
849 not cause the same behavior for @samp{<>} includes that @samp{""}
850 includes get with no special options.  @option{-I.} searches the
851 compiler's current working directory for header files.  That may or may
852 not be the same as the directory containing the current file.
853
854 If you need to look for headers in a directory named @file{-}, write
855 @option{-I./-}.
856
857 There are several more ways to adjust the header search path.  They are
858 generally less useful.  @xref{Invocation}.
859
860 @node Once-Only Headers
861 @section Once-Only Headers
862 @cindex repeated inclusion
863 @cindex including just once
864 @cindex wrapper @code{#ifndef}
865
866 If a header file happens to be included twice, the compiler will process
867 its contents twice.  This is very likely to cause an error, e.g.@: when the
868 compiler sees the same structure definition twice.  Even if it does not,
869 it will certainly waste time.
870
871 The standard way to prevent this is to enclose the entire real contents
872 of the file in a conditional, like this:
873
874 @example
875 @group
876 /* File foo.  */
877 #ifndef FILE_FOO_SEEN
878 #define FILE_FOO_SEEN
879
880 @var{the entire file}
881
882 #endif /* !FILE_FOO_SEEN */
883 @end group
884 @end example
885
886 This construct is commonly known as a @dfn{wrapper #ifndef}.
887 When the header is included again, the conditional will be false,
888 because @code{FILE_FOO_SEEN} is defined.  The preprocessor will skip
889 over the entire contents of the file, and the compiler will not see it
890 twice.
891
892 GNU CPP optimizes even further.  It remembers when a header file has a
893 wrapper @samp{#ifndef}.  If a subsequent @samp{#include} specifies that
894 header, and the macro in the @samp{#ifndef} is still defined, it does
895 not bother to rescan the file at all.
896
897 You can put comments outside the wrapper.  They will not interfere with
898 this optimization.
899
900 @cindex controlling macro
901 @cindex guard macro
902 The macro @code{FILE_FOO_SEEN} is called the @dfn{controlling macro} or
903 @dfn{guard macro}.  In a user header file, the macro name should not
904 begin with @samp{_}.  In a system header file, it should begin with
905 @samp{__} to avoid conflicts with user programs.  In any kind of header
906 file, the macro name should contain the name of the file and some
907 additional text, to avoid conflicts with other header files.
908
909 @node Computed Includes
910 @section Computed Includes
911 @cindex computed includes
912 @cindex macros in include
913
914 Sometimes it is necessary to select one of several different header
915 files to be included into your program.  They might specify
916 configuration parameters to be used on different sorts of operating
917 systems, for instance.  You could do this with a series of conditionals,
918
919 @example
920 #if SYSTEM_1
921 # include "system_1.h"
922 #elif SYSTEM_2
923 # include "system_2.h"
924 #elif SYSTEM_3
925 @dots{}
926 #endif
927 @end example
928
929 That rapidly becomes tedious.  Instead, the preprocessor offers the
930 ability to use a macro for the header name.  This is called a
931 @dfn{computed include}.  Instead of writing a header name as the direct
932 argument of @samp{#include}, you simply put a macro name there instead:
933
934 @example
935 #define SYSTEM_H "system_1.h"
936 @dots{}
937 #include SYSTEM_H
938 @end example
939
940 @noindent
941 @code{SYSTEM_H} will be expanded, and the preprocessor will look for
942 @file{system_1.h} as if the @samp{#include} had been written that way
943 originally.  @code{SYSTEM_H} could be defined by your Makefile with a
944 @option{-D} option.
945
946 You must be careful when you define the macro.  @samp{#define} saves
947 tokens, not text.  The preprocessor has no way of knowing that the macro
948 will be used as the argument of @samp{#include}, so it generates
949 ordinary tokens, not a header name.  This is unlikely to cause problems
950 if you use double-quote includes, which are close enough to string
951 constants.  If you use angle brackets, however, you may have trouble.
952
953 The syntax of a computed include is actually a bit more general than the
954 above.  If the first non-whitespace character after @samp{#include} is
955 not @samp{"} or @samp{<}, then the entire line is macro-expanded
956 like running text would be.
957
958 If the line expands to a single string constant, the contents of that
959 string constant are the file to be included.  CPP does not re-examine the
960 string for embedded quotes, but neither does it process backslash
961 escapes in the string.  Therefore
962
963 @example
964 #define HEADER "a\"b"
965 #include HEADER
966 @end example
967
968 @noindent
969 looks for a file named @file{a\"b}.  CPP searches for the file according
970 to the rules for double-quoted includes.
971
972 If the line expands to a token stream beginning with a @samp{<} token
973 and including a @samp{>} token, then the tokens between the @samp{<} and
974 the first @samp{>} are combined to form the filename to be included.
975 Any whitespace between tokens is reduced to a single space; then any
976 space after the initial @samp{<} is retained, but a trailing space
977 before the closing @samp{>} is ignored.  CPP searches for the file
978 according to the rules for angle-bracket includes.
979
980 In either case, if there are any tokens on the line after the file name,
981 an error occurs and the directive is not processed.  It is also an error
982 if the result of expansion does not match either of the two expected
983 forms.
984
985 These rules are implementation-defined behavior according to the C
986 standard.  To minimize the risk of different compilers interpreting your
987 computed includes differently, we recommend you use only a single
988 object-like macro which expands to a string constant.  This will also
989 minimize confusion for people reading your program.
990
991 @node Wrapper Headers
992 @section Wrapper Headers
993 @cindex wrapper headers
994 @cindex overriding a header file
995 @findex #include_next
996
997 Sometimes it is necessary to adjust the contents of a system-provided
998 header file without editing it directly.  GCC's @command{fixincludes}
999 operation does this, for example.  One way to do that would be to create
1000 a new header file with the same name and insert it in the search path
1001 before the original header.  That works fine as long as you're willing
1002 to replace the old header entirely.  But what if you want to refer to
1003 the old header from the new one?
1004
1005 You cannot simply include the old header with @samp{#include}.  That
1006 will start from the beginning, and find your new header again.  If your
1007 header is not protected from multiple inclusion (@pxref{Once-Only
1008 Headers}), it will recurse infinitely and cause a fatal error.
1009
1010 You could include the old header with an absolute pathname:
1011 @example
1012 #include "/usr/include/old-header.h"
1013 @end example
1014 @noindent
1015 This works, but is not clean; should the system headers ever move, you
1016 would have to edit the new headers to match.
1017
1018 There is no way to solve this problem within the C standard, but you can
1019 use the GNU extension @samp{#include_next}.  It means, ``Include the
1020 @emph{next} file with this name.''  This directive works like
1021 @samp{#include} except in searching for the specified file: it starts
1022 searching the list of header file directories @emph{after} the directory
1023 in which the current file was found.
1024
1025 Suppose you specify @option{-I /usr/local/include}, and the list of
1026 directories to search also includes @file{/usr/include}; and suppose
1027 both directories contain @file{signal.h}.  Ordinary @code{@w{#include
1028 <signal.h>}} finds the file under @file{/usr/local/include}.  If that
1029 file contains @code{@w{#include_next <signal.h>}}, it starts searching
1030 after that directory, and finds the file in @file{/usr/include}.
1031
1032 @samp{#include_next} does not distinguish between @code{<@var{file}>}
1033 and @code{"@var{file}"} inclusion, nor does it check that the file you
1034 specify has the same name as the current file.  It simply looks for the
1035 file named, starting with the directory in the search path after the one
1036 where the current file was found.
1037
1038 The use of @samp{#include_next} can lead to great confusion.  We
1039 recommend it be used only when there is no other alternative.  In
1040 particular, it should not be used in the headers belonging to a specific
1041 program; it should be used only to make global corrections along the
1042 lines of @command{fixincludes}.
1043
1044 @node System Headers
1045 @section System Headers
1046 @cindex system header files
1047
1048 The header files declaring interfaces to the operating system and
1049 runtime libraries often cannot be written in strictly conforming C@.
1050 Therefore, GCC gives code found in @dfn{system headers} special
1051 treatment.  All warnings, other than those generated by @samp{#warning}
1052 (@pxref{Diagnostics}), are suppressed while GCC is processing a system
1053 header.  Macros defined in a system header are immune to a few warnings
1054 wherever they are expanded.  This immunity is granted on an ad-hoc
1055 basis, when we find that a warning generates lots of false positives
1056 because of code in macros defined in system headers.
1057
1058 Normally, only the headers found in specific directories are considered
1059 system headers.  These directories are determined when GCC is compiled.
1060 There are, however, two ways to make normal headers into system headers.
1061
1062 The @option{-isystem} command line option adds its argument to the list of
1063 directories to search for headers, just like @option{-I}.  Any headers
1064 found in that directory will be considered system headers.
1065
1066 All directories named by @option{-isystem} are searched @emph{after} all
1067 directories named by @option{-I}, no matter what their order was on the
1068 command line.  If the same directory is named by both @option{-I} and
1069 @option{-isystem}, @option{-I} wins; it is as if the @option{-isystem} option
1070 had never been specified at all.
1071
1072 @findex #pragma GCC system_header
1073 There is also a directive, @code{@w{#pragma GCC system_header}}, which
1074 tells GCC to consider the rest of the current include file a system
1075 header, no matter where it was found.  Code that comes before the
1076 @samp{#pragma} in the file will not be affected.  @code{@w{#pragma GCC
1077 system_header}} has no effect in the primary source file.
1078
1079 On very old systems, some of the pre-defined system header directories
1080 get even more special treatment.  GNU C++ considers code in headers
1081 found in those directories to be surrounded by an @code{@w{extern "C"}}
1082 block.  There is no way to request this behavior with a @samp{#pragma},
1083 or from the command line.
1084
1085 @node Macros
1086 @chapter Macros
1087
1088 A @dfn{macro} is a fragment of code which has been given a name.
1089 Whenever the name is used, it is replaced by the contents of the macro.
1090 There are two kinds of macros.  They differ mostly in what they look
1091 like when they are used.  @dfn{Object-like} macros resemble data objects
1092 when used, @dfn{function-like} macros resemble function calls.
1093
1094 You may define any valid identifier as a macro, even if it is a C
1095 keyword.  The preprocessor does not know anything about keywords.  This
1096 can be useful if you wish to hide a keyword such as @code{const} from an
1097 older compiler that does not understand it.  However, the preprocessor
1098 operator @code{defined} (@pxref{Defined}) can never be defined as a
1099 macro, and C++'s named operators (@pxref{C++ Named Operators}) cannot be
1100 macros when you are compiling C++.
1101
1102 @menu
1103 * Object-like Macros::
1104 * Function-like Macros::
1105 * Macro Arguments::
1106 * Stringification::
1107 * Concatenation::
1108 * Variadic Macros::
1109 * Predefined Macros::
1110 * Undefining and Redefining Macros::
1111 * Macro Pitfalls::
1112 @end menu
1113
1114 @node Object-like Macros
1115 @section Object-like Macros
1116 @cindex object-like macro
1117 @cindex symbolic constants
1118 @cindex manifest constants
1119
1120 An @dfn{object-like macro} is a simple identifier which will be replaced
1121 by a code fragment.  It is called object-like because it looks like a
1122 data object in code that uses it.  They are most commonly used to give
1123 symbolic names to numeric constants.
1124
1125 @findex #define
1126 You create macros with the @samp{#define} directive.  @samp{#define} is
1127 followed by the name of the macro and then the token sequence it should
1128 be an abbreviation for, which is variously referred to as the macro's
1129 @dfn{body}, @dfn{expansion} or @dfn{replacement list}.  For example,
1130
1131 @example
1132 #define BUFFER_SIZE 1024
1133 @end example
1134
1135 @noindent
1136 defines a macro named @code{BUFFER_SIZE} as an abbreviation for the
1137 token @code{1024}.  If somewhere after this @samp{#define} directive
1138 there comes a C statement of the form
1139
1140 @example
1141 foo = (char *) malloc (BUFFER_SIZE);
1142 @end example
1143
1144 @noindent
1145 then the C preprocessor will recognize and @dfn{expand} the macro
1146 @code{BUFFER_SIZE}.  The C compiler will see the same tokens as it would
1147 if you had written
1148
1149 @example
1150 foo = (char *) malloc (1024);
1151 @end example
1152
1153 By convention, macro names are written in upper case.  Programs are
1154 easier to read when it is possible to tell at a glance which names are
1155 macros.
1156
1157 The macro's body ends at the end of the @samp{#define} line.  You may
1158 continue the definition onto multiple lines, if necessary, using
1159 backslash-newline.  When the macro is expanded, however, it will all
1160 come out on one line.  For example,
1161
1162 @example
1163 #define NUMBERS 1, \
1164                 2, \
1165                 3
1166 int x[] = @{ NUMBERS @};
1167      @expansion{} int x[] = @{ 1, 2, 3 @};
1168 @end example
1169
1170 @noindent
1171 The most common visible consequence of this is surprising line numbers
1172 in error messages.
1173
1174 There is no restriction on what can go in a macro body provided it
1175 decomposes into valid preprocessing tokens.  Parentheses need not
1176 balance, and the body need not resemble valid C code.  (If it does not,
1177 you may get error messages from the C compiler when you use the macro.)
1178
1179 The C preprocessor scans your program sequentially.  Macro definitions
1180 take effect at the place you write them.  Therefore, the following input
1181 to the C preprocessor
1182
1183 @example
1184 foo = X;
1185 #define X 4
1186 bar = X;
1187 @end example
1188
1189 @noindent
1190 produces
1191
1192 @example
1193 foo = X;
1194 bar = 4;
1195 @end example
1196
1197 When the preprocessor expands a macro name, the macro's expansion
1198 replaces the macro invocation, then the expansion is examined for more
1199 macros to expand.  For example,
1200
1201 @example
1202 @group
1203 #define TABLESIZE BUFSIZE
1204 #define BUFSIZE 1024
1205 TABLESIZE
1206      @expansion{} BUFSIZE
1207      @expansion{} 1024
1208 @end group
1209 @end example
1210
1211 @noindent
1212 @code{TABLESIZE} is expanded first to produce @code{BUFSIZE}, then that
1213 macro is expanded to produce the final result, @code{1024}.
1214
1215 Notice that @code{BUFSIZE} was not defined when @code{TABLESIZE} was
1216 defined.  The @samp{#define} for @code{TABLESIZE} uses exactly the
1217 expansion you specify---in this case, @code{BUFSIZE}---and does not
1218 check to see whether it too contains macro names.  Only when you
1219 @emph{use} @code{TABLESIZE} is the result of its expansion scanned for
1220 more macro names.
1221
1222 This makes a difference if you change the definition of @code{BUFSIZE}
1223 at some point in the source file.  @code{TABLESIZE}, defined as shown,
1224 will always expand using the definition of @code{BUFSIZE} that is
1225 currently in effect:
1226
1227 @example
1228 #define BUFSIZE 1020
1229 #define TABLESIZE BUFSIZE
1230 #undef BUFSIZE
1231 #define BUFSIZE 37
1232 @end example
1233
1234 @noindent
1235 Now @code{TABLESIZE} expands (in two stages) to @code{37}.
1236
1237 If the expansion of a macro contains its own name, either directly or
1238 via intermediate macros, it is not expanded again when the expansion is
1239 examined for more macros.  This prevents infinite recursion.
1240 @xref{Self-Referential Macros}, for the precise details.
1241
1242 @node Function-like Macros
1243 @section Function-like Macros
1244 @cindex function-like macros
1245
1246 You can also define macros whose use looks like a function call.  These
1247 are called @dfn{function-like macros}.  To define a function-like macro,
1248 you use the same @samp{#define} directive, but you put a pair of
1249 parentheses immediately after the macro name.  For example,
1250
1251 @example
1252 #define lang_init()  c_init()
1253 lang_init()
1254      @expansion{} c_init()
1255 @end example
1256
1257 A function-like macro is only expanded if its name appears with a pair
1258 of parentheses after it.  If you write just the name, it is left alone.
1259 This can be useful when you have a function and a macro of the same
1260 name, and you wish to use the function sometimes.
1261
1262 @example
1263 extern void foo(void);
1264 #define foo() /* optimized inline version */
1265 @dots{}
1266   foo();
1267   funcptr = foo;
1268 @end example
1269
1270 Here the call to @code{foo()} will use the macro, but the function
1271 pointer will get the address of the real function.  If the macro were to
1272 be expanded, it would cause a syntax error.
1273
1274 If you put spaces between the macro name and the parentheses in the
1275 macro definition, that does not define a function-like macro, it defines
1276 an object-like macro whose expansion happens to begin with a pair of
1277 parentheses.
1278
1279 @example
1280 #define lang_init ()    c_init()
1281 lang_init()
1282      @expansion{} () c_init()()
1283 @end example
1284
1285 The first two pairs of parentheses in this expansion come from the
1286 macro.  The third is the pair that was originally after the macro
1287 invocation.  Since @code{lang_init} is an object-like macro, it does not
1288 consume those parentheses.
1289
1290 @node Macro Arguments
1291 @section Macro Arguments
1292 @cindex arguments
1293 @cindex macros with arguments
1294 @cindex arguments in macro definitions
1295
1296 Function-like macros can take @dfn{arguments}, just like true functions.
1297 To define a macro that uses arguments, you insert @dfn{parameters}
1298 between the pair of parentheses in the macro definition that make the
1299 macro function-like.  The parameters must be valid C identifiers,
1300 separated by commas and optionally whitespace.
1301
1302 To invoke a macro that takes arguments, you write the name of the macro
1303 followed by a list of @dfn{actual arguments} in parentheses, separated
1304 by commas.  The invocation of the macro need not be restricted to a
1305 single logical line---it can cross as many lines in the source file as
1306 you wish.  The number of arguments you give must match the number of
1307 parameters in the macro definition.  When the macro is expanded, each
1308 use of a parameter in its body is replaced by the tokens of the
1309 corresponding argument.  (You need not use all of the parameters in the
1310 macro body.)
1311
1312 As an example, here is a macro that computes the minimum of two numeric
1313 values, as it is defined in many C programs, and some uses.
1314
1315 @example
1316 #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
1317   x = min(a, b);          @expansion{}  x = ((a) < (b) ? (a) : (b));
1318   y = min(1, 2);          @expansion{}  y = ((1) < (2) ? (1) : (2));
1319   z = min(a + 28, *p);    @expansion{}  z = ((a + 28) < (*p) ? (a + 28) : (*p));
1320 @end example
1321
1322 @noindent
1323 (In this small example you can already see several of the dangers of
1324 macro arguments.  @xref{Macro Pitfalls}, for detailed explanations.)
1325
1326 Leading and trailing whitespace in each argument is dropped, and all
1327 whitespace between the tokens of an argument is reduced to a single
1328 space.  Parentheses within each argument must balance; a comma within
1329 such parentheses does not end the argument.  However, there is no
1330 requirement for square brackets or braces to balance, and they do not
1331 prevent a comma from separating arguments.  Thus,
1332
1333 @example
1334 macro (array[x = y, x + 1])
1335 @end example
1336
1337 @noindent
1338 passes two arguments to @code{macro}: @code{array[x = y} and @code{x +
1339 1]}.  If you want to supply @code{array[x = y, x + 1]} as an argument,
1340 you can write it as @code{array[(x = y, x + 1)]}, which is equivalent C
1341 code.
1342
1343 All arguments to a macro are completely macro-expanded before they are
1344 substituted into the macro body.  After substitution, the complete text
1345 is scanned again for macros to expand, including the arguments.  This rule
1346 may seem strange, but it is carefully designed so you need not worry
1347 about whether any function call is actually a macro invocation.  You can
1348 run into trouble if you try to be too clever, though.  @xref{Argument
1349 Prescan}, for detailed discussion.
1350
1351 For example, @code{min (min (a, b), c)} is first expanded to
1352
1353 @example
1354   min (((a) < (b) ? (a) : (b)), (c))
1355 @end example
1356
1357 @noindent
1358 and then to
1359
1360 @example
1361 @group
1362 ((((a) < (b) ? (a) : (b))) < (c)
1363  ? (((a) < (b) ? (a) : (b)))
1364  : (c))
1365 @end group
1366 @end example
1367
1368 @noindent
1369 (Line breaks shown here for clarity would not actually be generated.)
1370
1371 @cindex empty macro arguments
1372 You can leave macro arguments empty; this is not an error to the
1373 preprocessor (but many macros will then expand to invalid code).
1374 You cannot leave out arguments entirely; if a macro takes two arguments,
1375 there must be exactly one comma at the top level of its argument list.
1376 Here are some silly examples using @code{min}:
1377
1378 @example
1379 min(, b)        @expansion{} ((   ) < (b) ? (   ) : (b))
1380 min(a, )        @expansion{} ((a  ) < ( ) ? (a  ) : ( ))
1381 min(,)          @expansion{} ((   ) < ( ) ? (   ) : ( ))
1382 min((,),)       @expansion{} (((,)) < ( ) ? ((,)) : ( ))
1383
1384 min()      @error{} macro "min" requires 2 arguments, but only 1 given
1385 min(,,)    @error{} macro "min" passed 3 arguments, but takes just 2
1386 @end example
1387
1388 Whitespace is not a preprocessing token, so if a macro @code{foo} takes
1389 one argument, @code{@w{foo ()}} and @code{@w{foo ( )}} both supply it an
1390 empty argument.  Previous GNU preprocessor implementations and
1391 documentation were incorrect on this point, insisting that a
1392 function-like macro that takes a single argument be passed a space if an
1393 empty argument was required.
1394
1395 Macro parameters appearing inside string literals are not replaced by
1396 their corresponding actual arguments.
1397
1398 @example
1399 #define foo(x) x, "x"
1400 foo(bar)        @expansion{} bar, "x"
1401 @end example
1402
1403 @node Stringification
1404 @section Stringification
1405 @cindex stringification
1406 @cindex @samp{#} operator
1407
1408 Sometimes you may want to convert a macro argument into a string
1409 constant.  Parameters are not replaced inside string constants, but you
1410 can use the @samp{#} preprocessing operator instead.  When a macro
1411 parameter is used with a leading @samp{#}, the preprocessor replaces it
1412 with the literal text of the actual argument, converted to a string
1413 constant.  Unlike normal parameter replacement, the argument is not
1414 macro-expanded first.  This is called @dfn{stringification}.
1415
1416 There is no way to combine an argument with surrounding text and
1417 stringify it all together.  Instead, you can write a series of adjacent
1418 string constants and stringified arguments.  The preprocessor will
1419 replace the stringified arguments with string constants.  The C
1420 compiler will then combine all the adjacent string constants into one
1421 long string.
1422
1423 Here is an example of a macro definition that uses stringification:
1424
1425 @example
1426 @group
1427 #define WARN_IF(EXP) \
1428 do @{ if (EXP) \
1429         fprintf (stderr, "Warning: " #EXP "\n"); @} \
1430 while (0)
1431 WARN_IF (x == 0);
1432      @expansion{} do @{ if (x == 0)
1433            fprintf (stderr, "Warning: " "x == 0" "\n"); @} while (0);
1434 @end group
1435 @end example
1436
1437 @noindent
1438 The argument for @code{EXP} is substituted once, as-is, into the
1439 @code{if} statement, and once, stringified, into the argument to
1440 @code{fprintf}.  If @code{x} were a macro, it would be expanded in the
1441 @code{if} statement, but not in the string.
1442
1443 The @code{do} and @code{while (0)} are a kludge to make it possible to
1444 write @code{WARN_IF (@var{arg});}, which the resemblance of
1445 @code{WARN_IF} to a function would make C programmers want to do; see
1446 @ref{Swallowing the Semicolon}.
1447
1448 Stringification in C involves more than putting double-quote characters
1449 around the fragment.  The preprocessor backslash-escapes the quotes
1450 surrounding embedded string constants, and all backslashes within string and
1451 character constants, in order to get a valid C string constant with the
1452 proper contents.  Thus, stringifying @code{@w{p = "foo\n";}} results in
1453 @t{@w{"p = \"foo\\n\";"}}.  However, backslashes that are not inside string
1454 or character constants are not duplicated: @samp{\n} by itself
1455 stringifies to @t{"\n"}.
1456
1457 All leading and trailing whitespace in text being stringified is
1458 ignored.  Any sequence of whitespace in the middle of the text is
1459 converted to a single space in the stringified result.  Comments are
1460 replaced by whitespace long before stringification happens, so they
1461 never appear in stringified text.
1462
1463 There is no way to convert a macro argument into a character constant.
1464
1465 If you want to stringify the result of expansion of a macro argument,
1466 you have to use two levels of macros.
1467
1468 @example
1469 #define xstr(s) str(s)
1470 #define str(s) #s
1471 #define foo 4
1472 str (foo)
1473      @expansion{} "foo"
1474 xstr (foo)
1475      @expansion{} xstr (4)
1476      @expansion{} str (4)
1477      @expansion{} "4"
1478 @end example
1479
1480 @code{s} is stringified when it is used in @code{str}, so it is not
1481 macro-expanded first.  But @code{s} is an ordinary argument to
1482 @code{xstr}, so it is completely macro-expanded before @code{xstr}
1483 itself is expanded (@pxref{Argument Prescan}).  Therefore, by the time
1484 @code{str} gets to its argument, it has already been macro-expanded.
1485
1486 @node Concatenation
1487 @section Concatenation
1488 @cindex concatenation
1489 @cindex token pasting
1490 @cindex token concatenation
1491 @cindex @samp{##} operator
1492
1493 It is often useful to merge two tokens into one while expanding macros.
1494 This is called @dfn{token pasting} or @dfn{token concatenation}.  The
1495 @samp{##} preprocessing operator performs token pasting.  When a macro
1496 is expanded, the two tokens on either side of each @samp{##} operator
1497 are combined into a single token, which then replaces the @samp{##} and
1498 the two original tokens in the macro expansion.  Usually both will be
1499 identifiers, or one will be an identifier and the other a preprocessing
1500 number.  When pasted, they make a longer identifier.  This isn't the
1501 only valid case.  It is also possible to concatenate two numbers (or a
1502 number and a name, such as @code{1.5} and @code{e3}) into a number.
1503 Also, multi-character operators such as @code{+=} can be formed by
1504 token pasting.
1505
1506 However, two tokens that don't together form a valid token cannot be
1507 pasted together.  For example, you cannot concatenate @code{x} with
1508 @code{+} in either order.  If you try, the preprocessor issues a warning
1509 and emits the two tokens as if they had been written next to each other.
1510 It is common to find unnecessary uses of @samp{##} in complex macros.
1511 If you get this warning, it is likely that you can simply remove the
1512 @samp{##}.
1513
1514 Both the tokens combined by @samp{##} could come from the macro body,
1515 but you could just as well write them as one token in the first place.
1516 Token pasting is most useful when one or both of the tokens comes from a
1517 macro argument.  If either of the tokens next to an @samp{##} is a
1518 parameter name, it is replaced by its actual argument before @samp{##}
1519 executes.  As with stringification, the actual argument is not
1520 macro-expanded first.  If the argument is empty, that @samp{##} has no
1521 effect.
1522
1523 Keep in mind that the C preprocessor converts comments to whitespace
1524 before macros are even considered.  Therefore, you cannot create a
1525 comment by concatenating @samp{/} and @samp{*}.  You can put as much
1526 whitespace between @samp{##} and its operands as you like, including
1527 comments, and you can put comments in arguments that will be
1528 concatenated.  However, it is an error if @samp{##} appears at either
1529 end of a macro body.
1530
1531 Consider a C program that interprets named commands.  There probably
1532 needs to be a table of commands, perhaps an array of structures declared
1533 as follows:
1534
1535 @example
1536 @group
1537 struct command
1538 @{
1539   char *name;
1540   void (*function) (void);
1541 @};
1542 @end group
1543
1544 @group
1545 struct command commands[] =
1546 @{
1547   @{ "quit", quit_command @},
1548   @{ "help", help_command @},
1549   @dots{}
1550 @};
1551 @end group
1552 @end example
1553
1554 It would be cleaner not to have to give each command name twice, once in
1555 the string constant and once in the function name.  A macro which takes the
1556 name of a command as an argument can make this unnecessary.  The string
1557 constant can be created with stringification, and the function name by
1558 concatenating the argument with @samp{_command}.  Here is how it is done:
1559
1560 @example
1561 #define COMMAND(NAME)  @{ #NAME, NAME ## _command @}
1562
1563 struct command commands[] =
1564 @{
1565   COMMAND (quit),
1566   COMMAND (help),
1567   @dots{}
1568 @};
1569 @end example
1570
1571 @node Variadic Macros
1572 @section Variadic Macros
1573 @cindex variable number of arguments
1574 @cindex macros with variable arguments
1575 @cindex variadic macros
1576
1577 A macro can be declared to accept a variable number of arguments much as
1578 a function can.  The syntax for defining the macro is similar to that of
1579 a function.  Here is an example:
1580
1581 @example
1582 #define eprintf(@dots{}) fprintf (stderr, __VA_ARGS__)
1583 @end example
1584
1585 This kind of macro is called @dfn{variadic}.  When the macro is invoked,
1586 all the tokens in its argument list after the last named argument (this
1587 macro has none), including any commas, become the @dfn{variable
1588 argument}.  This sequence of tokens replaces the identifier
1589 @code{@w{__VA_ARGS__}} in the macro body wherever it appears.  Thus, we
1590 have this expansion:
1591
1592 @example
1593 eprintf ("%s:%d: ", input_file, lineno)
1594      @expansion{}  fprintf (stderr, "%s:%d: ", input_file, lineno)
1595 @end example
1596
1597 The variable argument is completely macro-expanded before it is inserted
1598 into the macro expansion, just like an ordinary argument.  You may use
1599 the @samp{#} and @samp{##} operators to stringify the variable argument
1600 or to paste its leading or trailing token with another token.  (But see
1601 below for an important special case for @samp{##}.)
1602
1603 If your macro is complicated, you may want a more descriptive name for
1604 the variable argument than @code{@w{__VA_ARGS__}}.  GNU CPP permits
1605 this, as an extension.  You may write an argument name immediately
1606 before the @samp{@dots{}}; that name is used for the variable argument.
1607 The @code{eprintf} macro above could be written
1608
1609 @example
1610 #define eprintf(args@dots{}) fprintf (stderr, args)
1611 @end example
1612
1613 @noindent
1614 using this extension.  You cannot use @code{__VA_ARGS__} and this
1615 extension in the same macro.
1616
1617 You can have named arguments as well as variable arguments in a variadic
1618 macro.  We could define @code{eprintf} like this, instead:
1619
1620 @example
1621 #define eprintf(format, @dots{}) fprintf (stderr, format, __VA_ARGS__)
1622 @end example
1623
1624 @noindent
1625 This formulation looks more descriptive, but unfortunately it is less
1626 flexible: you must now supply at least one argument after the format
1627 string.  In standard C, you cannot omit the comma separating the named
1628 argument from the variable arguments.  Furthermore, if you leave the
1629 variable argument empty, you will get a syntax error, because
1630 there will be an extra comma after the format string.
1631
1632 @example
1633 eprintf("success!\n", );
1634      @expansion{} fprintf(stderr, "success!\n", );
1635 @end example
1636
1637 GNU CPP has a pair of extensions which deal with this problem.  First,
1638 you are allowed to leave the variable argument out entirely:
1639
1640 @example
1641 eprintf ("success!\n")
1642      @expansion{} fprintf(stderr, "success!\n", );
1643 @end example
1644
1645 @noindent
1646 Second, the @samp{##} token paste operator has a special meaning when
1647 placed between a comma and a variable argument.  If you write
1648
1649 @example
1650 #define eprintf(format, @dots{}) fprintf (stderr, format, ##__VA_ARGS__)
1651 @end example
1652
1653 @noindent
1654 and the variable argument is left out when the @code{eprintf} macro is
1655 used, then the comma before the @samp{##} will be deleted.  This does
1656 @emph{not} happen if you pass an empty argument, nor does it happen if
1657 the token preceding @samp{##} is anything other than a comma.
1658
1659 @example
1660 eprintf ("success!\n")
1661      @expansion{} fprintf(stderr, "success!\n");
1662 @end example
1663
1664 C99 mandates that the only place the identifier @code{@w{__VA_ARGS__}}
1665 can appear is in the replacement list of a variadic macro.  It may not
1666 be used as a macro name, macro argument name, or within a different type
1667 of macro.  It may also be forbidden in open text; the standard is
1668 ambiguous.  We recommend you avoid using it except for its defined
1669 purpose.
1670
1671 Variadic macros are a new feature in C99.  GNU CPP has supported them
1672 for a long time, but only with a named variable argument
1673 (@samp{args@dots{}}, not @samp{@dots{}} and @code{@w{__VA_ARGS__}}).  If you are
1674 concerned with portability to previous versions of GCC, you should use
1675 only named variable arguments.  On the other hand, if you are concerned
1676 with portability to other conforming implementations of C99, you should
1677 use only @code{@w{__VA_ARGS__}}.
1678
1679 Previous versions of GNU CPP implemented the comma-deletion extension
1680 much more generally.  We have restricted it in this release to minimize
1681 the differences from C99.  To get the same effect with both this and
1682 previous versions of GCC, the token preceding the special @samp{##} must
1683 be a comma, and there must be white space between that comma and
1684 whatever comes immediately before it:
1685
1686 @example
1687 #define eprintf(format, args@dots{}) fprintf (stderr, format , ##args)
1688 @end example
1689
1690 @noindent
1691 @xref{Differences from previous versions}, for the gory details.
1692
1693 @node Predefined Macros
1694 @section Predefined Macros
1695
1696 @cindex predefined macros
1697 Several object-like macros are predefined; you use them without
1698 supplying their definitions.  They fall into three classes: standard,
1699 common, and system-specific.
1700
1701 In C++, there is a fourth category, the named operators.  They act like
1702 predefined macros, but you cannot undefine them.
1703
1704 @menu
1705 * Standard Predefined Macros::
1706 * Common Predefined Macros::
1707 * System-specific Predefined Macros::
1708 * C++ Named Operators::
1709 @end menu
1710
1711 @node Standard Predefined Macros
1712 @subsection Standard Predefined Macros
1713 @cindex standard predefined macros.
1714
1715 The standard predefined macros are specified by the C and/or C++
1716 language standards, so they are available with all compilers that
1717 implement those standards.  Older compilers may not provide all of
1718 them.  Their names all start with double underscores.
1719
1720 @table @code
1721 @item __FILE__
1722 This macro expands to the name of the current input file, in the form of
1723 a C string constant.  This is the path by which the preprocessor opened
1724 the file, not the short name specified in @samp{#include} or as the
1725 input file name argument.  For example,
1726 @code{"/usr/local/include/myheader.h"} is a possible expansion of this
1727 macro.
1728
1729 @item __LINE__
1730 This macro expands to the current input line number, in the form of a
1731 decimal integer constant.  While we call it a predefined macro, it's
1732 a pretty strange macro, since its ``definition'' changes with each
1733 new line of source code.
1734 @end table
1735
1736 @code{__FILE__} and @code{__LINE__} are useful in generating an error
1737 message to report an inconsistency detected by the program; the message
1738 can state the source line at which the inconsistency was detected.  For
1739 example,
1740
1741 @example
1742 fprintf (stderr, "Internal error: "
1743                  "negative string length "
1744                  "%d at %s, line %d.",
1745          length, __FILE__, __LINE__);
1746 @end example
1747
1748 An @samp{#include} directive changes the expansions of @code{__FILE__}
1749 and @code{__LINE__} to correspond to the included file.  At the end of
1750 that file, when processing resumes on the input file that contained
1751 the @samp{#include} directive, the expansions of @code{__FILE__} and
1752 @code{__LINE__} revert to the values they had before the
1753 @samp{#include} (but @code{__LINE__} is then incremented by one as
1754 processing moves to the line after the @samp{#include}).
1755
1756 A @samp{#line} directive changes @code{__LINE__}, and may change
1757 @code{__FILE__} as well.  @xref{Line Control}.
1758
1759 C99 introduces @code{__func__}, and GCC has provided @code{__FUNCTION__}
1760 for a long time.  Both of these are strings containing the name of the
1761 current function (there are slight semantic differences; see the GCC
1762 manual).  Neither of them is a macro; the preprocessor does not know the
1763 name of the current function.  They tend to be useful in conjunction
1764 with @code{__FILE__} and @code{__LINE__}, though.
1765
1766 @table @code
1767
1768 @item __DATE__
1769 This macro expands to a string constant that describes the date on which
1770 the preprocessor is being run.  The string constant contains eleven
1771 characters and looks like @code{@w{"Feb 12 1996"}}.  If the day of the
1772 month is less than 10, it is padded with a space on the left.
1773
1774 @item __TIME__
1775 This macro expands to a string constant that describes the time at
1776 which the preprocessor is being run.  The string constant contains
1777 eight characters and looks like @code{"23:59:01"}.
1778
1779 @item __STDC__
1780 In normal operation, this macro expands to the constant 1, to signify
1781 that this compiler conforms to ISO Standard C@.  If GNU CPP is used with
1782 a compiler other than GCC, this is not necessarily true; however, the
1783 preprocessor always conforms to the standard, unless the
1784 @option{-traditional} option is used.
1785
1786 This macro is not defined if the @option{-traditional} option is used.
1787
1788 On some hosts, the system compiler uses a different convention, where
1789 @code{__STDC__} is normally 0, but is 1 if the user specifies strict
1790 conformance to the C Standard.  GNU CPP follows the host convention when
1791 processing system header files, but when processing user files
1792 @code{__STDC__} is always 1.  This has been reported to cause problems;
1793 for instance, some versions of Solaris provide X Windows headers that
1794 expect @code{__STDC__} to be either undefined or 1.  You may be able to
1795 work around this sort of problem by using an @option{-I} option to
1796 cancel treatment of those headers as system headers.  @xref{Invocation}.
1797
1798 @item __STDC_VERSION__
1799 This macro expands to the C Standard's version number, a long integer
1800 constant of the form @code{@var{yyyy}@var{mm}L} where @var{yyyy} and
1801 @var{mm} are the year and month of the Standard version.  This signifies
1802 which version of the C Standard the compiler conforms to.  Like
1803 @code{__STDC__}, this is not necessarily accurate for the entire
1804 implementation, unless GNU CPP is being used with GCC@.
1805
1806 The value @code{199409L} signifies the 1989 C standard as amended in
1807 1994, which is the current default; the value @code{199901L} signifies
1808 the 1999 revision of the C standard.  Support for the 1999 revision is
1809 not yet complete.
1810
1811 This macro is not defined if the @option{-traditional} option is used, nor
1812 when compiling C++ or Objective-C@.
1813
1814 @item __STDC_HOSTED__
1815 This macro is defined, with value 1, if the compiler's target is a
1816 @dfn{hosted environment}.  A hosted environment has the complete
1817 facilities of the standard C library available.
1818
1819 @item __cplusplus
1820 This macro is defined when the C++ compiler is in use.  You can use
1821 @code{__cplusplus} to test whether a header is compiled by a C compiler
1822 or a C++ compiler.  This macro is similar to @code{__STDC_VERSION__}, in
1823 that it expands to a version number.  A fully conforming implementation
1824 of the 1998 C++ standard will define this macro to @code{199711L}.  The
1825 GNU C++ compiler is not yet fully conforming, so it uses @code{1}
1826 instead.  We hope to complete our implementation in the near future.
1827
1828 @end table
1829
1830 @node Common Predefined Macros
1831 @subsection Common Predefined Macros
1832 @cindex common predefined macros
1833
1834 The common predefined macros are GNU C extensions.  They are available
1835 with the same meanings regardless of the machine or operating system on
1836 which you are using GNU C@.  Their names all start with double
1837 underscores.
1838
1839 @table @code
1840
1841 @item __GNUC__
1842 @itemx __GNUC_MINOR__
1843 @itemx __GNUC_PATCHLEVEL__
1844 These macros are defined by all GNU compilers that use the C
1845 preprocessor: C, C++, and Objective-C@.  Their values are the major
1846 version, minor version, and patch level of the compiler, as integer
1847 constants.  For example, GCC 3.2.1 will define @code{__GNUC__} to 3,
1848 @code{__GNUC_MINOR__} to 2, and @code{__GNUC_PATCHLEVEL__} to 1.  They
1849 are defined only when the entire compiler is in use; if you invoke the
1850 preprocessor directly, they are not defined.
1851
1852 @code{__GNUC_PATCHLEVEL__} is new to GCC 3.0; it is also present in the
1853 widely-used development snapshots leading up to 3.0 (which identify
1854 themselves as GCC 2.96 or 2.97, depending on which snapshot you have).
1855
1856 If all you need to know is whether or not your program is being compiled
1857 by GCC, you can simply test @code{__GNUC__}.  If you need to write code
1858 which depends on a specific version, you must be more careful.  Each
1859 time the minor version is increased, the patch level is reset to zero;
1860 each time the major version is increased (which happens rarely), the
1861 minor version and patch level are reset.  If you wish to use the
1862 predefined macros directly in the conditional, you will need to write it
1863 like this:
1864
1865 @example
1866 /* @r{Test for GCC > 3.2.0} */
1867 #if __GNUC__ > 3 || \
1868     (__GNUC__ == 3 && (__GNUC_MINOR__ > 2 || \
1869                        (__GNUC_MINOR__ == 2 && \
1870                         __GNUC_PATCHLEVEL__ > 0))
1871 @end example
1872
1873 @noindent
1874 Another approach is to use the predefined macros to
1875 calculate a single number, then compare that against a threshold:
1876
1877 @example
1878 #define GCC_VERSION (__GNUC__ * 10000 \
1879                      + __GNUC_MINOR__ * 100 \
1880                      + __GNUC_PATCHLEVEL__)
1881 @dots{}
1882 /* @r{Test for GCC > 3.2.0} */
1883 #if GCC_VERSION > 30200
1884 @end example
1885
1886 @noindent
1887 Many people find this form easier to understand.
1888
1889 @item __GNUG__
1890 The GNU C++ compiler defines this.  Testing it is equivalent to
1891 testing @code{@w{(__GNUC__ && __cplusplus)}}.
1892
1893 @item __STRICT_ANSI__
1894 GCC defines this macro if and only if the @option{-ansi} switch, or a
1895 @option{-std} switch specifying strict conformance to some version of ISO C,
1896 was specified when GCC was invoked.  It is defined to @samp{1}.
1897 This macro exists primarily to direct GNU libc's header files to
1898 restrict their definitions to the minimal set found in the 1989 C
1899 standard.
1900
1901 @item __BASE_FILE__
1902 This macro expands to the name of the main input file, in the form
1903 of a C string constant.  This is the source file that was specified
1904 on the command line of the preprocessor or C compiler.
1905
1906 @item __INCLUDE_LEVEL__
1907 This macro expands to a decimal integer constant that represents the
1908 depth of nesting in include files.  The value of this macro is
1909 incremented on every @samp{#include} directive and decremented at the
1910 end of every included file.  It starts out at 0, it's value within the
1911 base file specified on the command line.
1912
1913 @item __VERSION__
1914 This macro expands to a string constant which describes the version of
1915 the compiler in use.  You should not rely on its contents having any
1916 particular form, but it can be counted on to contain at least the
1917 release number.
1918
1919 @item __OPTIMIZE__
1920 @itemx __OPTIMIZE_SIZE__
1921 @itemx __NO_INLINE__
1922 These macros describe the compilation mode.  @code{__OPTIMIZE__} is
1923 defined in all optimizing compilations.  @code{__OPTIMIZE_SIZE__} is
1924 defined if the compiler is optimizing for size, not speed.
1925 @code{__NO_INLINE__} is defined if no functions will be inlined into
1926 their callers (when not optimizing, or when inlining has been
1927 specifically disabled by @option{-fno-inline}).
1928
1929 These macros cause certain GNU header files to provide optimized
1930 definitions, using macros or inline functions, of system library
1931 functions.  You should not use these macros in any way unless you make
1932 sure that programs will execute with the same effect whether or not they
1933 are defined.  If they are defined, their value is 1.
1934
1935 @item __CHAR_UNSIGNED__
1936 GCC defines this macro if and only if the data type @code{char} is
1937 unsigned on the target machine.  It exists to cause the standard header
1938 file @file{limits.h} to work correctly.  You should not use this macro
1939 yourself; instead, refer to the standard macros defined in @file{limits.h}.
1940
1941 @item __REGISTER_PREFIX__
1942 This macro expands to a single token (not a string constant) which is
1943 the prefix applied to CPU register names in assembly language for this
1944 target.  You can use it to write assembly that is usable in multiple
1945 environments.  For example, in the @code{m68k-aout} environment it
1946 expands to nothing, but in the @code{m68k-coff} environment it expands
1947 to a single @samp{%}.
1948
1949 @item __USER_LABEL_PREFIX__
1950 This macro expands to a single token which is the the prefix applied to
1951 user labels (symbols visible to C code) in assembly.  For example, in
1952 the @code{m68k-aout} environment it expands to an @samp{_}, but in the
1953 @code{m68k-coff} environment it expands to nothing.
1954
1955 This macro will have the correct definition even if
1956 @option{-f(no-)underscores} is in use, but it will not be correct if
1957 target-specific options that adjust this prefix are used (e.g.@: the
1958 OSF/rose @option{-mno-underscores} option).
1959
1960 @item __SIZE_TYPE__
1961 @itemx __PTRDIFF_TYPE__
1962 @itemx __WCHAR_TYPE__
1963 @itemx __WINT_TYPE__
1964 These macros are defined to the correct underlying types for the
1965 @code{size_t}, @code{ptrdiff_t}, @code{wchar_t}, and @code{wint_t}
1966 typedefs, respectively.  They exist to make the standard header files
1967 @file{stddef.h} and @file{wchar.h} work correctly.  You should not use
1968 these macros directly; instead, include the appropriate headers and use
1969 the typedefs.
1970
1971 @end table
1972
1973 @node System-specific Predefined Macros
1974 @subsection System-specific Predefined Macros
1975
1976 @cindex system-specific predefined macros
1977 @cindex predefined macros, system-specific
1978 @cindex reserved namespace
1979
1980 The C preprocessor normally predefines several macros that indicate what
1981 type of system and machine is in use.  They are obviously different on
1982 each target supported by GCC@.  This manual, being for all systems and
1983 machines, cannot tell you what their names are, but you can use
1984 @command{cpp -dM} to see them all.  @xref{Invocation}.  All system-specific
1985 predefined macros expand to the constant 1, so you can test them with
1986 either @samp{#ifdef} or @samp{#if}.
1987
1988 The C standard requires that all system-specific macros be part of the
1989 @dfn{reserved namespace}.  All names which begin with two underscores,
1990 or an underscore and a capital letter, are reserved for the compiler and
1991 library to use as they wish.  However, historically system-specific
1992 macros have had names with no special prefix; for instance, it is common
1993 to find @code{unix} defined on Unix systems.  For all such macros, GCC
1994 provides a parallel macro with two underscores added at the beginning
1995 and the end.  If @code{unix} is defined, @code{__unix__} will be defined
1996 too.  There will never be more than two underscores; the parallel of
1997 @code{_mips} is @code{__mips__}.
1998
1999 When the @option{-ansi} option, or any @option{-std} option that
2000 requests strict conformance, is given to the compiler, all the
2001 system-specific predefined macros outside the reserved namespace are
2002 suppressed.  The parallel macros, inside the reserved namespace, remain
2003 defined.
2004
2005 We are slowly phasing out all predefined macros which are outside the
2006 reserved namespace.  You should never use them in new programs, and we
2007 encourage you to correct older code to use the parallel macros whenever
2008 you find it.  We don't recommend you use the system-specific macros that
2009 are in the reserved namespace, either.  It is better in the long run to
2010 check specifically for features you need, using a tool such as
2011 @command{autoconf}.
2012
2013 @node C++ Named Operators
2014 @subsection C++ Named Operators
2015 @cindex named operators
2016 @cindex C++ named operators
2017 @cindex iso646.h
2018
2019 In C++, there are eleven keywords which are simply alternate spellings
2020 of operators normally written with punctuation.  These keywords are
2021 treated as such even in the preprocessor.  They function as operators in
2022 @samp{#if}, and they cannot be defined as macros or poisoned.  In C, you
2023 can request that those keywords take their C++ meaning by including
2024 @file{iso646.h}.  That header defines each one as a normal object-like
2025 macro expanding to the appropriate punctuator.
2026
2027 These are the named operators and their corresponding punctuators:
2028
2029 @multitable {Named Operator} {Punctuator}
2030 @item Named Operator @tab Punctuator
2031 @item @code{and}    @tab @code{&&}
2032 @item @code{and_eq} @tab @code{&=}
2033 @item @code{bitand} @tab @code{&}
2034 @item @code{bitor}  @tab @code{|}
2035 @item @code{compl}  @tab @code{~}
2036 @item @code{not}    @tab @code{!}
2037 @item @code{not_eq} @tab @code{!=}
2038 @item @code{or}     @tab @code{||}
2039 @item @code{or_eq}  @tab @code{|=}
2040 @item @code{xor}    @tab @code{^}
2041 @item @code{xor_eq} @tab @code{^=}
2042 @end multitable
2043
2044 @node Undefining and Redefining Macros
2045 @section Undefining and Redefining Macros
2046 @cindex undefining macros
2047 @cindex redefining macros
2048 @findex #undef
2049
2050 If a macro ceases to be useful, it may be @dfn{undefined} with the
2051 @samp{#undef} directive.  @samp{#undef} takes a single argument, the
2052 name of the macro to undefine.  You use the bare macro name, even if the
2053 macro is function-like.  It is an error if anything appears on the line
2054 after the macro name.  @samp{#undef} has no effect if the name is not a
2055 macro.
2056
2057 @example
2058 #define FOO 4
2059 x = FOO;        @expansion{} x = 4;
2060 #undef FOO
2061 x = FOO;        @expansion{} x = FOO;
2062 @end example
2063
2064 Once a macro has been undefined, that identifier may be @dfn{redefined}
2065 as a macro by a subsequent @samp{#define} directive.  The new definition
2066 need not have any resemblance to the old definition.
2067
2068 However, if an identifier which is currently a macro is redefined, then
2069 the new definition must be @dfn{effectively the same} as the old one.
2070 Two macro definitions are effectively the same if:
2071 @itemize @bullet
2072 @item Both are the same type of macro (object- or function-like).
2073 @item All the tokens of the replacement list are the same.
2074 @item If there are any parameters, they are the same.
2075 @item Whitespace appears in the same places in both.  It need not be
2076 exactly the same amount of whitespace, though.  Remember that comments
2077 count as whitespace.
2078 @end itemize
2079
2080 @noindent
2081 These definitions are effectively the same:
2082 @example
2083 #define FOUR (2 + 2)
2084 #define FOUR         (2    +    2)
2085 #define FOUR (2 /* two */ + 2)
2086 @end example
2087 @noindent
2088 but these are not:
2089 @example
2090 #define FOUR (2 + 2)
2091 #define FOUR ( 2+2 )
2092 #define FOUR (2 * 2)
2093 #define FOUR(score,and,seven,years,ago) (2 + 2)
2094 @end example
2095
2096 If a macro is redefined with a definition that is not effectively the
2097 same as the old one, the preprocessor issues a warning and changes the
2098 macro to use the new definition.  If the new definition is effectively
2099 the same, the redefinition is silently ignored.  This allows, for
2100 instance, two different headers to define a common macro.  The
2101 preprocessor will only complain if the definitions do not match.
2102
2103 @node Macro Pitfalls
2104 @section Macro Pitfalls
2105 @cindex problems with macros
2106 @cindex pitfalls of macros
2107
2108 In this section we describe some special rules that apply to macros and
2109 macro expansion, and point out certain cases in which the rules have
2110 counter-intuitive consequences that you must watch out for.
2111
2112 @menu
2113 * Misnesting::
2114 * Operator Precedence Problems::
2115 * Swallowing the Semicolon::
2116 * Duplication of Side Effects::
2117 * Self-Referential Macros::
2118 * Argument Prescan::
2119 * Newlines in Arguments::
2120 @end menu
2121
2122 @node Misnesting
2123 @subsection Misnesting
2124
2125 When a macro is called with arguments, the arguments are substituted
2126 into the macro body and the result is checked, together with the rest of
2127 the input file, for more macro calls.  It is possible to piece together
2128 a macro call coming partially from the macro body and partially from the
2129 arguments.  For example,
2130
2131 @example
2132 #define twice(x) (2*(x))
2133 #define call_with_1(x) x(1)
2134 call_with_1 (twice)
2135      @expansion{} twice(1)
2136      @expansion{} (2*(1))
2137 @end example
2138
2139 Macro definitions do not have to have balanced parentheses.  By writing
2140 an unbalanced open parenthesis in a macro body, it is possible to create
2141 a macro call that begins inside the macro body but ends outside of it.
2142 For example,
2143
2144 @example
2145 #define strange(file) fprintf (file, "%s %d",
2146 @dots{}
2147 strange(stderr) p, 35)
2148      @expansion{} fprintf (stderr, "%s %d", p, 35)
2149 @end example
2150
2151 The ability to piece together a macro call can be useful, but the use of
2152 unbalanced open parentheses in a macro body is just confusing, and
2153 should be avoided.
2154
2155 @node Operator Precedence Problems
2156 @subsection Operator Precedence Problems
2157 @cindex parentheses in macro bodies
2158
2159 You may have noticed that in most of the macro definition examples shown
2160 above, each occurrence of a macro argument name had parentheses around
2161 it.  In addition, another pair of parentheses usually surround the
2162 entire macro definition.  Here is why it is best to write macros that
2163 way.
2164
2165 Suppose you define a macro as follows,
2166
2167 @example
2168 #define ceil_div(x, y) (x + y - 1) / y
2169 @end example
2170
2171 @noindent
2172 whose purpose is to divide, rounding up.  (One use for this operation is
2173 to compute how many @code{int} objects are needed to hold a certain
2174 number of @code{char} objects.)  Then suppose it is used as follows:
2175
2176 @example
2177 a = ceil_div (b & c, sizeof (int));
2178      @expansion{} a = (b & c + sizeof (int) - 1) / sizeof (int);
2179 @end example
2180
2181 @noindent
2182 This does not do what is intended.  The operator-precedence rules of
2183 C make it equivalent to this:
2184
2185 @example
2186 a = (b & (c + sizeof (int) - 1)) / sizeof (int);
2187 @end example
2188
2189 @noindent
2190 What we want is this:
2191
2192 @example
2193 a = ((b & c) + sizeof (int) - 1)) / sizeof (int);
2194 @end example
2195
2196 @noindent
2197 Defining the macro as
2198
2199 @example
2200 #define ceil_div(x, y) ((x) + (y) - 1) / (y)
2201 @end example
2202
2203 @noindent
2204 provides the desired result.
2205
2206 Unintended grouping can result in another way.  Consider @code{sizeof
2207 ceil_div(1, 2)}.  That has the appearance of a C expression that would
2208 compute the size of the type of @code{ceil_div (1, 2)}, but in fact it
2209 means something very different.  Here is what it expands to:
2210
2211 @example
2212 sizeof ((1) + (2) - 1) / (2)
2213 @end example
2214
2215 @noindent
2216 This would take the size of an integer and divide it by two.  The
2217 precedence rules have put the division outside the @code{sizeof} when it
2218 was intended to be inside.
2219
2220 Parentheses around the entire macro definition prevent such problems.
2221 Here, then, is the recommended way to define @code{ceil_div}:
2222
2223 @example
2224 #define ceil_div(x, y) (((x) + (y) - 1) / (y))
2225 @end example
2226
2227 @node Swallowing the Semicolon
2228 @subsection Swallowing the Semicolon
2229 @cindex semicolons (after macro calls)
2230
2231 Often it is desirable to define a macro that expands into a compound
2232 statement.  Consider, for example, the following macro, that advances a
2233 pointer (the argument @code{p} says where to find it) across whitespace
2234 characters:
2235
2236 @example
2237 #define SKIP_SPACES(p, limit)  \
2238 @{ char *lim = (limit);         \
2239   while (p < lim) @{            \
2240     if (*p++ != ' ') @{         \
2241       p--; break; @}@}@}
2242 @end example
2243
2244 @noindent
2245 Here backslash-newline is used to split the macro definition, which must
2246 be a single logical line, so that it resembles the way such code would
2247 be laid out if not part of a macro definition.
2248
2249 A call to this macro might be @code{SKIP_SPACES (p, lim)}.  Strictly
2250 speaking, the call expands to a compound statement, which is a complete
2251 statement with no need for a semicolon to end it.  However, since it
2252 looks like a function call, it minimizes confusion if you can use it
2253 like a function call, writing a semicolon afterward, as in
2254 @code{SKIP_SPACES (p, lim);}
2255
2256 This can cause trouble before @code{else} statements, because the
2257 semicolon is actually a null statement.  Suppose you write
2258
2259 @example
2260 if (*p != 0)
2261   SKIP_SPACES (p, lim);
2262 else @dots{}
2263 @end example
2264
2265 @noindent
2266 The presence of two statements---the compound statement and a null
2267 statement---in between the @code{if} condition and the @code{else}
2268 makes invalid C code.
2269
2270 The definition of the macro @code{SKIP_SPACES} can be altered to solve
2271 this problem, using a @code{do @dots{} while} statement.  Here is how:
2272
2273 @example
2274 #define SKIP_SPACES(p, limit)     \
2275 do @{ char *lim = (limit);         \
2276      while (p < lim) @{            \
2277        if (*p++ != ' ') @{         \
2278          p--; break; @}@}@}          \
2279 while (0)
2280 @end example
2281
2282 Now @code{SKIP_SPACES (p, lim);} expands into
2283
2284 @example
2285 do @{@dots{}@} while (0);
2286 @end example
2287
2288 @noindent
2289 which is one statement.  The loop executes exactly once; most compilers
2290 generate no extra code for it.
2291
2292 @node Duplication of Side Effects
2293 @subsection Duplication of Side Effects
2294
2295 @cindex side effects (in macro arguments)
2296 @cindex unsafe macros
2297 Many C programs define a macro @code{min}, for ``minimum'', like this:
2298
2299 @example
2300 #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
2301 @end example
2302
2303 When you use this macro with an argument containing a side effect,
2304 as shown here,
2305
2306 @example
2307 next = min (x + y, foo (z));
2308 @end example
2309
2310 @noindent
2311 it expands as follows:
2312
2313 @example
2314 next = ((x + y) < (foo (z)) ? (x + y) : (foo (z)));
2315 @end example
2316
2317 @noindent
2318 where @code{x + y} has been substituted for @code{X} and @code{foo (z)}
2319 for @code{Y}.
2320
2321 The function @code{foo} is used only once in the statement as it appears
2322 in the program, but the expression @code{foo (z)} has been substituted
2323 twice into the macro expansion.  As a result, @code{foo} might be called
2324 two times when the statement is executed.  If it has side effects or if
2325 it takes a long time to compute, the results might not be what you
2326 intended.  We say that @code{min} is an @dfn{unsafe} macro.
2327
2328 The best solution to this problem is to define @code{min} in a way that
2329 computes the value of @code{foo (z)} only once.  The C language offers
2330 no standard way to do this, but it can be done with GNU extensions as
2331 follows:
2332
2333 @example
2334 #define min(X, Y)                \
2335 (@{ typeof (X) x_ = (X);          \
2336    typeof (Y) y_ = (Y);          \
2337    (x_ < y_) ? x_ : y_; @})
2338 @end example
2339
2340 The @samp{(@{ @dots{} @})} notation produces a compound statement that
2341 acts as an expression.  Its value is the value of its last statement.
2342 This permits us to define local variables and assign each argument to
2343 one.  The local variables have underscores after their names to reduce
2344 the risk of conflict with an identifier of wider scope (it is impossible
2345 to avoid this entirely).  Now each argument is evaluated exactly once.
2346
2347 If you do not wish to use GNU C extensions, the only solution is to be
2348 careful when @emph{using} the macro @code{min}.  For example, you can
2349 calculate the value of @code{foo (z)}, save it in a variable, and use
2350 that variable in @code{min}:
2351
2352 @example
2353 @group
2354 #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
2355 @dots{}
2356 @{
2357   int tem = foo (z);
2358   next = min (x + y, tem);
2359 @}
2360 @end group
2361 @end example
2362
2363 @noindent
2364 (where we assume that @code{foo} returns type @code{int}).
2365
2366 @node Self-Referential Macros
2367 @subsection Self-Referential Macros
2368 @cindex self-reference
2369
2370 A @dfn{self-referential} macro is one whose name appears in its
2371 definition.  Recall that all macro definitions are rescanned for more
2372 macros to replace.  If the self-reference were considered a use of the
2373 macro, it would produce an infinitely large expansion.  To prevent this,
2374 the self-reference is not considered a macro call.  It is passed into
2375 the preprocessor output unchanged.  Let's consider an example:
2376
2377 @example
2378 #define foo (4 + foo)
2379 @end example
2380
2381 @noindent
2382 where @code{foo} is also a variable in your program.
2383
2384 Following the ordinary rules, each reference to @code{foo} will expand
2385 into @code{(4 + foo)}; then this will be rescanned and will expand into
2386 @code{(4 + (4 + foo))}; and so on until the computer runs out of memory.
2387
2388 The self-reference rule cuts this process short after one step, at
2389 @code{(4 + foo)}.  Therefore, this macro definition has the possibly
2390 useful effect of causing the program to add 4 to the value of @code{foo}
2391 wherever @code{foo} is referred to.
2392
2393 In most cases, it is a bad idea to take advantage of this feature.  A
2394 person reading the program who sees that @code{foo} is a variable will
2395 not expect that it is a macro as well.  The reader will come across the
2396 identifier @code{foo} in the program and think its value should be that
2397 of the variable @code{foo}, whereas in fact the value is four greater.
2398
2399 One common, useful use of self-reference is to create a macro which
2400 expands to itself.  If you write
2401
2402 @example
2403 #define EPERM EPERM
2404 @end example
2405
2406 @noindent
2407 then the macro @code{EPERM} expands to @code{EPERM}.  Effectively, it is
2408 left alone by the preprocessor whenever it's used in running text.  You
2409 can tell that it's a macro with @samp{#ifdef}.  You might do this if you
2410 want to define numeric constants with an @code{enum}, but have
2411 @samp{#ifdef} be true for each constant.
2412
2413 If a macro @code{x} expands to use a macro @code{y}, and the expansion of
2414 @code{y} refers to the macro @code{x}, that is an @dfn{indirect
2415 self-reference} of @code{x}.  @code{x} is not expanded in this case
2416 either.  Thus, if we have
2417
2418 @example
2419 #define x (4 + y)
2420 #define y (2 * x)
2421 @end example
2422
2423 @noindent
2424 then @code{x} and @code{y} expand as follows:
2425
2426 @example
2427 @group
2428 x    @expansion{} (4 + y)
2429      @expansion{} (4 + (2 * x))
2430
2431 y    @expansion{} (2 * x)
2432      @expansion{} (2 * (4 + y))
2433 @end group
2434 @end example
2435
2436 @noindent
2437 Each macro is expanded when it appears in the definition of the other
2438 macro, but not when it indirectly appears in its own definition.
2439
2440 @node Argument Prescan
2441 @subsection Argument Prescan
2442 @cindex expansion of arguments
2443 @cindex macro argument expansion
2444 @cindex prescan of macro arguments
2445
2446 Macro arguments are completely macro-expanded before they are
2447 substituted into a macro body, unless they are stringified or pasted
2448 with other tokens.  After substitution, the entire macro body, including
2449 the substituted arguments, is scanned again for macros to be expanded.
2450 The result is that the arguments are scanned @emph{twice} to expand
2451 macro calls in them.
2452
2453 Most of the time, this has no effect.  If the argument contained any
2454 macro calls, they are expanded during the first scan.  The result
2455 therefore contains no macro calls, so the second scan does not change
2456 it.  If the argument were substituted as given, with no prescan, the
2457 single remaining scan would find the same macro calls and produce the
2458 same results.
2459
2460 You might expect the double scan to change the results when a
2461 self-referential macro is used in an argument of another macro
2462 (@pxref{Self-Referential Macros}): the self-referential macro would be
2463 expanded once in the first scan, and a second time in the second scan.
2464 However, this is not what happens.  The self-references that do not
2465 expand in the first scan are marked so that they will not expand in the
2466 second scan either.
2467
2468 You might wonder, ``Why mention the prescan, if it makes no difference?
2469 And why not skip it and make the preprocessor faster?''  The answer is
2470 that the prescan does make a difference in three special cases:
2471
2472 @itemize @bullet
2473 @item
2474 Nested calls to a macro.
2475
2476 We say that @dfn{nested} calls to a macro occur when a macro's argument
2477 contains a call to that very macro.  For example, if @code{f} is a macro
2478 that expects one argument, @code{f (f (1))} is a nested pair of calls to
2479 @code{f}.  The desired expansion is made by expanding @code{f (1)} and
2480 substituting that into the definition of @code{f}.  The prescan causes
2481 the expected result to happen.  Without the prescan, @code{f (1)} itself
2482 would be substituted as an argument, and the inner use of @code{f} would
2483 appear during the main scan as an indirect self-reference and would not
2484 be expanded.
2485
2486 @item
2487 Macros that call other macros that stringify or concatenate.
2488
2489 If an argument is stringified or concatenated, the prescan does not
2490 occur.  If you @emph{want} to expand a macro, then stringify or
2491 concatenate its expansion, you can do that by causing one macro to call
2492 another macro that does the stringification or concatenation.  For
2493 instance, if you have
2494
2495 @example
2496 #define AFTERX(x) X_ ## x
2497 #define XAFTERX(x) AFTERX(x)
2498 #define TABLESIZE 1024
2499 #define BUFSIZE TABLESIZE
2500 @end example
2501
2502 then @code{AFTERX(BUFSIZE)} expands to @code{X_BUFSIZE}, and
2503 @code{XAFTERX(BUFSIZE)} expands to @code{X_1024}.  (Not to
2504 @code{X_TABLESIZE}.  Prescan always does a complete expansion.)
2505
2506 @item
2507 Macros used in arguments, whose expansions contain unshielded commas.
2508
2509 This can cause a macro expanded on the second scan to be called with the
2510 wrong number of arguments.  Here is an example:
2511
2512 @example
2513 #define foo  a,b
2514 #define bar(x) lose(x)
2515 #define lose(x) (1 + (x))
2516 @end example
2517
2518 We would like @code{bar(foo)} to turn into @code{(1 + (foo))}, which
2519 would then turn into @code{(1 + (a,b))}.  Instead, @code{bar(foo)}
2520 expands into @code{lose(a,b)}, and you get an error because @code{lose}
2521 requires a single argument.  In this case, the problem is easily solved
2522 by the same parentheses that ought to be used to prevent misnesting of
2523 arithmetic operations:
2524
2525 @example
2526 #define foo (a,b)
2527 @exdent or
2528 #define bar(x) lose((x))
2529 @end example
2530
2531 The extra pair of parentheses prevents the comma in @code{foo}'s
2532 definition from being interpreted as an argument separator.
2533
2534 @end itemize
2535
2536 @node Newlines in Arguments
2537 @subsection Newlines in Arguments
2538 @cindex newlines in macro arguments
2539
2540 The invocation of a function-like macro can extend over many logical
2541 lines.  However, in the present implementation, the entire expansion
2542 comes out on one line.  Thus line numbers emitted by the compiler or
2543 debugger refer to the line the invocation started on, which might be
2544 different to the line containing the argument causing the problem.
2545
2546 Here is an example illustrating this:
2547
2548 @example
2549 #define ignore_second_arg(a,b,c) a; c
2550
2551 ignore_second_arg (foo (),
2552                    ignored (),
2553                    syntax error);
2554 @end example
2555
2556 @noindent
2557 The syntax error triggered by the tokens @code{syntax error} results in
2558 an error message citing line three---the line of ignore_second_arg---
2559 even though the problematic code comes from line five.
2560
2561 We consider this a bug, and intend to fix it in the near future.
2562
2563 @node Conditionals
2564 @chapter Conditionals
2565 @cindex conditionals
2566
2567 A @dfn{conditional} is a directive that instructs the preprocessor to
2568 select whether or not to include a chunk of code in the final token
2569 stream passed to the compiler.  Preprocessor conditionals can test
2570 arithmetic expressions, or whether a name is defined as a macro, or both
2571 simultaneously using the special @code{defined} operator.
2572
2573 A conditional in the C preprocessor resembles in some ways an @code{if}
2574 statement in C, but it is important to understand the difference between
2575 them.  The condition in an @code{if} statement is tested during the
2576 execution of your program.  Its purpose is to allow your program to
2577 behave differently from run to run, depending on the data it is
2578 operating on.  The condition in a preprocessing conditional directive is
2579 tested when your program is compiled.  Its purpose is to allow different
2580 code to be included in the program depending on the situation at the
2581 time of compilation.
2582
2583 However, the distinction is becoming less clear.  Modern compilers often
2584 do test @code{if} statements when a program is compiled, if their
2585 conditions are known not to vary at run time, and eliminate code which
2586 can never be executed.  If you can count on your compiler to do this,
2587 you may find that your program is more readable if you use @code{if}
2588 statements with constant conditions (perhaps determined by macros).  Of
2589 course, you can only use this to exclude code, not type definitions or
2590 other preprocessing directives, and you can only do it if the code
2591 remains syntactically valid when it is not to be used.
2592
2593 GCC version 3 eliminates this kind of never-executed code even when
2594 not optimizing.  Older versions did it only when optimizing.
2595
2596 @menu
2597 * Conditional Uses::
2598 * Conditional Syntax::
2599 * Deleted Code::
2600 @end menu
2601
2602 @node Conditional Uses
2603 @section Conditional Uses
2604
2605 There are three general reasons to use a conditional.
2606
2607 @itemize @bullet
2608 @item
2609 A program may need to use different code depending on the machine or
2610 operating system it is to run on.  In some cases the code for one
2611 operating system may be erroneous on another operating system; for
2612 example, it might refer to data types or constants that do not exist on
2613 the other system.  When this happens, it is not enough to avoid
2614 executing the invalid code.  Its mere presence will cause the compiler
2615 to reject the program.  With a preprocessing conditional, the offending
2616 code can be effectively excised from the program when it is not valid.
2617
2618 @item
2619 You may want to be able to compile the same source file into two
2620 different programs.  One version might make frequent time-consuming
2621 consistency checks on its intermediate data, or print the values of
2622 those data for debugging, and the other not.
2623
2624 @item
2625 A conditional whose condition is always false is one way to exclude code
2626 from the program but keep it as a sort of comment for future reference.
2627 @end itemize
2628
2629 Simple programs that do not need system-specific logic or complex
2630 debugging hooks generally will not need to use preprocessing
2631 conditionals.
2632
2633 @node Conditional Syntax
2634 @section Conditional Syntax
2635
2636 @findex #if
2637 A conditional in the C preprocessor begins with a @dfn{conditional
2638 directive}: @samp{#if}, @samp{#ifdef} or @samp{#ifndef}.
2639
2640 @menu
2641 * Ifdef::
2642 * If::
2643 * Defined::
2644 * Else::
2645 * Elif::
2646 @end menu
2647
2648 @node Ifdef
2649 @subsection Ifdef
2650 @findex #ifdef
2651 @findex #endif
2652
2653 The simplest sort of conditional is
2654
2655 @example
2656 @group
2657 #ifdef @var{MACRO}
2658
2659 @var{controlled text}
2660
2661 #endif /* @var{MACRO} */
2662 @end group
2663 @end example
2664
2665 @cindex conditional group
2666 This block is called a @dfn{conditional group}.  @var{controlled text}
2667 will be included in the output of the preprocessor if and only if
2668 @var{MACRO} is defined.  We say that the conditional @dfn{succeeds} if
2669 @var{MACRO} is defined, @dfn{fails} if it is not.
2670
2671 The @var{controlled text} inside of a conditional can include
2672 preprocessing directives.  They are executed only if the conditional
2673 succeeds.  You can nest conditional groups inside other conditional
2674 groups, but they must be completely nested.  In other words,
2675 @samp{#endif} always matches the nearest @samp{#ifdef} (or
2676 @samp{#ifndef}, or @samp{#if}).  Also, you cannot start a conditional
2677 group in one file and end it in another.
2678
2679 Even if a conditional fails, the @var{controlled text} inside it is
2680 still run through initial transformations and tokenization.  Therefore,
2681 it must all be lexically valid C@.  Normally the only way this matters is
2682 that all comments and string literals inside a failing conditional group
2683 must still be properly ended.
2684
2685 The comment following the @samp{#endif} is not required, but it is a
2686 good practice if there is a lot of @var{controlled text}, because it
2687 helps people match the @samp{#endif} to the corresponding @samp{#ifdef}.
2688 Older programs sometimes put @var{MACRO} directly after the
2689 @samp{#endif} without enclosing it in a comment.  This is invalid code
2690 according to the C standard.  GNU CPP accepts it with a warning.  It
2691 never affects which @samp{#ifndef} the @samp{#endif} matches.
2692
2693 @findex #ifndef
2694 Sometimes you wish to use some code if a macro is @emph{not} defined.
2695 You can do this by writing @samp{#ifndef} instead of @samp{#ifdef}.
2696 One common use of @samp{#ifndef} is to include code only the first
2697 time a header file is included.  @xref{Once-Only Headers}.
2698
2699 Macro definitions can vary between compilations for several reasons.
2700 Here are some samples.
2701
2702 @itemize @bullet
2703 @item
2704 Some macros are predefined on each kind of machine
2705 (@pxref{System-specific Predefined Macros}).  This allows you to provide
2706 code specially tuned for a particular machine.
2707
2708 @item
2709 System header files define more macros, associated with the features
2710 they implement.  You can test these macros with conditionals to avoid
2711 using a system feature on a machine where it is not implemented.
2712
2713 @item
2714 Macros can be defined or undefined with the @option{-D} and @option{-U}
2715 command line options when you compile the program.  You can arrange to
2716 compile the same source file into two different programs by choosing a
2717 macro name to specify which program you want, writing conditionals to
2718 test whether or how this macro is defined, and then controlling the
2719 state of the macro with command line options, perhaps set in the
2720 Makefile.  @xref{Invocation}.
2721
2722 @item
2723 Your program might have a special header file (often called
2724 @file{config.h}) that is adjusted when the program is compiled.  It can
2725 define or not define macros depending on the features of the system and
2726 the desired capabilities of the program.  The adjustment can be
2727 automated by a tool such as @command{autoconf}, or done by hand.
2728 @end itemize
2729
2730 @node If
2731 @subsection If
2732
2733 The @samp{#if} directive allows you to test the value of an arithmetic
2734 expression, rather than the mere existence of one macro.  Its syntax is
2735
2736 @example
2737 @group
2738 #if @var{expression}
2739
2740 @var{controlled text}
2741
2742 #endif /* @var{expression} */
2743 @end group
2744 @end example
2745
2746 @var{expression} is a C expression of integer type, subject to stringent
2747 restrictions.  It may contain
2748
2749 @itemize @bullet
2750 @item
2751 Integer constants.
2752
2753 @item
2754 Character constants, which are interpreted as they would be in normal
2755 code.
2756
2757 @item
2758 Arithmetic operators for addition, subtraction, multiplication,
2759 division, bitwise operations, shifts, comparisons, and logical
2760 operations (@code{&&} and @code{||}).  The latter two obey the usual
2761 short-circuiting rules of standard C@.
2762
2763 @item
2764 Macros.  All macros in the expression are expanded before actual
2765 computation of the expression's value begins.
2766
2767 @item
2768 Uses of the @code{defined} operator, which lets you check whether macros
2769 are defined in the middle of an @samp{#if}.
2770
2771 @item
2772 Identifiers that are not macros, which are all considered to be the
2773 number zero.  This allows you to write @code{@w{#if MACRO}} instead of
2774 @code{@w{#ifdef MACRO}}, if you know that MACRO, when defined, will
2775 always have a nonzero value.  Function-like macros used without their
2776 function call parentheses are also treated as zero.
2777
2778 In some contexts this shortcut is undesirable.  The @option{-Wundef}
2779 option causes GCC to warn whenever it encounters an identifier which is
2780 not a macro in an @samp{#if}.
2781 @end itemize
2782
2783 The preprocessor does not know anything about types in the language.
2784 Therefore, @code{sizeof} operators are not recognized in @samp{#if}, and
2785 neither are @code{enum} constants.  They will be taken as identifiers
2786 which are not macros, and replaced by zero.  In the case of
2787 @code{sizeof}, this is likely to cause the expression to be invalid.
2788
2789 The preprocessor calculates the value of @var{expression}.  It carries
2790 out all calculations in the widest integer type known to the compiler;
2791 on most machines supported by GCC this is 64 bits.  This is not the same
2792 rule as the compiler uses to calculate the value of a constant
2793 expression, and may give different results in some cases.  If the value
2794 comes out to be nonzero, the @samp{#if} succeeds and the @var{controlled
2795 text} is included; otherwise it is skipped.
2796
2797 If @var{expression} is not correctly formed, GCC issues an error and
2798 treats the conditional as having failed.
2799
2800 @node Defined
2801 @subsection Defined
2802
2803 @cindex @code{defined}
2804 The special operator @code{defined} is used in @samp{#if} and
2805 @samp{#elif} expressions to test whether a certain name is defined as a
2806 macro.  @code{defined @var{name}} and @code{defined (@var{name})} are
2807 both expressions whose value is 1 if @var{name} is defined as a macro at
2808 the current point in the program, and 0 otherwise.  Thus,  @code{@w{#if
2809 defined MACRO}} is precisely equivalent to @code{@w{#ifdef MACRO}}.
2810
2811 @code{defined} is useful when you wish to test more than one macro for
2812 existence at once.  For example,
2813
2814 @example
2815 #if defined (__vax__) || defined (__ns16000__)
2816 @end example
2817
2818 @noindent
2819 would succeed if either of the names @code{__vax__} or
2820 @code{__ns16000__} is defined as a macro.
2821
2822 Conditionals written like this:
2823
2824 @example
2825 #if defined BUFSIZE && BUFSIZE >= 1024
2826 @end example
2827
2828 @noindent
2829 can generally be simplified to just @code{@w{#if BUFSIZE >= 1024}},
2830 since if @code{BUFSIZE} is not defined, it will be interpreted as having
2831 the value zero.
2832
2833 If the @code{defined} operator appears as a result of a macro expansion,
2834 the C standard says the behavior is undefined.  GNU cpp treats it as a
2835 genuine @code{defined} operator and evaluates it normally.  It will warn
2836 wherever your code uses this feature if you use the command-line option
2837 @option{-pedantic}, since other compilers may handle it differently.
2838
2839 @node Else
2840 @subsection Else
2841
2842 @findex #else
2843 The @samp{#else} directive can be added to a conditional to provide
2844 alternative text to be used if the condition fails.  This is what it
2845 looks like:
2846
2847 @example
2848 @group
2849 #if @var{expression}
2850 @var{text-if-true}
2851 #else /* Not @var{expression} */
2852 @var{text-if-false}
2853 #endif /* Not @var{expression} */
2854 @end group
2855 @end example
2856
2857 @noindent
2858 If @var{expression} is nonzero, the @var{text-if-true} is included and
2859 the @var{text-if-false} is skipped.  If @var{expression} is zero, the
2860 opposite happens.
2861
2862 You can use @samp{#else} with @samp{#ifdef} and @samp{#ifndef}, too.
2863
2864 @node Elif
2865 @subsection Elif
2866
2867 @findex #elif
2868 One common case of nested conditionals is used to check for more than two
2869 possible alternatives.  For example, you might have
2870
2871 @example
2872 #if X == 1
2873 @dots{}
2874 #else /* X != 1 */
2875 #if X == 2
2876 @dots{}
2877 #else /* X != 2 */
2878 @dots{}
2879 #endif /* X != 2 */
2880 #endif /* X != 1 */
2881 @end example
2882
2883 Another conditional directive, @samp{#elif}, allows this to be
2884 abbreviated as follows:
2885
2886 @example
2887 #if X == 1
2888 @dots{}
2889 #elif X == 2
2890 @dots{}
2891 #else /* X != 2 and X != 1*/
2892 @dots{}
2893 #endif /* X != 2 and X != 1*/
2894 @end example
2895
2896 @samp{#elif} stands for ``else if''.  Like @samp{#else}, it goes in the
2897 middle of a conditional group and subdivides it; it does not require a
2898 matching @samp{#endif} of its own.  Like @samp{#if}, the @samp{#elif}
2899 directive includes an expression to be tested.  The text following the
2900 @samp{#elif} is processed only if the original @samp{#if}-condition
2901 failed and the @samp{#elif} condition succeeds.
2902
2903 More than one @samp{#elif} can go in the same conditional group.  Then
2904 the text after each @samp{#elif} is processed only if the @samp{#elif}
2905 condition succeeds after the original @samp{#if} and all previous
2906 @samp{#elif} directives within it have failed.
2907
2908 @samp{#else} is allowed after any number of @samp{#elif} directives, but
2909 @samp{#elif} may not follow @samp{#else}.
2910
2911 @node Deleted Code
2912 @section Deleted Code
2913 @cindex commenting out code
2914
2915 If you replace or delete a part of the program but want to keep the old
2916 code around for future reference, you often cannot simply comment it
2917 out.  Block comments do not nest, so the first comment inside the old
2918 code will end the commenting-out.  The probable result is a flood of
2919 syntax errors.
2920
2921 One way to avoid this problem is to use an always-false conditional
2922 instead.  For instance, put @code{#if 0} before the deleted code and
2923 @code{#endif} after it.  This This works even if the code being turned
2924 off contains conditionals, but they must be entire conditionals
2925 (balanced @samp{#if} and @samp{#endif}).
2926
2927 Some people use @code{#ifdef notdef} instead.  This is risky, because
2928 @code{notdef} might be accidentally defined as a macro, and then the
2929 conditional would succeed.  @code{#if 0} can be counted on to fail.
2930
2931 Do not use @code{#if 0} for comments which are not C code.  Use a real
2932 comment, instead.  The interior of @code{#if 0} must consist of complete
2933 tokens; in particular, single-quote characters must balance.  Comments
2934 often contain unbalanced single-quote characters (known in English as
2935 apostrophes).  These confuse @code{#if 0}.  They don't confuse
2936 @samp{/*}.
2937
2938 @node Diagnostics
2939 @chapter Diagnostics
2940 @cindex diagnostic
2941 @cindex reporting errors
2942 @cindex reporting warnings
2943
2944 @findex #error
2945 The directive @samp{#error} causes the preprocessor to report a fatal
2946 error.  The tokens forming the rest of the line following @samp{#error}
2947 are used as the error message.
2948
2949 You would use @samp{#error} inside of a conditional that detects a
2950 combination of parameters which you know the program does not properly
2951 support.  For example, if you know that the program will not run
2952 properly on a Vax, you might write
2953
2954 @example
2955 @group
2956 #ifdef __vax__
2957 #error "Won't work on Vaxen.  See comments at get_last_object."
2958 #endif
2959 @end group
2960 @end example
2961
2962 If you have several configuration parameters that must be set up by
2963 the installation in a consistent way, you can use conditionals to detect
2964 an inconsistency and report it with @samp{#error}.  For example,
2965
2966 @example
2967 #if !defined(UNALIGNED_INT_ASM_OP) && defined(DWARF2_DEBUGGING_INFO)
2968 #error "DWARF2_DEBUGGING_INFO requires UNALIGNED_INT_ASM_OP."
2969 #endif
2970 @end example
2971
2972 @findex #warning
2973 The directive @samp{#warning} is like @samp{#error}, but causes the
2974 preprocessor to issue a warning and continue preprocessing.  The tokens
2975 following @samp{#warning} are used as the warning message.
2976
2977 You might use @samp{#warning} in obsolete header files, with a message
2978 directing the user to the header file which should be used instead.
2979
2980 Neither @samp{#error} nor @samp{#warning} macro-expands its argument.
2981 Internal whitespace sequences are each replaced with a single space.
2982 The line must consist of complete tokens.  It is wisest to make the
2983 argument of these directives be a single string constant; this avoids
2984 problems with apostrophes and the like.
2985
2986 @node Line Control
2987 @chapter Line Control
2988 @cindex line control
2989
2990 The C preprocessor informs the C compiler of the location in your source
2991 code where each token came from.  Presently, this is just the file name
2992 and line number.  All the tokens resulting from macro expansion are
2993 reported as having appeared on the line of the source file where the
2994 outermost macro was used.  We intend to be more accurate in the future.
2995
2996 If you write a program which generates source code, such as the
2997 @command{bison} parser generator, you may want to adjust the preprocessor's
2998 notion of the current file name and line number by hand.  Parts of the
2999 output from @command{bison} are generated from scratch, other parts come
3000 from a standard parser file.  The rest are copied verbatim from
3001 @command{bison}'s input.  You would like compiler error messages and
3002 symbolic debuggers to be able to refer to @code{bison}'s input file.
3003
3004 @findex #line
3005 @command{bison} or any such program can arrange this by writing
3006 @samp{#line} directives into the output file.  @samp{#line} is a
3007 directive that specifies the original line number and source file name
3008 for subsequent input in the current preprocessor input file.
3009 @samp{#line} has three variants:
3010
3011 @table @code
3012 @item #line @var{linenum}
3013 @var{linenum} is a non-negative decimal integer constant.  It specifies
3014 the line number which should be reported for the following line of
3015 input.  Subsequent lines are counted from @var{linenum}.
3016
3017 @item #line @var{linenum} @var{filename}
3018 @var{linenum} is the same as for the first form, and has the same
3019 effect.  In addition, @var{filename} is a string constant.  The
3020 following line and all subsequent lines are reported to come from the
3021 file it specifies, until something else happens to change that.
3022
3023 @item #line @var{anything else}
3024 @var{anything else} is checked for macro calls, which are expanded.
3025 The result should match one of the above two forms.
3026 @end table
3027
3028 @samp{#line} directives alter the results of the @code{__FILE__} and
3029 @code{__LINE__} predefined macros from that point on.  @xref{Standard
3030 Predefined Macros}.  They do not have any effect on @samp{#include}'s
3031 idea of the directory containing the current file.
3032
3033 @node Pragmas
3034 @chapter Pragmas
3035
3036 The @samp{#pragma} directive is the method specified by the C standard
3037 for providing additional information to the compiler, beyond what is
3038 conveyed in the language itself.  Three forms of this directive
3039 (commonly known as @dfn{pragmas}) are specified by the 1999 C standard.
3040 A C compiler is free to attach any meaning it likes to other pragmas.
3041
3042 GCC has historically preferred to use extensions to the syntax of the
3043 language, such as @code{__attribute__}, for this purpose.  However, GCC
3044 does define a few pragmas of its own.  These mostly have effects on the
3045 entire translation unit or source file.
3046
3047 In GCC version 3, all GNU-defined, supported pragmas have been given a
3048 @code{GCC} prefix.  This is in line with the @code{STDC} prefix on all
3049 pragmas defined by C99.  For backward compatibility, pragmas which were
3050 recognized by previous versions are still recognized without the
3051 @code{GCC} prefix, but that usage is deprecated.  Some older pragmas are
3052 deprecated in their entirety.  They are not recognized with the
3053 @code{GCC} prefix.  @xref{Obsolete Features}.
3054
3055 @cindex @code{_Pragma}
3056 C99 introduces the @code{@w{_Pragma}} operator.  This feature addresses a
3057 major problem with @samp{#pragma}: being a directive, it cannot be
3058 produced as the result of macro expansion.  @code{@w{_Pragma}} is an
3059 operator, much like @code{sizeof} or @code{defined}, and can be embedded
3060 in a macro.
3061
3062 Its syntax is @code{@w{_Pragma (@var{string-literal})}}, where
3063 @var{string-literal} can be either a normal or wide-character string
3064 literal.  It is destringized, by replacing all @samp{\\} with a single
3065 @samp{\} and all @samp{\"} with a @samp{"}.  The result is then
3066 processed as if it had appeared as the right hand side of a
3067 @samp{#pragma} directive.  For example,
3068
3069 @example
3070 _Pragma ("GCC dependency \"parse.y\"")
3071 @end example
3072
3073 @noindent
3074 has the same effect as @code{#pragma GCC dependency "parse.y"}.  The
3075 same effect could be achieved using macros, for example
3076
3077 @example
3078 #define DO_PRAGMA(x) _Pragma (#x)
3079 DO_PRAGMA (GCC dependency "parse.y")
3080 @end example
3081
3082 The standard is unclear on where a @code{_Pragma} operator can appear.
3083 The preprocessor does not accept it within a preprocessing conditional
3084 directive like @samp{#if}.  To be safe, you are probably best keeping it
3085 out of directives other than @samp{#define}, and putting it on a line of
3086 its own.
3087
3088 This manual documents the pragmas which are meaningful to the
3089 preprocessor itself.  Other pragmas are meaningful to the C or C++
3090 compilers.  They are documented in the GCC manual.
3091
3092 @ftable @code
3093 @item #pragma GCC dependency
3094 @code{#pragma GCC dependency} allows you to check the relative dates of
3095 the current file and another file.  If the other file is more recent than
3096 the current file, a warning is issued.  This is useful if the current
3097 file is derived from the other file, and should be regenerated.  The
3098 other file is searched for using the normal include search path.
3099 Optional trailing text can be used to give more information in the
3100 warning message.
3101
3102 @example
3103 #pragma GCC dependency "parse.y"
3104 #pragma GCC dependency "/usr/include/time.h" rerun fixincludes
3105 @end example
3106
3107 @item #pragma GCC poison
3108 Sometimes, there is an identifier that you want to remove completely
3109 from your program, and make sure that it never creeps back in.  To
3110 enforce this, you can @dfn{poison} the identifier with this pragma.
3111 @code{#pragma GCC poison} is followed by a list of identifiers to
3112 poison.  If any of those identifiers appears anywhere in the source
3113 after the directive, it is a hard error.  For example,
3114
3115 @example
3116 #pragma GCC poison printf sprintf fprintf
3117 sprintf(some_string, "hello");
3118 @end example
3119
3120 @noindent
3121 will produce an error.
3122
3123 If a poisoned identifier appears as part of the expansion of a macro
3124 which was defined before the identifier was poisoned, it will @emph{not}
3125 cause an error.  This lets you poison an identifier without worrying
3126 about system headers defining macros that use it.
3127
3128 For example,
3129
3130 @example
3131 #define strrchr rindex
3132 #pragma GCC poison rindex
3133 strrchr(some_string, 'h');
3134 @end example
3135
3136 @noindent
3137 will not produce an error.
3138
3139 @item #pragma GCC system_header
3140 This pragma takes no arguments.  It causes the rest of the code in the
3141 current file to be treated as if it came from a system header.
3142 @xref{System Headers}.
3143
3144 @end ftable
3145
3146 @node Other Directives
3147 @chapter Other Directives
3148
3149 @findex #ident
3150 The @samp{#ident} directive takes one argument, a string constant.  On
3151 some systems, that string constant is copied into a special segment of
3152 the object file.  On other systems, the directive is ignored.
3153
3154 This directive is not part of the C standard, but it is not an official
3155 GNU extension either.  We believe it came from System V@.
3156
3157 @findex #sccs
3158 The @samp{#sccs} directive is recognized on some systems, because it
3159 appears in their header files.  It is a very old, obscure, extension
3160 which we did not invent, and we have been unable to find any
3161 documentation of what it should do, so GCC simply ignores it.
3162
3163 @cindex null directive
3164 The @dfn{null directive} consists of a @samp{#} followed by a newline,
3165 with only whitespace (including comments) in between.  A null directive
3166 is understood as a preprocessing directive but has no effect on the
3167 preprocessor output.  The primary significance of the existence of the
3168 null directive is that an input line consisting of just a @samp{#} will
3169 produce no output, rather than a line of output containing just a
3170 @samp{#}.  Supposedly some old C programs contain such lines.
3171
3172 @node Preprocessor Output
3173 @chapter Preprocessor Output
3174
3175 When the C preprocessor is used with the C, C++, or Objective-C
3176 compilers, it is integrated into the compiler and communicates a stream
3177 of binary tokens directly to the compiler's parser.  However, it can
3178 also be used in the more conventional standalone mode, where it produces
3179 textual output.
3180 @c FIXME: Document the library interface.
3181
3182 @cindex output format
3183 The output from the C preprocessor looks much like the input, except
3184 that all preprocessing directive lines have been replaced with blank
3185 lines and all comments with spaces.  Long runs of blank lines are
3186 discarded.
3187
3188 The ISO standard specifies that it is implementation defined whether a
3189 preprocessor preserves whitespace between tokens, or replaces it with
3190 e.g.@: a single space.  In GNU CPP, whitespace between tokens is collapsed
3191 to become a single space, with the exception that the first token on a
3192 non-directive line is preceded with sufficient spaces that it appears in
3193 the same column in the preprocessed output that it appeared in in the
3194 original source file.  This is so the output is easy to read.
3195 @xref{Differences from previous versions}.  CPP does not insert any
3196 whitespace where there was none in the original source, except where
3197 necessary to prevent an accidental token paste.
3198
3199 @cindex linemarkers
3200 Source file name and line number information is conveyed by lines
3201 of the form
3202
3203 @example
3204 # @var{linenum} @var{filename} @var{flags}
3205 @end example
3206
3207 @noindent
3208 These are called @dfn{linemarkers}.  They are inserted as needed into
3209 the output (but never within a string or character constant).  They mean
3210 that the following line originated in file @var{filename} at line
3211 @var{linenum}.
3212
3213 After the file name comes zero or more flags, which are @samp{1},
3214 @samp{2}, @samp{3}, or @samp{4}.  If there are multiple flags, spaces
3215 separate them.  Here is what the flags mean:
3216
3217 @table @samp
3218 @item 1
3219 This indicates the start of a new file.
3220 @item 2
3221 This indicates returning to a file (after having included another file).
3222 @item 3
3223 This indicates that the following text comes from a system header file,
3224 so certain warnings should be suppressed.
3225 @item 4
3226 This indicates that the following text should be treated as being
3227 wrapped in an implicit @code{extern "C"} block.
3228 @c maybe cross reference NO_IMPLICIT_EXTERN_C
3229 @end table
3230
3231 As an extension, the preprocessor accepts linemarkers in non-assembler
3232 input files.  They are treated like the corresponding @samp{#line}
3233 directive, (@pxref{Line Control}), except that trailing flags are
3234 permitted, and are interpreted with the meanings described above.  If
3235 multiple flags are given, they must be in ascending order.
3236
3237 Some directives may be duplicated in the output of the preprocessor.
3238 These are @samp{#ident} (always), @samp{#pragma} (only if the
3239 preprocessor does not handle the pragma itself), and @samp{#define} and
3240 @samp{#undef} (with certain debugging options).  If this happens, the
3241 @samp{#} of the directive will always be in the first column, and there
3242 will be no space between the @samp{#} and the directive name.  If macro
3243 expansion happens to generate tokens which might be mistaken for a
3244 duplicated directive, a space will be inserted between the @samp{#} and
3245 the directive name.
3246
3247 @node Traditional Mode
3248 @chapter Traditional Mode
3249
3250 Traditional (pre-standard) C preprocessing is rather different from
3251 the preprocessing specified by the standard.  When GCC is given the
3252 @option{-traditional} option, it attempts to emulate a traditional
3253 preprocessor.  We do not guarantee that GCC's behavior under
3254 @option{-traditional} matches any pre-standard preprocessor exactly.
3255
3256 Traditional mode exists only for backward compatibility.  We have no
3257 plans to augment it in any way nor will we change it except to fix
3258 catastrophic bugs.  You should be aware that modern C libraries often
3259 have header files which are incompatible with traditional mode.
3260
3261 This is a list of the differences.  It may not be complete, and may not
3262 correspond exactly to the behavior of either GCC or a true traditional
3263 preprocessor.
3264
3265 @itemize @bullet
3266 @item
3267 Traditional macro expansion pays no attention to single-quote or
3268 double-quote characters; macro argument symbols are replaced by the
3269 argument values even when they appear within apparent string or
3270 character constants.
3271
3272 @item
3273 Traditionally, it is permissible for a macro expansion to end in the
3274 middle of a string or character constant.  The constant continues into
3275 the text surrounding the macro call.
3276
3277 @item
3278 However, the end of the line terminates a string or character constant,
3279 with no error.  (This is a kluge.  Traditional mode is commonly used to
3280 preprocess things which are not C, and have a different comment syntax.
3281 Single apostrophes often appear in comments.  This kluge prevents the
3282 traditional preprocessor from issuing errors on such comments.)
3283
3284 @item
3285 Preprocessing directives are recognized in traditional C only when their
3286 leading @samp{#} appears in the first column.  There can be no
3287 whitespace between the beginning of the line and the @samp{#}.
3288
3289 @item
3290 In traditional C, a comment is equivalent to no text at all.  (In ISO
3291 C, a comment counts as whitespace.)  It can be used sort of the same way
3292 that @samp{##} is used in ISO C, to paste macro arguments together.
3293
3294 @item
3295 Traditional C does not have the concept of a preprocessing number.
3296
3297 @item
3298 A macro is not suppressed within its own definition, in traditional C@.
3299 Thus, any macro that is used recursively inevitably causes an error.
3300
3301 @item
3302 The @samp{#} and @samp{##} operators are not available in traditional
3303 C@.
3304
3305 @item
3306 In traditional C, the text at the end of a macro expansion can run
3307 together with the text after the macro call, to produce a single token.
3308 This is impossible in ISO C@.
3309
3310 @item
3311 None of the GNU extensions to the preprocessor are available in
3312 traditional mode, with the exception of a partial implementation of
3313 assertions, and those may be removed in the future.
3314
3315 @item
3316 A true traditional C preprocessor does not recognize @samp{#elif},
3317 @samp{#error}, or @samp{#pragma}.  GCC supports @samp{#elif} and
3318 @samp{#error} even in traditional mode, but not @samp{#pragma}.
3319
3320 @item
3321 Traditional mode is text-based, not token-based, and comments are
3322 stripped after macro expansion.  Therefore, @samp{/**/} can be used to
3323 paste tokens together provided that there is no whitespace between it
3324 and the tokens to be pasted.
3325
3326 @item
3327 Traditional mode preserves the amount and form of whitespace provided by
3328 the user.  Hard tabs remain hard tabs.  This can be useful, e.g.@: if you
3329 are preprocessing a Makefile (which we do not encourage).
3330 @end itemize
3331
3332 You can request warnings about features that did not exist, or worked
3333 differently, in traditional C with the @option{-Wtraditional} option.
3334 This works only if you do @emph{not} specify @option{-traditional}.  GCC
3335 does not warn about features of ISO C which you must use when you are
3336 using a conforming compiler, such as the @samp{#} and @samp{##}
3337 operators.
3338
3339 Presently @option{-Wtraditional} warns about:
3340
3341 @itemize @bullet
3342 @item
3343 Macro parameters that appear within string literals in the macro body.
3344 In traditional C macro replacement takes place within string literals,
3345 but does not in ISO C@.
3346
3347 @item
3348 In traditional C, some preprocessor directives did not exist.
3349 Traditional preprocessors would only consider a line to be a directive
3350 if the @samp{#} appeared in column 1 on the line.  Therefore
3351 @option{-Wtraditional} warns about directives that traditional C
3352 understands but would ignore because the @samp{#} does not appear as the
3353 first character on the line.  It also suggests you hide directives like
3354 @samp{#pragma} not understood by traditional C by indenting them.  Some
3355 traditional implementations would not recognise @samp{#elif}, so it
3356 suggests avoiding it altogether.
3357
3358 @item
3359 A function-like macro that appears without an argument list.  In
3360 traditional C this was an error.  In ISO C it merely means that the
3361 macro is not expanded.
3362
3363 @item
3364 The unary plus operator.  This did not exist in traditional C@.
3365
3366 @item
3367 The @samp{U} and @samp{LL} integer constant suffixes, which were not
3368 available in traditional C@.  (Traditional C does support the @samp{L}
3369 suffix for simple long integer constants.)  You are not warned about
3370 uses of these suffixes in macros defined in system headers.  For
3371 instance, @code{UINT_MAX} may well be defined as @code{4294967295U}, but
3372 you will not be warned if you use @code{UINT_MAX}.
3373
3374 You can usually avoid the warning, and the related warning about
3375 constants which are so large that they are unsigned, by writing the
3376 integer constant in question in hexadecimal, with no U suffix.  Take
3377 care, though, because this gives the wrong result in exotic cases.
3378 @end itemize
3379
3380 @node Implementation Details
3381 @chapter Implementation Details
3382
3383 Here we document details of how the preprocessor's implementation
3384 affects its user-visible behavior.  You should try to avoid undue
3385 reliance on behaviour described here, as it is possible that it will
3386 change subtly in future implementations.
3387
3388 Also documented here are obsolete features and changes from previous
3389 versions of GNU CPP@.
3390
3391 @menu
3392 * Implementation-defined behavior::
3393 * Implementation limits::
3394 * Obsolete Features::
3395 * Differences from previous versions::
3396 @end menu
3397
3398 @node Implementation-defined behavior
3399 @section Implementation-defined behavior
3400 @cindex implementation-defined behavior
3401
3402 This is how GNU CPP behaves in all the cases which the C standard
3403 describes as @dfn{implementation-defined}.  This term means that the
3404 implementation is free to do what it likes, but must document its choice
3405 and stick to it.
3406 @c FIXME: Check the C++ standard for more implementation-defined stuff.
3407
3408 @itemize @bullet
3409 @need 1000
3410 @item The mapping of physical source file multi-byte characters to the
3411 execution character set.
3412
3413 Currently, GNU cpp only supports character sets that are strict supersets
3414 of ASCII, and performs no translation of characters.
3415
3416 @item Non-empty sequences of whitespace characters.
3417
3418 In textual output, each whitespace sequence is collapsed to a single
3419 space.  For aesthetic reasons, the first token on each non-directive
3420 line of output is preceded with sufficient spaces that it appears in the
3421 same column as it did in the original source file.
3422
3423 @item The numeric value of character constants in preprocessor expressions.
3424
3425 The preprocessor and compiler interpret character constants in the same
3426 way; escape sequences such as @samp{\a} are given the values they would
3427 have on the target machine.
3428
3429 Multi-character character constants are interpreted a character at a
3430 time, shifting the previous result left by the number of bits per
3431 character on the host, and adding the new character.  For example, 'ab'
3432 on an 8-bit host would be interpreted as @w{'a' * 256 + 'b'}.  If there
3433 are more characters in the constant than can fit in the widest native
3434 integer type on the host, usually a @code{long}, the excess characters
3435 are ignored and a diagnostic is given.
3436
3437 @item Source file inclusion.
3438
3439 For a discussion on how the preprocessor locates header files,
3440 @ref{Include Operation}.
3441
3442 @item Interpretation of the filename resulting from a macro-expanded
3443 @samp{#include} directive.
3444
3445 @xref{Computed Includes}.
3446
3447 @item Treatment of a @samp{#pragma} directive that after macro-expansion
3448 results in a standard pragma.
3449
3450 No macro expansion occurs on any @samp{#pragma} directive line, so the
3451 question does not arise.
3452
3453 Note that GCC does not yet implement any of the standard
3454 pragmas.
3455
3456 @end itemize
3457
3458 @node Implementation limits
3459 @section Implementation limits
3460 @cindex implementation limits
3461
3462 GNU CPP has a small number of internal limits.  This section lists the
3463 limits which the C standard requires to be no lower than some minimum,
3464 and all the others we are aware of.  We intend there to be as few limits
3465 as possible.  If you encounter an undocumented or inconvenient limit,
3466 please report that to us as a bug.  (See the section on reporting bugs in
3467 the GCC manual.)
3468
3469 Where we say something is limited @dfn{only by available memory}, that
3470 means that internal data structures impose no intrinsic limit, and space
3471 is allocated with @code{malloc} or equivalent.  The actual limit will
3472 therefore depend on many things, such as the size of other things
3473 allocated by the compiler at the same time, the amount of memory
3474 consumed by other processes on the same computer, etc.
3475
3476 @itemize @bullet
3477
3478 @item Nesting levels of @samp{#include} files.
3479
3480 We impose an arbitrary limit of 200 levels, to avoid runaway recursion.
3481 The standard requires at least 15 levels.
3482
3483 @item Nesting levels of conditional inclusion.
3484
3485 The C standard mandates this be at least 63.  GNU CPP is limited only by
3486 available memory.
3487
3488 @item Levels of parenthesised expressions within a full expression.
3489
3490 The C standard requires this to be at least 63.  In preprocessor
3491 conditional expressions, it is limited only by available memory.
3492
3493 @item Significant initial characters in an identifier or macro name.
3494
3495 The preprocessor treats all characters as significant.  The C standard
3496 requires only that the first 63 be significant.
3497
3498 @item Number of macros simultaneously defined in a single translation unit.
3499
3500 The standard requires at least 4095 be possible.  GNU CPP is limited only
3501 by available memory.
3502
3503 @item Number of parameters in a macro definition and arguments in a macro call.
3504
3505 We allow @code{USHRT_MAX}, which is no smaller than 65,535.  The minimum
3506 required by the standard is 127.
3507
3508 @item Number of characters on a logical source line.
3509
3510 The C standard requires a minimum of 4096 be permitted.  GNU CPP places
3511 no limits on this, but you may get incorrect column numbers reported in
3512 diagnostics for lines longer than 65,535 characters.
3513
3514 @item Maximum size of a source file.
3515
3516 The standard does not specify any lower limit on the maximum size of a
3517 source file.  GNU cpp maps files into memory, so it is limited by the
3518 available address space.  This is generally at least two gigabytes.
3519 Depending on the operating system, the size of physical memory may or
3520 may not be a limitation.
3521
3522 @end itemize
3523
3524 @node Obsolete Features
3525 @section Obsolete Features
3526
3527 GNU CPP has a number of features which are present mainly for
3528 compatibility with older programs.  We discourage their use in new code.
3529 In some cases, we plan to remove the feature in a future version of GCC@.
3530
3531 @menu
3532 * Assertions::
3533 * Obsolete once-only headers::
3534 * Miscellaneous obsolete features::
3535 @end menu
3536
3537 @node Assertions
3538 @subsection Assertions
3539 @cindex assertions
3540
3541 @dfn{Assertions} are a deprecated alternative to macros in writing
3542 conditionals to test what sort of computer or system the compiled
3543 program will run on.  Assertions are usually predefined, but you can
3544 define them with preprocessing directives or command-line options.
3545
3546 Assertions were intended to provide a more systematic way to describe
3547 the compiler's target system.  However, in practice they are just as
3548 unpredictable as the system-specific predefined macros.  In addition, they
3549 are not part of any standard, and only a few compilers support them.
3550 Therefore, the use of assertions is @strong{less} portable than the use
3551 of system-specific predefined macros.  We recommend you do not use them at
3552 all.
3553
3554 @cindex predicates
3555 An assertion looks like this:
3556
3557 @example
3558 #@var{predicate} (@var{answer})
3559 @end example
3560
3561 @noindent
3562 @var{predicate} must be a single identifier.  @var{answer} can be any
3563 sequence of tokens; all characters are significant except for leading
3564 and trailing whitespace, and differences in internal whitespace
3565 sequences are ignored.  (This is similar to the rules governing macro
3566 redefinition.)  Thus, @code{(x + y)} is different from @code{(x+y)} but
3567 equivalent to @code{@w{( x + y )}}.  Parentheses do not nest inside an
3568 answer.
3569
3570 @cindex testing predicates
3571 To test an assertion, you write it in an @samp{#if}.  For example, this
3572 conditional succeeds if either @code{vax} or @code{ns16000} has been
3573 asserted as an answer for @code{machine}.
3574
3575 @example
3576 #if #machine (vax) || #machine (ns16000)
3577 @end example
3578
3579 @noindent
3580 You can test whether @emph{any} answer is asserted for a predicate by
3581 omitting the answer in the conditional:
3582
3583 @example
3584 #if #machine
3585 @end example
3586
3587 @findex #assert
3588 Assertions are made with the @samp{#assert} directive.  Its sole
3589 argument is the assertion to make, without the leading @samp{#} that
3590 identifies assertions in conditionals.
3591
3592 @example
3593 #assert @var{predicate} (@var{answer})
3594 @end example
3595
3596 @noindent
3597 You may make several assertions with the same predicate and different
3598 answers.  Subsequent assertions do not override previous ones for the
3599 same predicate.  All the answers for any given predicate are
3600 simultaneously true.
3601
3602 @cindex assertions, cancelling
3603 @findex #unassert
3604 Assertions can be cancelled with the the @samp{#unassert} directive.  It
3605 has the same syntax as @samp{#assert}.  In that form it cancels only the
3606 answer which was specified on the @samp{#unassert} line; other answers
3607 for that predicate remain true.  You can cancel an entire predicate by
3608 leaving out the answer:
3609
3610 @example
3611 #unassert @var{predicate}
3612 @end example
3613
3614 @noindent
3615 In either form, if no such assertion has been made, @samp{#unassert} has
3616 no effect.
3617
3618 You can also make or cancel assertions using command line options.
3619 @xref{Invocation}.
3620
3621 @node Obsolete once-only headers
3622 @subsection Obsolete once-only headers
3623
3624 GNU CPP supports two more ways of indicating that a header file should be
3625 read only once.  Neither one is as portable as a wrapper @samp{#ifndef},
3626 and we recommend you do not use them in new programs.
3627
3628 @findex #import
3629 In the Objective-C language, there is a variant of @samp{#include}
3630 called @samp{#import} which includes a file, but does so at most once.
3631 If you use @samp{#import} instead of @samp{#include}, then you don't
3632 need the conditionals inside the header file to prevent multiple
3633 inclusion of the contents.  GCC permits the use of @samp{#import} in C
3634 and C++ as well as Objective-C@.  However, it is not in standard C or C++
3635 and should therefore not be used by portable programs.
3636
3637 @samp{#import} is not a well designed feature.  It requires the users of
3638 a header file to know that it should only be included once.  It is much
3639 better for the header file's implementor to write the file so that users
3640 don't need to know this.  Using a wrapper @samp{#ifndef} accomplishes
3641 this goal.
3642
3643 In the present implementation, a single use of @samp{#import} will
3644 prevent the file from ever being read again, by either @samp{#import} or
3645 @samp{#include}.  You should not rely on this; do not use both
3646 @samp{#import} and @samp{#include} to refer to the same header file.
3647
3648 Another way to prevent a header file from being included more than once
3649 is with the @samp{#pragma once} directive.  If @samp{#pragma once} is
3650 seen when scanning a header file, that file will never be read again, no
3651 matter what.
3652
3653 @samp{#pragma once} does not have the problems that @samp{#import} does,
3654 but it is not recognized by all preprocessors, so you cannot rely on it
3655 in a portable program.
3656
3657 @node Miscellaneous obsolete features
3658 @subsection Miscellaneous obsolete features
3659
3660 Here are a few more obsolete features.
3661
3662 @itemize @bullet
3663 @cindex invalid token paste
3664 @item Attempting to paste two tokens which together do not form a valid
3665 preprocessing token.
3666
3667 The preprocessor currently warns about this and outputs the two tokens
3668 adjacently, which is probably the behavior the programmer intends.  It
3669 may not work in future, though.
3670
3671 Most of the time, when you get this warning, you will find that @samp{##}
3672 is being used superstitiously, to guard against whitespace appearing
3673 between two tokens.  It is almost always safe to delete the @samp{##}.
3674
3675 @cindex pragma poison
3676 @item @code{#pragma poison}
3677
3678 This is the same as @code{#pragma GCC poison}.  The version without the
3679 @code{GCC} prefix is deprecated.  @xref{Pragmas}.
3680
3681 @cindex multi-line string constants
3682 @item Multi-line string constants
3683
3684 GCC currently allows a string constant to extend across multiple logical
3685 lines of the source file.  This extension is deprecated and will be
3686 removed in a future version of GCC@.  Such string constants are already
3687 rejected in all directives apart from @samp{#define}.
3688
3689 Instead, make use of ISO C concatenation of adjacent string literals, or
3690 use @samp{\n} followed by a backslash-newline.
3691
3692 @end itemize
3693
3694 @node Differences from previous versions
3695 @section Differences from previous versions
3696 @cindex differences from previous versions
3697
3698 This section details behavior which has changed from previous versions
3699 of GNU CPP@.  We do not plan to change it again in the near future, but
3700 we do not promise not to, either.
3701
3702 The ``previous versions'' discussed here are 2.95 and before.  The
3703 behavior of GCC 3.0 is mostly the same as the behavior of the widely
3704 used 2.96 and 2.97 development snapshots.  Where there are differences,
3705 they generally represent bugs in the snapshots.
3706
3707 @itemize @bullet
3708
3709 @item Order of evaluation of @samp{#} and @samp{##} operators
3710
3711 The standard does not specify the order of evaluation of a chain of
3712 @samp{##} operators, nor whether @samp{#} is evaluated before, after, or
3713 at the same time as @samp{##}.  You should therefore not write any code
3714 which depends on any specific ordering.  It is possible to guarantee an
3715 ordering, if you need one, by suitable use of nested macros.
3716
3717 An example of where this might matter is pasting the arguments @samp{1},
3718 @samp{e} and @samp{-2}.  This would be fine for left-to-right pasting,
3719 but right-to-left pasting would produce an invalid token @samp{e-2}.
3720
3721 GCC 3.0 evaluates @samp{#} and @samp{##} at the same time and strictly
3722 left to right.  Older versions evaluated all @samp{#} operators first,
3723 then all @samp{##} operators, in an unreliable order.
3724
3725 @item The form of whitespace betwen tokens in preprocessor output
3726
3727 @xref{Preprocessor Output}, for the current textual format.  This is
3728 also the format used by stringification.  Normally, the preprocessor
3729 communicates tokens directly to the compiler's parser, and whitespace
3730 does not come up at all.
3731
3732 Older versions of GCC preserved all whitespace provided by the user and
3733 inserted lots more whitespace of their own, because they could not
3734 accurately predict when extra spaces were needed to prevent accidental
3735 token pasting.
3736
3737 @item Optional argument when invoking rest argument macros
3738
3739 As an extension, GCC permits you to omit the variable arguments entirely
3740 when you use a variable argument macro.  This is forbidden by the 1999 C
3741 standard, and will provoke a pedantic warning with GCC 3.0.  Previous
3742 versions accepted it silently.
3743
3744 @item @samp{##} swallowing preceding text in rest argument macros
3745
3746 Formerly, in a macro expansion, if @samp{##} appeared before a variable
3747 arguments parameter, and the set of tokens specified for that argument
3748 in the macro invocation was empty, previous versions of GNU CPP would
3749 back up and remove the preceding sequence of non-whitespace characters
3750 (@strong{not} the preceding token).  This extension is in direct
3751 conflict with the 1999 C standard and has been drastically pared back.
3752
3753 In the current version of the preprocessor, if @samp{##} appears between
3754 a comma and a variable arguments parameter, and the variable argument is
3755 omitted entirely, the comma will be removed from the expansion.  If the
3756 variable argument is empty, or the token before @samp{##} is not a
3757 comma, then @samp{##} behaves as a normal token paste.
3758
3759 @item Traditional mode and GNU extensions
3760
3761 Traditional mode used to be implemented in the same program as normal
3762 preprocessing.  Therefore, all the GNU extensions to the preprocessor
3763 were still available in traditional mode.  It is now a separate program
3764 and does not implement any of the GNU extensions, except for a partial
3765 implementation of assertions.  Even those may be removed in a future
3766 release.
3767 @end itemize
3768
3769 @node Invocation
3770 @chapter Invocation
3771 @cindex invocation
3772 @cindex command line
3773
3774 Most often when you use the C preprocessor you will not have to invoke it
3775 explicitly: the C compiler will do so automatically.  However, the
3776 preprocessor is sometimes useful on its own.  All the options listed
3777 here are also acceptable to the C compiler and have the same meaning,
3778 except that the C compiler has different rules for specifying the output
3779 file.
3780
3781 @strong{Note:} Whether you use the preprocessor by way of @command{gcc}
3782 or @command{cpp}, the @dfn{compiler driver} is run first.  This
3783 program's purpose is to translate your command into invocations of the
3784 programs that do the actual work.  Their command line interfaces are
3785 similar but not identical to the documented interface, and may change
3786 without notice.
3787
3788 @ignore
3789 @c man begin SYNOPSIS
3790 cpp [@option{-D}@var{macro}[=@var{defn}]@dots{}] [@option{-U}@var{macro}]
3791     [@option{-I}@var{dir}@dots{}] [@option{-W}@var{warn}@dots{}]
3792     [@option{-M}|@option{-MM}] [@option{-MG}] [@option{-MF} @var{filename}]
3793     [@option{-MP}] [@option{-MQ} @var{target}@dots{}] [@option{-MT} @var{target}@dots{}]
3794     [@option{-x} @var{language}] [@option{-std=}@var{standard}]
3795     @var{infile} @var{outfile}
3796
3797 Only the most useful options are listed here; see below for the remainder.
3798 @c man end
3799 @c man begin SEEALSO
3800 gcc(1), as(1), ld(1), and the Info entries for @file{cpp}, @file{gcc}, and
3801 @file{binutils}.
3802 @c man end
3803 @end ignore
3804
3805 @c man begin OPTIONS
3806 The C preprocessor expects two file names as arguments, @var{infile} and
3807 @var{outfile}.  The preprocessor reads @var{infile} together with any
3808 other files it specifies with @samp{#include}.  All the output generated
3809 by the combined input files is written in @var{outfile}.
3810
3811 Either @var{infile} or @var{outfile} may be @option{-}, which as
3812 @var{infile} means to read from standard input and as @var{outfile}
3813 means to write to standard output.  Also, if either file is omitted, it
3814 means the same as if @option{-} had been specified for that file.
3815
3816 Unless otherwise noted, or the option ends in @samp{=}, all options
3817 which take an argument may have that argument appear either immediately
3818 after the option, or with a space between option and argument:
3819 @option{-Ifoo} and @option{-I foo} have the same effect.
3820
3821 @cindex grouping options
3822 @cindex options, grouping
3823 Many options have multi-letter names; therefore multiple single-letter
3824 options may @emph{not} be grouped: @option{-dM} is very different from
3825 @w{@samp{-d -M}}.
3826
3827 @cindex options
3828 @table @gcctabopt
3829 @item -D @var{name}
3830 Predefine @var{name} as a macro, with definition @code{1}.
3831
3832 @item -D @var{name}=@var{definition}
3833 Predefine @var{name} as a macro, with definition @var{definition}.
3834 There are no restrictions on the contents of @var{definition}, but if
3835 you are invoking the preprocessor from a shell or shell-like program you
3836 may need to use the shell's quoting syntax to protect characters such as
3837 spaces that have a meaning in the shell syntax.  If you use more than
3838 one @option{-D} for the same @var{name}, the rightmost definition takes
3839 effect.
3840
3841 If you wish to define a function-like macro on the command line, write
3842 its argument list with surrounding parentheses before the equals sign
3843 (if any).  Parentheses are meaningful to most shells, so you will need
3844 to quote the option.  With @command{sh} and @command{csh},
3845 @option{-D'@var{name}(@var{args@dots{}})=@var{definition}'} works.
3846
3847 @item -U @var{name}
3848 Cancel any previous definition of @var{name}, either built in or
3849 provided with a @option{-D} option.
3850
3851 All @option{-imacros @var{file}} and @option{-include @var{file}} options
3852 are processed after all @option{-D} and @option{-U} options.
3853
3854 @item -undef
3855 Do not predefine any system-specific macros.  The common predefined
3856 macros remain defined.
3857
3858 @item -I @var{dir}
3859 Add the directory @var{dir} to the list of directories to be searched
3860 for header files.  @xref{Search Path}.  Directories named by @option{-I}
3861 are searched before the standard system include directories.
3862
3863 It is dangerous to specify a standard system include directory in an
3864 @option{-I} option.  This defeats the special treatment of system
3865 headers (@pxref{System Headers}).  It can also defeat the repairs to
3866 buggy system headers which GCC makes when it is installed.
3867
3868 @item -o @var{file}
3869 Write output to @var{file}.  This is the same as specifying @var{file}
3870 as the second non-option argument to @command{cpp}.  @command{gcc} has a
3871 different interpretation of a second non-option argument, so you must
3872 use @option{-o} to specify the output file.
3873
3874 @item -Wall
3875 Turns on all optional warnings which are desirable for normal code.  At
3876 present this is @option{-Wcomment} and @option{-Wtrigraphs}.  Note that
3877 many of the preprocessor's warnings are on by default and have no
3878 options to control them.
3879
3880 @item -Wcomment
3881 @itemx -Wcomments
3882 Warn whenever a comment-start sequence @samp{/*} appears in a @samp{/*}
3883 comment, or whenever a backslash-newline appears in a @samp{//} comment.
3884 (Both forms have the same effect.)
3885
3886 @item -Wtrigraphs
3887 Warn if any trigraphs are encountered.  This option used to take effect
3888 only if @option{-trigraphs} was also specified, but now works
3889 independently.  Warnings are not given for trigraphs within comments, as
3890 they do not affect the meaning of the program.
3891
3892 @item -Wtraditional
3893 Warn about certain constructs that behave differently in traditional and
3894 ISO C@.  Also warn about ISO C constructs that have no traditional C
3895 equivalent, and problematic constructs which should be avoided.
3896 @xref{Traditional Mode}.
3897
3898 @item -Wimport
3899 Warn the first time @samp{#import} is used.
3900
3901 @item -Wundef
3902 Warn whenever an identifier which is not a macro is encountered in an
3903 @samp{#if} directive, outside of @samp{defined}.  Such identifiers are
3904 replaced with zero.
3905
3906 @item -Werror
3907 Make all warnings into hard errors.  Source code which triggers warnings
3908 will be rejected.
3909
3910 @item -Wsystem-headers
3911 Issue warnings for code in system headers.  These are normally unhelpful
3912 in finding bugs in your own code, therefore suppressed.  If you are
3913 responsible for the system library, you may want to see them.
3914
3915 @item -w
3916 Suppress all warnings, including those which GNU CPP issues by default.
3917
3918 @item -pedantic
3919 Issue all the mandatory diagnostics listed in the C standard.  Some of
3920 them are left out by default, since they trigger frequently on harmless
3921 code.
3922
3923 @item -pedantic-errors
3924 Issue all the mandatory diagnostics, and make all mandatory diagnostics
3925 into errors.  This includes mandatory diagnostics that GCC issues
3926 without @samp{-pedantic} but treats as warnings.
3927
3928 @item -M
3929 Instead of outputting the result of preprocessing, output a rule
3930 suitable for @command{make} describing the dependencies of the main
3931 source file.  The preprocessor outputs one @command{make} rule containing
3932 the object file name for that source file, a colon, and the names of all
3933 the included files, including those coming from @option{-include} or
3934 @option{-imacros} command line options.
3935
3936 Unless specified explicitly (with @option{-MT} or @option{-MQ}), the
3937 object file name consists of the basename of the source file with any
3938 suffix replaced with object file suffix.  If there are many included
3939 files then the rule is split into several lines using @samp{\}-newline.
3940 The rule has no commands.
3941
3942 @item -MM
3943 Like @option{-M}, but mention only the files included with @code{@w{#include
3944 "@var{file}"}} or with @option{-include} or @option{-imacros} command line
3945 options.  System header files included with @code{@w{#include <@var{file}>}}
3946 are omitted.
3947
3948 @item -MF @var{file}
3949 When used with @option{-M} or @option{-MM}, specifies a file to write the
3950 dependencies to.  This allows the preprocessor to write the preprocessed
3951 file to stdout normally.  If no @option{-MF} switch is given, CPP sends
3952 the rules to stdout and suppresses normal preprocessed output.
3953
3954 @item -MG
3955 When used with @option{-M} or @option{-MM}, @option{-MG} says to treat missing
3956 header files as generated files and assume they live in the same
3957 directory as the source file.  It suppresses preprocessed output, as a
3958 missing header file is ordinarily an error.
3959
3960 This feature is used in automatic updating of makefiles.
3961
3962 @item -MP
3963 This option instructs CPP to add a phony target for each dependency
3964 other than the main file, causing each to depend on nothing.  These
3965 dummy rules work around errors @command{make} gives if you remove header
3966 files without updating the @file{Makefile} to match.
3967
3968 This is typical output:
3969
3970 @example
3971 test.o: test.c test.h
3972
3973 test.h:
3974 @end example
3975
3976 @item -MT @var{target}
3977
3978 Change the target of the rule emitted by dependency generation.  By
3979 default CPP takes the name of the main input file, including any path,
3980 deletes any file suffix such as @samp{.c}, and appends the platform's
3981 usual object suffix.  The result is the target.
3982
3983 An @option{-MT} option will set the target to be exactly the string you
3984 specify.  If you want multiple targets, you can specify them as a single
3985 argument to @option{-MT}, or use multiple @option{-MT} options.
3986
3987 For example, @option{@w{-MT '$(objpfx)foo.o'}} might give
3988
3989 @example
3990 $(objpfx)foo.o: foo.c
3991 @end example
3992
3993 @item -MQ @var{target}
3994
3995 Same as @option{-MT}, but it quotes any characters which are special to
3996 Make.  @option{@w{-MQ '$(objpfx)foo.o'}} gives
3997
3998 @example
3999 $$(objpfx)foo.o: foo.c
4000 @end example
4001
4002 The default target is automatically quoted, as if it were given with
4003 @option{-MQ}.
4004
4005 @item -MD @var{file}
4006 @itemx -MMD @var{file}
4007 @option{-MD @var{file}} is equivalent to @option{-M -MF @var{file}}, and
4008 @option{-MMD @var{file}} is equivalent to @option{-MM -MF @var{file}}.
4009
4010 Due to limitations in the compiler driver, you must use these switches
4011 when you want to generate a dependency file as a side-effect of normal
4012 compilation.
4013
4014 @item -x c
4015 @itemx -x c++
4016 @itemx -x objective-c
4017 @itemx -x assembler-with-cpp
4018 Specify the source language: C, C++, Objective-C, or assembly.  This has
4019 nothing to do with standards conformance or extensions; it merely
4020 selects which base syntax to expect.  If you give none of these options,
4021 cpp will deduce the language from the extension of the source file:
4022 @samp{.c}, @samp{.cc}, @samp{.m}, or @samp{.S}.  Some other common
4023 extensions for C++ and assembly are also recognized.  If cpp does not
4024 recognize the extension, it will treat the file as C; this is the most
4025 generic mode.
4026
4027 @strong{Note:} Previous versions of cpp accepted a @option{-lang} option
4028 which selected both the language and the standards conformance level.
4029 This option has been removed, because it conflicts with the @option{-l}
4030 option.
4031
4032 @item -std=@var{standard}
4033 @itemx -ansi
4034 Specify the standard to which the code should conform.  Currently cpp
4035 only knows about the standards for C; other language standards will be
4036 added in the future.
4037
4038 @var{standard}
4039 may be one of:
4040 @table @code
4041 @item iso9899:1990
4042 @itemx c89
4043 The ISO C standard from 1990.  @samp{c89} is the customary shorthand for
4044 this version of the standard.
4045
4046 The @option{-ansi} option is equivalent to @option{-std=c89}.
4047
4048 @item iso9899:199409
4049 The 1990 C standard, as amended in 1994.
4050
4051 @item iso9899:1999
4052 @itemx c99
4053 @itemx iso9899:199x
4054 @itemx c9x
4055 The revised ISO C standard, published in December 1999.  Before
4056 publication, this was known as C9X@.
4057
4058 @item gnu89
4059 The 1990 C standard plus GNU extensions.  This is the default.
4060
4061 @item gnu99
4062 @itemx gnu9x
4063 The 1999 C standard plus GNU extensions.
4064 @end table
4065
4066 @item -I-
4067 Split the include path.  Any directories specified with @option{-I}
4068 options before @option{-I-} are searched only for headers requested with
4069 @code{@w{#include "@var{file}"}}; they are not searched for
4070 @code{@w{#include <@var{file}>}}.  If additional directories are
4071 specified with @option{-I} options after the @option{-I-}, those
4072 directories are searched for all @samp{#include} directives.
4073
4074 In addition, @option{-I-} inhibits the use of the directory of the current
4075 file directory as the first search directory for @code{@w{#include
4076 "@var{file}"}}.  @xref{Search Path}.
4077
4078 @item -nostdinc
4079 Do not search the standard system directories for header files.
4080 Only the directories you have specified with @option{-I} options
4081 (and the directory of the current file, if appropriate) are searched.
4082
4083 @item -nostdinc++
4084 Do not search for header files in the C++-specific standard directories,
4085 but do still search the other standard directories.  (This option is
4086 used when building the C++ library.)
4087
4088 @item -include @var{file}
4089
4090 Process @var{file} as if @code{#include "file"} appeared as the first
4091 line of the primary source file.  However, the first directory searched
4092 for @var{file} is the preprocessor's working directory @emph{instead of}
4093 the directory containing the main source file.  If not found there, it
4094 is searched for in the remainder of the @code{#include "@dots{}"} search
4095 chain as normal.
4096
4097 If multiple @option{-include} options are given, the files are included
4098 in the order they appear on the command line.
4099
4100 @item -imacros @var{file}
4101
4102 Exactly like @option{-include}, except that any output produced by
4103 scanning @var{file} is thrown away.  Macros it defines remain defined.
4104 This allows you to acquire all the macros from a header without also
4105 processing its declarations.
4106
4107 All files specified by @option{-imacros} are processed before all files
4108 specified by @option{-include}.
4109
4110 @item -idirafter @var{dir}
4111 Search @var{dir} for header files, but do it @emph{after} all
4112 directories specified with @option{-I} and the standard system directories
4113 have been exhausted.  @var{dir} is treated as a system include directory.
4114
4115 @item -iprefix @var{prefix}
4116 Specify @var{prefix} as the prefix for subsequent @option{-iwithprefix}
4117 options.  If the prefix represents a directory, you should include the
4118 final @samp{/}.
4119
4120 @item -iwithprefix @var{dir}
4121 @itemx -iwithprefixbefore @var{dir}
4122
4123 Append @var{dir} to the prefix specified previously with
4124 @option{-iprefix}, and add the resulting directory to the include search
4125 path.  @option{-iwithprefixbefore} puts it in the same place @option{-I}
4126 would; @option{-iwithprefix} puts it where @option{-idirafter} would.
4127
4128 Use of these options is discouraged.
4129
4130 @item -isystem @var{dir}
4131 Search @var{dir} for header files, after all directories specified by
4132 @option{-I} but before the standard system directories.  Mark it
4133 as a system directory, so that it gets the same special treatment as
4134 is applied to the standard system directories.  @xref{System Headers}.
4135
4136 @item -fpreprocessed
4137 Indicate to the preprocessor that the input file has already been
4138 preprocessed.  This suppresses things like macro expansion, trigraph
4139 conversion, escaped newline splicing, and processing of most directives.
4140 The preprocessor still recognizes and removes comments, so that you can
4141 pass a file preprocessed with @option{-C} to the compiler without
4142 problems.  In this mode the integrated preprocessor is little more than
4143 a tokenizer for the front ends.
4144
4145 @option{-fpreprocessed} is implicit if the input file has one of the
4146 extensions @samp{.i}, @samp{.ii} or @samp{.mi}.  These are the
4147 extensions that GCC uses for preprocessed files created by
4148 @option{-save-temps}.
4149
4150 @item -ftabstop=@var{width}
4151 Set the distance between tab stops.  This helps the preprocessor report
4152 correct column numbers in warnings or errors, even if tabs appear on the
4153 line.  If the value is less than 1 or greater than 100, the option is
4154 ignored.  The default is 8.
4155
4156 @item -fno-show-column
4157 Do not print column numbers in diagnostics.  This may be necessary if
4158 diagnostics are being scanned by a program that does not understand the
4159 column numbers, such as @command{dejagnu}.
4160
4161 @item -A @var{predicate}=@var{answer}
4162 Make an assertion with the predicate @var{predicate} and answer
4163 @var{answer}.  This form is preferred to the older form @option{-A
4164 @var{predicate}(@var{answer})}, which is still supported, because
4165 it does not use shell special characters.  @xref{Assertions}.
4166
4167 @item -A -@var{predicate}=@var{answer}
4168 Cancel an assertion with the predicate @var{predicate} and answer
4169 @var{answer}.
4170
4171 @item -A-
4172 Cancel all predefined assertions and all assertions preceding it on
4173 the command line.  Also, undefine all predefined macros and all
4174 macros preceding it on the command line.  (This is a historical wart and
4175 may change in the future.)
4176
4177 @item -dCHARS
4178 @var{CHARS} is a sequence of one or more of the following characters,
4179 and must not be preceded by a space.  Other characters are interpreted
4180 by the compiler proper, or reserved for future versions of GCC, and so
4181 are silently ignored.  If you specify characters whose behavior
4182 conflicts, the result is undefined.
4183
4184 @table @samp
4185 @item M
4186 Instead of the normal output, generate a list of @samp{#define}
4187 directives for all the macros defined during the execution of the
4188 preprocessor, including predefined macros.  This gives you a way of
4189 finding out what is predefined in your version of the preprocessor.
4190 Assuming you have no file @file{foo.h}, the command
4191
4192 @example
4193 touch foo.h; cpp -dM foo.h
4194 @end example
4195
4196 @noindent
4197 will show all the predefined macros.
4198
4199 @item D
4200 Like @samp{M} except in two respects: it does @emph{not} include the
4201 predefined macros, and it outputs @emph{both} the @samp{#define}
4202 directives and the result of preprocessing.  Both kinds of output go to
4203 the standard output file.
4204
4205 @item N
4206 Like @samp{D}, but emit only the macro names, not their expansions.
4207
4208 @item I
4209 Output @samp{#include} directives in addition to the result of
4210 preprocessing.
4211 @end table
4212
4213 @item -P
4214 Inhibit generation of linemarkers in the output from the preprocessor.
4215 This might be useful when running the preprocessor on something that is
4216 not C code, and will be sent to a program which might be confused by the
4217 linemarkers.  @xref{Preprocessor Output}.
4218
4219 @item -C
4220 Do not discard comments.  All comments are passed through to the output
4221 file, except for comments in processed directives, which are deleted
4222 along with the directive.  Comments appearing in the expansion list of a
4223 macro will be preserved, and appear in place wherever the macro is
4224 invoked.
4225
4226 You should be prepared for side effects when using @option{-C}; it causes
4227 the preprocessor to treat comments as tokens in their own right.  For
4228 example, macro redefinitions that were trivial when comments were
4229 replaced by a single space might become significant when comments are
4230 retained.  Also, comments appearing at the start of what would be a
4231 directive line have the effect of turning that line into an ordinary
4232 source line, since the first token on the line is no longer a @samp{#}.
4233
4234 @item -gcc
4235 Define the macros @sc{__gnuc__}, @sc{__gnuc_minor__} and
4236 @sc{__gnuc_patchlevel__}.  These are defined automatically when you use
4237 @command{gcc -E}; you can turn them off in that case with
4238 @option{-no-gcc}.
4239
4240 @item -traditional
4241 Try to imitate the behavior of old-fashioned C, as opposed to ISO
4242 C@.  @xref{Traditional Mode}.
4243
4244 @item -trigraphs
4245 Process trigraph sequences.  @xref{Initial processing}.
4246
4247 @item -remap
4248 Enable special code to work around file systems which only permit very
4249 short file names, such as MS-DOS@.
4250
4251 @item -$
4252 Forbid the use of @samp{$} in identifiers.  The C standard allows
4253 implementations to define extra characters that can appear in
4254 identifiers.  By default GNU CPP permits @samp{$}, a common extension.
4255
4256 @item -h
4257 @itemx --help
4258 @itemx --target-help
4259 Print text describing all the command line options instead of
4260 preprocessing anything.
4261
4262 @item -v
4263 Verbose mode.  Print out GNU CPP's version number at the beginning of
4264 execution, and report the final form of the include path.
4265
4266 @item -H
4267 Print the name of each header file used, in addition to other normal
4268 activities.  Each name is indented to show how deep in the
4269 @samp{#include} stack it is.
4270
4271 @item -version
4272 @itemx --version
4273 Print out GNU CPP's version number.  With one dash, proceed to
4274 preprocess as normal.  With two dashes, exit immediately.
4275 @end table
4276 @c man end
4277
4278 @page
4279 @node Index of Directives
4280 @unnumbered Index of Directives
4281 @printindex fn
4282
4283 @node Concept Index
4284 @unnumbered Concept Index
4285 @printindex cp
4286
4287 @bye