OSDN Git Service

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