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