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