11debfc3dSmrg\input texinfo 21debfc3dSmrg@setfilename cpp.info 31debfc3dSmrg@settitle The C Preprocessor 41debfc3dSmrg@setchapternewpage off 51debfc3dSmrg@c @smallbook 61debfc3dSmrg@c @cropmarks 71debfc3dSmrg@c @finalout 81debfc3dSmrg 91debfc3dSmrg@include gcc-common.texi 101debfc3dSmrg 111debfc3dSmrg@copying 121debfc3dSmrg@c man begin COPYRIGHT 13*8feb0f0bSmrgCopyright @copyright{} 1987-2020 Free Software Foundation, Inc. 141debfc3dSmrg 151debfc3dSmrgPermission is granted to copy, distribute and/or modify this document 161debfc3dSmrgunder the terms of the GNU Free Documentation License, Version 1.3 or 171debfc3dSmrgany later version published by the Free Software Foundation. A copy of 181debfc3dSmrgthe license is included in the 191debfc3dSmrg@c man end 201debfc3dSmrgsection entitled ``GNU Free Documentation License''. 211debfc3dSmrg@ignore 221debfc3dSmrg@c man begin COPYRIGHT 231debfc3dSmrgman page gfdl(7). 241debfc3dSmrg@c man end 251debfc3dSmrg@end ignore 261debfc3dSmrg 271debfc3dSmrg@c man begin COPYRIGHT 281debfc3dSmrgThis manual contains no Invariant Sections. The Front-Cover Texts are 291debfc3dSmrg(a) (see below), and the Back-Cover Texts are (b) (see below). 301debfc3dSmrg 311debfc3dSmrg(a) The FSF's Front-Cover Text is: 321debfc3dSmrg 331debfc3dSmrg A GNU Manual 341debfc3dSmrg 351debfc3dSmrg(b) The FSF's Back-Cover Text is: 361debfc3dSmrg 371debfc3dSmrg You have freedom to copy and modify this GNU Manual, like GNU 381debfc3dSmrg software. Copies published by the Free Software Foundation raise 391debfc3dSmrg funds for GNU development. 401debfc3dSmrg@c man end 411debfc3dSmrg@end copying 421debfc3dSmrg 431debfc3dSmrg@c Create a separate index for command line options. 441debfc3dSmrg@defcodeindex op 451debfc3dSmrg@syncodeindex vr op 461debfc3dSmrg 471debfc3dSmrg@c Used in cppopts.texi and cppenv.texi. 481debfc3dSmrg@set cppmanual 491debfc3dSmrg 501debfc3dSmrg@ifinfo 511debfc3dSmrg@dircategory Software development 521debfc3dSmrg@direntry 531debfc3dSmrg* Cpp: (cpp). The GNU C preprocessor. 541debfc3dSmrg@end direntry 551debfc3dSmrg@end ifinfo 561debfc3dSmrg 571debfc3dSmrg@titlepage 581debfc3dSmrg@title The C Preprocessor 591debfc3dSmrg@versionsubtitle 601debfc3dSmrg@author Richard M. Stallman, Zachary Weinberg 611debfc3dSmrg@page 621debfc3dSmrg@c There is a fill at the bottom of the page, so we need a filll to 631debfc3dSmrg@c override it. 641debfc3dSmrg@vskip 0pt plus 1filll 651debfc3dSmrg@insertcopying 661debfc3dSmrg@end titlepage 671debfc3dSmrg@contents 681debfc3dSmrg@page 691debfc3dSmrg 701debfc3dSmrg@ifnottex 711debfc3dSmrg@node Top 721debfc3dSmrg@top 731debfc3dSmrgThe C preprocessor implements the macro language used to transform C, 741debfc3dSmrgC++, and Objective-C programs before they are compiled. It can also be 751debfc3dSmrguseful on its own. 761debfc3dSmrg 771debfc3dSmrg@menu 781debfc3dSmrg* Overview:: 791debfc3dSmrg* Header Files:: 801debfc3dSmrg* Macros:: 811debfc3dSmrg* Conditionals:: 821debfc3dSmrg* Diagnostics:: 831debfc3dSmrg* Line Control:: 841debfc3dSmrg* Pragmas:: 851debfc3dSmrg* Other Directives:: 861debfc3dSmrg* Preprocessor Output:: 871debfc3dSmrg* Traditional Mode:: 881debfc3dSmrg* Implementation Details:: 891debfc3dSmrg* Invocation:: 901debfc3dSmrg* Environment Variables:: 911debfc3dSmrg* GNU Free Documentation License:: 921debfc3dSmrg* Index of Directives:: 931debfc3dSmrg* Option Index:: 941debfc3dSmrg* Concept Index:: 951debfc3dSmrg 961debfc3dSmrg@detailmenu 971debfc3dSmrg --- The Detailed Node Listing --- 981debfc3dSmrg 991debfc3dSmrgOverview 1001debfc3dSmrg 1011debfc3dSmrg* Character sets:: 1021debfc3dSmrg* Initial processing:: 1031debfc3dSmrg* Tokenization:: 1041debfc3dSmrg* The preprocessing language:: 1051debfc3dSmrg 1061debfc3dSmrgHeader Files 1071debfc3dSmrg 1081debfc3dSmrg* Include Syntax:: 1091debfc3dSmrg* Include Operation:: 1101debfc3dSmrg* Search Path:: 1111debfc3dSmrg* Once-Only Headers:: 1121debfc3dSmrg* Alternatives to Wrapper #ifndef:: 1131debfc3dSmrg* Computed Includes:: 1141debfc3dSmrg* Wrapper Headers:: 1151debfc3dSmrg* System Headers:: 1161debfc3dSmrg 1171debfc3dSmrgMacros 1181debfc3dSmrg 1191debfc3dSmrg* Object-like Macros:: 1201debfc3dSmrg* Function-like Macros:: 1211debfc3dSmrg* Macro Arguments:: 1221debfc3dSmrg* Stringizing:: 1231debfc3dSmrg* Concatenation:: 1241debfc3dSmrg* Variadic Macros:: 1251debfc3dSmrg* Predefined Macros:: 1261debfc3dSmrg* Undefining and Redefining Macros:: 1271debfc3dSmrg* Directives Within Macro Arguments:: 1281debfc3dSmrg* Macro Pitfalls:: 1291debfc3dSmrg 1301debfc3dSmrgPredefined Macros 1311debfc3dSmrg 1321debfc3dSmrg* Standard Predefined Macros:: 1331debfc3dSmrg* Common Predefined Macros:: 1341debfc3dSmrg* System-specific Predefined Macros:: 1351debfc3dSmrg* C++ Named Operators:: 1361debfc3dSmrg 1371debfc3dSmrgMacro Pitfalls 1381debfc3dSmrg 1391debfc3dSmrg* Misnesting:: 1401debfc3dSmrg* Operator Precedence Problems:: 1411debfc3dSmrg* Swallowing the Semicolon:: 1421debfc3dSmrg* Duplication of Side Effects:: 1431debfc3dSmrg* Self-Referential Macros:: 1441debfc3dSmrg* Argument Prescan:: 1451debfc3dSmrg* Newlines in Arguments:: 1461debfc3dSmrg 1471debfc3dSmrgConditionals 1481debfc3dSmrg 1491debfc3dSmrg* Conditional Uses:: 1501debfc3dSmrg* Conditional Syntax:: 1511debfc3dSmrg* Deleted Code:: 1521debfc3dSmrg 1531debfc3dSmrgConditional Syntax 1541debfc3dSmrg 1551debfc3dSmrg* Ifdef:: 1561debfc3dSmrg* If:: 1571debfc3dSmrg* Defined:: 1581debfc3dSmrg* Else:: 1591debfc3dSmrg* Elif:: 1601debfc3dSmrg 1611debfc3dSmrgImplementation Details 1621debfc3dSmrg 1631debfc3dSmrg* Implementation-defined behavior:: 1641debfc3dSmrg* Implementation limits:: 1651debfc3dSmrg* Obsolete Features:: 1661debfc3dSmrg 1671debfc3dSmrgObsolete Features 1681debfc3dSmrg 1691debfc3dSmrg* Obsolete Features:: 1701debfc3dSmrg 1711debfc3dSmrg@end detailmenu 1721debfc3dSmrg@end menu 1731debfc3dSmrg 1741debfc3dSmrg@insertcopying 1751debfc3dSmrg@end ifnottex 1761debfc3dSmrg 1771debfc3dSmrg@node Overview 1781debfc3dSmrg@chapter Overview 1791debfc3dSmrg@c man begin DESCRIPTION 1801debfc3dSmrgThe C preprocessor, often known as @dfn{cpp}, is a @dfn{macro processor} 1811debfc3dSmrgthat is used automatically by the C compiler to transform your program 1821debfc3dSmrgbefore compilation. It is called a macro processor because it allows 1831debfc3dSmrgyou to define @dfn{macros}, which are brief abbreviations for longer 1841debfc3dSmrgconstructs. 1851debfc3dSmrg 1861debfc3dSmrgThe C preprocessor is intended to be used only with C, C++, and 1871debfc3dSmrgObjective-C source code. In the past, it has been abused as a general 1881debfc3dSmrgtext processor. It will choke on input which does not obey C's lexical 1891debfc3dSmrgrules. For example, apostrophes will be interpreted as the beginning of 1901debfc3dSmrgcharacter constants, and cause errors. Also, you cannot rely on it 1911debfc3dSmrgpreserving characteristics of the input which are not significant to 1921debfc3dSmrgC-family languages. If a Makefile is preprocessed, all the hard tabs 1931debfc3dSmrgwill be removed, and the Makefile will not work. 1941debfc3dSmrg 1951debfc3dSmrgHaving said that, you can often get away with using cpp on things which 1961debfc3dSmrgare not C@. Other Algol-ish programming languages are often safe 197c0a68be4Smrg(Ada, etc.) So is assembly, with caution. @option{-traditional-cpp} 1981debfc3dSmrgmode preserves more white space, and is otherwise more permissive. Many 1991debfc3dSmrgof the problems can be avoided by writing C or C++ style comments 2001debfc3dSmrginstead of native language comments, and keeping macros simple. 2011debfc3dSmrg 2021debfc3dSmrgWherever possible, you should use a preprocessor geared to the language 2031debfc3dSmrgyou are writing in. Modern versions of the GNU assembler have macro 2041debfc3dSmrgfacilities. Most high level programming languages have their own 2051debfc3dSmrgconditional compilation and inclusion mechanism. If all else fails, 2061debfc3dSmrgtry a true general text processor, such as GNU M4. 2071debfc3dSmrg 2081debfc3dSmrgC preprocessors vary in some details. This manual discusses the GNU C 2091debfc3dSmrgpreprocessor, which provides a small superset of the features of ISO 2101debfc3dSmrgStandard C@. In its default mode, the GNU C preprocessor does not do a 2111debfc3dSmrgfew things required by the standard. These are features which are 2121debfc3dSmrgrarely, if ever, used, and may cause surprising changes to the meaning 2131debfc3dSmrgof a program which does not expect them. To get strict ISO Standard C, 214a2dc1f3fSmrgyou should use the @option{-std=c90}, @option{-std=c99}, 215a2dc1f3fSmrg@option{-std=c11} or @option{-std=c17} options, depending 2161debfc3dSmrgon which version of the standard you want. To get all the mandatory 2171debfc3dSmrgdiagnostics, you must also use @option{-pedantic}. @xref{Invocation}. 2181debfc3dSmrg 2191debfc3dSmrgThis manual describes the behavior of the ISO preprocessor. To 2201debfc3dSmrgminimize gratuitous differences, where the ISO preprocessor's 2211debfc3dSmrgbehavior does not conflict with traditional semantics, the 2221debfc3dSmrgtraditional preprocessor should behave the same way. The various 2231debfc3dSmrgdifferences that do exist are detailed in the section @ref{Traditional 2241debfc3dSmrgMode}. 2251debfc3dSmrg 2261debfc3dSmrgFor clarity, unless noted otherwise, references to @samp{CPP} in this 2271debfc3dSmrgmanual refer to GNU CPP@. 2281debfc3dSmrg@c man end 2291debfc3dSmrg 2301debfc3dSmrg@menu 2311debfc3dSmrg* Character sets:: 2321debfc3dSmrg* Initial processing:: 2331debfc3dSmrg* Tokenization:: 2341debfc3dSmrg* The preprocessing language:: 2351debfc3dSmrg@end menu 2361debfc3dSmrg 2371debfc3dSmrg@node Character sets 2381debfc3dSmrg@section Character sets 2391debfc3dSmrg 2401debfc3dSmrgSource code character set processing in C and related languages is 2411debfc3dSmrgrather complicated. The C standard discusses two character sets, but 2421debfc3dSmrgthere are really at least four. 2431debfc3dSmrg 2441debfc3dSmrgThe files input to CPP might be in any character set at all. CPP's 2451debfc3dSmrgvery first action, before it even looks for line boundaries, is to 2461debfc3dSmrgconvert the file into the character set it uses for internal 2471debfc3dSmrgprocessing. That set is what the C standard calls the @dfn{source} 2481debfc3dSmrgcharacter set. It must be isomorphic with ISO 10646, also known as 2491debfc3dSmrgUnicode. CPP uses the UTF-8 encoding of Unicode. 2501debfc3dSmrg 2511debfc3dSmrgThe character sets of the input files are specified using the 2521debfc3dSmrg@option{-finput-charset=} option. 2531debfc3dSmrg 2541debfc3dSmrgAll preprocessing work (the subject of the rest of this manual) is 2551debfc3dSmrgcarried out in the source character set. If you request textual 2561debfc3dSmrgoutput from the preprocessor with the @option{-E} option, it will be 2571debfc3dSmrgin UTF-8. 2581debfc3dSmrg 2591debfc3dSmrgAfter preprocessing is complete, string and character constants are 2601debfc3dSmrgconverted again, into the @dfn{execution} character set. This 2611debfc3dSmrgcharacter set is under control of the user; the default is UTF-8, 2621debfc3dSmrgmatching the source character set. Wide string and character 2631debfc3dSmrgconstants have their own character set, which is not called out 2641debfc3dSmrgspecifically in the standard. Again, it is under control of the user. 2651debfc3dSmrgThe default is UTF-16 or UTF-32, whichever fits in the target's 2661debfc3dSmrg@code{wchar_t} type, in the target machine's byte 2671debfc3dSmrgorder.@footnote{UTF-16 does not meet the requirements of the C 2681debfc3dSmrgstandard for a wide character set, but the choice of 16-bit 2691debfc3dSmrg@code{wchar_t} is enshrined in some system ABIs so we cannot fix 2701debfc3dSmrgthis.} Octal and hexadecimal escape sequences do not undergo 2711debfc3dSmrgconversion; @t{'\x12'} has the value 0x12 regardless of the currently 2721debfc3dSmrgselected execution character set. All other escapes are replaced by 2731debfc3dSmrgthe character in the source character set that they represent, then 2741debfc3dSmrgconverted to the execution character set, just like unescaped 2751debfc3dSmrgcharacters. 2761debfc3dSmrg 277*8feb0f0bSmrgIn identifiers, characters outside the ASCII range can be specified 278*8feb0f0bSmrgwith the @samp{\u} and @samp{\U} escapes or used directly in the input 279*8feb0f0bSmrgencoding. If strict ISO C90 conformance is specified with an option 2801debfc3dSmrgsuch as @option{-std=c90}, or @option{-fno-extended-identifiers} is 281*8feb0f0bSmrgused, then those constructs are not permitted in identifiers. 2821debfc3dSmrg 2831debfc3dSmrg@node Initial processing 2841debfc3dSmrg@section Initial processing 2851debfc3dSmrg 2861debfc3dSmrgThe preprocessor performs a series of textual transformations on its 2871debfc3dSmrginput. These happen before all other processing. Conceptually, they 2881debfc3dSmrghappen in a rigid order, and the entire file is run through each 2891debfc3dSmrgtransformation before the next one begins. CPP actually does them 2901debfc3dSmrgall at once, for performance reasons. These transformations correspond 2911debfc3dSmrgroughly to the first three ``phases of translation'' described in the C 2921debfc3dSmrgstandard. 2931debfc3dSmrg 2941debfc3dSmrg@enumerate 2951debfc3dSmrg@item 2961debfc3dSmrg@cindex line endings 2971debfc3dSmrgThe input file is read into memory and broken into lines. 2981debfc3dSmrg 2991debfc3dSmrgDifferent systems use different conventions to indicate the end of a 3001debfc3dSmrgline. GCC accepts the ASCII control sequences @kbd{LF}, @kbd{@w{CR 3011debfc3dSmrgLF}} and @kbd{CR} as end-of-line markers. These are the canonical 3021debfc3dSmrgsequences used by Unix, DOS and VMS, and the classic Mac OS (before 3031debfc3dSmrgOSX) respectively. You may therefore safely copy source code written 3041debfc3dSmrgon any of those systems to a different one and use it without 3051debfc3dSmrgconversion. (GCC may lose track of the current line number if a file 3061debfc3dSmrgdoesn't consistently use one convention, as sometimes happens when it 3071debfc3dSmrgis edited on computers with different conventions that share a network 3081debfc3dSmrgfile system.) 3091debfc3dSmrg 3101debfc3dSmrgIf the last line of any input file lacks an end-of-line marker, the end 3111debfc3dSmrgof the file is considered to implicitly supply one. The C standard says 3121debfc3dSmrgthat this condition provokes undefined behavior, so GCC will emit a 3131debfc3dSmrgwarning message. 3141debfc3dSmrg 3151debfc3dSmrg@item 3161debfc3dSmrg@cindex trigraphs 3171debfc3dSmrg@anchor{trigraphs}If trigraphs are enabled, they are replaced by their 3181debfc3dSmrgcorresponding single characters. By default GCC ignores trigraphs, 3191debfc3dSmrgbut if you request a strictly conforming mode with the @option{-std} 3201debfc3dSmrgoption, or you specify the @option{-trigraphs} option, then it 3211debfc3dSmrgconverts them. 3221debfc3dSmrg 3231debfc3dSmrgThese are nine three-character sequences, all starting with @samp{??}, 3241debfc3dSmrgthat are defined by ISO C to stand for single characters. They permit 3251debfc3dSmrgobsolete systems that lack some of C's punctuation to use C@. For 3261debfc3dSmrgexample, @samp{??/} stands for @samp{\}, so @t{'??/n'} is a character 3271debfc3dSmrgconstant for a newline. 3281debfc3dSmrg 3291debfc3dSmrgTrigraphs are not popular and many compilers implement them 3301debfc3dSmrgincorrectly. Portable code should not rely on trigraphs being either 3311debfc3dSmrgconverted or ignored. With @option{-Wtrigraphs} GCC will warn you 3321debfc3dSmrgwhen a trigraph may change the meaning of your program if it were 3331debfc3dSmrgconverted. @xref{Wtrigraphs}. 3341debfc3dSmrg 3351debfc3dSmrgIn a string constant, you can prevent a sequence of question marks 3361debfc3dSmrgfrom being confused with a trigraph by inserting a backslash between 3371debfc3dSmrgthe question marks, or by separating the string literal at the 3381debfc3dSmrgtrigraph and making use of string literal concatenation. @t{"(??\?)"} 3391debfc3dSmrgis the string @samp{(???)}, not @samp{(?]}. Traditional C compilers 3401debfc3dSmrgdo not recognize these idioms. 3411debfc3dSmrg 3421debfc3dSmrgThe nine trigraphs and their replacements are 3431debfc3dSmrg 3441debfc3dSmrg@smallexample 3451debfc3dSmrgTrigraph: ??( ??) ??< ??> ??= ??/ ??' ??! ??- 3461debfc3dSmrgReplacement: [ ] @{ @} # \ ^ | ~ 3471debfc3dSmrg@end smallexample 3481debfc3dSmrg 3491debfc3dSmrg@item 3501debfc3dSmrg@cindex continued lines 3511debfc3dSmrg@cindex backslash-newline 3521debfc3dSmrgContinued lines are merged into one long line. 3531debfc3dSmrg 3541debfc3dSmrgA continued line is a line which ends with a backslash, @samp{\}. The 3551debfc3dSmrgbackslash is removed and the following line is joined with the current 3561debfc3dSmrgone. No space is inserted, so you may split a line anywhere, even in 3571debfc3dSmrgthe middle of a word. (It is generally more readable to split lines 3581debfc3dSmrgonly at white space.) 3591debfc3dSmrg 3601debfc3dSmrgThe trailing backslash on a continued line is commonly referred to as a 3611debfc3dSmrg@dfn{backslash-newline}. 3621debfc3dSmrg 3631debfc3dSmrgIf there is white space between a backslash and the end of a line, that 3641debfc3dSmrgis still a continued line. However, as this is usually the result of an 3651debfc3dSmrgediting mistake, and many compilers will not accept it as a continued 3661debfc3dSmrgline, GCC will warn you about it. 3671debfc3dSmrg 3681debfc3dSmrg@item 3691debfc3dSmrg@cindex comments 3701debfc3dSmrg@cindex line comments 3711debfc3dSmrg@cindex block comments 3721debfc3dSmrgAll comments are replaced with single spaces. 3731debfc3dSmrg 3741debfc3dSmrgThere are two kinds of comments. @dfn{Block comments} begin with 3751debfc3dSmrg@samp{/*} and continue until the next @samp{*/}. Block comments do not 3761debfc3dSmrgnest: 3771debfc3dSmrg 3781debfc3dSmrg@smallexample 3791debfc3dSmrg/* @r{this is} /* @r{one comment} */ @r{text outside comment} 3801debfc3dSmrg@end smallexample 3811debfc3dSmrg 3821debfc3dSmrg@dfn{Line comments} begin with @samp{//} and continue to the end of the 3831debfc3dSmrgcurrent line. Line comments do not nest either, but it does not matter, 3841debfc3dSmrgbecause they would end in the same place anyway. 3851debfc3dSmrg 3861debfc3dSmrg@smallexample 3871debfc3dSmrg// @r{this is} // @r{one comment} 3881debfc3dSmrg@r{text outside comment} 3891debfc3dSmrg@end smallexample 3901debfc3dSmrg@end enumerate 3911debfc3dSmrg 3921debfc3dSmrgIt is safe to put line comments inside block comments, or vice versa. 3931debfc3dSmrg 3941debfc3dSmrg@smallexample 3951debfc3dSmrg@group 3961debfc3dSmrg/* @r{block comment} 3971debfc3dSmrg // @r{contains line comment} 3981debfc3dSmrg @r{yet more comment} 3991debfc3dSmrg */ @r{outside comment} 4001debfc3dSmrg 4011debfc3dSmrg// @r{line comment} /* @r{contains block comment} */ 4021debfc3dSmrg@end group 4031debfc3dSmrg@end smallexample 4041debfc3dSmrg 4051debfc3dSmrgBut beware of commenting out one end of a block comment with a line 4061debfc3dSmrgcomment. 4071debfc3dSmrg 4081debfc3dSmrg@smallexample 4091debfc3dSmrg@group 4101debfc3dSmrg // @r{l.c.} /* @r{block comment begins} 4111debfc3dSmrg @r{oops! this isn't a comment anymore} */ 4121debfc3dSmrg@end group 4131debfc3dSmrg@end smallexample 4141debfc3dSmrg 4151debfc3dSmrgComments are not recognized within string literals. 4161debfc3dSmrg@t{@w{"/* blah */"}} is the string constant @samp{@w{/* blah */}}, not 4171debfc3dSmrgan empty string. 4181debfc3dSmrg 4191debfc3dSmrgLine comments are not in the 1989 edition of the C standard, but they 4201debfc3dSmrgare recognized by GCC as an extension. In C++ and in the 1999 edition 4211debfc3dSmrgof the C standard, they are an official part of the language. 4221debfc3dSmrg 4231debfc3dSmrgSince these transformations happen before all other processing, you can 4241debfc3dSmrgsplit a line mechanically with backslash-newline anywhere. You can 4251debfc3dSmrgcomment out the end of a line. You can continue a line comment onto the 4261debfc3dSmrgnext line with backslash-newline. You can even split @samp{/*}, 4271debfc3dSmrg@samp{*/}, and @samp{//} onto multiple lines with backslash-newline. 4281debfc3dSmrgFor example: 4291debfc3dSmrg 4301debfc3dSmrg@smallexample 4311debfc3dSmrg@group 4321debfc3dSmrg/\ 4331debfc3dSmrg* 4341debfc3dSmrg*/ # /* 4351debfc3dSmrg*/ defi\ 4361debfc3dSmrgne FO\ 4371debfc3dSmrgO 10\ 4381debfc3dSmrg20 4391debfc3dSmrg@end group 4401debfc3dSmrg@end smallexample 4411debfc3dSmrg 4421debfc3dSmrg@noindent 4431debfc3dSmrgis equivalent to @code{@w{#define FOO 1020}}. All these tricks are 4441debfc3dSmrgextremely confusing and should not be used in code intended to be 4451debfc3dSmrgreadable. 4461debfc3dSmrg 4471debfc3dSmrgThere is no way to prevent a backslash at the end of a line from being 4481debfc3dSmrginterpreted as a backslash-newline. This cannot affect any correct 4491debfc3dSmrgprogram, however. 4501debfc3dSmrg 4511debfc3dSmrg@node Tokenization 4521debfc3dSmrg@section Tokenization 4531debfc3dSmrg 4541debfc3dSmrg@cindex tokens 4551debfc3dSmrg@cindex preprocessing tokens 4561debfc3dSmrgAfter the textual transformations are finished, the input file is 4571debfc3dSmrgconverted into a sequence of @dfn{preprocessing tokens}. These mostly 4581debfc3dSmrgcorrespond to the syntactic tokens used by the C compiler, but there are 4591debfc3dSmrga few differences. White space separates tokens; it is not itself a 4601debfc3dSmrgtoken of any kind. Tokens do not have to be separated by white space, 4611debfc3dSmrgbut it is often necessary to avoid ambiguities. 4621debfc3dSmrg 4631debfc3dSmrgWhen faced with a sequence of characters that has more than one possible 4641debfc3dSmrgtokenization, the preprocessor is greedy. It always makes each token, 4651debfc3dSmrgstarting from the left, as big as possible before moving on to the next 4661debfc3dSmrgtoken. For instance, @code{a+++++b} is interpreted as 4671debfc3dSmrg@code{@w{a ++ ++ + b}}, not as @code{@w{a ++ + ++ b}}, even though the 4681debfc3dSmrglatter tokenization could be part of a valid C program and the former 4691debfc3dSmrgcould not. 4701debfc3dSmrg 4711debfc3dSmrgOnce the input file is broken into tokens, the token boundaries never 4721debfc3dSmrgchange, except when the @samp{##} preprocessing operator is used to paste 4731debfc3dSmrgtokens together. @xref{Concatenation}. For example, 4741debfc3dSmrg 4751debfc3dSmrg@smallexample 4761debfc3dSmrg@group 4771debfc3dSmrg#define foo() bar 4781debfc3dSmrgfoo()baz 4791debfc3dSmrg @expansion{} bar baz 4801debfc3dSmrg@emph{not} 4811debfc3dSmrg @expansion{} barbaz 4821debfc3dSmrg@end group 4831debfc3dSmrg@end smallexample 4841debfc3dSmrg 4851debfc3dSmrgThe compiler does not re-tokenize the preprocessor's output. Each 4861debfc3dSmrgpreprocessing token becomes one compiler token. 4871debfc3dSmrg 4881debfc3dSmrg@cindex identifiers 4891debfc3dSmrgPreprocessing tokens fall into five broad classes: identifiers, 4901debfc3dSmrgpreprocessing numbers, string literals, punctuators, and other. An 4911debfc3dSmrg@dfn{identifier} is the same as an identifier in C: any sequence of 4921debfc3dSmrgletters, digits, or underscores, which begins with a letter or 4931debfc3dSmrgunderscore. Keywords of C have no significance to the preprocessor; 4941debfc3dSmrgthey are ordinary identifiers. You can define a macro whose name is a 4951debfc3dSmrgkeyword, for instance. The only identifier which can be considered a 4961debfc3dSmrgpreprocessing keyword is @code{defined}. @xref{Defined}. 4971debfc3dSmrg 4981debfc3dSmrgThis is mostly true of other languages which use the C preprocessor. 4991debfc3dSmrgHowever, a few of the keywords of C++ are significant even in the 5001debfc3dSmrgpreprocessor. @xref{C++ Named Operators}. 5011debfc3dSmrg 5021debfc3dSmrgIn the 1999 C standard, identifiers may contain letters which are not 5031debfc3dSmrgpart of the ``basic source character set'', at the implementation's 5041debfc3dSmrgdiscretion (such as accented Latin letters, Greek letters, or Chinese 5051debfc3dSmrgideograms). This may be done with an extended character set, or the 506*8feb0f0bSmrg@samp{\u} and @samp{\U} escape sequences. 5071debfc3dSmrg 5081debfc3dSmrgAs an extension, GCC treats @samp{$} as a letter. This is for 5091debfc3dSmrgcompatibility with some systems, such as VMS, where @samp{$} is commonly 5101debfc3dSmrgused in system-defined function and object names. @samp{$} is not a 5111debfc3dSmrgletter in strictly conforming mode, or if you specify the @option{-$} 5121debfc3dSmrgoption. @xref{Invocation}. 5131debfc3dSmrg 5141debfc3dSmrg@cindex numbers 5151debfc3dSmrg@cindex preprocessing numbers 5161debfc3dSmrgA @dfn{preprocessing number} has a rather bizarre definition. The 5171debfc3dSmrgcategory includes all the normal integer and floating point constants 5181debfc3dSmrgone expects of C, but also a number of other things one might not 5191debfc3dSmrginitially recognize as a number. Formally, preprocessing numbers begin 5201debfc3dSmrgwith an optional period, a required decimal digit, and then continue 5211debfc3dSmrgwith any sequence of letters, digits, underscores, periods, and 5221debfc3dSmrgexponents. Exponents are the two-character sequences @samp{e+}, 5231debfc3dSmrg@samp{e-}, @samp{E+}, @samp{E-}, @samp{p+}, @samp{p-}, @samp{P+}, and 5241debfc3dSmrg@samp{P-}. (The exponents that begin with @samp{p} or @samp{P} are 5251debfc3dSmrgused for hexadecimal floating-point constants.) 5261debfc3dSmrg 5271debfc3dSmrgThe purpose of this unusual definition is to isolate the preprocessor 5281debfc3dSmrgfrom the full complexity of numeric constants. It does not have to 5291debfc3dSmrgdistinguish between lexically valid and invalid floating-point numbers, 5301debfc3dSmrgwhich is complicated. The definition also permits you to split an 5311debfc3dSmrgidentifier at any position and get exactly two tokens, which can then be 5321debfc3dSmrgpasted back together with the @samp{##} operator. 5331debfc3dSmrg 5341debfc3dSmrgIt's possible for preprocessing numbers to cause programs to be 5351debfc3dSmrgmisinterpreted. For example, @code{0xE+12} is a preprocessing number 5361debfc3dSmrgwhich does not translate to any valid numeric constant, therefore a 5371debfc3dSmrgsyntax error. It does not mean @code{@w{0xE + 12}}, which is what you 5381debfc3dSmrgmight have intended. 5391debfc3dSmrg 5401debfc3dSmrg@cindex string literals 5411debfc3dSmrg@cindex string constants 5421debfc3dSmrg@cindex character constants 5431debfc3dSmrg@cindex header file names 5441debfc3dSmrg@c the @: prevents makeinfo from turning '' into ". 5451debfc3dSmrg@dfn{String literals} are string constants, character constants, and 5461debfc3dSmrgheader file names (the argument of @samp{#include}).@footnote{The C 5471debfc3dSmrgstandard uses the term @dfn{string literal} to refer only to what we are 5481debfc3dSmrgcalling @dfn{string constants}.} String constants and character 5491debfc3dSmrgconstants are straightforward: @t{"@dots{}"} or @t{'@dots{}'}. In 5501debfc3dSmrgeither case embedded quotes should be escaped with a backslash: 5511debfc3dSmrg@t{'\'@:'} is the character constant for @samp{'}. There is no limit on 5521debfc3dSmrgthe length of a character constant, but the value of a character 5531debfc3dSmrgconstant that contains more than one character is 5541debfc3dSmrgimplementation-defined. @xref{Implementation Details}. 5551debfc3dSmrg 5561debfc3dSmrgHeader file names either look like string constants, @t{"@dots{}"}, or are 5571debfc3dSmrgwritten with angle brackets instead, @t{<@dots{}>}. In either case, 5581debfc3dSmrgbackslash is an ordinary character. There is no way to escape the 5591debfc3dSmrgclosing quote or angle bracket. The preprocessor looks for the header 5601debfc3dSmrgfile in different places depending on which form you use. @xref{Include 5611debfc3dSmrgOperation}. 5621debfc3dSmrg 5631debfc3dSmrgNo string literal may extend past the end of a line. You may use continued 5641debfc3dSmrglines instead, or string constant concatenation. 5651debfc3dSmrg 5661debfc3dSmrg@cindex punctuators 5671debfc3dSmrg@cindex digraphs 5681debfc3dSmrg@cindex alternative tokens 5691debfc3dSmrg@dfn{Punctuators} are all the usual bits of punctuation which are 5701debfc3dSmrgmeaningful to C and C++. All but three of the punctuation characters in 5711debfc3dSmrgASCII are C punctuators. The exceptions are @samp{@@}, @samp{$}, and 5721debfc3dSmrg@samp{`}. In addition, all the two- and three-character operators are 5731debfc3dSmrgpunctuators. There are also six @dfn{digraphs}, which the C++ standard 5741debfc3dSmrgcalls @dfn{alternative tokens}, which are merely alternate ways to spell 5751debfc3dSmrgother punctuators. This is a second attempt to work around missing 5761debfc3dSmrgpunctuation in obsolete systems. It has no negative side effects, 5771debfc3dSmrgunlike trigraphs, but does not cover as much ground. The digraphs and 5781debfc3dSmrgtheir corresponding normal punctuators are: 5791debfc3dSmrg 5801debfc3dSmrg@smallexample 5811debfc3dSmrgDigraph: <% %> <: :> %: %:%: 5821debfc3dSmrgPunctuator: @{ @} [ ] # ## 5831debfc3dSmrg@end smallexample 5841debfc3dSmrg 5851debfc3dSmrg@cindex other tokens 586*8feb0f0bSmrgAny other single byte is considered ``other'' and passed on to the 587*8feb0f0bSmrgpreprocessor's output unchanged. The C compiler will almost certainly 588*8feb0f0bSmrgreject source code containing ``other'' tokens. In ASCII, the only 589*8feb0f0bSmrg``other'' characters are @samp{@@}, @samp{$}, @samp{`}, and control 5901debfc3dSmrgcharacters other than NUL (all bits zero). (Note that @samp{$} is 591*8feb0f0bSmrgnormally considered a letter.) All bytes with the high bit set 592*8feb0f0bSmrg(numeric range 0x7F--0xFF) that were not succesfully interpreted as 593*8feb0f0bSmrgpart of an extended character in the input encoding are also ``other'' 594*8feb0f0bSmrgin the present implementation. 5951debfc3dSmrg 5961debfc3dSmrgNUL is a special case because of the high probability that its 5971debfc3dSmrgappearance is accidental, and because it may be invisible to the user 5981debfc3dSmrg(many terminals do not display NUL at all). Within comments, NULs are 5991debfc3dSmrgsilently ignored, just as any other character would be. In running 6001debfc3dSmrgtext, NUL is considered white space. For example, these two directives 6011debfc3dSmrghave the same meaning. 6021debfc3dSmrg 6031debfc3dSmrg@smallexample 6041debfc3dSmrg#define X^@@1 6051debfc3dSmrg#define X 1 6061debfc3dSmrg@end smallexample 6071debfc3dSmrg 6081debfc3dSmrg@noindent 6091debfc3dSmrg(where @samp{^@@} is ASCII NUL)@. Within string or character constants, 6101debfc3dSmrgNULs are preserved. In the latter two cases the preprocessor emits a 6111debfc3dSmrgwarning message. 6121debfc3dSmrg 6131debfc3dSmrg@node The preprocessing language 6141debfc3dSmrg@section The preprocessing language 6151debfc3dSmrg@cindex directives 6161debfc3dSmrg@cindex preprocessing directives 6171debfc3dSmrg@cindex directive line 6181debfc3dSmrg@cindex directive name 6191debfc3dSmrg 6201debfc3dSmrgAfter tokenization, the stream of tokens may simply be passed straight 6211debfc3dSmrgto the compiler's parser. However, if it contains any operations in the 6221debfc3dSmrg@dfn{preprocessing language}, it will be transformed first. This stage 6231debfc3dSmrgcorresponds roughly to the standard's ``translation phase 4'' and is 6241debfc3dSmrgwhat most people think of as the preprocessor's job. 6251debfc3dSmrg 6261debfc3dSmrgThe preprocessing language consists of @dfn{directives} to be executed 6271debfc3dSmrgand @dfn{macros} to be expanded. Its primary capabilities are: 6281debfc3dSmrg 6291debfc3dSmrg@itemize @bullet 6301debfc3dSmrg@item 6311debfc3dSmrgInclusion of header files. These are files of declarations that can be 6321debfc3dSmrgsubstituted into your program. 6331debfc3dSmrg 6341debfc3dSmrg@item 6351debfc3dSmrgMacro expansion. You can define @dfn{macros}, which are abbreviations 6361debfc3dSmrgfor arbitrary fragments of C code. The preprocessor will replace the 6371debfc3dSmrgmacros with their definitions throughout the program. Some macros are 6381debfc3dSmrgautomatically defined for you. 6391debfc3dSmrg 6401debfc3dSmrg@item 6411debfc3dSmrgConditional compilation. You can include or exclude parts of the 6421debfc3dSmrgprogram according to various conditions. 6431debfc3dSmrg 6441debfc3dSmrg@item 6451debfc3dSmrgLine control. If you use a program to combine or rearrange source files 6461debfc3dSmrginto an intermediate file which is then compiled, you can use line 6471debfc3dSmrgcontrol to inform the compiler where each source line originally came 6481debfc3dSmrgfrom. 6491debfc3dSmrg 6501debfc3dSmrg@item 6511debfc3dSmrgDiagnostics. You can detect problems at compile time and issue errors 6521debfc3dSmrgor warnings. 6531debfc3dSmrg@end itemize 6541debfc3dSmrg 6551debfc3dSmrgThere are a few more, less useful, features. 6561debfc3dSmrg 6571debfc3dSmrgExcept for expansion of predefined macros, all these operations are 6581debfc3dSmrgtriggered with @dfn{preprocessing directives}. Preprocessing directives 6591debfc3dSmrgare lines in your program that start with @samp{#}. Whitespace is 6601debfc3dSmrgallowed before and after the @samp{#}. The @samp{#} is followed by an 6611debfc3dSmrgidentifier, the @dfn{directive name}. It specifies the operation to 6621debfc3dSmrgperform. Directives are commonly referred to as @samp{#@var{name}} 6631debfc3dSmrgwhere @var{name} is the directive name. For example, @samp{#define} is 6641debfc3dSmrgthe directive that defines a macro. 6651debfc3dSmrg 6661debfc3dSmrgThe @samp{#} which begins a directive cannot come from a macro 6671debfc3dSmrgexpansion. Also, the directive name is not macro expanded. Thus, if 6681debfc3dSmrg@code{foo} is defined as a macro expanding to @code{define}, that does 6691debfc3dSmrgnot make @samp{#foo} a valid preprocessing directive. 6701debfc3dSmrg 6711debfc3dSmrgThe set of valid directive names is fixed. Programs cannot define new 6721debfc3dSmrgpreprocessing directives. 6731debfc3dSmrg 6741debfc3dSmrgSome directives require arguments; these make up the rest of the 6751debfc3dSmrgdirective line and must be separated from the directive name by 6761debfc3dSmrgwhitespace. For example, @samp{#define} must be followed by a macro 6771debfc3dSmrgname and the intended expansion of the macro. 6781debfc3dSmrg 6791debfc3dSmrgA preprocessing directive cannot cover more than one line. The line 6801debfc3dSmrgmay, however, be continued with backslash-newline, or by a block comment 6811debfc3dSmrgwhich extends past the end of the line. In either case, when the 6821debfc3dSmrgdirective is processed, the continuations have already been merged with 6831debfc3dSmrgthe first line to make one long line. 6841debfc3dSmrg 6851debfc3dSmrg@node Header Files 6861debfc3dSmrg@chapter Header Files 6871debfc3dSmrg 6881debfc3dSmrg@cindex header file 6891debfc3dSmrgA header file is a file containing C declarations and macro definitions 6901debfc3dSmrg(@pxref{Macros}) to be shared between several source files. You request 6911debfc3dSmrgthe use of a header file in your program by @dfn{including} it, with the 6921debfc3dSmrgC preprocessing directive @samp{#include}. 6931debfc3dSmrg 6941debfc3dSmrgHeader files serve two purposes. 6951debfc3dSmrg 6961debfc3dSmrg@itemize @bullet 6971debfc3dSmrg@item 6981debfc3dSmrg@cindex system header files 6991debfc3dSmrgSystem header files declare the interfaces to parts of the operating 7001debfc3dSmrgsystem. You include them in your program to supply the definitions and 7011debfc3dSmrgdeclarations you need to invoke system calls and libraries. 7021debfc3dSmrg 7031debfc3dSmrg@item 7041debfc3dSmrgYour own header files contain declarations for interfaces between the 7051debfc3dSmrgsource files of your program. Each time you have a group of related 7061debfc3dSmrgdeclarations and macro definitions all or most of which are needed in 7071debfc3dSmrgseveral different source files, it is a good idea to create a header 7081debfc3dSmrgfile for them. 7091debfc3dSmrg@end itemize 7101debfc3dSmrg 7111debfc3dSmrgIncluding a header file produces the same results as copying the header 7121debfc3dSmrgfile into each source file that needs it. Such copying would be 7131debfc3dSmrgtime-consuming and error-prone. With a header file, the related 7141debfc3dSmrgdeclarations appear in only one place. If they need to be changed, they 7151debfc3dSmrgcan be changed in one place, and programs that include the header file 7161debfc3dSmrgwill automatically use the new version when next recompiled. The header 7171debfc3dSmrgfile eliminates the labor of finding and changing all the copies as well 7181debfc3dSmrgas the risk that a failure to find one copy will result in 7191debfc3dSmrginconsistencies within a program. 7201debfc3dSmrg 7211debfc3dSmrgIn C, the usual convention is to give header files names that end with 7221debfc3dSmrg@file{.h}. It is most portable to use only letters, digits, dashes, and 7231debfc3dSmrgunderscores in header file names, and at most one dot. 7241debfc3dSmrg 7251debfc3dSmrg@menu 7261debfc3dSmrg* Include Syntax:: 7271debfc3dSmrg* Include Operation:: 7281debfc3dSmrg* Search Path:: 7291debfc3dSmrg* Once-Only Headers:: 7301debfc3dSmrg* Alternatives to Wrapper #ifndef:: 7311debfc3dSmrg* Computed Includes:: 7321debfc3dSmrg* Wrapper Headers:: 7331debfc3dSmrg* System Headers:: 7341debfc3dSmrg@end menu 7351debfc3dSmrg 7361debfc3dSmrg@node Include Syntax 7371debfc3dSmrg@section Include Syntax 7381debfc3dSmrg 7391debfc3dSmrg@findex #include 7401debfc3dSmrgBoth user and system header files are included using the preprocessing 7411debfc3dSmrgdirective @samp{#include}. It has two variants: 7421debfc3dSmrg 7431debfc3dSmrg@table @code 7441debfc3dSmrg@item #include <@var{file}> 7451debfc3dSmrgThis variant is used for system header files. It searches for a file 7461debfc3dSmrgnamed @var{file} in a standard list of system directories. You can prepend 7471debfc3dSmrgdirectories to this list with the @option{-I} option (@pxref{Invocation}). 7481debfc3dSmrg 7491debfc3dSmrg@item #include "@var{file}" 7501debfc3dSmrgThis variant is used for header files of your own program. It 7511debfc3dSmrgsearches for a file named @var{file} first in the directory containing 7521debfc3dSmrgthe current file, then in the quote directories and then the same 7531debfc3dSmrgdirectories used for @code{<@var{file}>}. You can prepend directories 7541debfc3dSmrgto the list of quote directories with the @option{-iquote} option. 7551debfc3dSmrg@end table 7561debfc3dSmrg 7571debfc3dSmrgThe argument of @samp{#include}, whether delimited with quote marks or 7581debfc3dSmrgangle brackets, behaves like a string constant in that comments are not 7591debfc3dSmrgrecognized, and macro names are not expanded. Thus, @code{@w{#include 7601debfc3dSmrg<x/*y>}} specifies inclusion of a system header file named @file{x/*y}. 7611debfc3dSmrg 7621debfc3dSmrgHowever, if backslashes occur within @var{file}, they are considered 7631debfc3dSmrgordinary text characters, not escape characters. None of the character 7641debfc3dSmrgescape sequences appropriate to string constants in C are processed. 7651debfc3dSmrgThus, @code{@w{#include "x\n\\y"}} specifies a filename containing three 7661debfc3dSmrgbackslashes. (Some systems interpret @samp{\} as a pathname separator. 7671debfc3dSmrgAll of these also interpret @samp{/} the same way. It is most portable 7681debfc3dSmrgto use only @samp{/}.) 7691debfc3dSmrg 7701debfc3dSmrgIt is an error if there is anything (other than comments) on the line 7711debfc3dSmrgafter the file name. 7721debfc3dSmrg 7731debfc3dSmrg@node Include Operation 7741debfc3dSmrg@section Include Operation 7751debfc3dSmrg 7761debfc3dSmrgThe @samp{#include} directive works by directing the C preprocessor to 7771debfc3dSmrgscan the specified file as input before continuing with the rest of the 7781debfc3dSmrgcurrent file. The output from the preprocessor contains the output 7791debfc3dSmrgalready generated, followed by the output resulting from the included 7801debfc3dSmrgfile, followed by the output that comes from the text after the 7811debfc3dSmrg@samp{#include} directive. For example, if you have a header file 7821debfc3dSmrg@file{header.h} as follows, 7831debfc3dSmrg 7841debfc3dSmrg@smallexample 7851debfc3dSmrgchar *test (void); 7861debfc3dSmrg@end smallexample 7871debfc3dSmrg 7881debfc3dSmrg@noindent 7891debfc3dSmrgand a main program called @file{program.c} that uses the header file, 7901debfc3dSmrglike this, 7911debfc3dSmrg 7921debfc3dSmrg@smallexample 7931debfc3dSmrgint x; 7941debfc3dSmrg#include "header.h" 7951debfc3dSmrg 7961debfc3dSmrgint 7971debfc3dSmrgmain (void) 7981debfc3dSmrg@{ 7991debfc3dSmrg puts (test ()); 8001debfc3dSmrg@} 8011debfc3dSmrg@end smallexample 8021debfc3dSmrg 8031debfc3dSmrg@noindent 8041debfc3dSmrgthe compiler will see the same token stream as it would if 8051debfc3dSmrg@file{program.c} read 8061debfc3dSmrg 8071debfc3dSmrg@smallexample 8081debfc3dSmrgint x; 8091debfc3dSmrgchar *test (void); 8101debfc3dSmrg 8111debfc3dSmrgint 8121debfc3dSmrgmain (void) 8131debfc3dSmrg@{ 8141debfc3dSmrg puts (test ()); 8151debfc3dSmrg@} 8161debfc3dSmrg@end smallexample 8171debfc3dSmrg 8181debfc3dSmrgIncluded files are not limited to declarations and macro definitions; 8191debfc3dSmrgthose are merely the typical uses. Any fragment of a C program can be 8201debfc3dSmrgincluded from another file. The include file could even contain the 8211debfc3dSmrgbeginning of a statement that is concluded in the containing file, or 8221debfc3dSmrgthe end of a statement that was started in the including file. However, 8231debfc3dSmrgan included file must consist of complete tokens. Comments and string 8241debfc3dSmrgliterals which have not been closed by the end of an included file are 8251debfc3dSmrginvalid. For error recovery, they are considered to end at the end of 8261debfc3dSmrgthe file. 8271debfc3dSmrg 8281debfc3dSmrgTo avoid confusion, it is best if header files contain only complete 8291debfc3dSmrgsyntactic units---function declarations or definitions, type 8301debfc3dSmrgdeclarations, etc. 8311debfc3dSmrg 8321debfc3dSmrgThe line following the @samp{#include} directive is always treated as a 8331debfc3dSmrgseparate line by the C preprocessor, even if the included file lacks a 8341debfc3dSmrgfinal newline. 8351debfc3dSmrg 8361debfc3dSmrg@node Search Path 8371debfc3dSmrg@section Search Path 8381debfc3dSmrg 8391debfc3dSmrgBy default, the preprocessor looks for header files included by the quote 8401debfc3dSmrgform of the directive @code{@w{#include "@var{file}"}} first relative to 8411debfc3dSmrgthe directory of the current file, and then in a preconfigured list 8421debfc3dSmrgof standard system directories. 8431debfc3dSmrgFor example, if @file{/usr/include/sys/stat.h} contains 8441debfc3dSmrg@code{@w{#include "types.h"}}, GCC looks for @file{types.h} first in 8451debfc3dSmrg@file{/usr/include/sys}, then in its usual search path. 8461debfc3dSmrg 8471debfc3dSmrgFor the angle-bracket form @code{@w{#include <@var{file}>}}, the 8481debfc3dSmrgpreprocessor's default behavior is to look only in the standard system 8491debfc3dSmrgdirectories. The exact search directory list depends on the target 8501debfc3dSmrgsystem, how GCC is configured, and where it is installed. You can 8511debfc3dSmrgfind the default search directory list for your version of CPP by 8521debfc3dSmrginvoking it with the @option{-v} option. For example, 8531debfc3dSmrg 8541debfc3dSmrg@smallexample 8551debfc3dSmrgcpp -v /dev/null -o /dev/null 8561debfc3dSmrg@end smallexample 8571debfc3dSmrg 8581debfc3dSmrgThere are a number of command-line options you can use to add 8591debfc3dSmrgadditional directories to the search path. 8601debfc3dSmrgThe most commonly-used option is @option{-I@var{dir}}, which causes 8611debfc3dSmrg@var{dir} to be searched after the current directory (for the quote 8621debfc3dSmrgform of the directive) and ahead of the standard system directories. 8631debfc3dSmrgYou can specify multiple @option{-I} options on the command line, 8641debfc3dSmrgin which case the directories are searched in left-to-right order. 8651debfc3dSmrg 8661debfc3dSmrgIf you need separate control over the search paths for the quote and 8671debfc3dSmrgangle-bracket forms of the @samp{#include} directive, you can use the 8681debfc3dSmrg@option{-iquote} and/or @option{-isystem} options instead of @option{-I}. 8691debfc3dSmrg@xref{Invocation}, for a detailed description of these options, as 8701debfc3dSmrgwell as others that are less generally useful. 8711debfc3dSmrg 8721debfc3dSmrgIf you specify other options on the command line, such as @option{-I}, 8731debfc3dSmrgthat affect where the preprocessor searches for header files, the 8741debfc3dSmrgdirectory list printed by the @option{-v} option reflects the actual 8751debfc3dSmrgsearch path used by the preprocessor. 8761debfc3dSmrg 8771debfc3dSmrgNote that you can also prevent the preprocessor from searching any of 8781debfc3dSmrgthe default system header directories with the @option{-nostdinc} 8791debfc3dSmrgoption. This is useful when you are compiling an operating system 8801debfc3dSmrgkernel or some other program that does not use the standard C library 8811debfc3dSmrgfacilities, or the standard C library itself. 8821debfc3dSmrg 8831debfc3dSmrg@node Once-Only Headers 8841debfc3dSmrg@section Once-Only Headers 8851debfc3dSmrg@cindex repeated inclusion 8861debfc3dSmrg@cindex including just once 8871debfc3dSmrg@cindex wrapper @code{#ifndef} 8881debfc3dSmrg 8891debfc3dSmrgIf a header file happens to be included twice, the compiler will process 8901debfc3dSmrgits contents twice. This is very likely to cause an error, e.g.@: when the 8911debfc3dSmrgcompiler sees the same structure definition twice. Even if it does not, 8921debfc3dSmrgit will certainly waste time. 8931debfc3dSmrg 8941debfc3dSmrgThe standard way to prevent this is to enclose the entire real contents 8951debfc3dSmrgof the file in a conditional, like this: 8961debfc3dSmrg 8971debfc3dSmrg@smallexample 8981debfc3dSmrg@group 8991debfc3dSmrg/* File foo. */ 9001debfc3dSmrg#ifndef FILE_FOO_SEEN 9011debfc3dSmrg#define FILE_FOO_SEEN 9021debfc3dSmrg 9031debfc3dSmrg@var{the entire file} 9041debfc3dSmrg 9051debfc3dSmrg#endif /* !FILE_FOO_SEEN */ 9061debfc3dSmrg@end group 9071debfc3dSmrg@end smallexample 9081debfc3dSmrg 9091debfc3dSmrgThis construct is commonly known as a @dfn{wrapper #ifndef}. 9101debfc3dSmrgWhen the header is included again, the conditional will be false, 9111debfc3dSmrgbecause @code{FILE_FOO_SEEN} is defined. The preprocessor will skip 9121debfc3dSmrgover the entire contents of the file, and the compiler will not see it 9131debfc3dSmrgtwice. 9141debfc3dSmrg 9151debfc3dSmrgCPP optimizes even further. It remembers when a header file has a 9161debfc3dSmrgwrapper @samp{#ifndef}. If a subsequent @samp{#include} specifies that 9171debfc3dSmrgheader, and the macro in the @samp{#ifndef} is still defined, it does 9181debfc3dSmrgnot bother to rescan the file at all. 9191debfc3dSmrg 9201debfc3dSmrgYou can put comments outside the wrapper. They will not interfere with 9211debfc3dSmrgthis optimization. 9221debfc3dSmrg 9231debfc3dSmrg@cindex controlling macro 9241debfc3dSmrg@cindex guard macro 9251debfc3dSmrgThe macro @code{FILE_FOO_SEEN} is called the @dfn{controlling macro} or 9261debfc3dSmrg@dfn{guard macro}. In a user header file, the macro name should not 9271debfc3dSmrgbegin with @samp{_}. In a system header file, it should begin with 9281debfc3dSmrg@samp{__} to avoid conflicts with user programs. In any kind of header 9291debfc3dSmrgfile, the macro name should contain the name of the file and some 9301debfc3dSmrgadditional text, to avoid conflicts with other header files. 9311debfc3dSmrg 9321debfc3dSmrg@node Alternatives to Wrapper #ifndef 9331debfc3dSmrg@section Alternatives to Wrapper #ifndef 9341debfc3dSmrg 9351debfc3dSmrgCPP supports two more ways of indicating that a header file should be 9361debfc3dSmrgread only once. Neither one is as portable as a wrapper @samp{#ifndef} 9371debfc3dSmrgand we recommend you do not use them in new programs, with the caveat 9381debfc3dSmrgthat @samp{#import} is standard practice in Objective-C. 9391debfc3dSmrg 9401debfc3dSmrg@findex #import 9411debfc3dSmrgCPP supports a variant of @samp{#include} called @samp{#import} which 9421debfc3dSmrgincludes a file, but does so at most once. If you use @samp{#import} 9431debfc3dSmrginstead of @samp{#include}, then you don't need the conditionals 9441debfc3dSmrginside the header file to prevent multiple inclusion of the contents. 9451debfc3dSmrg@samp{#import} is standard in Objective-C, but is considered a 9461debfc3dSmrgdeprecated extension in C and C++. 9471debfc3dSmrg 9481debfc3dSmrg@samp{#import} is not a well designed feature. It requires the users of 9491debfc3dSmrga header file to know that it should only be included once. It is much 9501debfc3dSmrgbetter for the header file's implementor to write the file so that users 9511debfc3dSmrgdon't need to know this. Using a wrapper @samp{#ifndef} accomplishes 9521debfc3dSmrgthis goal. 9531debfc3dSmrg 9541debfc3dSmrgIn the present implementation, a single use of @samp{#import} will 9551debfc3dSmrgprevent the file from ever being read again, by either @samp{#import} or 9561debfc3dSmrg@samp{#include}. You should not rely on this; do not use both 9571debfc3dSmrg@samp{#import} and @samp{#include} to refer to the same header file. 9581debfc3dSmrg 9591debfc3dSmrgAnother way to prevent a header file from being included more than once 960c0a68be4Smrgis with the @samp{#pragma once} directive (@pxref{Pragmas}). 9611debfc3dSmrg@samp{#pragma once} does not have the problems that @samp{#import} does, 9621debfc3dSmrgbut it is not recognized by all preprocessors, so you cannot rely on it 9631debfc3dSmrgin a portable program. 9641debfc3dSmrg 9651debfc3dSmrg@node Computed Includes 9661debfc3dSmrg@section Computed Includes 9671debfc3dSmrg@cindex computed includes 9681debfc3dSmrg@cindex macros in include 9691debfc3dSmrg 9701debfc3dSmrgSometimes it is necessary to select one of several different header 9711debfc3dSmrgfiles to be included into your program. They might specify 9721debfc3dSmrgconfiguration parameters to be used on different sorts of operating 9731debfc3dSmrgsystems, for instance. You could do this with a series of conditionals, 9741debfc3dSmrg 9751debfc3dSmrg@smallexample 9761debfc3dSmrg#if SYSTEM_1 9771debfc3dSmrg# include "system_1.h" 9781debfc3dSmrg#elif SYSTEM_2 9791debfc3dSmrg# include "system_2.h" 9801debfc3dSmrg#elif SYSTEM_3 9811debfc3dSmrg@dots{} 9821debfc3dSmrg#endif 9831debfc3dSmrg@end smallexample 9841debfc3dSmrg 9851debfc3dSmrgThat rapidly becomes tedious. Instead, the preprocessor offers the 9861debfc3dSmrgability to use a macro for the header name. This is called a 9871debfc3dSmrg@dfn{computed include}. Instead of writing a header name as the direct 9881debfc3dSmrgargument of @samp{#include}, you simply put a macro name there instead: 9891debfc3dSmrg 9901debfc3dSmrg@smallexample 9911debfc3dSmrg#define SYSTEM_H "system_1.h" 9921debfc3dSmrg@dots{} 9931debfc3dSmrg#include SYSTEM_H 9941debfc3dSmrg@end smallexample 9951debfc3dSmrg 9961debfc3dSmrg@noindent 9971debfc3dSmrg@code{SYSTEM_H} will be expanded, and the preprocessor will look for 9981debfc3dSmrg@file{system_1.h} as if the @samp{#include} had been written that way 9991debfc3dSmrgoriginally. @code{SYSTEM_H} could be defined by your Makefile with a 10001debfc3dSmrg@option{-D} option. 10011debfc3dSmrg 10021debfc3dSmrgYou must be careful when you define the macro. @samp{#define} saves 10031debfc3dSmrgtokens, not text. The preprocessor has no way of knowing that the macro 10041debfc3dSmrgwill be used as the argument of @samp{#include}, so it generates 10051debfc3dSmrgordinary tokens, not a header name. This is unlikely to cause problems 10061debfc3dSmrgif you use double-quote includes, which are close enough to string 10071debfc3dSmrgconstants. If you use angle brackets, however, you may have trouble. 10081debfc3dSmrg 10091debfc3dSmrgThe syntax of a computed include is actually a bit more general than the 10101debfc3dSmrgabove. If the first non-whitespace character after @samp{#include} is 10111debfc3dSmrgnot @samp{"} or @samp{<}, then the entire line is macro-expanded 10121debfc3dSmrglike running text would be. 10131debfc3dSmrg 10141debfc3dSmrgIf the line expands to a single string constant, the contents of that 10151debfc3dSmrgstring constant are the file to be included. CPP does not re-examine the 10161debfc3dSmrgstring for embedded quotes, but neither does it process backslash 10171debfc3dSmrgescapes in the string. Therefore 10181debfc3dSmrg 10191debfc3dSmrg@smallexample 10201debfc3dSmrg#define HEADER "a\"b" 10211debfc3dSmrg#include HEADER 10221debfc3dSmrg@end smallexample 10231debfc3dSmrg 10241debfc3dSmrg@noindent 10251debfc3dSmrglooks for a file named @file{a\"b}. CPP searches for the file according 10261debfc3dSmrgto the rules for double-quoted includes. 10271debfc3dSmrg 10281debfc3dSmrgIf the line expands to a token stream beginning with a @samp{<} token 10291debfc3dSmrgand including a @samp{>} token, then the tokens between the @samp{<} and 10301debfc3dSmrgthe first @samp{>} are combined to form the filename to be included. 10311debfc3dSmrgAny whitespace between tokens is reduced to a single space; then any 10321debfc3dSmrgspace after the initial @samp{<} is retained, but a trailing space 10331debfc3dSmrgbefore the closing @samp{>} is ignored. CPP searches for the file 10341debfc3dSmrgaccording to the rules for angle-bracket includes. 10351debfc3dSmrg 10361debfc3dSmrgIn either case, if there are any tokens on the line after the file name, 10371debfc3dSmrgan error occurs and the directive is not processed. It is also an error 10381debfc3dSmrgif the result of expansion does not match either of the two expected 10391debfc3dSmrgforms. 10401debfc3dSmrg 10411debfc3dSmrgThese rules are implementation-defined behavior according to the C 10421debfc3dSmrgstandard. To minimize the risk of different compilers interpreting your 10431debfc3dSmrgcomputed includes differently, we recommend you use only a single 10441debfc3dSmrgobject-like macro which expands to a string constant. This will also 10451debfc3dSmrgminimize confusion for people reading your program. 10461debfc3dSmrg 10471debfc3dSmrg@node Wrapper Headers 10481debfc3dSmrg@section Wrapper Headers 10491debfc3dSmrg@cindex wrapper headers 10501debfc3dSmrg@cindex overriding a header file 10511debfc3dSmrg@findex #include_next 10521debfc3dSmrg 10531debfc3dSmrgSometimes it is necessary to adjust the contents of a system-provided 10541debfc3dSmrgheader file without editing it directly. GCC's @command{fixincludes} 10551debfc3dSmrgoperation does this, for example. One way to do that would be to create 10561debfc3dSmrga new header file with the same name and insert it in the search path 10571debfc3dSmrgbefore the original header. That works fine as long as you're willing 10581debfc3dSmrgto replace the old header entirely. But what if you want to refer to 10591debfc3dSmrgthe old header from the new one? 10601debfc3dSmrg 10611debfc3dSmrgYou cannot simply include the old header with @samp{#include}. That 10621debfc3dSmrgwill start from the beginning, and find your new header again. If your 10631debfc3dSmrgheader is not protected from multiple inclusion (@pxref{Once-Only 10641debfc3dSmrgHeaders}), it will recurse infinitely and cause a fatal error. 10651debfc3dSmrg 10661debfc3dSmrgYou could include the old header with an absolute pathname: 10671debfc3dSmrg@smallexample 10681debfc3dSmrg#include "/usr/include/old-header.h" 10691debfc3dSmrg@end smallexample 10701debfc3dSmrg@noindent 10711debfc3dSmrgThis works, but is not clean; should the system headers ever move, you 10721debfc3dSmrgwould have to edit the new headers to match. 10731debfc3dSmrg 10741debfc3dSmrgThere is no way to solve this problem within the C standard, but you can 10751debfc3dSmrguse the GNU extension @samp{#include_next}. It means, ``Include the 10761debfc3dSmrg@emph{next} file with this name''. This directive works like 10771debfc3dSmrg@samp{#include} except in searching for the specified file: it starts 10781debfc3dSmrgsearching the list of header file directories @emph{after} the directory 10791debfc3dSmrgin which the current file was found. 10801debfc3dSmrg 10811debfc3dSmrgSuppose you specify @option{-I /usr/local/include}, and the list of 10821debfc3dSmrgdirectories to search also includes @file{/usr/include}; and suppose 10831debfc3dSmrgboth directories contain @file{signal.h}. Ordinary @code{@w{#include 10841debfc3dSmrg<signal.h>}} finds the file under @file{/usr/local/include}. If that 10851debfc3dSmrgfile contains @code{@w{#include_next <signal.h>}}, it starts searching 10861debfc3dSmrgafter that directory, and finds the file in @file{/usr/include}. 10871debfc3dSmrg 10881debfc3dSmrg@samp{#include_next} does not distinguish between @code{<@var{file}>} 10891debfc3dSmrgand @code{"@var{file}"} inclusion, nor does it check that the file you 10901debfc3dSmrgspecify has the same name as the current file. It simply looks for the 10911debfc3dSmrgfile named, starting with the directory in the search path after the one 10921debfc3dSmrgwhere the current file was found. 10931debfc3dSmrg 10941debfc3dSmrgThe use of @samp{#include_next} can lead to great confusion. We 10951debfc3dSmrgrecommend it be used only when there is no other alternative. In 10961debfc3dSmrgparticular, it should not be used in the headers belonging to a specific 10971debfc3dSmrgprogram; it should be used only to make global corrections along the 10981debfc3dSmrglines of @command{fixincludes}. 10991debfc3dSmrg 11001debfc3dSmrg@node System Headers 11011debfc3dSmrg@section System Headers 11021debfc3dSmrg@cindex system header files 11031debfc3dSmrg 11041debfc3dSmrgThe header files declaring interfaces to the operating system and 11051debfc3dSmrgruntime libraries often cannot be written in strictly conforming C@. 11061debfc3dSmrgTherefore, GCC gives code found in @dfn{system headers} special 11071debfc3dSmrgtreatment. All warnings, other than those generated by @samp{#warning} 11081debfc3dSmrg(@pxref{Diagnostics}), are suppressed while GCC is processing a system 11091debfc3dSmrgheader. Macros defined in a system header are immune to a few warnings 11101debfc3dSmrgwherever they are expanded. This immunity is granted on an ad-hoc 11111debfc3dSmrgbasis, when we find that a warning generates lots of false positives 11121debfc3dSmrgbecause of code in macros defined in system headers. 11131debfc3dSmrg 11141debfc3dSmrgNormally, only the headers found in specific directories are considered 11151debfc3dSmrgsystem headers. These directories are determined when GCC is compiled. 11161debfc3dSmrgThere are, however, two ways to make normal headers into system headers: 11171debfc3dSmrg 11181debfc3dSmrg@itemize @bullet 11191debfc3dSmrg@item 11201debfc3dSmrgHeader files found in directories added to the search path with the 11211debfc3dSmrg@option{-isystem} and @option{-idirafter} command-line options are 11221debfc3dSmrgtreated as system headers for the purposes of diagnostics. 11231debfc3dSmrg 11241debfc3dSmrgThe @option{-cxx-isystem} command line option adds its argument to the 11251debfc3dSmrglist of C++ system headers, similar to @option{-isystem} for C headers. 11261debfc3dSmrg 11271debfc3dSmrg@item 11281debfc3dSmrg@findex #pragma GCC system_header 11291debfc3dSmrgThere is also a directive, @code{@w{#pragma GCC system_header}}, which 11301debfc3dSmrgtells GCC to consider the rest of the current include file a system 11311debfc3dSmrgheader, no matter where it was found. Code that comes before the 11321debfc3dSmrg@samp{#pragma} in the file is not affected. @code{@w{#pragma GCC 11331debfc3dSmrgsystem_header}} has no effect in the primary source file. 11341debfc3dSmrg@end itemize 11351debfc3dSmrg 1136c0a68be4SmrgOn some targets, such as RS/6000 AIX, GCC implicitly surrounds all 1137c0a68be4Smrgsystem headers with an @samp{extern "C"} block when compiling as C++. 1138c0a68be4Smrg 11391debfc3dSmrg@node Macros 11401debfc3dSmrg@chapter Macros 11411debfc3dSmrg 11421debfc3dSmrgA @dfn{macro} is a fragment of code which has been given a name. 11431debfc3dSmrgWhenever the name is used, it is replaced by the contents of the macro. 11441debfc3dSmrgThere are two kinds of macros. They differ mostly in what they look 11451debfc3dSmrglike when they are used. @dfn{Object-like} macros resemble data objects 11461debfc3dSmrgwhen used, @dfn{function-like} macros resemble function calls. 11471debfc3dSmrg 11481debfc3dSmrgYou may define any valid identifier as a macro, even if it is a C 11491debfc3dSmrgkeyword. The preprocessor does not know anything about keywords. This 11501debfc3dSmrgcan be useful if you wish to hide a keyword such as @code{const} from an 11511debfc3dSmrgolder compiler that does not understand it. However, the preprocessor 11521debfc3dSmrgoperator @code{defined} (@pxref{Defined}) can never be defined as a 11531debfc3dSmrgmacro, and C++'s named operators (@pxref{C++ Named Operators}) cannot be 11541debfc3dSmrgmacros when you are compiling C++. 11551debfc3dSmrg 11561debfc3dSmrg@menu 11571debfc3dSmrg* Object-like Macros:: 11581debfc3dSmrg* Function-like Macros:: 11591debfc3dSmrg* Macro Arguments:: 11601debfc3dSmrg* Stringizing:: 11611debfc3dSmrg* Concatenation:: 11621debfc3dSmrg* Variadic Macros:: 11631debfc3dSmrg* Predefined Macros:: 11641debfc3dSmrg* Undefining and Redefining Macros:: 11651debfc3dSmrg* Directives Within Macro Arguments:: 11661debfc3dSmrg* Macro Pitfalls:: 11671debfc3dSmrg@end menu 11681debfc3dSmrg 11691debfc3dSmrg@node Object-like Macros 11701debfc3dSmrg@section Object-like Macros 11711debfc3dSmrg@cindex object-like macro 11721debfc3dSmrg@cindex symbolic constants 11731debfc3dSmrg@cindex manifest constants 11741debfc3dSmrg 11751debfc3dSmrgAn @dfn{object-like macro} is a simple identifier which will be replaced 11761debfc3dSmrgby a code fragment. It is called object-like because it looks like a 11771debfc3dSmrgdata object in code that uses it. They are most commonly used to give 11781debfc3dSmrgsymbolic names to numeric constants. 11791debfc3dSmrg 11801debfc3dSmrg@findex #define 11811debfc3dSmrgYou create macros with the @samp{#define} directive. @samp{#define} is 11821debfc3dSmrgfollowed by the name of the macro and then the token sequence it should 11831debfc3dSmrgbe an abbreviation for, which is variously referred to as the macro's 11841debfc3dSmrg@dfn{body}, @dfn{expansion} or @dfn{replacement list}. For example, 11851debfc3dSmrg 11861debfc3dSmrg@smallexample 11871debfc3dSmrg#define BUFFER_SIZE 1024 11881debfc3dSmrg@end smallexample 11891debfc3dSmrg 11901debfc3dSmrg@noindent 11911debfc3dSmrgdefines a macro named @code{BUFFER_SIZE} as an abbreviation for the 11921debfc3dSmrgtoken @code{1024}. If somewhere after this @samp{#define} directive 11931debfc3dSmrgthere comes a C statement of the form 11941debfc3dSmrg 11951debfc3dSmrg@smallexample 11961debfc3dSmrgfoo = (char *) malloc (BUFFER_SIZE); 11971debfc3dSmrg@end smallexample 11981debfc3dSmrg 11991debfc3dSmrg@noindent 12001debfc3dSmrgthen the C preprocessor will recognize and @dfn{expand} the macro 12011debfc3dSmrg@code{BUFFER_SIZE}. The C compiler will see the same tokens as it would 12021debfc3dSmrgif you had written 12031debfc3dSmrg 12041debfc3dSmrg@smallexample 12051debfc3dSmrgfoo = (char *) malloc (1024); 12061debfc3dSmrg@end smallexample 12071debfc3dSmrg 12081debfc3dSmrgBy convention, macro names are written in uppercase. Programs are 12091debfc3dSmrgeasier to read when it is possible to tell at a glance which names are 12101debfc3dSmrgmacros. 12111debfc3dSmrg 12121debfc3dSmrgThe macro's body ends at the end of the @samp{#define} line. You may 12131debfc3dSmrgcontinue the definition onto multiple lines, if necessary, using 12141debfc3dSmrgbackslash-newline. When the macro is expanded, however, it will all 12151debfc3dSmrgcome out on one line. For example, 12161debfc3dSmrg 12171debfc3dSmrg@smallexample 12181debfc3dSmrg#define NUMBERS 1, \ 12191debfc3dSmrg 2, \ 12201debfc3dSmrg 3 12211debfc3dSmrgint x[] = @{ NUMBERS @}; 12221debfc3dSmrg @expansion{} int x[] = @{ 1, 2, 3 @}; 12231debfc3dSmrg@end smallexample 12241debfc3dSmrg 12251debfc3dSmrg@noindent 12261debfc3dSmrgThe most common visible consequence of this is surprising line numbers 12271debfc3dSmrgin error messages. 12281debfc3dSmrg 12291debfc3dSmrgThere is no restriction on what can go in a macro body provided it 12301debfc3dSmrgdecomposes into valid preprocessing tokens. Parentheses need not 12311debfc3dSmrgbalance, and the body need not resemble valid C code. (If it does not, 12321debfc3dSmrgyou may get error messages from the C compiler when you use the macro.) 12331debfc3dSmrg 12341debfc3dSmrgThe C preprocessor scans your program sequentially. Macro definitions 12351debfc3dSmrgtake effect at the place you write them. Therefore, the following input 12361debfc3dSmrgto the C preprocessor 12371debfc3dSmrg 12381debfc3dSmrg@smallexample 12391debfc3dSmrgfoo = X; 12401debfc3dSmrg#define X 4 12411debfc3dSmrgbar = X; 12421debfc3dSmrg@end smallexample 12431debfc3dSmrg 12441debfc3dSmrg@noindent 12451debfc3dSmrgproduces 12461debfc3dSmrg 12471debfc3dSmrg@smallexample 12481debfc3dSmrgfoo = X; 12491debfc3dSmrgbar = 4; 12501debfc3dSmrg@end smallexample 12511debfc3dSmrg 12521debfc3dSmrgWhen the preprocessor expands a macro name, the macro's expansion 12531debfc3dSmrgreplaces the macro invocation, then the expansion is examined for more 12541debfc3dSmrgmacros to expand. For example, 12551debfc3dSmrg 12561debfc3dSmrg@smallexample 12571debfc3dSmrg@group 12581debfc3dSmrg#define TABLESIZE BUFSIZE 12591debfc3dSmrg#define BUFSIZE 1024 12601debfc3dSmrgTABLESIZE 12611debfc3dSmrg @expansion{} BUFSIZE 12621debfc3dSmrg @expansion{} 1024 12631debfc3dSmrg@end group 12641debfc3dSmrg@end smallexample 12651debfc3dSmrg 12661debfc3dSmrg@noindent 12671debfc3dSmrg@code{TABLESIZE} is expanded first to produce @code{BUFSIZE}, then that 12681debfc3dSmrgmacro is expanded to produce the final result, @code{1024}. 12691debfc3dSmrg 12701debfc3dSmrgNotice that @code{BUFSIZE} was not defined when @code{TABLESIZE} was 12711debfc3dSmrgdefined. The @samp{#define} for @code{TABLESIZE} uses exactly the 12721debfc3dSmrgexpansion you specify---in this case, @code{BUFSIZE}---and does not 12731debfc3dSmrgcheck to see whether it too contains macro names. Only when you 12741debfc3dSmrg@emph{use} @code{TABLESIZE} is the result of its expansion scanned for 12751debfc3dSmrgmore macro names. 12761debfc3dSmrg 12771debfc3dSmrgThis makes a difference if you change the definition of @code{BUFSIZE} 12781debfc3dSmrgat some point in the source file. @code{TABLESIZE}, defined as shown, 12791debfc3dSmrgwill always expand using the definition of @code{BUFSIZE} that is 12801debfc3dSmrgcurrently in effect: 12811debfc3dSmrg 12821debfc3dSmrg@smallexample 12831debfc3dSmrg#define BUFSIZE 1020 12841debfc3dSmrg#define TABLESIZE BUFSIZE 12851debfc3dSmrg#undef BUFSIZE 12861debfc3dSmrg#define BUFSIZE 37 12871debfc3dSmrg@end smallexample 12881debfc3dSmrg 12891debfc3dSmrg@noindent 12901debfc3dSmrgNow @code{TABLESIZE} expands (in two stages) to @code{37}. 12911debfc3dSmrg 12921debfc3dSmrgIf the expansion of a macro contains its own name, either directly or 12931debfc3dSmrgvia intermediate macros, it is not expanded again when the expansion is 12941debfc3dSmrgexamined for more macros. This prevents infinite recursion. 12951debfc3dSmrg@xref{Self-Referential Macros}, for the precise details. 12961debfc3dSmrg 12971debfc3dSmrg@node Function-like Macros 12981debfc3dSmrg@section Function-like Macros 12991debfc3dSmrg@cindex function-like macros 13001debfc3dSmrg 13011debfc3dSmrgYou can also define macros whose use looks like a function call. These 13021debfc3dSmrgare called @dfn{function-like macros}. To define a function-like macro, 13031debfc3dSmrgyou use the same @samp{#define} directive, but you put a pair of 13041debfc3dSmrgparentheses immediately after the macro name. For example, 13051debfc3dSmrg 13061debfc3dSmrg@smallexample 13071debfc3dSmrg#define lang_init() c_init() 13081debfc3dSmrglang_init() 13091debfc3dSmrg @expansion{} c_init() 13101debfc3dSmrg@end smallexample 13111debfc3dSmrg 13121debfc3dSmrgA function-like macro is only expanded if its name appears with a pair 13131debfc3dSmrgof parentheses after it. If you write just the name, it is left alone. 13141debfc3dSmrgThis can be useful when you have a function and a macro of the same 13151debfc3dSmrgname, and you wish to use the function sometimes. 13161debfc3dSmrg 13171debfc3dSmrg@smallexample 13181debfc3dSmrgextern void foo(void); 13191debfc3dSmrg#define foo() /* @r{optimized inline version} */ 13201debfc3dSmrg@dots{} 13211debfc3dSmrg foo(); 13221debfc3dSmrg funcptr = foo; 13231debfc3dSmrg@end smallexample 13241debfc3dSmrg 13251debfc3dSmrgHere the call to @code{foo()} will use the macro, but the function 13261debfc3dSmrgpointer will get the address of the real function. If the macro were to 13271debfc3dSmrgbe expanded, it would cause a syntax error. 13281debfc3dSmrg 13291debfc3dSmrgIf you put spaces between the macro name and the parentheses in the 13301debfc3dSmrgmacro definition, that does not define a function-like macro, it defines 13311debfc3dSmrgan object-like macro whose expansion happens to begin with a pair of 13321debfc3dSmrgparentheses. 13331debfc3dSmrg 13341debfc3dSmrg@smallexample 13351debfc3dSmrg#define lang_init () c_init() 13361debfc3dSmrglang_init() 13371debfc3dSmrg @expansion{} () c_init()() 13381debfc3dSmrg@end smallexample 13391debfc3dSmrg 13401debfc3dSmrgThe first two pairs of parentheses in this expansion come from the 13411debfc3dSmrgmacro. The third is the pair that was originally after the macro 13421debfc3dSmrginvocation. Since @code{lang_init} is an object-like macro, it does not 13431debfc3dSmrgconsume those parentheses. 13441debfc3dSmrg 13451debfc3dSmrg@node Macro Arguments 13461debfc3dSmrg@section Macro Arguments 13471debfc3dSmrg@cindex arguments 13481debfc3dSmrg@cindex macros with arguments 13491debfc3dSmrg@cindex arguments in macro definitions 13501debfc3dSmrg 13511debfc3dSmrgFunction-like macros can take @dfn{arguments}, just like true functions. 13521debfc3dSmrgTo define a macro that uses arguments, you insert @dfn{parameters} 13531debfc3dSmrgbetween the pair of parentheses in the macro definition that make the 13541debfc3dSmrgmacro function-like. The parameters must be valid C identifiers, 13551debfc3dSmrgseparated by commas and optionally whitespace. 13561debfc3dSmrg 13571debfc3dSmrgTo invoke a macro that takes arguments, you write the name of the macro 13581debfc3dSmrgfollowed by a list of @dfn{actual arguments} in parentheses, separated 13591debfc3dSmrgby commas. The invocation of the macro need not be restricted to a 13601debfc3dSmrgsingle logical line---it can cross as many lines in the source file as 13611debfc3dSmrgyou wish. The number of arguments you give must match the number of 13621debfc3dSmrgparameters in the macro definition. When the macro is expanded, each 13631debfc3dSmrguse of a parameter in its body is replaced by the tokens of the 13641debfc3dSmrgcorresponding argument. (You need not use all of the parameters in the 13651debfc3dSmrgmacro body.) 13661debfc3dSmrg 13671debfc3dSmrgAs an example, here is a macro that computes the minimum of two numeric 13681debfc3dSmrgvalues, as it is defined in many C programs, and some uses. 13691debfc3dSmrg 13701debfc3dSmrg@smallexample 13711debfc3dSmrg#define min(X, Y) ((X) < (Y) ? (X) : (Y)) 13721debfc3dSmrg x = min(a, b); @expansion{} x = ((a) < (b) ? (a) : (b)); 13731debfc3dSmrg y = min(1, 2); @expansion{} y = ((1) < (2) ? (1) : (2)); 13741debfc3dSmrg z = min(a + 28, *p); @expansion{} z = ((a + 28) < (*p) ? (a + 28) : (*p)); 13751debfc3dSmrg@end smallexample 13761debfc3dSmrg 13771debfc3dSmrg@noindent 13781debfc3dSmrg(In this small example you can already see several of the dangers of 13791debfc3dSmrgmacro arguments. @xref{Macro Pitfalls}, for detailed explanations.) 13801debfc3dSmrg 13811debfc3dSmrgLeading and trailing whitespace in each argument is dropped, and all 13821debfc3dSmrgwhitespace between the tokens of an argument is reduced to a single 13831debfc3dSmrgspace. Parentheses within each argument must balance; a comma within 13841debfc3dSmrgsuch parentheses does not end the argument. However, there is no 13851debfc3dSmrgrequirement for square brackets or braces to balance, and they do not 13861debfc3dSmrgprevent a comma from separating arguments. Thus, 13871debfc3dSmrg 13881debfc3dSmrg@smallexample 13891debfc3dSmrgmacro (array[x = y, x + 1]) 13901debfc3dSmrg@end smallexample 13911debfc3dSmrg 13921debfc3dSmrg@noindent 13931debfc3dSmrgpasses two arguments to @code{macro}: @code{array[x = y} and @code{x + 13941debfc3dSmrg1]}. If you want to supply @code{array[x = y, x + 1]} as an argument, 13951debfc3dSmrgyou can write it as @code{array[(x = y, x + 1)]}, which is equivalent C 13961debfc3dSmrgcode. 13971debfc3dSmrg 13981debfc3dSmrgAll arguments to a macro are completely macro-expanded before they are 13991debfc3dSmrgsubstituted into the macro body. After substitution, the complete text 14001debfc3dSmrgis scanned again for macros to expand, including the arguments. This rule 14011debfc3dSmrgmay seem strange, but it is carefully designed so you need not worry 14021debfc3dSmrgabout whether any function call is actually a macro invocation. You can 14031debfc3dSmrgrun into trouble if you try to be too clever, though. @xref{Argument 14041debfc3dSmrgPrescan}, for detailed discussion. 14051debfc3dSmrg 14061debfc3dSmrgFor example, @code{min (min (a, b), c)} is first expanded to 14071debfc3dSmrg 14081debfc3dSmrg@smallexample 14091debfc3dSmrg min (((a) < (b) ? (a) : (b)), (c)) 14101debfc3dSmrg@end smallexample 14111debfc3dSmrg 14121debfc3dSmrg@noindent 14131debfc3dSmrgand then to 14141debfc3dSmrg 14151debfc3dSmrg@smallexample 14161debfc3dSmrg@group 14171debfc3dSmrg((((a) < (b) ? (a) : (b))) < (c) 14181debfc3dSmrg ? (((a) < (b) ? (a) : (b))) 14191debfc3dSmrg : (c)) 14201debfc3dSmrg@end group 14211debfc3dSmrg@end smallexample 14221debfc3dSmrg 14231debfc3dSmrg@noindent 14241debfc3dSmrg(Line breaks shown here for clarity would not actually be generated.) 14251debfc3dSmrg 14261debfc3dSmrg@cindex empty macro arguments 14271debfc3dSmrgYou can leave macro arguments empty; this is not an error to the 14281debfc3dSmrgpreprocessor (but many macros will then expand to invalid code). 14291debfc3dSmrgYou cannot leave out arguments entirely; if a macro takes two arguments, 14301debfc3dSmrgthere must be exactly one comma at the top level of its argument list. 14311debfc3dSmrgHere are some silly examples using @code{min}: 14321debfc3dSmrg 14331debfc3dSmrg@smallexample 14341debfc3dSmrgmin(, b) @expansion{} (( ) < (b) ? ( ) : (b)) 14351debfc3dSmrgmin(a, ) @expansion{} ((a ) < ( ) ? (a ) : ( )) 14361debfc3dSmrgmin(,) @expansion{} (( ) < ( ) ? ( ) : ( )) 14371debfc3dSmrgmin((,),) @expansion{} (((,)) < ( ) ? ((,)) : ( )) 14381debfc3dSmrg 14391debfc3dSmrgmin() @error{} macro "min" requires 2 arguments, but only 1 given 14401debfc3dSmrgmin(,,) @error{} macro "min" passed 3 arguments, but takes just 2 14411debfc3dSmrg@end smallexample 14421debfc3dSmrg 14431debfc3dSmrgWhitespace is not a preprocessing token, so if a macro @code{foo} takes 14441debfc3dSmrgone argument, @code{@w{foo ()}} and @code{@w{foo ( )}} both supply it an 14451debfc3dSmrgempty argument. Previous GNU preprocessor implementations and 14461debfc3dSmrgdocumentation were incorrect on this point, insisting that a 14471debfc3dSmrgfunction-like macro that takes a single argument be passed a space if an 14481debfc3dSmrgempty argument was required. 14491debfc3dSmrg 14501debfc3dSmrgMacro parameters appearing inside string literals are not replaced by 14511debfc3dSmrgtheir corresponding actual arguments. 14521debfc3dSmrg 14531debfc3dSmrg@smallexample 14541debfc3dSmrg#define foo(x) x, "x" 14551debfc3dSmrgfoo(bar) @expansion{} bar, "x" 14561debfc3dSmrg@end smallexample 14571debfc3dSmrg 14581debfc3dSmrg@node Stringizing 14591debfc3dSmrg@section Stringizing 14601debfc3dSmrg@cindex stringizing 14611debfc3dSmrg@cindex @samp{#} operator 14621debfc3dSmrg 14631debfc3dSmrgSometimes you may want to convert a macro argument into a string 14641debfc3dSmrgconstant. Parameters are not replaced inside string constants, but you 14651debfc3dSmrgcan use the @samp{#} preprocessing operator instead. When a macro 14661debfc3dSmrgparameter is used with a leading @samp{#}, the preprocessor replaces it 14671debfc3dSmrgwith the literal text of the actual argument, converted to a string 14681debfc3dSmrgconstant. Unlike normal parameter replacement, the argument is not 14691debfc3dSmrgmacro-expanded first. This is called @dfn{stringizing}. 14701debfc3dSmrg 14711debfc3dSmrgThere is no way to combine an argument with surrounding text and 14721debfc3dSmrgstringize it all together. Instead, you can write a series of adjacent 14731debfc3dSmrgstring constants and stringized arguments. The preprocessor 14741debfc3dSmrgreplaces the stringized arguments with string constants. The C 14751debfc3dSmrgcompiler then combines all the adjacent string constants into one 14761debfc3dSmrglong string. 14771debfc3dSmrg 14781debfc3dSmrgHere is an example of a macro definition that uses stringizing: 14791debfc3dSmrg 14801debfc3dSmrg@smallexample 14811debfc3dSmrg@group 14821debfc3dSmrg#define WARN_IF(EXP) \ 14831debfc3dSmrgdo @{ if (EXP) \ 14841debfc3dSmrg fprintf (stderr, "Warning: " #EXP "\n"); @} \ 14851debfc3dSmrgwhile (0) 14861debfc3dSmrgWARN_IF (x == 0); 14871debfc3dSmrg @expansion{} do @{ if (x == 0) 14881debfc3dSmrg fprintf (stderr, "Warning: " "x == 0" "\n"); @} while (0); 14891debfc3dSmrg@end group 14901debfc3dSmrg@end smallexample 14911debfc3dSmrg 14921debfc3dSmrg@noindent 14931debfc3dSmrgThe argument for @code{EXP} is substituted once, as-is, into the 14941debfc3dSmrg@code{if} statement, and once, stringized, into the argument to 14951debfc3dSmrg@code{fprintf}. If @code{x} were a macro, it would be expanded in the 14961debfc3dSmrg@code{if} statement, but not in the string. 14971debfc3dSmrg 14981debfc3dSmrgThe @code{do} and @code{while (0)} are a kludge to make it possible to 14991debfc3dSmrgwrite @code{WARN_IF (@var{arg});}, which the resemblance of 15001debfc3dSmrg@code{WARN_IF} to a function would make C programmers want to do; see 15011debfc3dSmrg@ref{Swallowing the Semicolon}. 15021debfc3dSmrg 15031debfc3dSmrgStringizing in C involves more than putting double-quote characters 15041debfc3dSmrgaround the fragment. The preprocessor backslash-escapes the quotes 15051debfc3dSmrgsurrounding embedded string constants, and all backslashes within string and 15061debfc3dSmrgcharacter constants, in order to get a valid C string constant with the 15071debfc3dSmrgproper contents. Thus, stringizing @code{@w{p = "foo\n";}} results in 15081debfc3dSmrg@t{@w{"p = \"foo\\n\";"}}. However, backslashes that are not inside string 15091debfc3dSmrgor character constants are not duplicated: @samp{\n} by itself 15101debfc3dSmrgstringizes to @t{"\n"}. 15111debfc3dSmrg 15121debfc3dSmrgAll leading and trailing whitespace in text being stringized is 15131debfc3dSmrgignored. Any sequence of whitespace in the middle of the text is 15141debfc3dSmrgconverted to a single space in the stringized result. Comments are 15151debfc3dSmrgreplaced by whitespace long before stringizing happens, so they 15161debfc3dSmrgnever appear in stringized text. 15171debfc3dSmrg 15181debfc3dSmrgThere is no way to convert a macro argument into a character constant. 15191debfc3dSmrg 15201debfc3dSmrgIf you want to stringize the result of expansion of a macro argument, 15211debfc3dSmrgyou have to use two levels of macros. 15221debfc3dSmrg 15231debfc3dSmrg@smallexample 15241debfc3dSmrg#define xstr(s) str(s) 15251debfc3dSmrg#define str(s) #s 15261debfc3dSmrg#define foo 4 15271debfc3dSmrgstr (foo) 15281debfc3dSmrg @expansion{} "foo" 15291debfc3dSmrgxstr (foo) 15301debfc3dSmrg @expansion{} xstr (4) 15311debfc3dSmrg @expansion{} str (4) 15321debfc3dSmrg @expansion{} "4" 15331debfc3dSmrg@end smallexample 15341debfc3dSmrg 15351debfc3dSmrg@code{s} is stringized when it is used in @code{str}, so it is not 15361debfc3dSmrgmacro-expanded first. But @code{s} is an ordinary argument to 15371debfc3dSmrg@code{xstr}, so it is completely macro-expanded before @code{xstr} 15381debfc3dSmrgitself is expanded (@pxref{Argument Prescan}). Therefore, by the time 15391debfc3dSmrg@code{str} gets to its argument, it has already been macro-expanded. 15401debfc3dSmrg 15411debfc3dSmrg@node Concatenation 15421debfc3dSmrg@section Concatenation 15431debfc3dSmrg@cindex concatenation 15441debfc3dSmrg@cindex token pasting 15451debfc3dSmrg@cindex token concatenation 15461debfc3dSmrg@cindex @samp{##} operator 15471debfc3dSmrg 15481debfc3dSmrgIt is often useful to merge two tokens into one while expanding macros. 15491debfc3dSmrgThis is called @dfn{token pasting} or @dfn{token concatenation}. The 15501debfc3dSmrg@samp{##} preprocessing operator performs token pasting. When a macro 15511debfc3dSmrgis expanded, the two tokens on either side of each @samp{##} operator 15521debfc3dSmrgare combined into a single token, which then replaces the @samp{##} and 15531debfc3dSmrgthe two original tokens in the macro expansion. Usually both will be 15541debfc3dSmrgidentifiers, or one will be an identifier and the other a preprocessing 15551debfc3dSmrgnumber. When pasted, they make a longer identifier. This isn't the 15561debfc3dSmrgonly valid case. It is also possible to concatenate two numbers (or a 15571debfc3dSmrgnumber and a name, such as @code{1.5} and @code{e3}) into a number. 15581debfc3dSmrgAlso, multi-character operators such as @code{+=} can be formed by 15591debfc3dSmrgtoken pasting. 15601debfc3dSmrg 15611debfc3dSmrgHowever, two tokens that don't together form a valid token cannot be 15621debfc3dSmrgpasted together. For example, you cannot concatenate @code{x} with 15631debfc3dSmrg@code{+} in either order. If you try, the preprocessor issues a warning 15641debfc3dSmrgand emits the two tokens. Whether it puts white space between the 15651debfc3dSmrgtokens is undefined. It is common to find unnecessary uses of @samp{##} 15661debfc3dSmrgin complex macros. If you get this warning, it is likely that you can 15671debfc3dSmrgsimply remove the @samp{##}. 15681debfc3dSmrg 15691debfc3dSmrgBoth the tokens combined by @samp{##} could come from the macro body, 15701debfc3dSmrgbut you could just as well write them as one token in the first place. 15711debfc3dSmrgToken pasting is most useful when one or both of the tokens comes from a 15721debfc3dSmrgmacro argument. If either of the tokens next to an @samp{##} is a 15731debfc3dSmrgparameter name, it is replaced by its actual argument before @samp{##} 15741debfc3dSmrgexecutes. As with stringizing, the actual argument is not 15751debfc3dSmrgmacro-expanded first. If the argument is empty, that @samp{##} has no 15761debfc3dSmrgeffect. 15771debfc3dSmrg 15781debfc3dSmrgKeep in mind that the C preprocessor converts comments to whitespace 15791debfc3dSmrgbefore macros are even considered. Therefore, you cannot create a 15801debfc3dSmrgcomment by concatenating @samp{/} and @samp{*}. You can put as much 15811debfc3dSmrgwhitespace between @samp{##} and its operands as you like, including 15821debfc3dSmrgcomments, and you can put comments in arguments that will be 15831debfc3dSmrgconcatenated. However, it is an error if @samp{##} appears at either 15841debfc3dSmrgend of a macro body. 15851debfc3dSmrg 15861debfc3dSmrgConsider a C program that interprets named commands. There probably 15871debfc3dSmrgneeds to be a table of commands, perhaps an array of structures declared 15881debfc3dSmrgas follows: 15891debfc3dSmrg 15901debfc3dSmrg@smallexample 15911debfc3dSmrg@group 15921debfc3dSmrgstruct command 15931debfc3dSmrg@{ 15941debfc3dSmrg char *name; 15951debfc3dSmrg void (*function) (void); 15961debfc3dSmrg@}; 15971debfc3dSmrg@end group 15981debfc3dSmrg 15991debfc3dSmrg@group 16001debfc3dSmrgstruct command commands[] = 16011debfc3dSmrg@{ 16021debfc3dSmrg @{ "quit", quit_command @}, 16031debfc3dSmrg @{ "help", help_command @}, 16041debfc3dSmrg @dots{} 16051debfc3dSmrg@}; 16061debfc3dSmrg@end group 16071debfc3dSmrg@end smallexample 16081debfc3dSmrg 16091debfc3dSmrgIt would be cleaner not to have to give each command name twice, once in 16101debfc3dSmrgthe string constant and once in the function name. A macro which takes the 16111debfc3dSmrgname of a command as an argument can make this unnecessary. The string 16121debfc3dSmrgconstant can be created with stringizing, and the function name by 16131debfc3dSmrgconcatenating the argument with @samp{_command}. Here is how it is done: 16141debfc3dSmrg 16151debfc3dSmrg@smallexample 16161debfc3dSmrg#define COMMAND(NAME) @{ #NAME, NAME ## _command @} 16171debfc3dSmrg 16181debfc3dSmrgstruct command commands[] = 16191debfc3dSmrg@{ 16201debfc3dSmrg COMMAND (quit), 16211debfc3dSmrg COMMAND (help), 16221debfc3dSmrg @dots{} 16231debfc3dSmrg@}; 16241debfc3dSmrg@end smallexample 16251debfc3dSmrg 16261debfc3dSmrg@node Variadic Macros 16271debfc3dSmrg@section Variadic Macros 16281debfc3dSmrg@cindex variable number of arguments 16291debfc3dSmrg@cindex macros with variable arguments 16301debfc3dSmrg@cindex variadic macros 16311debfc3dSmrg 16321debfc3dSmrgA macro can be declared to accept a variable number of arguments much as 16331debfc3dSmrga function can. The syntax for defining the macro is similar to that of 16341debfc3dSmrga function. Here is an example: 16351debfc3dSmrg 16361debfc3dSmrg@smallexample 1637*8feb0f0bSmrg#define eprintf(...) fprintf (stderr, __VA_ARGS__) 16381debfc3dSmrg@end smallexample 16391debfc3dSmrg 16401debfc3dSmrgThis kind of macro is called @dfn{variadic}. When the macro is invoked, 16411debfc3dSmrgall the tokens in its argument list after the last named argument (this 16421debfc3dSmrgmacro has none), including any commas, become the @dfn{variable 16431debfc3dSmrgargument}. This sequence of tokens replaces the identifier 16441debfc3dSmrg@code{@w{__VA_ARGS__}} in the macro body wherever it appears. Thus, we 16451debfc3dSmrghave this expansion: 16461debfc3dSmrg 16471debfc3dSmrg@smallexample 16481debfc3dSmrgeprintf ("%s:%d: ", input_file, lineno) 16491debfc3dSmrg @expansion{} fprintf (stderr, "%s:%d: ", input_file, lineno) 16501debfc3dSmrg@end smallexample 16511debfc3dSmrg 16521debfc3dSmrgThe variable argument is completely macro-expanded before it is inserted 16531debfc3dSmrginto the macro expansion, just like an ordinary argument. You may use 16541debfc3dSmrgthe @samp{#} and @samp{##} operators to stringize the variable argument 16551debfc3dSmrgor to paste its leading or trailing token with another token. (But see 16561debfc3dSmrgbelow for an important special case for @samp{##}.) 16571debfc3dSmrg 16581debfc3dSmrgIf your macro is complicated, you may want a more descriptive name for 16591debfc3dSmrgthe variable argument than @code{@w{__VA_ARGS__}}. CPP permits 16601debfc3dSmrgthis, as an extension. You may write an argument name immediately 1661*8feb0f0bSmrgbefore the @samp{...}; that name is used for the variable argument. 16621debfc3dSmrgThe @code{eprintf} macro above could be written 16631debfc3dSmrg 16641debfc3dSmrg@smallexample 1665*8feb0f0bSmrg#define eprintf(args...) fprintf (stderr, args) 16661debfc3dSmrg@end smallexample 16671debfc3dSmrg 16681debfc3dSmrg@noindent 16691debfc3dSmrgusing this extension. You cannot use @code{@w{__VA_ARGS__}} and this 16701debfc3dSmrgextension in the same macro. 16711debfc3dSmrg 16721debfc3dSmrgYou can have named arguments as well as variable arguments in a variadic 16731debfc3dSmrgmacro. We could define @code{eprintf} like this, instead: 16741debfc3dSmrg 16751debfc3dSmrg@smallexample 1676*8feb0f0bSmrg#define eprintf(format, ...) fprintf (stderr, format, __VA_ARGS__) 16771debfc3dSmrg@end smallexample 16781debfc3dSmrg 16791debfc3dSmrg@noindent 1680a2dc1f3fSmrgThis formulation looks more descriptive, but historically it was less 1681a2dc1f3fSmrgflexible: you had to supply at least one argument after the format 1682a2dc1f3fSmrgstring. In standard C, you could not omit the comma separating the 1683a2dc1f3fSmrgnamed argument from the variable arguments. (Note that this 1684a2dc1f3fSmrgrestriction has been lifted in C++2a, and never existed in GNU C; see 1685a2dc1f3fSmrgbelow.) 1686a2dc1f3fSmrg 1687a2dc1f3fSmrgFurthermore, if you left the variable argument empty, you would have 1688a2dc1f3fSmrggotten a syntax error, because there would have been an extra comma 1689a2dc1f3fSmrgafter the format string. 16901debfc3dSmrg 16911debfc3dSmrg@smallexample 16921debfc3dSmrgeprintf("success!\n", ); 16931debfc3dSmrg @expansion{} fprintf(stderr, "success!\n", ); 16941debfc3dSmrg@end smallexample 16951debfc3dSmrg 1696a2dc1f3fSmrgThis has been fixed in C++2a, and GNU CPP also has a pair of 1697a2dc1f3fSmrgextensions which deal with this problem. 1698a2dc1f3fSmrg 1699a2dc1f3fSmrgFirst, in GNU CPP, and in C++ beginning in C++2a, you are allowed to 1700a2dc1f3fSmrgleave the variable argument out entirely: 17011debfc3dSmrg 17021debfc3dSmrg@smallexample 17031debfc3dSmrgeprintf ("success!\n") 17041debfc3dSmrg @expansion{} fprintf(stderr, "success!\n", ); 17051debfc3dSmrg@end smallexample 17061debfc3dSmrg 17071debfc3dSmrg@noindent 1708a2dc1f3fSmrgSecond, C++2a introduces the @code{@w{__VA_OPT__}} function macro. 1709a2dc1f3fSmrgThis macro may only appear in the definition of a variadic macro. If 1710a2dc1f3fSmrgthe variable argument has any tokens, then a @code{@w{__VA_OPT__}} 1711a2dc1f3fSmrginvocation expands to its argument; but if the variable argument does 1712a2dc1f3fSmrgnot have any tokens, the @code{@w{__VA_OPT__}} expands to nothing: 1713a2dc1f3fSmrg 1714a2dc1f3fSmrg@smallexample 1715*8feb0f0bSmrg#define eprintf(format, ...) \ 1716a2dc1f3fSmrg fprintf (stderr, format __VA_OPT__(,) __VA_ARGS__) 1717a2dc1f3fSmrg@end smallexample 1718a2dc1f3fSmrg 1719a2dc1f3fSmrg@code{@w{__VA_OPT__}} is also available in GNU C and GNU C++. 1720a2dc1f3fSmrg 1721a2dc1f3fSmrgHistorically, GNU CPP has also had another extension to handle the 1722a2dc1f3fSmrgtrailing comma: the @samp{##} token paste operator has a special 1723a2dc1f3fSmrgmeaning when placed between a comma and a variable argument. Despite 1724a2dc1f3fSmrgthe introduction of @code{@w{__VA_OPT__}}, this extension remains 1725a2dc1f3fSmrgsupported in GNU CPP, for backward compatibility. If you write 17261debfc3dSmrg 17271debfc3dSmrg@smallexample 1728*8feb0f0bSmrg#define eprintf(format, ...) fprintf (stderr, format, ##__VA_ARGS__) 17291debfc3dSmrg@end smallexample 17301debfc3dSmrg 17311debfc3dSmrg@noindent 17321debfc3dSmrgand the variable argument is left out when the @code{eprintf} macro is 17331debfc3dSmrgused, then the comma before the @samp{##} will be deleted. This does 17341debfc3dSmrg@emph{not} happen if you pass an empty argument, nor does it happen if 17351debfc3dSmrgthe token preceding @samp{##} is anything other than a comma. 17361debfc3dSmrg 17371debfc3dSmrg@smallexample 17381debfc3dSmrgeprintf ("success!\n") 17391debfc3dSmrg @expansion{} fprintf(stderr, "success!\n"); 17401debfc3dSmrg@end smallexample 17411debfc3dSmrg 17421debfc3dSmrg@noindent 17431debfc3dSmrgThe above explanation is ambiguous about the case where the only macro 17441debfc3dSmrgparameter is a variable arguments parameter, as it is meaningless to 17451debfc3dSmrgtry to distinguish whether no argument at all is an empty argument or 17461debfc3dSmrga missing argument. 17471debfc3dSmrgCPP retains the comma when conforming to a specific C 17481debfc3dSmrgstandard. Otherwise the comma is dropped as an extension to the standard. 17491debfc3dSmrg 17501debfc3dSmrgThe C standard 17511debfc3dSmrgmandates that the only place the identifier @code{@w{__VA_ARGS__}} 17521debfc3dSmrgcan appear is in the replacement list of a variadic macro. It may not 17531debfc3dSmrgbe used as a macro name, macro argument name, or within a different type 17541debfc3dSmrgof macro. It may also be forbidden in open text; the standard is 17551debfc3dSmrgambiguous. We recommend you avoid using it except for its defined 17561debfc3dSmrgpurpose. 17571debfc3dSmrg 1758a2dc1f3fSmrgLikewise, C++ forbids @code{@w{__VA_OPT__}} anywhere outside the 1759a2dc1f3fSmrgreplacement list of a variadic macro. 1760a2dc1f3fSmrg 17611debfc3dSmrgVariadic macros became a standard part of the C language with C99. 17621debfc3dSmrgGNU CPP previously supported them 17631debfc3dSmrgwith a named variable argument 1764*8feb0f0bSmrg(@samp{args...}, not @samp{...} and @code{@w{__VA_ARGS__}}), which 17651debfc3dSmrgis still supported for backward compatibility. 17661debfc3dSmrg 17671debfc3dSmrg@node Predefined Macros 17681debfc3dSmrg@section Predefined Macros 17691debfc3dSmrg 17701debfc3dSmrg@cindex predefined macros 17711debfc3dSmrgSeveral object-like macros are predefined; you use them without 17721debfc3dSmrgsupplying their definitions. They fall into three classes: standard, 17731debfc3dSmrgcommon, and system-specific. 17741debfc3dSmrg 17751debfc3dSmrgIn C++, there is a fourth category, the named operators. They act like 17761debfc3dSmrgpredefined macros, but you cannot undefine them. 17771debfc3dSmrg 17781debfc3dSmrg@menu 17791debfc3dSmrg* Standard Predefined Macros:: 17801debfc3dSmrg* Common Predefined Macros:: 17811debfc3dSmrg* System-specific Predefined Macros:: 17821debfc3dSmrg* C++ Named Operators:: 17831debfc3dSmrg@end menu 17841debfc3dSmrg 17851debfc3dSmrg@node Standard Predefined Macros 17861debfc3dSmrg@subsection Standard Predefined Macros 17871debfc3dSmrg@cindex standard predefined macros. 17881debfc3dSmrg 17891debfc3dSmrgThe standard predefined macros are specified by the relevant 17901debfc3dSmrglanguage standards, so they are available with all compilers that 17911debfc3dSmrgimplement those standards. Older compilers may not provide all of 17921debfc3dSmrgthem. Their names all start with double underscores. 17931debfc3dSmrg 17941debfc3dSmrg@table @code 17951debfc3dSmrg@item __FILE__ 17961debfc3dSmrgThis macro expands to the name of the current input file, in the form of 17971debfc3dSmrga C string constant. This is the path by which the preprocessor opened 17981debfc3dSmrgthe file, not the short name specified in @samp{#include} or as the 17991debfc3dSmrginput file name argument. For example, 18001debfc3dSmrg@code{"/usr/local/include/myheader.h"} is a possible expansion of this 18011debfc3dSmrgmacro. 18021debfc3dSmrg 18031debfc3dSmrg@item __LINE__ 18041debfc3dSmrgThis macro expands to the current input line number, in the form of a 18051debfc3dSmrgdecimal integer constant. While we call it a predefined macro, it's 18061debfc3dSmrga pretty strange macro, since its ``definition'' changes with each 18071debfc3dSmrgnew line of source code. 18081debfc3dSmrg@end table 18091debfc3dSmrg 18101debfc3dSmrg@code{__FILE__} and @code{__LINE__} are useful in generating an error 18111debfc3dSmrgmessage to report an inconsistency detected by the program; the message 18121debfc3dSmrgcan state the source line at which the inconsistency was detected. For 18131debfc3dSmrgexample, 18141debfc3dSmrg 18151debfc3dSmrg@smallexample 18161debfc3dSmrgfprintf (stderr, "Internal error: " 18171debfc3dSmrg "negative string length " 18181debfc3dSmrg "%d at %s, line %d.", 18191debfc3dSmrg length, __FILE__, __LINE__); 18201debfc3dSmrg@end smallexample 18211debfc3dSmrg 18221debfc3dSmrgAn @samp{#include} directive changes the expansions of @code{__FILE__} 18231debfc3dSmrgand @code{__LINE__} to correspond to the included file. At the end of 18241debfc3dSmrgthat file, when processing resumes on the input file that contained 18251debfc3dSmrgthe @samp{#include} directive, the expansions of @code{__FILE__} and 18261debfc3dSmrg@code{__LINE__} revert to the values they had before the 18271debfc3dSmrg@samp{#include} (but @code{__LINE__} is then incremented by one as 18281debfc3dSmrgprocessing moves to the line after the @samp{#include}). 18291debfc3dSmrg 18301debfc3dSmrgA @samp{#line} directive changes @code{__LINE__}, and may change 18311debfc3dSmrg@code{__FILE__} as well. @xref{Line Control}. 18321debfc3dSmrg 18331debfc3dSmrgC99 introduced @code{__func__}, and GCC has provided @code{__FUNCTION__} 18341debfc3dSmrgfor a long time. Both of these are strings containing the name of the 18351debfc3dSmrgcurrent function (there are slight semantic differences; see the GCC 18361debfc3dSmrgmanual). Neither of them is a macro; the preprocessor does not know the 18371debfc3dSmrgname of the current function. They tend to be useful in conjunction 18381debfc3dSmrgwith @code{__FILE__} and @code{__LINE__}, though. 18391debfc3dSmrg 18401debfc3dSmrg@table @code 18411debfc3dSmrg 18421debfc3dSmrg@item __DATE__ 18431debfc3dSmrgThis macro expands to a string constant that describes the date on which 18441debfc3dSmrgthe preprocessor is being run. The string constant contains eleven 18451debfc3dSmrgcharacters and looks like @code{@w{"Feb 12 1996"}}. If the day of the 18461debfc3dSmrgmonth is less than 10, it is padded with a space on the left. 18471debfc3dSmrg 18481debfc3dSmrgIf GCC cannot determine the current date, it will emit a warning message 18491debfc3dSmrg(once per compilation) and @code{__DATE__} will expand to 18501debfc3dSmrg@code{@w{"??? ?? ????"}}. 18511debfc3dSmrg 18521debfc3dSmrg@item __TIME__ 18531debfc3dSmrgThis macro expands to a string constant that describes the time at 18541debfc3dSmrgwhich the preprocessor is being run. The string constant contains 18551debfc3dSmrgeight characters and looks like @code{"23:59:01"}. 18561debfc3dSmrg 18571debfc3dSmrgIf GCC cannot determine the current time, it will emit a warning message 18581debfc3dSmrg(once per compilation) and @code{__TIME__} will expand to 18591debfc3dSmrg@code{"??:??:??"}. 18601debfc3dSmrg 18611debfc3dSmrg@item __STDC__ 18621debfc3dSmrgIn normal operation, this macro expands to the constant 1, to signify 18631debfc3dSmrgthat this compiler conforms to ISO Standard C@. If GNU CPP is used with 18641debfc3dSmrga compiler other than GCC, this is not necessarily true; however, the 18651debfc3dSmrgpreprocessor always conforms to the standard unless the 18661debfc3dSmrg@option{-traditional-cpp} option is used. 18671debfc3dSmrg 18681debfc3dSmrgThis macro is not defined if the @option{-traditional-cpp} option is used. 18691debfc3dSmrg 18701debfc3dSmrgOn some hosts, the system compiler uses a different convention, where 18711debfc3dSmrg@code{__STDC__} is normally 0, but is 1 if the user specifies strict 18721debfc3dSmrgconformance to the C Standard. CPP follows the host convention when 18731debfc3dSmrgprocessing system header files, but when processing user files 18741debfc3dSmrg@code{__STDC__} is always 1. This has been reported to cause problems; 18751debfc3dSmrgfor instance, some versions of Solaris provide X Windows headers that 18761debfc3dSmrgexpect @code{__STDC__} to be either undefined or 1. @xref{Invocation}. 18771debfc3dSmrg 18781debfc3dSmrg@item __STDC_VERSION__ 18791debfc3dSmrgThis macro expands to the C Standard's version number, a long integer 18801debfc3dSmrgconstant of the form @code{@var{yyyy}@var{mm}L} where @var{yyyy} and 18811debfc3dSmrg@var{mm} are the year and month of the Standard version. This signifies 18821debfc3dSmrgwhich version of the C Standard the compiler conforms to. Like 18831debfc3dSmrg@code{__STDC__}, this is not necessarily accurate for the entire 18841debfc3dSmrgimplementation, unless GNU CPP is being used with GCC@. 18851debfc3dSmrg 18861debfc3dSmrgThe value @code{199409L} signifies the 1989 C standard as amended in 18871debfc3dSmrg1994, which is the current default; the value @code{199901L} signifies 1888a2dc1f3fSmrgthe 1999 revision of the C standard; the value @code{201112L} 1889a2dc1f3fSmrgsignifies the 2011 revision of the C standard; the value 1890a2dc1f3fSmrg@code{201710L} signifies the 2017 revision of the C standard (which is 1891a2dc1f3fSmrgotherwise identical to the 2011 version apart from correction of 1892c0a68be4Smrgdefects). An unspecified value larger than @code{201710L} is used for 1893c0a68be4Smrgthe experimental @option{-std=c2x} and @option{-std=gnu2x} modes. 18941debfc3dSmrg 18951debfc3dSmrgThis macro is not defined if the @option{-traditional-cpp} option is 18961debfc3dSmrgused, nor when compiling C++ or Objective-C@. 18971debfc3dSmrg 18981debfc3dSmrg@item __STDC_HOSTED__ 18991debfc3dSmrgThis macro is defined, with value 1, if the compiler's target is a 19001debfc3dSmrg@dfn{hosted environment}. A hosted environment has the complete 19011debfc3dSmrgfacilities of the standard C library available. 19021debfc3dSmrg 19031debfc3dSmrg@item __cplusplus 19041debfc3dSmrgThis macro is defined when the C++ compiler is in use. You can use 19051debfc3dSmrg@code{__cplusplus} to test whether a header is compiled by a C compiler 19061debfc3dSmrgor a C++ compiler. This macro is similar to @code{__STDC_VERSION__}, in 19071debfc3dSmrgthat it expands to a version number. Depending on the language standard 19081debfc3dSmrgselected, the value of the macro is 19091debfc3dSmrg@code{199711L} for the 1998 C++ standard, 19101debfc3dSmrg@code{201103L} for the 2011 C++ standard, 19111debfc3dSmrg@code{201402L} for the 2014 C++ standard, 1912a2dc1f3fSmrg@code{201703L} for the 2017 C++ standard, 1913a2dc1f3fSmrgor an unspecified value strictly larger than @code{201703L} for the 1914a2dc1f3fSmrgexperimental languages enabled by @option{-std=c++2a} and 1915a2dc1f3fSmrg@option{-std=gnu++2a}. 19161debfc3dSmrg 19171debfc3dSmrg@item __OBJC__ 19181debfc3dSmrgThis macro is defined, with value 1, when the Objective-C compiler is in 19191debfc3dSmrguse. You can use @code{__OBJC__} to test whether a header is compiled 19201debfc3dSmrgby a C compiler or an Objective-C compiler. 19211debfc3dSmrg 19221debfc3dSmrg@item __ASSEMBLER__ 19231debfc3dSmrgThis macro is defined with value 1 when preprocessing assembly 19241debfc3dSmrglanguage. 19251debfc3dSmrg 19261debfc3dSmrg@end table 19271debfc3dSmrg 19281debfc3dSmrg@node Common Predefined Macros 19291debfc3dSmrg@subsection Common Predefined Macros 19301debfc3dSmrg@cindex common predefined macros 19311debfc3dSmrg 19321debfc3dSmrgThe common predefined macros are GNU C extensions. They are available 19331debfc3dSmrgwith the same meanings regardless of the machine or operating system on 19341debfc3dSmrgwhich you are using GNU C or GNU Fortran. Their names all start with 19351debfc3dSmrgdouble underscores. 19361debfc3dSmrg 19371debfc3dSmrg@table @code 19381debfc3dSmrg 19391debfc3dSmrg@item __COUNTER__ 19401debfc3dSmrgThis macro expands to sequential integral values starting from 0. In 19411debfc3dSmrgconjunction with the @code{##} operator, this provides a convenient means to 19421debfc3dSmrggenerate unique identifiers. Care must be taken to ensure that 19431debfc3dSmrg@code{__COUNTER__} is not expanded prior to inclusion of precompiled headers 19441debfc3dSmrgwhich use it. Otherwise, the precompiled headers will not be used. 19451debfc3dSmrg 19461debfc3dSmrg@item __GFORTRAN__ 19471debfc3dSmrgThe GNU Fortran compiler defines this. 19481debfc3dSmrg 19491debfc3dSmrg@item __GNUC__ 19501debfc3dSmrg@itemx __GNUC_MINOR__ 19511debfc3dSmrg@itemx __GNUC_PATCHLEVEL__ 19521debfc3dSmrgThese macros are defined by all GNU compilers that use the C 19531debfc3dSmrgpreprocessor: C, C++, Objective-C and Fortran. Their values are the major 19541debfc3dSmrgversion, minor version, and patch level of the compiler, as integer 19551debfc3dSmrgconstants. For example, GCC version @var{x}.@var{y}.@var{z} 19561debfc3dSmrgdefines @code{__GNUC__} to @var{x}, @code{__GNUC_MINOR__} to @var{y}, 19571debfc3dSmrgand @code{__GNUC_PATCHLEVEL__} to @var{z}. These 19581debfc3dSmrgmacros are also defined if you invoke the preprocessor directly. 19591debfc3dSmrg 19601debfc3dSmrgIf all you need to know is whether or not your program is being compiled 19611debfc3dSmrgby GCC, or a non-GCC compiler that claims to accept the GNU C dialects, 19621debfc3dSmrgyou can simply test @code{__GNUC__}. If you need to write code 19631debfc3dSmrgwhich depends on a specific version, you must be more careful. Each 19641debfc3dSmrgtime the minor version is increased, the patch level is reset to zero; 19651debfc3dSmrgeach time the major version is increased, the 19661debfc3dSmrgminor version and patch level are reset. If you wish to use the 19671debfc3dSmrgpredefined macros directly in the conditional, you will need to write it 19681debfc3dSmrglike this: 19691debfc3dSmrg 19701debfc3dSmrg@smallexample 19711debfc3dSmrg/* @r{Test for GCC > 3.2.0} */ 19721debfc3dSmrg#if __GNUC__ > 3 || \ 19731debfc3dSmrg (__GNUC__ == 3 && (__GNUC_MINOR__ > 2 || \ 19741debfc3dSmrg (__GNUC_MINOR__ == 2 && \ 19751debfc3dSmrg __GNUC_PATCHLEVEL__ > 0)) 19761debfc3dSmrg@end smallexample 19771debfc3dSmrg 19781debfc3dSmrg@noindent 19791debfc3dSmrgAnother approach is to use the predefined macros to 19801debfc3dSmrgcalculate a single number, then compare that against a threshold: 19811debfc3dSmrg 19821debfc3dSmrg@smallexample 19831debfc3dSmrg#define GCC_VERSION (__GNUC__ * 10000 \ 19841debfc3dSmrg + __GNUC_MINOR__ * 100 \ 19851debfc3dSmrg + __GNUC_PATCHLEVEL__) 19861debfc3dSmrg@dots{} 19871debfc3dSmrg/* @r{Test for GCC > 3.2.0} */ 19881debfc3dSmrg#if GCC_VERSION > 30200 19891debfc3dSmrg@end smallexample 19901debfc3dSmrg 19911debfc3dSmrg@noindent 19921debfc3dSmrgMany people find this form easier to understand. 19931debfc3dSmrg 19941debfc3dSmrg@item __GNUG__ 19951debfc3dSmrgThe GNU C++ compiler defines this. Testing it is equivalent to 19961debfc3dSmrgtesting @code{@w{(__GNUC__ && __cplusplus)}}. 19971debfc3dSmrg 19981debfc3dSmrg@item __STRICT_ANSI__ 19991debfc3dSmrgGCC defines this macro if and only if the @option{-ansi} switch, or a 20001debfc3dSmrg@option{-std} switch specifying strict conformance to some version of ISO C 20011debfc3dSmrgor ISO C++, was specified when GCC was invoked. It is defined to @samp{1}. 20021debfc3dSmrgThis macro exists primarily to direct GNU libc's header files to use only 20031debfc3dSmrgdefinitions found in standard C. 20041debfc3dSmrg 20051debfc3dSmrg@item __BASE_FILE__ 20061debfc3dSmrgThis macro expands to the name of the main input file, in the form 20071debfc3dSmrgof a C string constant. This is the source file that was specified 20081debfc3dSmrgon the command line of the preprocessor or C compiler. 20091debfc3dSmrg 20101debfc3dSmrg@item __INCLUDE_LEVEL__ 20111debfc3dSmrgThis macro expands to a decimal integer constant that represents the 20121debfc3dSmrgdepth of nesting in include files. The value of this macro is 20131debfc3dSmrgincremented on every @samp{#include} directive and decremented at the 20141debfc3dSmrgend of every included file. It starts out at 0, its value within the 20151debfc3dSmrgbase file specified on the command line. 20161debfc3dSmrg 20171debfc3dSmrg@item __ELF__ 20181debfc3dSmrgThis macro is defined if the target uses the ELF object format. 20191debfc3dSmrg 20201debfc3dSmrg@item __VERSION__ 20211debfc3dSmrgThis macro expands to a string constant which describes the version of 20221debfc3dSmrgthe compiler in use. You should not rely on its contents having any 20231debfc3dSmrgparticular form, but it can be counted on to contain at least the 20241debfc3dSmrgrelease number. 20251debfc3dSmrg 20261debfc3dSmrg@item __OPTIMIZE__ 20271debfc3dSmrg@itemx __OPTIMIZE_SIZE__ 20281debfc3dSmrg@itemx __NO_INLINE__ 20291debfc3dSmrgThese macros describe the compilation mode. @code{__OPTIMIZE__} is 20301debfc3dSmrgdefined in all optimizing compilations. @code{__OPTIMIZE_SIZE__} is 20311debfc3dSmrgdefined if the compiler is optimizing for size, not speed. 20321debfc3dSmrg@code{__NO_INLINE__} is defined if no functions will be inlined into 20331debfc3dSmrgtheir callers (when not optimizing, or when inlining has been 20341debfc3dSmrgspecifically disabled by @option{-fno-inline}). 20351debfc3dSmrg 20361debfc3dSmrgThese macros cause certain GNU header files to provide optimized 20371debfc3dSmrgdefinitions, using macros or inline functions, of system library 20381debfc3dSmrgfunctions. You should not use these macros in any way unless you make 20391debfc3dSmrgsure that programs will execute with the same effect whether or not they 20401debfc3dSmrgare defined. If they are defined, their value is 1. 20411debfc3dSmrg 20421debfc3dSmrg@item __GNUC_GNU_INLINE__ 20431debfc3dSmrgGCC defines this macro if functions declared @code{inline} will be 20441debfc3dSmrghandled in GCC's traditional gnu90 mode. Object files will contain 20451debfc3dSmrgexternally visible definitions of all functions declared @code{inline} 20461debfc3dSmrgwithout @code{extern} or @code{static}. They will not contain any 20471debfc3dSmrgdefinitions of any functions declared @code{extern inline}. 20481debfc3dSmrg 20491debfc3dSmrg@item __GNUC_STDC_INLINE__ 20501debfc3dSmrgGCC defines this macro if functions declared @code{inline} will be 20511debfc3dSmrghandled according to the ISO C99 or later standards. Object files will contain 20521debfc3dSmrgexternally visible definitions of all functions declared @code{extern 20531debfc3dSmrginline}. They will not contain definitions of any functions declared 20541debfc3dSmrg@code{inline} without @code{extern}. 20551debfc3dSmrg 20561debfc3dSmrgIf this macro is defined, GCC supports the @code{gnu_inline} function 20571debfc3dSmrgattribute as a way to always get the gnu90 behavior. 20581debfc3dSmrg 20591debfc3dSmrg@item __CHAR_UNSIGNED__ 20601debfc3dSmrgGCC defines this macro if and only if the data type @code{char} is 20611debfc3dSmrgunsigned on the target machine. It exists to cause the standard header 20621debfc3dSmrgfile @file{limits.h} to work correctly. You should not use this macro 20631debfc3dSmrgyourself; instead, refer to the standard macros defined in @file{limits.h}. 20641debfc3dSmrg 20651debfc3dSmrg@item __WCHAR_UNSIGNED__ 20661debfc3dSmrgLike @code{__CHAR_UNSIGNED__}, this macro is defined if and only if the 20671debfc3dSmrgdata type @code{wchar_t} is unsigned and the front-end is in C++ mode. 20681debfc3dSmrg 20691debfc3dSmrg@item __REGISTER_PREFIX__ 20701debfc3dSmrgThis macro expands to a single token (not a string constant) which is 20711debfc3dSmrgthe prefix applied to CPU register names in assembly language for this 20721debfc3dSmrgtarget. You can use it to write assembly that is usable in multiple 20731debfc3dSmrgenvironments. For example, in the @code{m68k-aout} environment it 20741debfc3dSmrgexpands to nothing, but in the @code{m68k-coff} environment it expands 20751debfc3dSmrgto a single @samp{%}. 20761debfc3dSmrg 20771debfc3dSmrg@item __USER_LABEL_PREFIX__ 20781debfc3dSmrgThis macro expands to a single token which is the prefix applied to 20791debfc3dSmrguser labels (symbols visible to C code) in assembly. For example, in 20801debfc3dSmrgthe @code{m68k-aout} environment it expands to an @samp{_}, but in the 20811debfc3dSmrg@code{m68k-coff} environment it expands to nothing. 20821debfc3dSmrg 20831debfc3dSmrgThis macro will have the correct definition even if 20841debfc3dSmrg@option{-f(no-)underscores} is in use, but it will not be correct if 20851debfc3dSmrgtarget-specific options that adjust this prefix are used (e.g.@: the 20861debfc3dSmrgOSF/rose @option{-mno-underscores} option). 20871debfc3dSmrg 20881debfc3dSmrg@item __SIZE_TYPE__ 20891debfc3dSmrg@itemx __PTRDIFF_TYPE__ 20901debfc3dSmrg@itemx __WCHAR_TYPE__ 20911debfc3dSmrg@itemx __WINT_TYPE__ 20921debfc3dSmrg@itemx __INTMAX_TYPE__ 20931debfc3dSmrg@itemx __UINTMAX_TYPE__ 20941debfc3dSmrg@itemx __SIG_ATOMIC_TYPE__ 20951debfc3dSmrg@itemx __INT8_TYPE__ 20961debfc3dSmrg@itemx __INT16_TYPE__ 20971debfc3dSmrg@itemx __INT32_TYPE__ 20981debfc3dSmrg@itemx __INT64_TYPE__ 20991debfc3dSmrg@itemx __UINT8_TYPE__ 21001debfc3dSmrg@itemx __UINT16_TYPE__ 21011debfc3dSmrg@itemx __UINT32_TYPE__ 21021debfc3dSmrg@itemx __UINT64_TYPE__ 21031debfc3dSmrg@itemx __INT_LEAST8_TYPE__ 21041debfc3dSmrg@itemx __INT_LEAST16_TYPE__ 21051debfc3dSmrg@itemx __INT_LEAST32_TYPE__ 21061debfc3dSmrg@itemx __INT_LEAST64_TYPE__ 21071debfc3dSmrg@itemx __UINT_LEAST8_TYPE__ 21081debfc3dSmrg@itemx __UINT_LEAST16_TYPE__ 21091debfc3dSmrg@itemx __UINT_LEAST32_TYPE__ 21101debfc3dSmrg@itemx __UINT_LEAST64_TYPE__ 21111debfc3dSmrg@itemx __INT_FAST8_TYPE__ 21121debfc3dSmrg@itemx __INT_FAST16_TYPE__ 21131debfc3dSmrg@itemx __INT_FAST32_TYPE__ 21141debfc3dSmrg@itemx __INT_FAST64_TYPE__ 21151debfc3dSmrg@itemx __UINT_FAST8_TYPE__ 21161debfc3dSmrg@itemx __UINT_FAST16_TYPE__ 21171debfc3dSmrg@itemx __UINT_FAST32_TYPE__ 21181debfc3dSmrg@itemx __UINT_FAST64_TYPE__ 21191debfc3dSmrg@itemx __INTPTR_TYPE__ 21201debfc3dSmrg@itemx __UINTPTR_TYPE__ 21211debfc3dSmrgThese macros are defined to the correct underlying types for the 21221debfc3dSmrg@code{size_t}, @code{ptrdiff_t}, @code{wchar_t}, @code{wint_t}, 21231debfc3dSmrg@code{intmax_t}, @code{uintmax_t}, @code{sig_atomic_t}, @code{int8_t}, 21241debfc3dSmrg@code{int16_t}, @code{int32_t}, @code{int64_t}, @code{uint8_t}, 21251debfc3dSmrg@code{uint16_t}, @code{uint32_t}, @code{uint64_t}, 21261debfc3dSmrg@code{int_least8_t}, @code{int_least16_t}, @code{int_least32_t}, 21271debfc3dSmrg@code{int_least64_t}, @code{uint_least8_t}, @code{uint_least16_t}, 21281debfc3dSmrg@code{uint_least32_t}, @code{uint_least64_t}, @code{int_fast8_t}, 21291debfc3dSmrg@code{int_fast16_t}, @code{int_fast32_t}, @code{int_fast64_t}, 21301debfc3dSmrg@code{uint_fast8_t}, @code{uint_fast16_t}, @code{uint_fast32_t}, 21311debfc3dSmrg@code{uint_fast64_t}, @code{intptr_t}, and @code{uintptr_t} typedefs, 21321debfc3dSmrgrespectively. They exist to make the standard header files 21331debfc3dSmrg@file{stddef.h}, @file{stdint.h}, and @file{wchar.h} work correctly. 21341debfc3dSmrgYou should not use these macros directly; instead, include the 21351debfc3dSmrgappropriate headers and use the typedefs. Some of these macros may 21361debfc3dSmrgnot be defined on particular systems if GCC does not provide a 21371debfc3dSmrg@file{stdint.h} header on those systems. 21381debfc3dSmrg 21391debfc3dSmrg@item __CHAR_BIT__ 21401debfc3dSmrgDefined to the number of bits used in the representation of the 21411debfc3dSmrg@code{char} data type. It exists to make the standard header given 21421debfc3dSmrgnumerical limits work correctly. You should not use 21431debfc3dSmrgthis macro directly; instead, include the appropriate headers. 21441debfc3dSmrg 21451debfc3dSmrg@item __SCHAR_MAX__ 21461debfc3dSmrg@itemx __WCHAR_MAX__ 21471debfc3dSmrg@itemx __SHRT_MAX__ 21481debfc3dSmrg@itemx __INT_MAX__ 21491debfc3dSmrg@itemx __LONG_MAX__ 21501debfc3dSmrg@itemx __LONG_LONG_MAX__ 21511debfc3dSmrg@itemx __WINT_MAX__ 21521debfc3dSmrg@itemx __SIZE_MAX__ 21531debfc3dSmrg@itemx __PTRDIFF_MAX__ 21541debfc3dSmrg@itemx __INTMAX_MAX__ 21551debfc3dSmrg@itemx __UINTMAX_MAX__ 21561debfc3dSmrg@itemx __SIG_ATOMIC_MAX__ 21571debfc3dSmrg@itemx __INT8_MAX__ 21581debfc3dSmrg@itemx __INT16_MAX__ 21591debfc3dSmrg@itemx __INT32_MAX__ 21601debfc3dSmrg@itemx __INT64_MAX__ 21611debfc3dSmrg@itemx __UINT8_MAX__ 21621debfc3dSmrg@itemx __UINT16_MAX__ 21631debfc3dSmrg@itemx __UINT32_MAX__ 21641debfc3dSmrg@itemx __UINT64_MAX__ 21651debfc3dSmrg@itemx __INT_LEAST8_MAX__ 21661debfc3dSmrg@itemx __INT_LEAST16_MAX__ 21671debfc3dSmrg@itemx __INT_LEAST32_MAX__ 21681debfc3dSmrg@itemx __INT_LEAST64_MAX__ 21691debfc3dSmrg@itemx __UINT_LEAST8_MAX__ 21701debfc3dSmrg@itemx __UINT_LEAST16_MAX__ 21711debfc3dSmrg@itemx __UINT_LEAST32_MAX__ 21721debfc3dSmrg@itemx __UINT_LEAST64_MAX__ 21731debfc3dSmrg@itemx __INT_FAST8_MAX__ 21741debfc3dSmrg@itemx __INT_FAST16_MAX__ 21751debfc3dSmrg@itemx __INT_FAST32_MAX__ 21761debfc3dSmrg@itemx __INT_FAST64_MAX__ 21771debfc3dSmrg@itemx __UINT_FAST8_MAX__ 21781debfc3dSmrg@itemx __UINT_FAST16_MAX__ 21791debfc3dSmrg@itemx __UINT_FAST32_MAX__ 21801debfc3dSmrg@itemx __UINT_FAST64_MAX__ 21811debfc3dSmrg@itemx __INTPTR_MAX__ 21821debfc3dSmrg@itemx __UINTPTR_MAX__ 21831debfc3dSmrg@itemx __WCHAR_MIN__ 21841debfc3dSmrg@itemx __WINT_MIN__ 21851debfc3dSmrg@itemx __SIG_ATOMIC_MIN__ 21861debfc3dSmrgDefined to the maximum value of the @code{signed char}, @code{wchar_t}, 21871debfc3dSmrg@code{signed short}, 21881debfc3dSmrg@code{signed int}, @code{signed long}, @code{signed long long}, 21891debfc3dSmrg@code{wint_t}, @code{size_t}, @code{ptrdiff_t}, 21901debfc3dSmrg@code{intmax_t}, @code{uintmax_t}, @code{sig_atomic_t}, @code{int8_t}, 21911debfc3dSmrg@code{int16_t}, @code{int32_t}, @code{int64_t}, @code{uint8_t}, 21921debfc3dSmrg@code{uint16_t}, @code{uint32_t}, @code{uint64_t}, 21931debfc3dSmrg@code{int_least8_t}, @code{int_least16_t}, @code{int_least32_t}, 21941debfc3dSmrg@code{int_least64_t}, @code{uint_least8_t}, @code{uint_least16_t}, 21951debfc3dSmrg@code{uint_least32_t}, @code{uint_least64_t}, @code{int_fast8_t}, 21961debfc3dSmrg@code{int_fast16_t}, @code{int_fast32_t}, @code{int_fast64_t}, 21971debfc3dSmrg@code{uint_fast8_t}, @code{uint_fast16_t}, @code{uint_fast32_t}, 21981debfc3dSmrg@code{uint_fast64_t}, @code{intptr_t}, and @code{uintptr_t} types and 21991debfc3dSmrgto the minimum value of the @code{wchar_t}, @code{wint_t}, and 22001debfc3dSmrg@code{sig_atomic_t} types respectively. They exist to make the 22011debfc3dSmrgstandard header given numerical limits work correctly. You should not 22021debfc3dSmrguse these macros directly; instead, include the appropriate headers. 22031debfc3dSmrgSome of these macros may not be defined on particular systems if GCC 22041debfc3dSmrgdoes not provide a @file{stdint.h} header on those systems. 22051debfc3dSmrg 22061debfc3dSmrg@item __INT8_C 22071debfc3dSmrg@itemx __INT16_C 22081debfc3dSmrg@itemx __INT32_C 22091debfc3dSmrg@itemx __INT64_C 22101debfc3dSmrg@itemx __UINT8_C 22111debfc3dSmrg@itemx __UINT16_C 22121debfc3dSmrg@itemx __UINT32_C 22131debfc3dSmrg@itemx __UINT64_C 22141debfc3dSmrg@itemx __INTMAX_C 22151debfc3dSmrg@itemx __UINTMAX_C 22161debfc3dSmrgDefined to implementations of the standard @file{stdint.h} macros with 22171debfc3dSmrgthe same names without the leading @code{__}. They exist the make the 22181debfc3dSmrgimplementation of that header work correctly. You should not use 22191debfc3dSmrgthese macros directly; instead, include the appropriate headers. Some 22201debfc3dSmrgof these macros may not be defined on particular systems if GCC does 22211debfc3dSmrgnot provide a @file{stdint.h} header on those systems. 22221debfc3dSmrg 22231debfc3dSmrg@item __SCHAR_WIDTH__ 22241debfc3dSmrg@itemx __SHRT_WIDTH__ 22251debfc3dSmrg@itemx __INT_WIDTH__ 22261debfc3dSmrg@itemx __LONG_WIDTH__ 22271debfc3dSmrg@itemx __LONG_LONG_WIDTH__ 22281debfc3dSmrg@itemx __PTRDIFF_WIDTH__ 22291debfc3dSmrg@itemx __SIG_ATOMIC_WIDTH__ 22301debfc3dSmrg@itemx __SIZE_WIDTH__ 22311debfc3dSmrg@itemx __WCHAR_WIDTH__ 22321debfc3dSmrg@itemx __WINT_WIDTH__ 22331debfc3dSmrg@itemx __INT_LEAST8_WIDTH__ 22341debfc3dSmrg@itemx __INT_LEAST16_WIDTH__ 22351debfc3dSmrg@itemx __INT_LEAST32_WIDTH__ 22361debfc3dSmrg@itemx __INT_LEAST64_WIDTH__ 22371debfc3dSmrg@itemx __INT_FAST8_WIDTH__ 22381debfc3dSmrg@itemx __INT_FAST16_WIDTH__ 22391debfc3dSmrg@itemx __INT_FAST32_WIDTH__ 22401debfc3dSmrg@itemx __INT_FAST64_WIDTH__ 22411debfc3dSmrg@itemx __INTPTR_WIDTH__ 22421debfc3dSmrg@itemx __INTMAX_WIDTH__ 22431debfc3dSmrgDefined to the bit widths of the corresponding types. They exist to 22441debfc3dSmrgmake the implementations of @file{limits.h} and @file{stdint.h} behave 22451debfc3dSmrgcorrectly. You should not use these macros directly; instead, include 22461debfc3dSmrgthe appropriate headers. Some of these macros may not be defined on 22471debfc3dSmrgparticular systems if GCC does not provide a @file{stdint.h} header on 22481debfc3dSmrgthose systems. 22491debfc3dSmrg 22501debfc3dSmrg@item __SIZEOF_INT__ 22511debfc3dSmrg@itemx __SIZEOF_LONG__ 22521debfc3dSmrg@itemx __SIZEOF_LONG_LONG__ 22531debfc3dSmrg@itemx __SIZEOF_SHORT__ 22541debfc3dSmrg@itemx __SIZEOF_POINTER__ 22551debfc3dSmrg@itemx __SIZEOF_FLOAT__ 22561debfc3dSmrg@itemx __SIZEOF_DOUBLE__ 22571debfc3dSmrg@itemx __SIZEOF_LONG_DOUBLE__ 22581debfc3dSmrg@itemx __SIZEOF_SIZE_T__ 22591debfc3dSmrg@itemx __SIZEOF_WCHAR_T__ 22601debfc3dSmrg@itemx __SIZEOF_WINT_T__ 22611debfc3dSmrg@itemx __SIZEOF_PTRDIFF_T__ 22621debfc3dSmrgDefined to the number of bytes of the C standard data types: @code{int}, 22631debfc3dSmrg@code{long}, @code{long long}, @code{short}, @code{void *}, @code{float}, 22641debfc3dSmrg@code{double}, @code{long double}, @code{size_t}, @code{wchar_t}, @code{wint_t} 22651debfc3dSmrgand @code{ptrdiff_t}. 22661debfc3dSmrg 22671debfc3dSmrg@item __BYTE_ORDER__ 22681debfc3dSmrg@itemx __ORDER_LITTLE_ENDIAN__ 22691debfc3dSmrg@itemx __ORDER_BIG_ENDIAN__ 22701debfc3dSmrg@itemx __ORDER_PDP_ENDIAN__ 22711debfc3dSmrg@code{__BYTE_ORDER__} is defined to one of the values 22721debfc3dSmrg@code{__ORDER_LITTLE_ENDIAN__}, @code{__ORDER_BIG_ENDIAN__}, or 22731debfc3dSmrg@code{__ORDER_PDP_ENDIAN__} to reflect the layout of multi-byte and 22741debfc3dSmrgmulti-word quantities in memory. If @code{__BYTE_ORDER__} is equal to 22751debfc3dSmrg@code{__ORDER_LITTLE_ENDIAN__} or @code{__ORDER_BIG_ENDIAN__}, then 22761debfc3dSmrgmulti-byte and multi-word quantities are laid out identically: the 22771debfc3dSmrgbyte (word) at the lowest address is the least significant or most 22781debfc3dSmrgsignificant byte (word) of the quantity, respectively. If 22791debfc3dSmrg@code{__BYTE_ORDER__} is equal to @code{__ORDER_PDP_ENDIAN__}, then 22801debfc3dSmrgbytes in 16-bit words are laid out in a little-endian fashion, whereas 22811debfc3dSmrgthe 16-bit subwords of a 32-bit quantity are laid out in big-endian 22821debfc3dSmrgfashion. 22831debfc3dSmrg 22841debfc3dSmrgYou should use these macros for testing like this: 22851debfc3dSmrg 22861debfc3dSmrg@smallexample 22871debfc3dSmrg/* @r{Test for a little-endian machine} */ 22881debfc3dSmrg#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 22891debfc3dSmrg@end smallexample 22901debfc3dSmrg 22911debfc3dSmrg@item __FLOAT_WORD_ORDER__ 22921debfc3dSmrg@code{__FLOAT_WORD_ORDER__} is defined to one of the values 22931debfc3dSmrg@code{__ORDER_LITTLE_ENDIAN__} or @code{__ORDER_BIG_ENDIAN__} to reflect 22941debfc3dSmrgthe layout of the words of multi-word floating-point quantities. 22951debfc3dSmrg 22961debfc3dSmrg@item __DEPRECATED 22971debfc3dSmrgThis macro is defined, with value 1, when compiling a C++ source file 22981debfc3dSmrgwith warnings about deprecated constructs enabled. These warnings are 22991debfc3dSmrgenabled by default, but can be disabled with @option{-Wno-deprecated}. 23001debfc3dSmrg 23011debfc3dSmrg@item __EXCEPTIONS 23021debfc3dSmrgThis macro is defined, with value 1, when compiling a C++ source file 23031debfc3dSmrgwith exceptions enabled. If @option{-fno-exceptions} is used when 23041debfc3dSmrgcompiling the file, then this macro is not defined. 23051debfc3dSmrg 23061debfc3dSmrg@item __GXX_RTTI 23071debfc3dSmrgThis macro is defined, with value 1, when compiling a C++ source file 23081debfc3dSmrgwith runtime type identification enabled. If @option{-fno-rtti} is 23091debfc3dSmrgused when compiling the file, then this macro is not defined. 23101debfc3dSmrg 23111debfc3dSmrg@item __USING_SJLJ_EXCEPTIONS__ 23121debfc3dSmrgThis macro is defined, with value 1, if the compiler uses the old 23131debfc3dSmrgmechanism based on @code{setjmp} and @code{longjmp} for exception 23141debfc3dSmrghandling. 23151debfc3dSmrg 23161debfc3dSmrg@item __GXX_EXPERIMENTAL_CXX0X__ 2317*8feb0f0bSmrgThis macro is defined when compiling a C++ source file with C++11 features 2318*8feb0f0bSmrgenabled, i.e., for all C++ language dialects except @option{-std=c++98} 2319*8feb0f0bSmrgand @option{-std=gnu++98}. This macro is obsolete, but can be used to 2320*8feb0f0bSmrgdetect experimental C++0x features in very old versions of GCC. Since 2321*8feb0f0bSmrgGCC 4.7.0 the @code{__cplusplus} macro is defined correctly, so most 2322*8feb0f0bSmrgcode should test @code{__cplusplus >= 201103L} instead of using this 2323*8feb0f0bSmrgmacro. 23241debfc3dSmrg 23251debfc3dSmrg@item __GXX_WEAK__ 23261debfc3dSmrgThis macro is defined when compiling a C++ source file. It has the 23271debfc3dSmrgvalue 1 if the compiler will use weak symbols, COMDAT sections, or 23281debfc3dSmrgother similar techniques to collapse symbols with ``vague linkage'' 23291debfc3dSmrgthat are defined in multiple translation units. If the compiler will 23301debfc3dSmrgnot collapse such symbols, this macro is defined with value 0. In 23311debfc3dSmrggeneral, user code should not need to make use of this macro; the 23321debfc3dSmrgpurpose of this macro is to ease implementation of the C++ runtime 23331debfc3dSmrglibrary provided with G++. 23341debfc3dSmrg 23351debfc3dSmrg@item __NEXT_RUNTIME__ 23361debfc3dSmrgThis macro is defined, with value 1, if (and only if) the NeXT runtime 23371debfc3dSmrg(as in @option{-fnext-runtime}) is in use for Objective-C@. If the GNU 23381debfc3dSmrgruntime is used, this macro is not defined, so that you can use this 23391debfc3dSmrgmacro to determine which runtime (NeXT or GNU) is being used. 23401debfc3dSmrg 23411debfc3dSmrg@item __LP64__ 23421debfc3dSmrg@itemx _LP64 23431debfc3dSmrgThese macros are defined, with value 1, if (and only if) the compilation 23441debfc3dSmrgis for a target where @code{long int} and pointer both use 64-bits and 23451debfc3dSmrg@code{int} uses 32-bit. 23461debfc3dSmrg 23471debfc3dSmrg@item __SSP__ 23481debfc3dSmrgThis macro is defined, with value 1, when @option{-fstack-protector} is in 23491debfc3dSmrguse. 23501debfc3dSmrg 23511debfc3dSmrg@item __SSP_ALL__ 23521debfc3dSmrgThis macro is defined, with value 2, when @option{-fstack-protector-all} is 23531debfc3dSmrgin use. 23541debfc3dSmrg 23551debfc3dSmrg@item __SSP_STRONG__ 23561debfc3dSmrgThis macro is defined, with value 3, when @option{-fstack-protector-strong} is 23571debfc3dSmrgin use. 23581debfc3dSmrg 23591debfc3dSmrg@item __SSP_EXPLICIT__ 23601debfc3dSmrgThis macro is defined, with value 4, when @option{-fstack-protector-explicit} is 23611debfc3dSmrgin use. 23621debfc3dSmrg 23631debfc3dSmrg@item __SANITIZE_ADDRESS__ 23641debfc3dSmrgThis macro is defined, with value 1, when @option{-fsanitize=address} 23651debfc3dSmrgor @option{-fsanitize=kernel-address} are in use. 23661debfc3dSmrg 23671debfc3dSmrg@item __SANITIZE_THREAD__ 23681debfc3dSmrgThis macro is defined, with value 1, when @option{-fsanitize=thread} is in use. 23691debfc3dSmrg 23701debfc3dSmrg@item __TIMESTAMP__ 23711debfc3dSmrgThis macro expands to a string constant that describes the date and time 23721debfc3dSmrgof the last modification of the current source file. The string constant 23731debfc3dSmrgcontains abbreviated day of the week, month, day of the month, time in 23741debfc3dSmrghh:mm:ss form, year and looks like @code{@w{"Sun Sep 16 01:03:52 1973"}}. 23751debfc3dSmrgIf the day of the month is less than 10, it is padded with a space on the left. 23761debfc3dSmrg 23771debfc3dSmrgIf GCC cannot determine the current date, it will emit a warning message 23781debfc3dSmrg(once per compilation) and @code{__TIMESTAMP__} will expand to 23791debfc3dSmrg@code{@w{"??? ??? ?? ??:??:?? ????"}}. 23801debfc3dSmrg 23811debfc3dSmrg@item __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1 23821debfc3dSmrg@itemx __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 23831debfc3dSmrg@itemx __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 23841debfc3dSmrg@itemx __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 23851debfc3dSmrg@itemx __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16 23861debfc3dSmrgThese macros are defined when the target processor supports atomic compare 23871debfc3dSmrgand swap operations on operands 1, 2, 4, 8 or 16 bytes in length, respectively. 23881debfc3dSmrg 2389c0a68be4Smrg@item __HAVE_SPECULATION_SAFE_VALUE 2390c0a68be4SmrgThis macro is defined with the value 1 to show that this version of GCC 2391c0a68be4Smrgsupports @code{__builtin_speculation_safe_value}. 2392c0a68be4Smrg 23931debfc3dSmrg@item __GCC_HAVE_DWARF2_CFI_ASM 23941debfc3dSmrgThis macro is defined when the compiler is emitting DWARF CFI directives 23951debfc3dSmrgto the assembler. When this is defined, it is possible to emit those same 23961debfc3dSmrgdirectives in inline assembly. 23971debfc3dSmrg 23981debfc3dSmrg@item __FP_FAST_FMA 23991debfc3dSmrg@itemx __FP_FAST_FMAF 24001debfc3dSmrg@itemx __FP_FAST_FMAL 24011debfc3dSmrgThese macros are defined with value 1 if the backend supports the 24021debfc3dSmrg@code{fma}, @code{fmaf}, and @code{fmal} builtin functions, so that 24031debfc3dSmrgthe include file @file{math.h} can define the macros 24041debfc3dSmrg@code{FP_FAST_FMA}, @code{FP_FAST_FMAF}, and @code{FP_FAST_FMAL} 24051debfc3dSmrgfor compatibility with the 1999 C standard. 24061debfc3dSmrg 2407a2dc1f3fSmrg@item __FP_FAST_FMAF16 2408a2dc1f3fSmrg@itemx __FP_FAST_FMAF32 2409a2dc1f3fSmrg@itemx __FP_FAST_FMAF64 2410a2dc1f3fSmrg@itemx __FP_FAST_FMAF128 2411a2dc1f3fSmrg@itemx __FP_FAST_FMAF32X 2412a2dc1f3fSmrg@itemx __FP_FAST_FMAF64X 2413a2dc1f3fSmrg@itemx __FP_FAST_FMAF128X 2414a2dc1f3fSmrgThese macros are defined with the value 1 if the backend supports the 2415a2dc1f3fSmrg@code{fma} functions using the additional @code{_Float@var{n}} and 2416a2dc1f3fSmrg@code{_Float@var{n}x} types that are defined in ISO/IEC TS 2417a2dc1f3fSmrg18661-3:2015. The include file @file{math.h} can define the 2418a2dc1f3fSmrg@code{FP_FAST_FMAF@var{n}} and @code{FP_FAST_FMAF@var{n}x} macros if 2419a2dc1f3fSmrgthe user defined @code{__STDC_WANT_IEC_60559_TYPES_EXT__} before 2420a2dc1f3fSmrgincluding @file{math.h}. 2421a2dc1f3fSmrg 24221debfc3dSmrg@item __GCC_IEC_559 24231debfc3dSmrgThis macro is defined to indicate the intended level of support for 24241debfc3dSmrgIEEE 754 (IEC 60559) floating-point arithmetic. It expands to a 24251debfc3dSmrgnonnegative integer value. If 0, it indicates that the combination of 24261debfc3dSmrgthe compiler configuration and the command-line options is not 24271debfc3dSmrgintended to support IEEE 754 arithmetic for @code{float} and 24281debfc3dSmrg@code{double} as defined in C99 and C11 Annex F (for example, that the 24291debfc3dSmrgstandard rounding modes and exceptions are not supported, or that 24301debfc3dSmrgoptimizations are enabled that conflict with IEEE 754 semantics). If 24311debfc3dSmrg1, it indicates that IEEE 754 arithmetic is intended to be supported; 24321debfc3dSmrgthis does not mean that all relevant language features are supported 24331debfc3dSmrgby GCC. If 2 or more, it additionally indicates support for IEEE 24341debfc3dSmrg754-2008 (in particular, that the binary encodings for quiet and 24351debfc3dSmrgsignaling NaNs are as specified in IEEE 754-2008). 24361debfc3dSmrg 24371debfc3dSmrgThis macro does not indicate the default state of command-line options 24381debfc3dSmrgthat control optimizations that C99 and C11 permit to be controlled by 24391debfc3dSmrgstandard pragmas, where those standards do not require a particular 24401debfc3dSmrgdefault state. It does not indicate whether optimizations respect 24411debfc3dSmrgsignaling NaN semantics (the macro for that is 24421debfc3dSmrg@code{__SUPPORT_SNAN__}). It does not indicate support for decimal 24431debfc3dSmrgfloating point or the IEEE 754 binary16 and binary128 types. 24441debfc3dSmrg 24451debfc3dSmrg@item __GCC_IEC_559_COMPLEX 24461debfc3dSmrgThis macro is defined to indicate the intended level of support for 24471debfc3dSmrgIEEE 754 (IEC 60559) floating-point arithmetic for complex numbers, as 24481debfc3dSmrgdefined in C99 and C11 Annex G. It expands to a nonnegative integer 24491debfc3dSmrgvalue. If 0, it indicates that the combination of the compiler 24501debfc3dSmrgconfiguration and the command-line options is not intended to support 24511debfc3dSmrgAnnex G requirements (for example, because @option{-fcx-limited-range} 24521debfc3dSmrgwas used). If 1 or more, it indicates that it is intended to support 24531debfc3dSmrgthose requirements; this does not mean that all relevant language 24541debfc3dSmrgfeatures are supported by GCC. 24551debfc3dSmrg 24561debfc3dSmrg@item __NO_MATH_ERRNO__ 24571debfc3dSmrgThis macro is defined if @option{-fno-math-errno} is used, or enabled 24581debfc3dSmrgby another option such as @option{-ffast-math} or by default. 24591debfc3dSmrg@end table 24601debfc3dSmrg 24611debfc3dSmrg@node System-specific Predefined Macros 24621debfc3dSmrg@subsection System-specific Predefined Macros 24631debfc3dSmrg 24641debfc3dSmrg@cindex system-specific predefined macros 24651debfc3dSmrg@cindex predefined macros, system-specific 24661debfc3dSmrg@cindex reserved namespace 24671debfc3dSmrg 24681debfc3dSmrgThe C preprocessor normally predefines several macros that indicate what 24691debfc3dSmrgtype of system and machine is in use. They are obviously different on 24701debfc3dSmrgeach target supported by GCC@. This manual, being for all systems and 24711debfc3dSmrgmachines, cannot tell you what their names are, but you can use 24721debfc3dSmrg@command{cpp -dM} to see them all. @xref{Invocation}. All system-specific 24731debfc3dSmrgpredefined macros expand to a constant value, so you can test them with 24741debfc3dSmrgeither @samp{#ifdef} or @samp{#if}. 24751debfc3dSmrg 24761debfc3dSmrgThe C standard requires that all system-specific macros be part of the 24771debfc3dSmrg@dfn{reserved namespace}. All names which begin with two underscores, 24781debfc3dSmrgor an underscore and a capital letter, are reserved for the compiler and 24791debfc3dSmrglibrary to use as they wish. However, historically system-specific 24801debfc3dSmrgmacros have had names with no special prefix; for instance, it is common 24811debfc3dSmrgto find @code{unix} defined on Unix systems. For all such macros, GCC 24821debfc3dSmrgprovides a parallel macro with two underscores added at the beginning 24831debfc3dSmrgand the end. If @code{unix} is defined, @code{__unix__} will be defined 24841debfc3dSmrgtoo. There will never be more than two underscores; the parallel of 24851debfc3dSmrg@code{_mips} is @code{__mips__}. 24861debfc3dSmrg 24871debfc3dSmrgWhen the @option{-ansi} option, or any @option{-std} option that 24881debfc3dSmrgrequests strict conformance, is given to the compiler, all the 24891debfc3dSmrgsystem-specific predefined macros outside the reserved namespace are 24901debfc3dSmrgsuppressed. The parallel macros, inside the reserved namespace, remain 24911debfc3dSmrgdefined. 24921debfc3dSmrg 24931debfc3dSmrgWe are slowly phasing out all predefined macros which are outside the 24941debfc3dSmrgreserved namespace. You should never use them in new programs, and we 24951debfc3dSmrgencourage you to correct older code to use the parallel macros whenever 24961debfc3dSmrgyou find it. We don't recommend you use the system-specific macros that 24971debfc3dSmrgare in the reserved namespace, either. It is better in the long run to 24981debfc3dSmrgcheck specifically for features you need, using a tool such as 24991debfc3dSmrg@command{autoconf}. 25001debfc3dSmrg 25011debfc3dSmrg@node C++ Named Operators 25021debfc3dSmrg@subsection C++ Named Operators 25031debfc3dSmrg@cindex named operators 25041debfc3dSmrg@cindex C++ named operators 25051debfc3dSmrg@cindex @file{iso646.h} 25061debfc3dSmrg 25071debfc3dSmrgIn C++, there are eleven keywords which are simply alternate spellings 25081debfc3dSmrgof operators normally written with punctuation. These keywords are 25091debfc3dSmrgtreated as such even in the preprocessor. They function as operators in 25101debfc3dSmrg@samp{#if}, and they cannot be defined as macros or poisoned. In C, you 25111debfc3dSmrgcan request that those keywords take their C++ meaning by including 25121debfc3dSmrg@file{iso646.h}. That header defines each one as a normal object-like 25131debfc3dSmrgmacro expanding to the appropriate punctuator. 25141debfc3dSmrg 25151debfc3dSmrgThese are the named operators and their corresponding punctuators: 25161debfc3dSmrg 25171debfc3dSmrg@multitable {Named Operator} {Punctuator} 25181debfc3dSmrg@item Named Operator @tab Punctuator 25191debfc3dSmrg@item @code{and} @tab @code{&&} 25201debfc3dSmrg@item @code{and_eq} @tab @code{&=} 25211debfc3dSmrg@item @code{bitand} @tab @code{&} 25221debfc3dSmrg@item @code{bitor} @tab @code{|} 25231debfc3dSmrg@item @code{compl} @tab @code{~} 25241debfc3dSmrg@item @code{not} @tab @code{!} 25251debfc3dSmrg@item @code{not_eq} @tab @code{!=} 25261debfc3dSmrg@item @code{or} @tab @code{||} 25271debfc3dSmrg@item @code{or_eq} @tab @code{|=} 25281debfc3dSmrg@item @code{xor} @tab @code{^} 25291debfc3dSmrg@item @code{xor_eq} @tab @code{^=} 25301debfc3dSmrg@end multitable 25311debfc3dSmrg 25321debfc3dSmrg@node Undefining and Redefining Macros 25331debfc3dSmrg@section Undefining and Redefining Macros 25341debfc3dSmrg@cindex undefining macros 25351debfc3dSmrg@cindex redefining macros 25361debfc3dSmrg@findex #undef 25371debfc3dSmrg 25381debfc3dSmrgIf a macro ceases to be useful, it may be @dfn{undefined} with the 25391debfc3dSmrg@samp{#undef} directive. @samp{#undef} takes a single argument, the 25401debfc3dSmrgname of the macro to undefine. You use the bare macro name, even if the 25411debfc3dSmrgmacro is function-like. It is an error if anything appears on the line 25421debfc3dSmrgafter the macro name. @samp{#undef} has no effect if the name is not a 25431debfc3dSmrgmacro. 25441debfc3dSmrg 25451debfc3dSmrg@smallexample 25461debfc3dSmrg#define FOO 4 25471debfc3dSmrgx = FOO; @expansion{} x = 4; 25481debfc3dSmrg#undef FOO 25491debfc3dSmrgx = FOO; @expansion{} x = FOO; 25501debfc3dSmrg@end smallexample 25511debfc3dSmrg 25521debfc3dSmrgOnce a macro has been undefined, that identifier may be @dfn{redefined} 25531debfc3dSmrgas a macro by a subsequent @samp{#define} directive. The new definition 25541debfc3dSmrgneed not have any resemblance to the old definition. 25551debfc3dSmrg 25561debfc3dSmrgHowever, if an identifier which is currently a macro is redefined, then 25571debfc3dSmrgthe new definition must be @dfn{effectively the same} as the old one. 25581debfc3dSmrgTwo macro definitions are effectively the same if: 25591debfc3dSmrg@itemize @bullet 25601debfc3dSmrg@item Both are the same type of macro (object- or function-like). 25611debfc3dSmrg@item All the tokens of the replacement list are the same. 25621debfc3dSmrg@item If there are any parameters, they are the same. 25631debfc3dSmrg@item Whitespace appears in the same places in both. It need not be 25641debfc3dSmrgexactly the same amount of whitespace, though. Remember that comments 25651debfc3dSmrgcount as whitespace. 25661debfc3dSmrg@end itemize 25671debfc3dSmrg 25681debfc3dSmrg@noindent 25691debfc3dSmrgThese definitions are effectively the same: 25701debfc3dSmrg@smallexample 25711debfc3dSmrg#define FOUR (2 + 2) 25721debfc3dSmrg#define FOUR (2 + 2) 25731debfc3dSmrg#define FOUR (2 /* @r{two} */ + 2) 25741debfc3dSmrg@end smallexample 25751debfc3dSmrg@noindent 25761debfc3dSmrgbut these are not: 25771debfc3dSmrg@smallexample 25781debfc3dSmrg#define FOUR (2 + 2) 25791debfc3dSmrg#define FOUR ( 2+2 ) 25801debfc3dSmrg#define FOUR (2 * 2) 25811debfc3dSmrg#define FOUR(score,and,seven,years,ago) (2 + 2) 25821debfc3dSmrg@end smallexample 25831debfc3dSmrg 25841debfc3dSmrgIf a macro is redefined with a definition that is not effectively the 25851debfc3dSmrgsame as the old one, the preprocessor issues a warning and changes the 25861debfc3dSmrgmacro to use the new definition. If the new definition is effectively 25871debfc3dSmrgthe same, the redefinition is silently ignored. This allows, for 25881debfc3dSmrginstance, two different headers to define a common macro. The 25891debfc3dSmrgpreprocessor will only complain if the definitions do not match. 25901debfc3dSmrg 25911debfc3dSmrg@node Directives Within Macro Arguments 25921debfc3dSmrg@section Directives Within Macro Arguments 25931debfc3dSmrg@cindex macro arguments and directives 25941debfc3dSmrg 25951debfc3dSmrgOccasionally it is convenient to use preprocessor directives within 25961debfc3dSmrgthe arguments of a macro. The C and C++ standards declare that 25971debfc3dSmrgbehavior in these cases is undefined. GNU CPP 25981debfc3dSmrgprocesses arbitrary directives within macro arguments in 25991debfc3dSmrgexactly the same way as it would have processed the directive were the 26001debfc3dSmrgfunction-like macro invocation not present. 26011debfc3dSmrg 26021debfc3dSmrgIf, within a macro invocation, that macro is redefined, then the new 26031debfc3dSmrgdefinition takes effect in time for argument pre-expansion, but the 26041debfc3dSmrgoriginal definition is still used for argument replacement. Here is a 26051debfc3dSmrgpathological example: 26061debfc3dSmrg 26071debfc3dSmrg@smallexample 26081debfc3dSmrg#define f(x) x x 26091debfc3dSmrgf (1 26101debfc3dSmrg#undef f 26111debfc3dSmrg#define f 2 26121debfc3dSmrgf) 26131debfc3dSmrg@end smallexample 26141debfc3dSmrg 26151debfc3dSmrg@noindent 26161debfc3dSmrgwhich expands to 26171debfc3dSmrg 26181debfc3dSmrg@smallexample 26191debfc3dSmrg1 2 1 2 26201debfc3dSmrg@end smallexample 26211debfc3dSmrg 26221debfc3dSmrg@noindent 26231debfc3dSmrgwith the semantics described above. 26241debfc3dSmrg 26251debfc3dSmrg@node Macro Pitfalls 26261debfc3dSmrg@section Macro Pitfalls 26271debfc3dSmrg@cindex problems with macros 26281debfc3dSmrg@cindex pitfalls of macros 26291debfc3dSmrg 26301debfc3dSmrgIn this section we describe some special rules that apply to macros and 26311debfc3dSmrgmacro expansion, and point out certain cases in which the rules have 26321debfc3dSmrgcounter-intuitive consequences that you must watch out for. 26331debfc3dSmrg 26341debfc3dSmrg@menu 26351debfc3dSmrg* Misnesting:: 26361debfc3dSmrg* Operator Precedence Problems:: 26371debfc3dSmrg* Swallowing the Semicolon:: 26381debfc3dSmrg* Duplication of Side Effects:: 26391debfc3dSmrg* Self-Referential Macros:: 26401debfc3dSmrg* Argument Prescan:: 26411debfc3dSmrg* Newlines in Arguments:: 26421debfc3dSmrg@end menu 26431debfc3dSmrg 26441debfc3dSmrg@node Misnesting 26451debfc3dSmrg@subsection Misnesting 26461debfc3dSmrg 26471debfc3dSmrgWhen a macro is called with arguments, the arguments are substituted 26481debfc3dSmrginto the macro body and the result is checked, together with the rest of 26491debfc3dSmrgthe input file, for more macro calls. It is possible to piece together 26501debfc3dSmrga macro call coming partially from the macro body and partially from the 26511debfc3dSmrgarguments. For example, 26521debfc3dSmrg 26531debfc3dSmrg@smallexample 26541debfc3dSmrg#define twice(x) (2*(x)) 26551debfc3dSmrg#define call_with_1(x) x(1) 26561debfc3dSmrgcall_with_1 (twice) 26571debfc3dSmrg @expansion{} twice(1) 26581debfc3dSmrg @expansion{} (2*(1)) 26591debfc3dSmrg@end smallexample 26601debfc3dSmrg 26611debfc3dSmrgMacro definitions do not have to have balanced parentheses. By writing 26621debfc3dSmrgan unbalanced open parenthesis in a macro body, it is possible to create 26631debfc3dSmrga macro call that begins inside the macro body but ends outside of it. 26641debfc3dSmrgFor example, 26651debfc3dSmrg 26661debfc3dSmrg@smallexample 26671debfc3dSmrg#define strange(file) fprintf (file, "%s %d", 26681debfc3dSmrg@dots{} 26691debfc3dSmrgstrange(stderr) p, 35) 26701debfc3dSmrg @expansion{} fprintf (stderr, "%s %d", p, 35) 26711debfc3dSmrg@end smallexample 26721debfc3dSmrg 26731debfc3dSmrgThe ability to piece together a macro call can be useful, but the use of 26741debfc3dSmrgunbalanced open parentheses in a macro body is just confusing, and 26751debfc3dSmrgshould be avoided. 26761debfc3dSmrg 26771debfc3dSmrg@node Operator Precedence Problems 26781debfc3dSmrg@subsection Operator Precedence Problems 26791debfc3dSmrg@cindex parentheses in macro bodies 26801debfc3dSmrg 26811debfc3dSmrgYou may have noticed that in most of the macro definition examples shown 26821debfc3dSmrgabove, each occurrence of a macro argument name had parentheses around 26831debfc3dSmrgit. In addition, another pair of parentheses usually surround the 26841debfc3dSmrgentire macro definition. Here is why it is best to write macros that 26851debfc3dSmrgway. 26861debfc3dSmrg 26871debfc3dSmrgSuppose you define a macro as follows, 26881debfc3dSmrg 26891debfc3dSmrg@smallexample 26901debfc3dSmrg#define ceil_div(x, y) (x + y - 1) / y 26911debfc3dSmrg@end smallexample 26921debfc3dSmrg 26931debfc3dSmrg@noindent 26941debfc3dSmrgwhose purpose is to divide, rounding up. (One use for this operation is 26951debfc3dSmrgto compute how many @code{int} objects are needed to hold a certain 26961debfc3dSmrgnumber of @code{char} objects.) Then suppose it is used as follows: 26971debfc3dSmrg 26981debfc3dSmrg@smallexample 26991debfc3dSmrga = ceil_div (b & c, sizeof (int)); 27001debfc3dSmrg @expansion{} a = (b & c + sizeof (int) - 1) / sizeof (int); 27011debfc3dSmrg@end smallexample 27021debfc3dSmrg 27031debfc3dSmrg@noindent 27041debfc3dSmrgThis does not do what is intended. The operator-precedence rules of 27051debfc3dSmrgC make it equivalent to this: 27061debfc3dSmrg 27071debfc3dSmrg@smallexample 27081debfc3dSmrga = (b & (c + sizeof (int) - 1)) / sizeof (int); 27091debfc3dSmrg@end smallexample 27101debfc3dSmrg 27111debfc3dSmrg@noindent 27121debfc3dSmrgWhat we want is this: 27131debfc3dSmrg 27141debfc3dSmrg@smallexample 27151debfc3dSmrga = ((b & c) + sizeof (int) - 1)) / sizeof (int); 27161debfc3dSmrg@end smallexample 27171debfc3dSmrg 27181debfc3dSmrg@noindent 27191debfc3dSmrgDefining the macro as 27201debfc3dSmrg 27211debfc3dSmrg@smallexample 27221debfc3dSmrg#define ceil_div(x, y) ((x) + (y) - 1) / (y) 27231debfc3dSmrg@end smallexample 27241debfc3dSmrg 27251debfc3dSmrg@noindent 27261debfc3dSmrgprovides the desired result. 27271debfc3dSmrg 27281debfc3dSmrgUnintended grouping can result in another way. Consider @code{sizeof 27291debfc3dSmrgceil_div(1, 2)}. That has the appearance of a C expression that would 27301debfc3dSmrgcompute the size of the type of @code{ceil_div (1, 2)}, but in fact it 27311debfc3dSmrgmeans something very different. Here is what it expands to: 27321debfc3dSmrg 27331debfc3dSmrg@smallexample 27341debfc3dSmrgsizeof ((1) + (2) - 1) / (2) 27351debfc3dSmrg@end smallexample 27361debfc3dSmrg 27371debfc3dSmrg@noindent 27381debfc3dSmrgThis would take the size of an integer and divide it by two. The 27391debfc3dSmrgprecedence rules have put the division outside the @code{sizeof} when it 27401debfc3dSmrgwas intended to be inside. 27411debfc3dSmrg 27421debfc3dSmrgParentheses around the entire macro definition prevent such problems. 27431debfc3dSmrgHere, then, is the recommended way to define @code{ceil_div}: 27441debfc3dSmrg 27451debfc3dSmrg@smallexample 27461debfc3dSmrg#define ceil_div(x, y) (((x) + (y) - 1) / (y)) 27471debfc3dSmrg@end smallexample 27481debfc3dSmrg 27491debfc3dSmrg@node Swallowing the Semicolon 27501debfc3dSmrg@subsection Swallowing the Semicolon 27511debfc3dSmrg@cindex semicolons (after macro calls) 27521debfc3dSmrg 27531debfc3dSmrgOften it is desirable to define a macro that expands into a compound 27541debfc3dSmrgstatement. Consider, for example, the following macro, that advances a 27551debfc3dSmrgpointer (the argument @code{p} says where to find it) across whitespace 27561debfc3dSmrgcharacters: 27571debfc3dSmrg 27581debfc3dSmrg@smallexample 27591debfc3dSmrg#define SKIP_SPACES(p, limit) \ 27601debfc3dSmrg@{ char *lim = (limit); \ 27611debfc3dSmrg while (p < lim) @{ \ 27621debfc3dSmrg if (*p++ != ' ') @{ \ 27631debfc3dSmrg p--; break; @}@}@} 27641debfc3dSmrg@end smallexample 27651debfc3dSmrg 27661debfc3dSmrg@noindent 27671debfc3dSmrgHere backslash-newline is used to split the macro definition, which must 27681debfc3dSmrgbe a single logical line, so that it resembles the way such code would 27691debfc3dSmrgbe laid out if not part of a macro definition. 27701debfc3dSmrg 27711debfc3dSmrgA call to this macro might be @code{SKIP_SPACES (p, lim)}. Strictly 27721debfc3dSmrgspeaking, the call expands to a compound statement, which is a complete 27731debfc3dSmrgstatement with no need for a semicolon to end it. However, since it 27741debfc3dSmrglooks like a function call, it minimizes confusion if you can use it 27751debfc3dSmrglike a function call, writing a semicolon afterward, as in 27761debfc3dSmrg@code{SKIP_SPACES (p, lim);} 27771debfc3dSmrg 27781debfc3dSmrgThis can cause trouble before @code{else} statements, because the 27791debfc3dSmrgsemicolon is actually a null statement. Suppose you write 27801debfc3dSmrg 27811debfc3dSmrg@smallexample 27821debfc3dSmrgif (*p != 0) 27831debfc3dSmrg SKIP_SPACES (p, lim); 27841debfc3dSmrgelse @dots{} 27851debfc3dSmrg@end smallexample 27861debfc3dSmrg 27871debfc3dSmrg@noindent 27881debfc3dSmrgThe presence of two statements---the compound statement and a null 27891debfc3dSmrgstatement---in between the @code{if} condition and the @code{else} 27901debfc3dSmrgmakes invalid C code. 27911debfc3dSmrg 27921debfc3dSmrgThe definition of the macro @code{SKIP_SPACES} can be altered to solve 27931debfc3dSmrgthis problem, using a @code{do @dots{} while} statement. Here is how: 27941debfc3dSmrg 27951debfc3dSmrg@smallexample 27961debfc3dSmrg#define SKIP_SPACES(p, limit) \ 27971debfc3dSmrgdo @{ char *lim = (limit); \ 27981debfc3dSmrg while (p < lim) @{ \ 27991debfc3dSmrg if (*p++ != ' ') @{ \ 28001debfc3dSmrg p--; break; @}@}@} \ 28011debfc3dSmrgwhile (0) 28021debfc3dSmrg@end smallexample 28031debfc3dSmrg 28041debfc3dSmrgNow @code{SKIP_SPACES (p, lim);} expands into 28051debfc3dSmrg 28061debfc3dSmrg@smallexample 28071debfc3dSmrgdo @{@dots{}@} while (0); 28081debfc3dSmrg@end smallexample 28091debfc3dSmrg 28101debfc3dSmrg@noindent 28111debfc3dSmrgwhich is one statement. The loop executes exactly once; most compilers 28121debfc3dSmrggenerate no extra code for it. 28131debfc3dSmrg 28141debfc3dSmrg@node Duplication of Side Effects 28151debfc3dSmrg@subsection Duplication of Side Effects 28161debfc3dSmrg 28171debfc3dSmrg@cindex side effects (in macro arguments) 28181debfc3dSmrg@cindex unsafe macros 28191debfc3dSmrgMany C programs define a macro @code{min}, for ``minimum'', like this: 28201debfc3dSmrg 28211debfc3dSmrg@smallexample 28221debfc3dSmrg#define min(X, Y) ((X) < (Y) ? (X) : (Y)) 28231debfc3dSmrg@end smallexample 28241debfc3dSmrg 28251debfc3dSmrgWhen you use this macro with an argument containing a side effect, 28261debfc3dSmrgas shown here, 28271debfc3dSmrg 28281debfc3dSmrg@smallexample 28291debfc3dSmrgnext = min (x + y, foo (z)); 28301debfc3dSmrg@end smallexample 28311debfc3dSmrg 28321debfc3dSmrg@noindent 28331debfc3dSmrgit expands as follows: 28341debfc3dSmrg 28351debfc3dSmrg@smallexample 28361debfc3dSmrgnext = ((x + y) < (foo (z)) ? (x + y) : (foo (z))); 28371debfc3dSmrg@end smallexample 28381debfc3dSmrg 28391debfc3dSmrg@noindent 28401debfc3dSmrgwhere @code{x + y} has been substituted for @code{X} and @code{foo (z)} 28411debfc3dSmrgfor @code{Y}. 28421debfc3dSmrg 28431debfc3dSmrgThe function @code{foo} is used only once in the statement as it appears 28441debfc3dSmrgin the program, but the expression @code{foo (z)} has been substituted 28451debfc3dSmrgtwice into the macro expansion. As a result, @code{foo} might be called 28461debfc3dSmrgtwo times when the statement is executed. If it has side effects or if 28471debfc3dSmrgit takes a long time to compute, the results might not be what you 28481debfc3dSmrgintended. We say that @code{min} is an @dfn{unsafe} macro. 28491debfc3dSmrg 28501debfc3dSmrgThe best solution to this problem is to define @code{min} in a way that 28511debfc3dSmrgcomputes the value of @code{foo (z)} only once. The C language offers 28521debfc3dSmrgno standard way to do this, but it can be done with GNU extensions as 28531debfc3dSmrgfollows: 28541debfc3dSmrg 28551debfc3dSmrg@smallexample 28561debfc3dSmrg#define min(X, Y) \ 28571debfc3dSmrg(@{ typeof (X) x_ = (X); \ 28581debfc3dSmrg typeof (Y) y_ = (Y); \ 28591debfc3dSmrg (x_ < y_) ? x_ : y_; @}) 28601debfc3dSmrg@end smallexample 28611debfc3dSmrg 28621debfc3dSmrgThe @samp{(@{ @dots{} @})} notation produces a compound statement that 28631debfc3dSmrgacts as an expression. Its value is the value of its last statement. 28641debfc3dSmrgThis permits us to define local variables and assign each argument to 28651debfc3dSmrgone. The local variables have underscores after their names to reduce 28661debfc3dSmrgthe risk of conflict with an identifier of wider scope (it is impossible 28671debfc3dSmrgto avoid this entirely). Now each argument is evaluated exactly once. 28681debfc3dSmrg 28691debfc3dSmrgIf you do not wish to use GNU C extensions, the only solution is to be 28701debfc3dSmrgcareful when @emph{using} the macro @code{min}. For example, you can 28711debfc3dSmrgcalculate the value of @code{foo (z)}, save it in a variable, and use 28721debfc3dSmrgthat variable in @code{min}: 28731debfc3dSmrg 28741debfc3dSmrg@smallexample 28751debfc3dSmrg@group 28761debfc3dSmrg#define min(X, Y) ((X) < (Y) ? (X) : (Y)) 28771debfc3dSmrg@dots{} 28781debfc3dSmrg@{ 28791debfc3dSmrg int tem = foo (z); 28801debfc3dSmrg next = min (x + y, tem); 28811debfc3dSmrg@} 28821debfc3dSmrg@end group 28831debfc3dSmrg@end smallexample 28841debfc3dSmrg 28851debfc3dSmrg@noindent 28861debfc3dSmrg(where we assume that @code{foo} returns type @code{int}). 28871debfc3dSmrg 28881debfc3dSmrg@node Self-Referential Macros 28891debfc3dSmrg@subsection Self-Referential Macros 28901debfc3dSmrg@cindex self-reference 28911debfc3dSmrg 28921debfc3dSmrgA @dfn{self-referential} macro is one whose name appears in its 28931debfc3dSmrgdefinition. Recall that all macro definitions are rescanned for more 28941debfc3dSmrgmacros to replace. If the self-reference were considered a use of the 28951debfc3dSmrgmacro, it would produce an infinitely large expansion. To prevent this, 28961debfc3dSmrgthe self-reference is not considered a macro call. It is passed into 28971debfc3dSmrgthe preprocessor output unchanged. Consider an example: 28981debfc3dSmrg 28991debfc3dSmrg@smallexample 29001debfc3dSmrg#define foo (4 + foo) 29011debfc3dSmrg@end smallexample 29021debfc3dSmrg 29031debfc3dSmrg@noindent 29041debfc3dSmrgwhere @code{foo} is also a variable in your program. 29051debfc3dSmrg 29061debfc3dSmrgFollowing the ordinary rules, each reference to @code{foo} will expand 29071debfc3dSmrginto @code{(4 + foo)}; then this will be rescanned and will expand into 29081debfc3dSmrg@code{(4 + (4 + foo))}; and so on until the computer runs out of memory. 29091debfc3dSmrg 29101debfc3dSmrgThe self-reference rule cuts this process short after one step, at 29111debfc3dSmrg@code{(4 + foo)}. Therefore, this macro definition has the possibly 29121debfc3dSmrguseful effect of causing the program to add 4 to the value of @code{foo} 29131debfc3dSmrgwherever @code{foo} is referred to. 29141debfc3dSmrg 29151debfc3dSmrgIn most cases, it is a bad idea to take advantage of this feature. A 29161debfc3dSmrgperson reading the program who sees that @code{foo} is a variable will 29171debfc3dSmrgnot expect that it is a macro as well. The reader will come across the 29181debfc3dSmrgidentifier @code{foo} in the program and think its value should be that 29191debfc3dSmrgof the variable @code{foo}, whereas in fact the value is four greater. 29201debfc3dSmrg 29211debfc3dSmrgOne common, useful use of self-reference is to create a macro which 29221debfc3dSmrgexpands to itself. If you write 29231debfc3dSmrg 29241debfc3dSmrg@smallexample 29251debfc3dSmrg#define EPERM EPERM 29261debfc3dSmrg@end smallexample 29271debfc3dSmrg 29281debfc3dSmrg@noindent 29291debfc3dSmrgthen the macro @code{EPERM} expands to @code{EPERM}. Effectively, it is 29301debfc3dSmrgleft alone by the preprocessor whenever it's used in running text. You 29311debfc3dSmrgcan tell that it's a macro with @samp{#ifdef}. You might do this if you 29321debfc3dSmrgwant to define numeric constants with an @code{enum}, but have 29331debfc3dSmrg@samp{#ifdef} be true for each constant. 29341debfc3dSmrg 29351debfc3dSmrgIf a macro @code{x} expands to use a macro @code{y}, and the expansion of 29361debfc3dSmrg@code{y} refers to the macro @code{x}, that is an @dfn{indirect 29371debfc3dSmrgself-reference} of @code{x}. @code{x} is not expanded in this case 29381debfc3dSmrgeither. Thus, if we have 29391debfc3dSmrg 29401debfc3dSmrg@smallexample 29411debfc3dSmrg#define x (4 + y) 29421debfc3dSmrg#define y (2 * x) 29431debfc3dSmrg@end smallexample 29441debfc3dSmrg 29451debfc3dSmrg@noindent 29461debfc3dSmrgthen @code{x} and @code{y} expand as follows: 29471debfc3dSmrg 29481debfc3dSmrg@smallexample 29491debfc3dSmrg@group 29501debfc3dSmrgx @expansion{} (4 + y) 29511debfc3dSmrg @expansion{} (4 + (2 * x)) 29521debfc3dSmrg 29531debfc3dSmrgy @expansion{} (2 * x) 29541debfc3dSmrg @expansion{} (2 * (4 + y)) 29551debfc3dSmrg@end group 29561debfc3dSmrg@end smallexample 29571debfc3dSmrg 29581debfc3dSmrg@noindent 29591debfc3dSmrgEach macro is expanded when it appears in the definition of the other 29601debfc3dSmrgmacro, but not when it indirectly appears in its own definition. 29611debfc3dSmrg 29621debfc3dSmrg@node Argument Prescan 29631debfc3dSmrg@subsection Argument Prescan 29641debfc3dSmrg@cindex expansion of arguments 29651debfc3dSmrg@cindex macro argument expansion 29661debfc3dSmrg@cindex prescan of macro arguments 29671debfc3dSmrg 29681debfc3dSmrgMacro arguments are completely macro-expanded before they are 29691debfc3dSmrgsubstituted into a macro body, unless they are stringized or pasted 29701debfc3dSmrgwith other tokens. After substitution, the entire macro body, including 29711debfc3dSmrgthe substituted arguments, is scanned again for macros to be expanded. 29721debfc3dSmrgThe result is that the arguments are scanned @emph{twice} to expand 29731debfc3dSmrgmacro calls in them. 29741debfc3dSmrg 29751debfc3dSmrgMost of the time, this has no effect. If the argument contained any 29761debfc3dSmrgmacro calls, they are expanded during the first scan. The result 29771debfc3dSmrgtherefore contains no macro calls, so the second scan does not change 29781debfc3dSmrgit. If the argument were substituted as given, with no prescan, the 29791debfc3dSmrgsingle remaining scan would find the same macro calls and produce the 29801debfc3dSmrgsame results. 29811debfc3dSmrg 29821debfc3dSmrgYou might expect the double scan to change the results when a 29831debfc3dSmrgself-referential macro is used in an argument of another macro 29841debfc3dSmrg(@pxref{Self-Referential Macros}): the self-referential macro would be 29851debfc3dSmrgexpanded once in the first scan, and a second time in the second scan. 29861debfc3dSmrgHowever, this is not what happens. The self-references that do not 29871debfc3dSmrgexpand in the first scan are marked so that they will not expand in the 29881debfc3dSmrgsecond scan either. 29891debfc3dSmrg 29901debfc3dSmrgYou might wonder, ``Why mention the prescan, if it makes no difference? 29911debfc3dSmrgAnd why not skip it and make the preprocessor faster?'' The answer is 29921debfc3dSmrgthat the prescan does make a difference in three special cases: 29931debfc3dSmrg 29941debfc3dSmrg@itemize @bullet 29951debfc3dSmrg@item 29961debfc3dSmrgNested calls to a macro. 29971debfc3dSmrg 29981debfc3dSmrgWe say that @dfn{nested} calls to a macro occur when a macro's argument 29991debfc3dSmrgcontains a call to that very macro. For example, if @code{f} is a macro 30001debfc3dSmrgthat expects one argument, @code{f (f (1))} is a nested pair of calls to 30011debfc3dSmrg@code{f}. The desired expansion is made by expanding @code{f (1)} and 30021debfc3dSmrgsubstituting that into the definition of @code{f}. The prescan causes 30031debfc3dSmrgthe expected result to happen. Without the prescan, @code{f (1)} itself 30041debfc3dSmrgwould be substituted as an argument, and the inner use of @code{f} would 30051debfc3dSmrgappear during the main scan as an indirect self-reference and would not 30061debfc3dSmrgbe expanded. 30071debfc3dSmrg 30081debfc3dSmrg@item 30091debfc3dSmrgMacros that call other macros that stringize or concatenate. 30101debfc3dSmrg 30111debfc3dSmrgIf an argument is stringized or concatenated, the prescan does not 30121debfc3dSmrgoccur. If you @emph{want} to expand a macro, then stringize or 30131debfc3dSmrgconcatenate its expansion, you can do that by causing one macro to call 30141debfc3dSmrganother macro that does the stringizing or concatenation. For 30151debfc3dSmrginstance, if you have 30161debfc3dSmrg 30171debfc3dSmrg@smallexample 30181debfc3dSmrg#define AFTERX(x) X_ ## x 30191debfc3dSmrg#define XAFTERX(x) AFTERX(x) 30201debfc3dSmrg#define TABLESIZE 1024 30211debfc3dSmrg#define BUFSIZE TABLESIZE 30221debfc3dSmrg@end smallexample 30231debfc3dSmrg 30241debfc3dSmrgthen @code{AFTERX(BUFSIZE)} expands to @code{X_BUFSIZE}, and 30251debfc3dSmrg@code{XAFTERX(BUFSIZE)} expands to @code{X_1024}. (Not to 30261debfc3dSmrg@code{X_TABLESIZE}. Prescan always does a complete expansion.) 30271debfc3dSmrg 30281debfc3dSmrg@item 30291debfc3dSmrgMacros used in arguments, whose expansions contain unshielded commas. 30301debfc3dSmrg 30311debfc3dSmrgThis can cause a macro expanded on the second scan to be called with the 30321debfc3dSmrgwrong number of arguments. Here is an example: 30331debfc3dSmrg 30341debfc3dSmrg@smallexample 30351debfc3dSmrg#define foo a,b 30361debfc3dSmrg#define bar(x) lose(x) 30371debfc3dSmrg#define lose(x) (1 + (x)) 30381debfc3dSmrg@end smallexample 30391debfc3dSmrg 30401debfc3dSmrgWe would like @code{bar(foo)} to turn into @code{(1 + (foo))}, which 30411debfc3dSmrgwould then turn into @code{(1 + (a,b))}. Instead, @code{bar(foo)} 30421debfc3dSmrgexpands into @code{lose(a,b)}, and you get an error because @code{lose} 30431debfc3dSmrgrequires a single argument. In this case, the problem is easily solved 30441debfc3dSmrgby the same parentheses that ought to be used to prevent misnesting of 30451debfc3dSmrgarithmetic operations: 30461debfc3dSmrg 30471debfc3dSmrg@smallexample 30481debfc3dSmrg#define foo (a,b) 30491debfc3dSmrg@exdent or 30501debfc3dSmrg#define bar(x) lose((x)) 30511debfc3dSmrg@end smallexample 30521debfc3dSmrg 30531debfc3dSmrgThe extra pair of parentheses prevents the comma in @code{foo}'s 30541debfc3dSmrgdefinition from being interpreted as an argument separator. 30551debfc3dSmrg 30561debfc3dSmrg@end itemize 30571debfc3dSmrg 30581debfc3dSmrg@node Newlines in Arguments 30591debfc3dSmrg@subsection Newlines in Arguments 30601debfc3dSmrg@cindex newlines in macro arguments 30611debfc3dSmrg 30621debfc3dSmrgThe invocation of a function-like macro can extend over many logical 30631debfc3dSmrglines. However, in the present implementation, the entire expansion 30641debfc3dSmrgcomes out on one line. Thus line numbers emitted by the compiler or 30651debfc3dSmrgdebugger refer to the line the invocation started on, which might be 30661debfc3dSmrgdifferent to the line containing the argument causing the problem. 30671debfc3dSmrg 30681debfc3dSmrgHere is an example illustrating this: 30691debfc3dSmrg 30701debfc3dSmrg@smallexample 30711debfc3dSmrg#define ignore_second_arg(a,b,c) a; c 30721debfc3dSmrg 30731debfc3dSmrgignore_second_arg (foo (), 30741debfc3dSmrg ignored (), 30751debfc3dSmrg syntax error); 30761debfc3dSmrg@end smallexample 30771debfc3dSmrg 30781debfc3dSmrg@noindent 30791debfc3dSmrgThe syntax error triggered by the tokens @code{syntax error} results in 30801debfc3dSmrgan error message citing line three---the line of ignore_second_arg--- 30811debfc3dSmrgeven though the problematic code comes from line five. 30821debfc3dSmrg 30831debfc3dSmrgWe consider this a bug, and intend to fix it in the near future. 30841debfc3dSmrg 30851debfc3dSmrg@node Conditionals 30861debfc3dSmrg@chapter Conditionals 30871debfc3dSmrg@cindex conditionals 30881debfc3dSmrg 30891debfc3dSmrgA @dfn{conditional} is a directive that instructs the preprocessor to 30901debfc3dSmrgselect whether or not to include a chunk of code in the final token 30911debfc3dSmrgstream passed to the compiler. Preprocessor conditionals can test 30921debfc3dSmrgarithmetic expressions, or whether a name is defined as a macro, or both 30931debfc3dSmrgsimultaneously using the special @code{defined} operator. 30941debfc3dSmrg 30951debfc3dSmrgA conditional in the C preprocessor resembles in some ways an @code{if} 30961debfc3dSmrgstatement in C, but it is important to understand the difference between 30971debfc3dSmrgthem. The condition in an @code{if} statement is tested during the 30981debfc3dSmrgexecution of your program. Its purpose is to allow your program to 30991debfc3dSmrgbehave differently from run to run, depending on the data it is 31001debfc3dSmrgoperating on. The condition in a preprocessing conditional directive is 31011debfc3dSmrgtested when your program is compiled. Its purpose is to allow different 31021debfc3dSmrgcode to be included in the program depending on the situation at the 31031debfc3dSmrgtime of compilation. 31041debfc3dSmrg 31051debfc3dSmrgHowever, the distinction is becoming less clear. Modern compilers often 31061debfc3dSmrgdo test @code{if} statements when a program is compiled, if their 31071debfc3dSmrgconditions are known not to vary at run time, and eliminate code which 31081debfc3dSmrgcan never be executed. If you can count on your compiler to do this, 31091debfc3dSmrgyou may find that your program is more readable if you use @code{if} 31101debfc3dSmrgstatements with constant conditions (perhaps determined by macros). Of 31111debfc3dSmrgcourse, you can only use this to exclude code, not type definitions or 31121debfc3dSmrgother preprocessing directives, and you can only do it if the code 31131debfc3dSmrgremains syntactically valid when it is not to be used. 31141debfc3dSmrg 31151debfc3dSmrg@menu 31161debfc3dSmrg* Conditional Uses:: 31171debfc3dSmrg* Conditional Syntax:: 31181debfc3dSmrg* Deleted Code:: 31191debfc3dSmrg@end menu 31201debfc3dSmrg 31211debfc3dSmrg@node Conditional Uses 31221debfc3dSmrg@section Conditional Uses 31231debfc3dSmrg 31241debfc3dSmrgThere are three general reasons to use a conditional. 31251debfc3dSmrg 31261debfc3dSmrg@itemize @bullet 31271debfc3dSmrg@item 31281debfc3dSmrgA program may need to use different code depending on the machine or 31291debfc3dSmrgoperating system it is to run on. In some cases the code for one 31301debfc3dSmrgoperating system may be erroneous on another operating system; for 31311debfc3dSmrgexample, it might refer to data types or constants that do not exist on 31321debfc3dSmrgthe other system. When this happens, it is not enough to avoid 31331debfc3dSmrgexecuting the invalid code. Its mere presence will cause the compiler 31341debfc3dSmrgto reject the program. With a preprocessing conditional, the offending 31351debfc3dSmrgcode can be effectively excised from the program when it is not valid. 31361debfc3dSmrg 31371debfc3dSmrg@item 31381debfc3dSmrgYou may want to be able to compile the same source file into two 31391debfc3dSmrgdifferent programs. One version might make frequent time-consuming 31401debfc3dSmrgconsistency checks on its intermediate data, or print the values of 31411debfc3dSmrgthose data for debugging, and the other not. 31421debfc3dSmrg 31431debfc3dSmrg@item 31441debfc3dSmrgA conditional whose condition is always false is one way to exclude code 31451debfc3dSmrgfrom the program but keep it as a sort of comment for future reference. 31461debfc3dSmrg@end itemize 31471debfc3dSmrg 31481debfc3dSmrgSimple programs that do not need system-specific logic or complex 31491debfc3dSmrgdebugging hooks generally will not need to use preprocessing 31501debfc3dSmrgconditionals. 31511debfc3dSmrg 31521debfc3dSmrg@node Conditional Syntax 31531debfc3dSmrg@section Conditional Syntax 31541debfc3dSmrg 31551debfc3dSmrg@findex #if 31561debfc3dSmrgA conditional in the C preprocessor begins with a @dfn{conditional 31571debfc3dSmrgdirective}: @samp{#if}, @samp{#ifdef} or @samp{#ifndef}. 31581debfc3dSmrg 31591debfc3dSmrg@menu 31601debfc3dSmrg* Ifdef:: 31611debfc3dSmrg* If:: 31621debfc3dSmrg* Defined:: 31631debfc3dSmrg* Else:: 31641debfc3dSmrg* Elif:: 3165c0a68be4Smrg* @code{__has_attribute}:: 3166c0a68be4Smrg* @code{__has_cpp_attribute}:: 3167*8feb0f0bSmrg* @code{__has_builtin}:: 3168c0a68be4Smrg* @code{__has_include}:: 31691debfc3dSmrg@end menu 31701debfc3dSmrg 31711debfc3dSmrg@node Ifdef 31721debfc3dSmrg@subsection Ifdef 31731debfc3dSmrg@findex #ifdef 31741debfc3dSmrg@findex #endif 31751debfc3dSmrg 31761debfc3dSmrgThe simplest sort of conditional is 31771debfc3dSmrg 31781debfc3dSmrg@smallexample 31791debfc3dSmrg@group 31801debfc3dSmrg#ifdef @var{MACRO} 31811debfc3dSmrg 31821debfc3dSmrg@var{controlled text} 31831debfc3dSmrg 31841debfc3dSmrg#endif /* @var{MACRO} */ 31851debfc3dSmrg@end group 31861debfc3dSmrg@end smallexample 31871debfc3dSmrg 31881debfc3dSmrg@cindex conditional group 31891debfc3dSmrgThis block is called a @dfn{conditional group}. @var{controlled text} 31901debfc3dSmrgwill be included in the output of the preprocessor if and only if 31911debfc3dSmrg@var{MACRO} is defined. We say that the conditional @dfn{succeeds} if 31921debfc3dSmrg@var{MACRO} is defined, @dfn{fails} if it is not. 31931debfc3dSmrg 31941debfc3dSmrgThe @var{controlled text} inside of a conditional can include 31951debfc3dSmrgpreprocessing directives. They are executed only if the conditional 31961debfc3dSmrgsucceeds. You can nest conditional groups inside other conditional 31971debfc3dSmrggroups, but they must be completely nested. In other words, 31981debfc3dSmrg@samp{#endif} always matches the nearest @samp{#ifdef} (or 31991debfc3dSmrg@samp{#ifndef}, or @samp{#if}). Also, you cannot start a conditional 32001debfc3dSmrggroup in one file and end it in another. 32011debfc3dSmrg 32021debfc3dSmrgEven if a conditional fails, the @var{controlled text} inside it is 32031debfc3dSmrgstill run through initial transformations and tokenization. Therefore, 32041debfc3dSmrgit must all be lexically valid C@. Normally the only way this matters is 32051debfc3dSmrgthat all comments and string literals inside a failing conditional group 32061debfc3dSmrgmust still be properly ended. 32071debfc3dSmrg 32081debfc3dSmrgThe comment following the @samp{#endif} is not required, but it is a 32091debfc3dSmrggood practice if there is a lot of @var{controlled text}, because it 32101debfc3dSmrghelps people match the @samp{#endif} to the corresponding @samp{#ifdef}. 32111debfc3dSmrgOlder programs sometimes put @var{MACRO} directly after the 32121debfc3dSmrg@samp{#endif} without enclosing it in a comment. This is invalid code 32131debfc3dSmrgaccording to the C standard. CPP accepts it with a warning. It 32141debfc3dSmrgnever affects which @samp{#ifndef} the @samp{#endif} matches. 32151debfc3dSmrg 32161debfc3dSmrg@findex #ifndef 32171debfc3dSmrgSometimes you wish to use some code if a macro is @emph{not} defined. 32181debfc3dSmrgYou can do this by writing @samp{#ifndef} instead of @samp{#ifdef}. 32191debfc3dSmrgOne common use of @samp{#ifndef} is to include code only the first 32201debfc3dSmrgtime a header file is included. @xref{Once-Only Headers}. 32211debfc3dSmrg 32221debfc3dSmrgMacro definitions can vary between compilations for several reasons. 32231debfc3dSmrgHere are some samples. 32241debfc3dSmrg 32251debfc3dSmrg@itemize @bullet 32261debfc3dSmrg@item 32271debfc3dSmrgSome macros are predefined on each kind of machine 32281debfc3dSmrg(@pxref{System-specific Predefined Macros}). This allows you to provide 32291debfc3dSmrgcode specially tuned for a particular machine. 32301debfc3dSmrg 32311debfc3dSmrg@item 32321debfc3dSmrgSystem header files define more macros, associated with the features 32331debfc3dSmrgthey implement. You can test these macros with conditionals to avoid 32341debfc3dSmrgusing a system feature on a machine where it is not implemented. 32351debfc3dSmrg 32361debfc3dSmrg@item 32371debfc3dSmrgMacros can be defined or undefined with the @option{-D} and @option{-U} 32381debfc3dSmrgcommand-line options when you compile the program. You can arrange to 32391debfc3dSmrgcompile the same source file into two different programs by choosing a 32401debfc3dSmrgmacro name to specify which program you want, writing conditionals to 32411debfc3dSmrgtest whether or how this macro is defined, and then controlling the 32421debfc3dSmrgstate of the macro with command-line options, perhaps set in the 32431debfc3dSmrgMakefile. @xref{Invocation}. 32441debfc3dSmrg 32451debfc3dSmrg@item 32461debfc3dSmrgYour program might have a special header file (often called 32471debfc3dSmrg@file{config.h}) that is adjusted when the program is compiled. It can 32481debfc3dSmrgdefine or not define macros depending on the features of the system and 32491debfc3dSmrgthe desired capabilities of the program. The adjustment can be 32501debfc3dSmrgautomated by a tool such as @command{autoconf}, or done by hand. 32511debfc3dSmrg@end itemize 32521debfc3dSmrg 32531debfc3dSmrg@node If 32541debfc3dSmrg@subsection If 32551debfc3dSmrg 32561debfc3dSmrgThe @samp{#if} directive allows you to test the value of an arithmetic 32571debfc3dSmrgexpression, rather than the mere existence of one macro. Its syntax is 32581debfc3dSmrg 32591debfc3dSmrg@smallexample 32601debfc3dSmrg@group 32611debfc3dSmrg#if @var{expression} 32621debfc3dSmrg 32631debfc3dSmrg@var{controlled text} 32641debfc3dSmrg 32651debfc3dSmrg#endif /* @var{expression} */ 32661debfc3dSmrg@end group 32671debfc3dSmrg@end smallexample 32681debfc3dSmrg 32691debfc3dSmrg@var{expression} is a C expression of integer type, subject to stringent 32701debfc3dSmrgrestrictions. It may contain 32711debfc3dSmrg 32721debfc3dSmrg@itemize @bullet 32731debfc3dSmrg@item 32741debfc3dSmrgInteger constants. 32751debfc3dSmrg 32761debfc3dSmrg@item 32771debfc3dSmrgCharacter constants, which are interpreted as they would be in normal 32781debfc3dSmrgcode. 32791debfc3dSmrg 32801debfc3dSmrg@item 32811debfc3dSmrgArithmetic operators for addition, subtraction, multiplication, 32821debfc3dSmrgdivision, bitwise operations, shifts, comparisons, and logical 32831debfc3dSmrgoperations (@code{&&} and @code{||}). The latter two obey the usual 32841debfc3dSmrgshort-circuiting rules of standard C@. 32851debfc3dSmrg 32861debfc3dSmrg@item 32871debfc3dSmrgMacros. All macros in the expression are expanded before actual 32881debfc3dSmrgcomputation of the expression's value begins. 32891debfc3dSmrg 32901debfc3dSmrg@item 32911debfc3dSmrgUses of the @code{defined} operator, which lets you check whether macros 32921debfc3dSmrgare defined in the middle of an @samp{#if}. 32931debfc3dSmrg 32941debfc3dSmrg@item 32951debfc3dSmrgIdentifiers that are not macros, which are all considered to be the 32961debfc3dSmrgnumber zero. This allows you to write @code{@w{#if MACRO}} instead of 32971debfc3dSmrg@code{@w{#ifdef MACRO}}, if you know that MACRO, when defined, will 32981debfc3dSmrgalways have a nonzero value. Function-like macros used without their 32991debfc3dSmrgfunction call parentheses are also treated as zero. 33001debfc3dSmrg 33011debfc3dSmrgIn some contexts this shortcut is undesirable. The @option{-Wundef} 33021debfc3dSmrgoption causes GCC to warn whenever it encounters an identifier which is 33031debfc3dSmrgnot a macro in an @samp{#if}. 33041debfc3dSmrg@end itemize 33051debfc3dSmrg 33061debfc3dSmrgThe preprocessor does not know anything about types in the language. 33071debfc3dSmrgTherefore, @code{sizeof} operators are not recognized in @samp{#if}, and 33081debfc3dSmrgneither are @code{enum} constants. They will be taken as identifiers 33091debfc3dSmrgwhich are not macros, and replaced by zero. In the case of 33101debfc3dSmrg@code{sizeof}, this is likely to cause the expression to be invalid. 33111debfc3dSmrg 33121debfc3dSmrgThe preprocessor calculates the value of @var{expression}. It carries 33131debfc3dSmrgout all calculations in the widest integer type known to the compiler; 33141debfc3dSmrgon most machines supported by GCC this is 64 bits. This is not the same 33151debfc3dSmrgrule as the compiler uses to calculate the value of a constant 33161debfc3dSmrgexpression, and may give different results in some cases. If the value 33171debfc3dSmrgcomes out to be nonzero, the @samp{#if} succeeds and the @var{controlled 33181debfc3dSmrgtext} is included; otherwise it is skipped. 33191debfc3dSmrg 33201debfc3dSmrg@node Defined 33211debfc3dSmrg@subsection Defined 33221debfc3dSmrg 33231debfc3dSmrg@cindex @code{defined} 33241debfc3dSmrgThe special operator @code{defined} is used in @samp{#if} and 33251debfc3dSmrg@samp{#elif} expressions to test whether a certain name is defined as a 33261debfc3dSmrgmacro. @code{defined @var{name}} and @code{defined (@var{name})} are 33271debfc3dSmrgboth expressions whose value is 1 if @var{name} is defined as a macro at 33281debfc3dSmrgthe current point in the program, and 0 otherwise. Thus, @code{@w{#if 33291debfc3dSmrgdefined MACRO}} is precisely equivalent to @code{@w{#ifdef MACRO}}. 33301debfc3dSmrg 33311debfc3dSmrg@code{defined} is useful when you wish to test more than one macro for 33321debfc3dSmrgexistence at once. For example, 33331debfc3dSmrg 33341debfc3dSmrg@smallexample 33351debfc3dSmrg#if defined (__vax__) || defined (__ns16000__) 33361debfc3dSmrg@end smallexample 33371debfc3dSmrg 33381debfc3dSmrg@noindent 33391debfc3dSmrgwould succeed if either of the names @code{__vax__} or 33401debfc3dSmrg@code{__ns16000__} is defined as a macro. 33411debfc3dSmrg 33421debfc3dSmrgConditionals written like this: 33431debfc3dSmrg 33441debfc3dSmrg@smallexample 33451debfc3dSmrg#if defined BUFSIZE && BUFSIZE >= 1024 33461debfc3dSmrg@end smallexample 33471debfc3dSmrg 33481debfc3dSmrg@noindent 33491debfc3dSmrgcan generally be simplified to just @code{@w{#if BUFSIZE >= 1024}}, 33501debfc3dSmrgsince if @code{BUFSIZE} is not defined, it will be interpreted as having 33511debfc3dSmrgthe value zero. 33521debfc3dSmrg 33531debfc3dSmrgIf the @code{defined} operator appears as a result of a macro expansion, 33541debfc3dSmrgthe C standard says the behavior is undefined. GNU cpp treats it as a 33551debfc3dSmrggenuine @code{defined} operator and evaluates it normally. It will warn 33561debfc3dSmrgwherever your code uses this feature if you use the command-line option 33571debfc3dSmrg@option{-Wpedantic}, since other compilers may handle it differently. The 33581debfc3dSmrgwarning is also enabled by @option{-Wextra}, and can also be enabled 33591debfc3dSmrgindividually with @option{-Wexpansion-to-defined}. 33601debfc3dSmrg 33611debfc3dSmrg@node Else 33621debfc3dSmrg@subsection Else 33631debfc3dSmrg 33641debfc3dSmrg@findex #else 33651debfc3dSmrgThe @samp{#else} directive can be added to a conditional to provide 33661debfc3dSmrgalternative text to be used if the condition fails. This is what it 33671debfc3dSmrglooks like: 33681debfc3dSmrg 33691debfc3dSmrg@smallexample 33701debfc3dSmrg@group 33711debfc3dSmrg#if @var{expression} 33721debfc3dSmrg@var{text-if-true} 33731debfc3dSmrg#else /* Not @var{expression} */ 33741debfc3dSmrg@var{text-if-false} 33751debfc3dSmrg#endif /* Not @var{expression} */ 33761debfc3dSmrg@end group 33771debfc3dSmrg@end smallexample 33781debfc3dSmrg 33791debfc3dSmrg@noindent 33801debfc3dSmrgIf @var{expression} is nonzero, the @var{text-if-true} is included and 33811debfc3dSmrgthe @var{text-if-false} is skipped. If @var{expression} is zero, the 33821debfc3dSmrgopposite happens. 33831debfc3dSmrg 33841debfc3dSmrgYou can use @samp{#else} with @samp{#ifdef} and @samp{#ifndef}, too. 33851debfc3dSmrg 33861debfc3dSmrg@node Elif 33871debfc3dSmrg@subsection Elif 33881debfc3dSmrg 33891debfc3dSmrg@findex #elif 33901debfc3dSmrgOne common case of nested conditionals is used to check for more than two 33911debfc3dSmrgpossible alternatives. For example, you might have 33921debfc3dSmrg 33931debfc3dSmrg@smallexample 33941debfc3dSmrg#if X == 1 33951debfc3dSmrg@dots{} 33961debfc3dSmrg#else /* X != 1 */ 33971debfc3dSmrg#if X == 2 33981debfc3dSmrg@dots{} 33991debfc3dSmrg#else /* X != 2 */ 34001debfc3dSmrg@dots{} 34011debfc3dSmrg#endif /* X != 2 */ 34021debfc3dSmrg#endif /* X != 1 */ 34031debfc3dSmrg@end smallexample 34041debfc3dSmrg 34051debfc3dSmrgAnother conditional directive, @samp{#elif}, allows this to be 34061debfc3dSmrgabbreviated as follows: 34071debfc3dSmrg 34081debfc3dSmrg@smallexample 34091debfc3dSmrg#if X == 1 34101debfc3dSmrg@dots{} 34111debfc3dSmrg#elif X == 2 34121debfc3dSmrg@dots{} 34131debfc3dSmrg#else /* X != 2 and X != 1*/ 34141debfc3dSmrg@dots{} 34151debfc3dSmrg#endif /* X != 2 and X != 1*/ 34161debfc3dSmrg@end smallexample 34171debfc3dSmrg 34181debfc3dSmrg@samp{#elif} stands for ``else if''. Like @samp{#else}, it goes in the 34191debfc3dSmrgmiddle of a conditional group and subdivides it; it does not require a 34201debfc3dSmrgmatching @samp{#endif} of its own. Like @samp{#if}, the @samp{#elif} 34211debfc3dSmrgdirective includes an expression to be tested. The text following the 34221debfc3dSmrg@samp{#elif} is processed only if the original @samp{#if}-condition 34231debfc3dSmrgfailed and the @samp{#elif} condition succeeds. 34241debfc3dSmrg 34251debfc3dSmrgMore than one @samp{#elif} can go in the same conditional group. Then 34261debfc3dSmrgthe text after each @samp{#elif} is processed only if the @samp{#elif} 34271debfc3dSmrgcondition succeeds after the original @samp{#if} and all previous 34281debfc3dSmrg@samp{#elif} directives within it have failed. 34291debfc3dSmrg 34301debfc3dSmrg@samp{#else} is allowed after any number of @samp{#elif} directives, but 34311debfc3dSmrg@samp{#elif} may not follow @samp{#else}. 34321debfc3dSmrg 3433c0a68be4Smrg@node @code{__has_attribute} 3434c0a68be4Smrg@subsection @code{__has_attribute} 3435c0a68be4Smrg@cindex @code{__has_attribute} 3436c0a68be4Smrg 3437c0a68be4SmrgThe special operator @code{__has_attribute (@var{operand})} may be used 3438c0a68be4Smrgin @samp{#if} and @samp{#elif} expressions to test whether the attribute 3439c0a68be4Smrgreferenced by its @var{operand} is recognized by GCC. Using the operator 3440c0a68be4Smrgin other contexts is not valid. In C code, @var{operand} must be 3441c0a68be4Smrga valid identifier. In C++ code, @var{operand} may be optionally 3442c0a68be4Smrgintroduced by the @code{@var{attribute-scope}::} prefix. 3443c0a68be4SmrgThe @var{attribute-scope} prefix identifies the ``namespace'' within 3444c0a68be4Smrgwhich the attribute is recognized. The scope of GCC attributes is 3445c0a68be4Smrg@samp{gnu} or @samp{__gnu__}. The @code{__has_attribute} operator by 3446c0a68be4Smrgitself, without any @var{operand} or parentheses, acts as a predefined 3447c0a68be4Smrgmacro so that support for it can be tested in portable code. Thus, 3448c0a68be4Smrgthe recommended use of the operator is as follows: 3449c0a68be4Smrg 3450c0a68be4Smrg@smallexample 3451c0a68be4Smrg#if defined __has_attribute 3452c0a68be4Smrg# if __has_attribute (nonnull) 3453c0a68be4Smrg# define ATTR_NONNULL __attribute__ ((nonnull)) 3454c0a68be4Smrg# endif 3455c0a68be4Smrg#endif 3456c0a68be4Smrg@end smallexample 3457c0a68be4Smrg 3458c0a68be4SmrgThe first @samp{#if} test succeeds only when the operator is supported 3459c0a68be4Smrgby the version of GCC (or another compiler) being used. Only when that 3460c0a68be4Smrgtest succeeds is it valid to use @code{__has_attribute} as a preprocessor 3461c0a68be4Smrgoperator. As a result, combining the two tests into a single expression as 3462c0a68be4Smrgshown below would only be valid with a compiler that supports the operator 3463c0a68be4Smrgbut not with others that don't. 3464c0a68be4Smrg 3465c0a68be4Smrg@smallexample 3466c0a68be4Smrg#if defined __has_attribute && __has_attribute (nonnull) /* not portable */ 3467c0a68be4Smrg@dots{} 3468c0a68be4Smrg#endif 3469c0a68be4Smrg@end smallexample 3470c0a68be4Smrg 3471c0a68be4Smrg@node @code{__has_cpp_attribute} 3472c0a68be4Smrg@subsection @code{__has_cpp_attribute} 3473c0a68be4Smrg@cindex @code{__has_cpp_attribute} 3474c0a68be4Smrg 3475c0a68be4SmrgThe special operator @code{__has_cpp_attribute (@var{operand})} may be used 3476c0a68be4Smrgin @samp{#if} and @samp{#elif} expressions in C++ code to test whether 3477c0a68be4Smrgthe attribute referenced by its @var{operand} is recognized by GCC. 3478c0a68be4Smrg@code{__has_cpp_attribute (@var{operand})} is equivalent to 3479c0a68be4Smrg@code{__has_attribute (@var{operand})} except that when @var{operand} 3480c0a68be4Smrgdesignates a supported standard attribute it evaluates to an integer 3481c0a68be4Smrgconstant of the form @code{YYYYMM} indicating the year and month when 3482c0a68be4Smrgthe attribute was first introduced into the C++ standard. For additional 3483c0a68be4Smrginformation including the dates of the introduction of current standard 3484c0a68be4Smrgattributes, see @w{@uref{https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations/, 3485c0a68be4SmrgSD-6: SG10 Feature Test Recommendations}}. 3486c0a68be4Smrg 3487*8feb0f0bSmrg@node @code{__has_builtin} 3488*8feb0f0bSmrg@subsection @code{__has_builtin} 3489*8feb0f0bSmrg@cindex @code{__has_builtin} 3490*8feb0f0bSmrg 3491*8feb0f0bSmrgThe special operator @code{__has_builtin (@var{operand})} may be used in 3492*8feb0f0bSmrgconstant integer contexts and in preprocessor @samp{#if} and @samp{#elif} 3493*8feb0f0bSmrgexpressions to test whether the symbol named by its @var{operand} is 3494*8feb0f0bSmrgrecognized as a built-in function by GCC in the current language and 3495*8feb0f0bSmrgconformance mode. It evaluates to a constant integer with a nonzero 3496*8feb0f0bSmrgvalue if the argument refers to such a function, and to zero otherwise. 3497*8feb0f0bSmrgThe operator may also be used in preprocessor @samp{#if} and @samp{#elif} 3498*8feb0f0bSmrgexpressions. The @code{__has_builtin} operator by itself, without any 3499*8feb0f0bSmrg@var{operand} or parentheses, acts as a predefined macro so that support 3500*8feb0f0bSmrgfor it can be tested in portable code. Thus, the recommended use of 3501*8feb0f0bSmrgthe operator is as follows: 3502*8feb0f0bSmrg 3503*8feb0f0bSmrg@smallexample 3504*8feb0f0bSmrg#if defined __has_builtin 3505*8feb0f0bSmrg# if __has_builtin (__builtin_object_size) 3506*8feb0f0bSmrg# define builtin_object_size(ptr) __builtin_object_size (ptr, 2) 3507*8feb0f0bSmrg# endif 3508*8feb0f0bSmrg#endif 3509*8feb0f0bSmrg#ifndef builtin_object_size 3510*8feb0f0bSmrg# define builtin_object_size(ptr) ((size_t)-1) 3511*8feb0f0bSmrg#endif 3512*8feb0f0bSmrg@end smallexample 3513*8feb0f0bSmrg 3514c0a68be4Smrg@node @code{__has_include} 3515c0a68be4Smrg@subsection @code{__has_include} 3516c0a68be4Smrg@cindex @code{__has_include} 3517c0a68be4Smrg 3518c0a68be4SmrgThe special operator @code{__has_include (@var{operand})} may be used in 3519c0a68be4Smrg@samp{#if} and @samp{#elif} expressions to test whether the header referenced 3520c0a68be4Smrgby its @var{operand} can be included using the @samp{#include} directive. Using 3521c0a68be4Smrgthe operator in other contexts is not valid. The @var{operand} takes 3522c0a68be4Smrgthe same form as the file in the @samp{#include} directive (@pxref{Include 3523c0a68be4SmrgSyntax}) and evaluates to a nonzero value if the header can be included and 3524c0a68be4Smrgto zero otherwise. Note that that the ability to include a header doesn't 3525c0a68be4Smrgimply that the header doesn't contain invalid constructs or @samp{#error} 3526c0a68be4Smrgdirectives that would cause the preprocessor to fail. 3527c0a68be4Smrg 3528c0a68be4SmrgThe @code{__has_include} operator by itself, without any @var{operand} or 3529c0a68be4Smrgparentheses, acts as a predefined macro so that support for it can be tested 3530c0a68be4Smrgin portable code. Thus, the recommended use of the operator is as follows: 3531c0a68be4Smrg 3532c0a68be4Smrg@smallexample 3533c0a68be4Smrg#if defined __has_include 3534c0a68be4Smrg# if __has_include (<stdatomic.h>) 3535c0a68be4Smrg# include <stdatomic.h> 3536c0a68be4Smrg# endif 3537c0a68be4Smrg#endif 3538c0a68be4Smrg@end smallexample 3539c0a68be4Smrg 3540c0a68be4SmrgThe first @samp{#if} test succeeds only when the operator is supported 3541c0a68be4Smrgby the version of GCC (or another compiler) being used. Only when that 3542c0a68be4Smrgtest succeeds is it valid to use @code{__has_include} as a preprocessor 3543c0a68be4Smrgoperator. As a result, combining the two tests into a single expression 3544c0a68be4Smrgas shown below would only be valid with a compiler that supports the operator 3545c0a68be4Smrgbut not with others that don't. 3546c0a68be4Smrg 3547c0a68be4Smrg@smallexample 3548c0a68be4Smrg#if defined __has_include && __has_include ("header.h") /* not portable */ 3549c0a68be4Smrg@dots{} 3550c0a68be4Smrg#endif 3551c0a68be4Smrg@end smallexample 3552c0a68be4Smrg 35531debfc3dSmrg@node Deleted Code 35541debfc3dSmrg@section Deleted Code 35551debfc3dSmrg@cindex commenting out code 35561debfc3dSmrg 35571debfc3dSmrgIf you replace or delete a part of the program but want to keep the old 35581debfc3dSmrgcode around for future reference, you often cannot simply comment it 35591debfc3dSmrgout. Block comments do not nest, so the first comment inside the old 35601debfc3dSmrgcode will end the commenting-out. The probable result is a flood of 35611debfc3dSmrgsyntax errors. 35621debfc3dSmrg 35631debfc3dSmrgOne way to avoid this problem is to use an always-false conditional 35641debfc3dSmrginstead. For instance, put @code{#if 0} before the deleted code and 35651debfc3dSmrg@code{#endif} after it. This works even if the code being turned 35661debfc3dSmrgoff contains conditionals, but they must be entire conditionals 35671debfc3dSmrg(balanced @samp{#if} and @samp{#endif}). 35681debfc3dSmrg 35691debfc3dSmrgSome people use @code{#ifdef notdef} instead. This is risky, because 35701debfc3dSmrg@code{notdef} might be accidentally defined as a macro, and then the 35711debfc3dSmrgconditional would succeed. @code{#if 0} can be counted on to fail. 35721debfc3dSmrg 35731debfc3dSmrgDo not use @code{#if 0} for comments which are not C code. Use a real 35741debfc3dSmrgcomment, instead. The interior of @code{#if 0} must consist of complete 35751debfc3dSmrgtokens; in particular, single-quote characters must balance. Comments 35761debfc3dSmrgoften contain unbalanced single-quote characters (known in English as 35771debfc3dSmrgapostrophes). These confuse @code{#if 0}. They don't confuse 35781debfc3dSmrg@samp{/*}. 35791debfc3dSmrg 35801debfc3dSmrg@node Diagnostics 35811debfc3dSmrg@chapter Diagnostics 35821debfc3dSmrg@cindex diagnostic 35831debfc3dSmrg@cindex reporting errors 35841debfc3dSmrg@cindex reporting warnings 35851debfc3dSmrg 35861debfc3dSmrg@findex #error 35871debfc3dSmrgThe directive @samp{#error} causes the preprocessor to report a fatal 35881debfc3dSmrgerror. The tokens forming the rest of the line following @samp{#error} 35891debfc3dSmrgare used as the error message. 35901debfc3dSmrg 35911debfc3dSmrgYou would use @samp{#error} inside of a conditional that detects a 35921debfc3dSmrgcombination of parameters which you know the program does not properly 35931debfc3dSmrgsupport. For example, if you know that the program will not run 35941debfc3dSmrgproperly on a VAX, you might write 35951debfc3dSmrg 35961debfc3dSmrg@smallexample 35971debfc3dSmrg@group 35981debfc3dSmrg#ifdef __vax__ 35991debfc3dSmrg#error "Won't work on VAXen. See comments at get_last_object." 36001debfc3dSmrg#endif 36011debfc3dSmrg@end group 36021debfc3dSmrg@end smallexample 36031debfc3dSmrg 36041debfc3dSmrgIf you have several configuration parameters that must be set up by 36051debfc3dSmrgthe installation in a consistent way, you can use conditionals to detect 36061debfc3dSmrgan inconsistency and report it with @samp{#error}. For example, 36071debfc3dSmrg 36081debfc3dSmrg@smallexample 36091debfc3dSmrg#if !defined(FOO) && defined(BAR) 36101debfc3dSmrg#error "BAR requires FOO." 36111debfc3dSmrg#endif 36121debfc3dSmrg@end smallexample 36131debfc3dSmrg 36141debfc3dSmrg@findex #warning 36151debfc3dSmrgThe directive @samp{#warning} is like @samp{#error}, but causes the 36161debfc3dSmrgpreprocessor to issue a warning and continue preprocessing. The tokens 36171debfc3dSmrgfollowing @samp{#warning} are used as the warning message. 36181debfc3dSmrg 36191debfc3dSmrgYou might use @samp{#warning} in obsolete header files, with a message 36201debfc3dSmrgdirecting the user to the header file which should be used instead. 36211debfc3dSmrg 36221debfc3dSmrgNeither @samp{#error} nor @samp{#warning} macro-expands its argument. 36231debfc3dSmrgInternal whitespace sequences are each replaced with a single space. 36241debfc3dSmrgThe line must consist of complete tokens. It is wisest to make the 36251debfc3dSmrgargument of these directives be a single string constant; this avoids 36261debfc3dSmrgproblems with apostrophes and the like. 36271debfc3dSmrg 36281debfc3dSmrg@node Line Control 36291debfc3dSmrg@chapter Line Control 36301debfc3dSmrg@cindex line control 36311debfc3dSmrg 36321debfc3dSmrgThe C preprocessor informs the C compiler of the location in your source 36331debfc3dSmrgcode where each token came from. Presently, this is just the file name 36341debfc3dSmrgand line number. All the tokens resulting from macro expansion are 36351debfc3dSmrgreported as having appeared on the line of the source file where the 36361debfc3dSmrgoutermost macro was used. We intend to be more accurate in the future. 36371debfc3dSmrg 36381debfc3dSmrgIf you write a program which generates source code, such as the 36391debfc3dSmrg@command{bison} parser generator, you may want to adjust the preprocessor's 36401debfc3dSmrgnotion of the current file name and line number by hand. Parts of the 36411debfc3dSmrgoutput from @command{bison} are generated from scratch, other parts come 36421debfc3dSmrgfrom a standard parser file. The rest are copied verbatim from 36431debfc3dSmrg@command{bison}'s input. You would like compiler error messages and 36441debfc3dSmrgsymbolic debuggers to be able to refer to @code{bison}'s input file. 36451debfc3dSmrg 36461debfc3dSmrg@findex #line 36471debfc3dSmrg@command{bison} or any such program can arrange this by writing 36481debfc3dSmrg@samp{#line} directives into the output file. @samp{#line} is a 36491debfc3dSmrgdirective that specifies the original line number and source file name 36501debfc3dSmrgfor subsequent input in the current preprocessor input file. 36511debfc3dSmrg@samp{#line} has three variants: 36521debfc3dSmrg 36531debfc3dSmrg@table @code 36541debfc3dSmrg@item #line @var{linenum} 36551debfc3dSmrg@var{linenum} is a non-negative decimal integer constant. It specifies 36561debfc3dSmrgthe line number which should be reported for the following line of 36571debfc3dSmrginput. Subsequent lines are counted from @var{linenum}. 36581debfc3dSmrg 36591debfc3dSmrg@item #line @var{linenum} @var{filename} 36601debfc3dSmrg@var{linenum} is the same as for the first form, and has the same 36611debfc3dSmrgeffect. In addition, @var{filename} is a string constant. The 36621debfc3dSmrgfollowing line and all subsequent lines are reported to come from the 36631debfc3dSmrgfile it specifies, until something else happens to change that. 36641debfc3dSmrg@var{filename} is interpreted according to the normal rules for a string 36651debfc3dSmrgconstant: backslash escapes are interpreted. This is different from 36661debfc3dSmrg@samp{#include}. 36671debfc3dSmrg 36681debfc3dSmrg@item #line @var{anything else} 36691debfc3dSmrg@var{anything else} is checked for macro calls, which are expanded. 36701debfc3dSmrgThe result should match one of the above two forms. 36711debfc3dSmrg@end table 36721debfc3dSmrg 36731debfc3dSmrg@samp{#line} directives alter the results of the @code{__FILE__} and 36741debfc3dSmrg@code{__LINE__} predefined macros from that point on. @xref{Standard 36751debfc3dSmrgPredefined Macros}. They do not have any effect on @samp{#include}'s 36761debfc3dSmrgidea of the directory containing the current file. 36771debfc3dSmrg 36781debfc3dSmrg@node Pragmas 36791debfc3dSmrg@chapter Pragmas 36801debfc3dSmrg 3681c0a68be4Smrg@cindex pragma directive 3682c0a68be4Smrg 36831debfc3dSmrgThe @samp{#pragma} directive is the method specified by the C standard 36841debfc3dSmrgfor providing additional information to the compiler, beyond what is 36851debfc3dSmrgconveyed in the language itself. The forms of this directive 36861debfc3dSmrg(commonly known as @dfn{pragmas}) specified by C standard are prefixed with 36871debfc3dSmrg@code{STDC}. A C compiler is free to attach any meaning it likes to other 3688c0a68be4Smrgpragmas. Most GNU-defined, supported pragmas have been given a 36891debfc3dSmrg@code{GCC} prefix. 36901debfc3dSmrg 36911debfc3dSmrg@cindex @code{_Pragma} 36921debfc3dSmrgC99 introduced the @code{@w{_Pragma}} operator. This feature addresses a 36931debfc3dSmrgmajor problem with @samp{#pragma}: being a directive, it cannot be 36941debfc3dSmrgproduced as the result of macro expansion. @code{@w{_Pragma}} is an 36951debfc3dSmrgoperator, much like @code{sizeof} or @code{defined}, and can be embedded 36961debfc3dSmrgin a macro. 36971debfc3dSmrg 36981debfc3dSmrgIts syntax is @code{@w{_Pragma (@var{string-literal})}}, where 36991debfc3dSmrg@var{string-literal} can be either a normal or wide-character string 37001debfc3dSmrgliteral. It is destringized, by replacing all @samp{\\} with a single 37011debfc3dSmrg@samp{\} and all @samp{\"} with a @samp{"}. The result is then 37021debfc3dSmrgprocessed as if it had appeared as the right hand side of a 37031debfc3dSmrg@samp{#pragma} directive. For example, 37041debfc3dSmrg 37051debfc3dSmrg@smallexample 37061debfc3dSmrg_Pragma ("GCC dependency \"parse.y\"") 37071debfc3dSmrg@end smallexample 37081debfc3dSmrg 37091debfc3dSmrg@noindent 37101debfc3dSmrghas the same effect as @code{#pragma GCC dependency "parse.y"}. The 37111debfc3dSmrgsame effect could be achieved using macros, for example 37121debfc3dSmrg 37131debfc3dSmrg@smallexample 37141debfc3dSmrg#define DO_PRAGMA(x) _Pragma (#x) 37151debfc3dSmrgDO_PRAGMA (GCC dependency "parse.y") 37161debfc3dSmrg@end smallexample 37171debfc3dSmrg 37181debfc3dSmrgThe standard is unclear on where a @code{_Pragma} operator can appear. 37191debfc3dSmrgThe preprocessor does not accept it within a preprocessing conditional 37201debfc3dSmrgdirective like @samp{#if}. To be safe, you are probably best keeping it 37211debfc3dSmrgout of directives other than @samp{#define}, and putting it on a line of 37221debfc3dSmrgits own. 37231debfc3dSmrg 37241debfc3dSmrgThis manual documents the pragmas which are meaningful to the 37251debfc3dSmrgpreprocessor itself. Other pragmas are meaningful to the C or C++ 37261debfc3dSmrgcompilers. They are documented in the GCC manual. 37271debfc3dSmrg 37281debfc3dSmrgGCC plugins may provide their own pragmas. 37291debfc3dSmrg 37301debfc3dSmrg@ftable @code 37311debfc3dSmrg@item #pragma GCC dependency 37321debfc3dSmrg@code{#pragma GCC dependency} allows you to check the relative dates of 37331debfc3dSmrgthe current file and another file. If the other file is more recent than 37341debfc3dSmrgthe current file, a warning is issued. This is useful if the current 37351debfc3dSmrgfile is derived from the other file, and should be regenerated. The 37361debfc3dSmrgother file is searched for using the normal include search path. 37371debfc3dSmrgOptional trailing text can be used to give more information in the 37381debfc3dSmrgwarning message. 37391debfc3dSmrg 37401debfc3dSmrg@smallexample 37411debfc3dSmrg#pragma GCC dependency "parse.y" 37421debfc3dSmrg#pragma GCC dependency "/usr/include/time.h" rerun fixincludes 37431debfc3dSmrg@end smallexample 37441debfc3dSmrg 37451debfc3dSmrg@item #pragma GCC poison 37461debfc3dSmrgSometimes, there is an identifier that you want to remove completely 37471debfc3dSmrgfrom your program, and make sure that it never creeps back in. To 37481debfc3dSmrgenforce this, you can @dfn{poison} the identifier with this pragma. 37491debfc3dSmrg@code{#pragma GCC poison} is followed by a list of identifiers to 37501debfc3dSmrgpoison. If any of those identifiers appears anywhere in the source 37511debfc3dSmrgafter the directive, it is a hard error. For example, 37521debfc3dSmrg 37531debfc3dSmrg@smallexample 37541debfc3dSmrg#pragma GCC poison printf sprintf fprintf 37551debfc3dSmrgsprintf(some_string, "hello"); 37561debfc3dSmrg@end smallexample 37571debfc3dSmrg 37581debfc3dSmrg@noindent 37591debfc3dSmrgwill produce an error. 37601debfc3dSmrg 37611debfc3dSmrgIf a poisoned identifier appears as part of the expansion of a macro 37621debfc3dSmrgwhich was defined before the identifier was poisoned, it will @emph{not} 37631debfc3dSmrgcause an error. This lets you poison an identifier without worrying 37641debfc3dSmrgabout system headers defining macros that use it. 37651debfc3dSmrg 37661debfc3dSmrgFor example, 37671debfc3dSmrg 37681debfc3dSmrg@smallexample 37691debfc3dSmrg#define strrchr rindex 37701debfc3dSmrg#pragma GCC poison rindex 37711debfc3dSmrgstrrchr(some_string, 'h'); 37721debfc3dSmrg@end smallexample 37731debfc3dSmrg 37741debfc3dSmrg@noindent 37751debfc3dSmrgwill not produce an error. 37761debfc3dSmrg 37771debfc3dSmrg@item #pragma GCC system_header 37781debfc3dSmrgThis pragma takes no arguments. It causes the rest of the code in the 37791debfc3dSmrgcurrent file to be treated as if it came from a system header. 37801debfc3dSmrg@xref{System Headers}. 37811debfc3dSmrg 37821debfc3dSmrg@item #pragma GCC warning 37831debfc3dSmrg@itemx #pragma GCC error 37841debfc3dSmrg@code{#pragma GCC warning "message"} causes the preprocessor to issue 37851debfc3dSmrga warning diagnostic with the text @samp{message}. The message 37861debfc3dSmrgcontained in the pragma must be a single string literal. Similarly, 37871debfc3dSmrg@code{#pragma GCC error "message"} issues an error message. Unlike 37881debfc3dSmrgthe @samp{#warning} and @samp{#error} directives, these pragmas can be 37891debfc3dSmrgembedded in preprocessor macros using @samp{_Pragma}. 37901debfc3dSmrg 3791c0a68be4Smrg@item #pragma once 3792c0a68be4SmrgIf @code{#pragma once} is seen when scanning a header file, that 3793c0a68be4Smrgfile will never be read again, no matter what. It is a less-portable 3794c0a68be4Smrgalternative to using @samp{#ifndef} to guard the contents of header files 3795c0a68be4Smrgagainst multiple inclusions. 3796c0a68be4Smrg 37971debfc3dSmrg@end ftable 37981debfc3dSmrg 37991debfc3dSmrg@node Other Directives 38001debfc3dSmrg@chapter Other Directives 38011debfc3dSmrg 38021debfc3dSmrg@findex #ident 38031debfc3dSmrg@findex #sccs 38041debfc3dSmrgThe @samp{#ident} directive takes one argument, a string constant. On 38051debfc3dSmrgsome systems, that string constant is copied into a special segment of 38061debfc3dSmrgthe object file. On other systems, the directive is ignored. The 38071debfc3dSmrg@samp{#sccs} directive is a synonym for @samp{#ident}. 38081debfc3dSmrg 38091debfc3dSmrgThese directives are not part of the C standard, but they are not 38101debfc3dSmrgofficial GNU extensions either. What historical information we have 38111debfc3dSmrgbeen able to find, suggests they originated with System V@. 38121debfc3dSmrg 38131debfc3dSmrg@cindex null directive 38141debfc3dSmrgThe @dfn{null directive} consists of a @samp{#} followed by a newline, 38151debfc3dSmrgwith only whitespace (including comments) in between. A null directive 38161debfc3dSmrgis understood as a preprocessing directive but has no effect on the 38171debfc3dSmrgpreprocessor output. The primary significance of the existence of the 38181debfc3dSmrgnull directive is that an input line consisting of just a @samp{#} will 38191debfc3dSmrgproduce no output, rather than a line of output containing just a 38201debfc3dSmrg@samp{#}. Supposedly some old C programs contain such lines. 38211debfc3dSmrg 38221debfc3dSmrg@node Preprocessor Output 38231debfc3dSmrg@chapter Preprocessor Output 38241debfc3dSmrg 38251debfc3dSmrgWhen the C preprocessor is used with the C, C++, or Objective-C 38261debfc3dSmrgcompilers, it is integrated into the compiler and communicates a stream 38271debfc3dSmrgof binary tokens directly to the compiler's parser. However, it can 38281debfc3dSmrgalso be used in the more conventional standalone mode, where it produces 38291debfc3dSmrgtextual output. 38301debfc3dSmrg@c FIXME: Document the library interface. 38311debfc3dSmrg 38321debfc3dSmrg@cindex output format 38331debfc3dSmrgThe output from the C preprocessor looks much like the input, except 38341debfc3dSmrgthat all preprocessing directive lines have been replaced with blank 38351debfc3dSmrglines and all comments with spaces. Long runs of blank lines are 38361debfc3dSmrgdiscarded. 38371debfc3dSmrg 38381debfc3dSmrgThe ISO standard specifies that it is implementation defined whether a 38391debfc3dSmrgpreprocessor preserves whitespace between tokens, or replaces it with 38401debfc3dSmrge.g.@: a single space. In GNU CPP, whitespace between tokens is collapsed 38411debfc3dSmrgto become a single space, with the exception that the first token on a 38421debfc3dSmrgnon-directive line is preceded with sufficient spaces that it appears in 38431debfc3dSmrgthe same column in the preprocessed output that it appeared in the 38441debfc3dSmrgoriginal source file. This is so the output is easy to read. 38451debfc3dSmrgCPP does not insert any 38461debfc3dSmrgwhitespace where there was none in the original source, except where 38471debfc3dSmrgnecessary to prevent an accidental token paste. 38481debfc3dSmrg 38491debfc3dSmrg@cindex linemarkers 38501debfc3dSmrgSource file name and line number information is conveyed by lines 38511debfc3dSmrgof the form 38521debfc3dSmrg 38531debfc3dSmrg@smallexample 38541debfc3dSmrg# @var{linenum} @var{filename} @var{flags} 38551debfc3dSmrg@end smallexample 38561debfc3dSmrg 38571debfc3dSmrg@noindent 38581debfc3dSmrgThese are called @dfn{linemarkers}. They are inserted as needed into 38591debfc3dSmrgthe output (but never within a string or character constant). They mean 38601debfc3dSmrgthat the following line originated in file @var{filename} at line 38611debfc3dSmrg@var{linenum}. @var{filename} will never contain any non-printing 38621debfc3dSmrgcharacters; they are replaced with octal escape sequences. 38631debfc3dSmrg 38641debfc3dSmrgAfter the file name comes zero or more flags, which are @samp{1}, 38651debfc3dSmrg@samp{2}, @samp{3}, or @samp{4}. If there are multiple flags, spaces 38661debfc3dSmrgseparate them. Here is what the flags mean: 38671debfc3dSmrg 38681debfc3dSmrg@table @samp 38691debfc3dSmrg@item 1 38701debfc3dSmrgThis indicates the start of a new file. 38711debfc3dSmrg@item 2 38721debfc3dSmrgThis indicates returning to a file (after having included another file). 38731debfc3dSmrg@item 3 38741debfc3dSmrgThis indicates that the following text comes from a system header file, 38751debfc3dSmrgso certain warnings should be suppressed. 38761debfc3dSmrg@item 4 38771debfc3dSmrgThis indicates that the following text should be treated as being 38781debfc3dSmrgwrapped in an implicit @code{extern "C"} block. 3879c0a68be4Smrg@c maybe cross reference SYSTEM_IMPLICIT_EXTERN_C 38801debfc3dSmrg@end table 38811debfc3dSmrg 38821debfc3dSmrgAs an extension, the preprocessor accepts linemarkers in non-assembler 38831debfc3dSmrginput files. They are treated like the corresponding @samp{#line} 38841debfc3dSmrgdirective, (@pxref{Line Control}), except that trailing flags are 38851debfc3dSmrgpermitted, and are interpreted with the meanings described above. If 38861debfc3dSmrgmultiple flags are given, they must be in ascending order. 38871debfc3dSmrg 38881debfc3dSmrgSome directives may be duplicated in the output of the preprocessor. 38891debfc3dSmrgThese are @samp{#ident} (always), @samp{#pragma} (only if the 38901debfc3dSmrgpreprocessor does not handle the pragma itself), and @samp{#define} and 38911debfc3dSmrg@samp{#undef} (with certain debugging options). If this happens, the 38921debfc3dSmrg@samp{#} of the directive will always be in the first column, and there 38931debfc3dSmrgwill be no space between the @samp{#} and the directive name. If macro 38941debfc3dSmrgexpansion happens to generate tokens which might be mistaken for a 38951debfc3dSmrgduplicated directive, a space will be inserted between the @samp{#} and 38961debfc3dSmrgthe directive name. 38971debfc3dSmrg 38981debfc3dSmrg@node Traditional Mode 38991debfc3dSmrg@chapter Traditional Mode 39001debfc3dSmrg 39011debfc3dSmrgTraditional (pre-standard) C preprocessing is rather different from 39021debfc3dSmrgthe preprocessing specified by the standard. When the preprocessor 39031debfc3dSmrgis invoked with the 39041debfc3dSmrg@option{-traditional-cpp} option, it attempts to emulate a traditional 39051debfc3dSmrgpreprocessor. 39061debfc3dSmrg 39071debfc3dSmrgThis mode is not useful for compiling C code with GCC, 39081debfc3dSmrgbut is intended for use with non-C preprocessing applications. Thus 39091debfc3dSmrgtraditional mode semantics are supported only when invoking 39101debfc3dSmrgthe preprocessor explicitly, and not in the compiler front ends. 39111debfc3dSmrg 39121debfc3dSmrgThe implementation does not correspond precisely to the behavior of 39131debfc3dSmrgearly pre-standard versions of GCC, nor to any true traditional preprocessor. 39141debfc3dSmrgAfter all, inconsistencies among traditional implementations were a 39151debfc3dSmrgmajor motivation for C standardization. However, we intend that it 39161debfc3dSmrgshould be compatible with true traditional preprocessors in all ways 39171debfc3dSmrgthat actually matter. 39181debfc3dSmrg 39191debfc3dSmrg@menu 39201debfc3dSmrg* Traditional lexical analysis:: 39211debfc3dSmrg* Traditional macros:: 39221debfc3dSmrg* Traditional miscellany:: 39231debfc3dSmrg* Traditional warnings:: 39241debfc3dSmrg@end menu 39251debfc3dSmrg 39261debfc3dSmrg@node Traditional lexical analysis 39271debfc3dSmrg@section Traditional lexical analysis 39281debfc3dSmrg 39291debfc3dSmrgThe traditional preprocessor does not decompose its input into tokens 39301debfc3dSmrgthe same way a standards-conforming preprocessor does. The input is 39311debfc3dSmrgsimply treated as a stream of text with minimal internal form. 39321debfc3dSmrg 39331debfc3dSmrgThis implementation does not treat trigraphs (@pxref{trigraphs}) 39341debfc3dSmrgspecially since they were an invention of the standards committee. It 39351debfc3dSmrghandles arbitrarily-positioned escaped newlines properly and splices 39361debfc3dSmrgthe lines as you would expect; many traditional preprocessors did not 39371debfc3dSmrgdo this. 39381debfc3dSmrg 39391debfc3dSmrgThe form of horizontal whitespace in the input file is preserved in 39401debfc3dSmrgthe output. In particular, hard tabs remain hard tabs. This can be 39411debfc3dSmrguseful if, for example, you are preprocessing a Makefile. 39421debfc3dSmrg 39431debfc3dSmrgTraditional CPP only recognizes C-style block comments, and treats the 39441debfc3dSmrg@samp{/*} sequence as introducing a comment only if it lies outside 39451debfc3dSmrgquoted text. Quoted text is introduced by the usual single and double 39461debfc3dSmrgquotes, and also by an initial @samp{<} in a @code{#include} 39471debfc3dSmrgdirective. 39481debfc3dSmrg 39491debfc3dSmrgTraditionally, comments are completely removed and are not replaced 39501debfc3dSmrgwith a space. Since a traditional compiler does its own tokenization 39511debfc3dSmrgof the output of the preprocessor, this means that comments can 39521debfc3dSmrgeffectively be used as token paste operators. However, comments 39531debfc3dSmrgbehave like separators for text handled by the preprocessor itself, 39541debfc3dSmrgsince it doesn't re-lex its input. For example, in 39551debfc3dSmrg 39561debfc3dSmrg@smallexample 39571debfc3dSmrg#if foo/**/bar 39581debfc3dSmrg@end smallexample 39591debfc3dSmrg 39601debfc3dSmrg@noindent 39611debfc3dSmrg@samp{foo} and @samp{bar} are distinct identifiers and expanded 39621debfc3dSmrgseparately if they happen to be macros. In other words, this 39631debfc3dSmrgdirective is equivalent to 39641debfc3dSmrg 39651debfc3dSmrg@smallexample 39661debfc3dSmrg#if foo bar 39671debfc3dSmrg@end smallexample 39681debfc3dSmrg 39691debfc3dSmrg@noindent 39701debfc3dSmrgrather than 39711debfc3dSmrg 39721debfc3dSmrg@smallexample 39731debfc3dSmrg#if foobar 39741debfc3dSmrg@end smallexample 39751debfc3dSmrg 39761debfc3dSmrgGenerally speaking, in traditional mode an opening quote need not have 39771debfc3dSmrga matching closing quote. In particular, a macro may be defined with 39781debfc3dSmrgreplacement text that contains an unmatched quote. Of course, if you 39791debfc3dSmrgattempt to compile preprocessed output containing an unmatched quote 39801debfc3dSmrgyou will get a syntax error. 39811debfc3dSmrg 39821debfc3dSmrgHowever, all preprocessing directives other than @code{#define} 39831debfc3dSmrgrequire matching quotes. For example: 39841debfc3dSmrg 39851debfc3dSmrg@smallexample 39861debfc3dSmrg#define m This macro's fine and has an unmatched quote 39871debfc3dSmrg"/* This is not a comment. */ 39881debfc3dSmrg/* @r{This is a comment. The following #include directive 39891debfc3dSmrg is ill-formed.} */ 39901debfc3dSmrg#include <stdio.h 39911debfc3dSmrg@end smallexample 39921debfc3dSmrg 39931debfc3dSmrgJust as for the ISO preprocessor, what would be a closing quote can be 39941debfc3dSmrgescaped with a backslash to prevent the quoted text from closing. 39951debfc3dSmrg 39961debfc3dSmrg@node Traditional macros 39971debfc3dSmrg@section Traditional macros 39981debfc3dSmrg 39991debfc3dSmrgThe major difference between traditional and ISO macros is that the 40001debfc3dSmrgformer expand to text rather than to a token sequence. CPP removes 40011debfc3dSmrgall leading and trailing horizontal whitespace from a macro's 40021debfc3dSmrgreplacement text before storing it, but preserves the form of internal 40031debfc3dSmrgwhitespace. 40041debfc3dSmrg 40051debfc3dSmrgOne consequence is that it is legitimate for the replacement text to 40061debfc3dSmrgcontain an unmatched quote (@pxref{Traditional lexical analysis}). An 40071debfc3dSmrgunclosed string or character constant continues into the text 40081debfc3dSmrgfollowing the macro call. Similarly, the text at the end of a macro's 40091debfc3dSmrgexpansion can run together with the text after the macro invocation to 40101debfc3dSmrgproduce a single token. 40111debfc3dSmrg 40121debfc3dSmrgNormally comments are removed from the replacement text after the 40131debfc3dSmrgmacro is expanded, but if the @option{-CC} option is passed on the 40141debfc3dSmrgcommand-line comments are preserved. (In fact, the current 40151debfc3dSmrgimplementation removes comments even before saving the macro 40161debfc3dSmrgreplacement text, but it careful to do it in such a way that the 40171debfc3dSmrgobserved effect is identical even in the function-like macro case.) 40181debfc3dSmrg 40191debfc3dSmrgThe ISO stringizing operator @samp{#} and token paste operator 40201debfc3dSmrg@samp{##} have no special meaning. As explained later, an effect 40211debfc3dSmrgsimilar to these operators can be obtained in a different way. Macro 40221debfc3dSmrgnames that are embedded in quotes, either from the main file or after 40231debfc3dSmrgmacro replacement, do not expand. 40241debfc3dSmrg 40251debfc3dSmrgCPP replaces an unquoted object-like macro name with its replacement 40261debfc3dSmrgtext, and then rescans it for further macros to replace. Unlike 40271debfc3dSmrgstandard macro expansion, traditional macro expansion has no provision 40281debfc3dSmrgto prevent recursion. If an object-like macro appears unquoted in its 40291debfc3dSmrgreplacement text, it will be replaced again during the rescan pass, 40301debfc3dSmrgand so on @emph{ad infinitum}. GCC detects when it is expanding 40311debfc3dSmrgrecursive macros, emits an error message, and continues after the 40321debfc3dSmrgoffending macro invocation. 40331debfc3dSmrg 40341debfc3dSmrg@smallexample 40351debfc3dSmrg#define PLUS + 40361debfc3dSmrg#define INC(x) PLUS+x 40371debfc3dSmrgINC(foo); 40381debfc3dSmrg @expansion{} ++foo; 40391debfc3dSmrg@end smallexample 40401debfc3dSmrg 40411debfc3dSmrgFunction-like macros are similar in form but quite different in 40421debfc3dSmrgbehavior to their ISO counterparts. Their arguments are contained 40431debfc3dSmrgwithin parentheses, are comma-separated, and can cross physical lines. 40441debfc3dSmrgCommas within nested parentheses are not treated as argument 40451debfc3dSmrgseparators. Similarly, a quote in an argument cannot be left 40461debfc3dSmrgunclosed; a following comma or parenthesis that comes before the 40471debfc3dSmrgclosing quote is treated like any other character. There is no 40481debfc3dSmrgfacility for handling variadic macros. 40491debfc3dSmrg 40501debfc3dSmrgThis implementation removes all comments from macro arguments, unless 40511debfc3dSmrgthe @option{-C} option is given. The form of all other horizontal 40521debfc3dSmrgwhitespace in arguments is preserved, including leading and trailing 40531debfc3dSmrgwhitespace. In particular 40541debfc3dSmrg 40551debfc3dSmrg@smallexample 40561debfc3dSmrgf( ) 40571debfc3dSmrg@end smallexample 40581debfc3dSmrg 40591debfc3dSmrg@noindent 40601debfc3dSmrgis treated as an invocation of the macro @samp{f} with a single 40611debfc3dSmrgargument consisting of a single space. If you want to invoke a 40621debfc3dSmrgfunction-like macro that takes no arguments, you must not leave any 40631debfc3dSmrgwhitespace between the parentheses. 40641debfc3dSmrg 40651debfc3dSmrgIf a macro argument crosses a new line, the new line is replaced with 40661debfc3dSmrga space when forming the argument. If the previous line contained an 40671debfc3dSmrgunterminated quote, the following line inherits the quoted state. 40681debfc3dSmrg 40691debfc3dSmrgTraditional preprocessors replace parameters in the replacement text 40701debfc3dSmrgwith their arguments regardless of whether the parameters are within 40711debfc3dSmrgquotes or not. This provides a way to stringize arguments. For 40721debfc3dSmrgexample 40731debfc3dSmrg 40741debfc3dSmrg@smallexample 40751debfc3dSmrg#define str(x) "x" 40761debfc3dSmrgstr(/* @r{A comment} */some text ) 40771debfc3dSmrg @expansion{} "some text " 40781debfc3dSmrg@end smallexample 40791debfc3dSmrg 40801debfc3dSmrg@noindent 40811debfc3dSmrgNote that the comment is removed, but that the trailing space is 40821debfc3dSmrgpreserved. Here is an example of using a comment to effect token 40831debfc3dSmrgpasting. 40841debfc3dSmrg 40851debfc3dSmrg@smallexample 40861debfc3dSmrg#define suffix(x) foo_/**/x 40871debfc3dSmrgsuffix(bar) 40881debfc3dSmrg @expansion{} foo_bar 40891debfc3dSmrg@end smallexample 40901debfc3dSmrg 40911debfc3dSmrg@node Traditional miscellany 40921debfc3dSmrg@section Traditional miscellany 40931debfc3dSmrg 40941debfc3dSmrgHere are some things to be aware of when using the traditional 40951debfc3dSmrgpreprocessor. 40961debfc3dSmrg 40971debfc3dSmrg@itemize @bullet 40981debfc3dSmrg@item 40991debfc3dSmrgPreprocessing directives are recognized only when their leading 41001debfc3dSmrg@samp{#} appears in the first column. There can be no whitespace 41011debfc3dSmrgbetween the beginning of the line and the @samp{#}, but whitespace can 41021debfc3dSmrgfollow the @samp{#}. 41031debfc3dSmrg 41041debfc3dSmrg@item 41051debfc3dSmrgA true traditional C preprocessor does not recognize @samp{#error} or 41061debfc3dSmrg@samp{#pragma}, and may not recognize @samp{#elif}. CPP supports all 41071debfc3dSmrgthe directives in traditional mode that it supports in ISO mode, 41081debfc3dSmrgincluding extensions, with the exception that the effects of 41091debfc3dSmrg@samp{#pragma GCC poison} are undefined. 41101debfc3dSmrg 41111debfc3dSmrg@item 41121debfc3dSmrg__STDC__ is not defined. 41131debfc3dSmrg 41141debfc3dSmrg@item 41151debfc3dSmrgIf you use digraphs the behavior is undefined. 41161debfc3dSmrg 41171debfc3dSmrg@item 41181debfc3dSmrgIf a line that looks like a directive appears within macro arguments, 41191debfc3dSmrgthe behavior is undefined. 41201debfc3dSmrg 41211debfc3dSmrg@end itemize 41221debfc3dSmrg 41231debfc3dSmrg@node Traditional warnings 41241debfc3dSmrg@section Traditional warnings 41251debfc3dSmrgYou can request warnings about features that did not exist, or worked 41261debfc3dSmrgdifferently, in traditional C with the @option{-Wtraditional} option. 41271debfc3dSmrgGCC does not warn about features of ISO C which you must use when you 41281debfc3dSmrgare using a conforming compiler, such as the @samp{#} and @samp{##} 41291debfc3dSmrgoperators. 41301debfc3dSmrg 41311debfc3dSmrgPresently @option{-Wtraditional} warns about: 41321debfc3dSmrg 41331debfc3dSmrg@itemize @bullet 41341debfc3dSmrg@item 41351debfc3dSmrgMacro parameters that appear within string literals in the macro body. 41361debfc3dSmrgIn traditional C macro replacement takes place within string literals, 41371debfc3dSmrgbut does not in ISO C@. 41381debfc3dSmrg 41391debfc3dSmrg@item 41401debfc3dSmrgIn traditional C, some preprocessor directives did not exist. 41411debfc3dSmrgTraditional preprocessors would only consider a line to be a directive 41421debfc3dSmrgif the @samp{#} appeared in column 1 on the line. Therefore 41431debfc3dSmrg@option{-Wtraditional} warns about directives that traditional C 41441debfc3dSmrgunderstands but would ignore because the @samp{#} does not appear as the 41451debfc3dSmrgfirst character on the line. It also suggests you hide directives like 41461debfc3dSmrg@samp{#pragma} not understood by traditional C by indenting them. Some 41471debfc3dSmrgtraditional implementations would not recognize @samp{#elif}, so it 41481debfc3dSmrgsuggests avoiding it altogether. 41491debfc3dSmrg 41501debfc3dSmrg@item 41511debfc3dSmrgA function-like macro that appears without an argument list. In some 41521debfc3dSmrgtraditional preprocessors this was an error. In ISO C it merely means 41531debfc3dSmrgthat the macro is not expanded. 41541debfc3dSmrg 41551debfc3dSmrg@item 41561debfc3dSmrgThe unary plus operator. This did not exist in traditional C@. 41571debfc3dSmrg 41581debfc3dSmrg@item 41591debfc3dSmrgThe @samp{U} and @samp{LL} integer constant suffixes, which were not 41601debfc3dSmrgavailable in traditional C@. (Traditional C does support the @samp{L} 41611debfc3dSmrgsuffix for simple long integer constants.) You are not warned about 41621debfc3dSmrguses of these suffixes in macros defined in system headers. For 41631debfc3dSmrginstance, @code{UINT_MAX} may well be defined as @code{4294967295U}, but 41641debfc3dSmrgyou will not be warned if you use @code{UINT_MAX}. 41651debfc3dSmrg 41661debfc3dSmrgYou can usually avoid the warning, and the related warning about 41671debfc3dSmrgconstants which are so large that they are unsigned, by writing the 41681debfc3dSmrginteger constant in question in hexadecimal, with no U suffix. Take 41691debfc3dSmrgcare, though, because this gives the wrong result in exotic cases. 41701debfc3dSmrg@end itemize 41711debfc3dSmrg 41721debfc3dSmrg@node Implementation Details 41731debfc3dSmrg@chapter Implementation Details 41741debfc3dSmrg 41751debfc3dSmrgHere we document details of how the preprocessor's implementation 41761debfc3dSmrgaffects its user-visible behavior. You should try to avoid undue 41771debfc3dSmrgreliance on behavior described here, as it is possible that it will 41781debfc3dSmrgchange subtly in future implementations. 41791debfc3dSmrg 41801debfc3dSmrgAlso documented here are obsolete features still supported by CPP@. 41811debfc3dSmrg 41821debfc3dSmrg@menu 41831debfc3dSmrg* Implementation-defined behavior:: 41841debfc3dSmrg* Implementation limits:: 41851debfc3dSmrg* Obsolete Features:: 41861debfc3dSmrg@end menu 41871debfc3dSmrg 41881debfc3dSmrg@node Implementation-defined behavior 41891debfc3dSmrg@section Implementation-defined behavior 41901debfc3dSmrg@cindex implementation-defined behavior 41911debfc3dSmrg 41921debfc3dSmrgThis is how CPP behaves in all the cases which the C standard 41931debfc3dSmrgdescribes as @dfn{implementation-defined}. This term means that the 41941debfc3dSmrgimplementation is free to do what it likes, but must document its choice 41951debfc3dSmrgand stick to it. 41961debfc3dSmrg@c FIXME: Check the C++ standard for more implementation-defined stuff. 41971debfc3dSmrg 41981debfc3dSmrg@itemize @bullet 41991debfc3dSmrg@need 1000 42001debfc3dSmrg@item The mapping of physical source file multi-byte characters to the 42011debfc3dSmrgexecution character set. 42021debfc3dSmrg 42031debfc3dSmrgThe input character set can be specified using the 42041debfc3dSmrg@option{-finput-charset} option, while the execution character set may 42051debfc3dSmrgbe controlled using the @option{-fexec-charset} and 42061debfc3dSmrg@option{-fwide-exec-charset} options. 42071debfc3dSmrg 42081debfc3dSmrg@item Identifier characters. 42091debfc3dSmrg@anchor{Identifier characters} 42101debfc3dSmrg 42111debfc3dSmrgThe C and C++ standards allow identifiers to be composed of @samp{_} 42121debfc3dSmrgand the alphanumeric characters. C++ also allows universal character 42131debfc3dSmrgnames. C99 and later C standards permit both universal character 4214*8feb0f0bSmrgnames and implementation-defined characters. In both C and C++ modes, 4215*8feb0f0bSmrgGCC accepts in identifiers exactly those extended characters that 4216*8feb0f0bSmrgcorrespond to universal character names permitted by the chosen 4217*8feb0f0bSmrgstandard. 42181debfc3dSmrg 42191debfc3dSmrgGCC allows the @samp{$} character in identifiers as an extension for 42201debfc3dSmrgmost targets. This is true regardless of the @option{std=} switch, 42211debfc3dSmrgsince this extension cannot conflict with standards-conforming 42221debfc3dSmrgprograms. When preprocessing assembler, however, dollars are not 42231debfc3dSmrgidentifier characters by default. 42241debfc3dSmrg 42251debfc3dSmrgCurrently the targets that by default do not permit @samp{$} are AVR, 42261debfc3dSmrgIP2K, MMIX, MIPS Irix 3, ARM aout, and PowerPC targets for the AIX 42271debfc3dSmrgoperating system. 42281debfc3dSmrg 42291debfc3dSmrgYou can override the default with @option{-fdollars-in-identifiers} or 42301debfc3dSmrg@option{fno-dollars-in-identifiers}. @xref{fdollars-in-identifiers}. 42311debfc3dSmrg 42321debfc3dSmrg@item Non-empty sequences of whitespace characters. 42331debfc3dSmrg 42341debfc3dSmrgIn textual output, each whitespace sequence is collapsed to a single 42351debfc3dSmrgspace. For aesthetic reasons, the first token on each non-directive 42361debfc3dSmrgline of output is preceded with sufficient spaces that it appears in the 42371debfc3dSmrgsame column as it did in the original source file. 42381debfc3dSmrg 42391debfc3dSmrg@item The numeric value of character constants in preprocessor expressions. 42401debfc3dSmrg 42411debfc3dSmrgThe preprocessor and compiler interpret character constants in the 42421debfc3dSmrgsame way; i.e.@: escape sequences such as @samp{\a} are given the 42431debfc3dSmrgvalues they would have on the target machine. 42441debfc3dSmrg 42451debfc3dSmrgThe compiler evaluates a multi-character character constant a character 42461debfc3dSmrgat a time, shifting the previous value left by the number of bits per 42471debfc3dSmrgtarget character, and then or-ing in the bit-pattern of the new 42481debfc3dSmrgcharacter truncated to the width of a target character. The final 42491debfc3dSmrgbit-pattern is given type @code{int}, and is therefore signed, 42501debfc3dSmrgregardless of whether single characters are signed or not. 42511debfc3dSmrgIf there are more 42521debfc3dSmrgcharacters in the constant than would fit in the target @code{int} the 42531debfc3dSmrgcompiler issues a warning, and the excess leading characters are 42541debfc3dSmrgignored. 42551debfc3dSmrg 42561debfc3dSmrgFor example, @code{'ab'} for a target with an 8-bit @code{char} would be 42571debfc3dSmrginterpreted as @w{@samp{(int) ((unsigned char) 'a' * 256 + (unsigned char) 42581debfc3dSmrg'b')}}, and @code{'\234a'} as @w{@samp{(int) ((unsigned char) '\234' * 42591debfc3dSmrg256 + (unsigned char) 'a')}}. 42601debfc3dSmrg 42611debfc3dSmrg@item Source file inclusion. 42621debfc3dSmrg 42631debfc3dSmrgFor a discussion on how the preprocessor locates header files, 42641debfc3dSmrg@ref{Include Operation}. 42651debfc3dSmrg 42661debfc3dSmrg@item Interpretation of the filename resulting from a macro-expanded 42671debfc3dSmrg@samp{#include} directive. 42681debfc3dSmrg 42691debfc3dSmrg@xref{Computed Includes}. 42701debfc3dSmrg 42711debfc3dSmrg@item Treatment of a @samp{#pragma} directive that after macro-expansion 42721debfc3dSmrgresults in a standard pragma. 42731debfc3dSmrg 42741debfc3dSmrgNo macro expansion occurs on any @samp{#pragma} directive line, so the 42751debfc3dSmrgquestion does not arise. 42761debfc3dSmrg 42771debfc3dSmrgNote that GCC does not yet implement any of the standard 42781debfc3dSmrgpragmas. 42791debfc3dSmrg 42801debfc3dSmrg@end itemize 42811debfc3dSmrg 42821debfc3dSmrg@node Implementation limits 42831debfc3dSmrg@section Implementation limits 42841debfc3dSmrg@cindex implementation limits 42851debfc3dSmrg 42861debfc3dSmrgCPP has a small number of internal limits. This section lists the 42871debfc3dSmrglimits which the C standard requires to be no lower than some minimum, 42881debfc3dSmrgand all the others known. It is intended that there should be as few limits 42891debfc3dSmrgas possible. If you encounter an undocumented or inconvenient limit, 42901debfc3dSmrgplease report that as a bug. @xref{Bugs, , Reporting Bugs, gcc, Using 42911debfc3dSmrgthe GNU Compiler Collection (GCC)}. 42921debfc3dSmrg 42931debfc3dSmrgWhere we say something is limited @dfn{only by available memory}, that 42941debfc3dSmrgmeans that internal data structures impose no intrinsic limit, and space 42951debfc3dSmrgis allocated with @code{malloc} or equivalent. The actual limit will 42961debfc3dSmrgtherefore depend on many things, such as the size of other things 42971debfc3dSmrgallocated by the compiler at the same time, the amount of memory 42981debfc3dSmrgconsumed by other processes on the same computer, etc. 42991debfc3dSmrg 43001debfc3dSmrg@itemize @bullet 43011debfc3dSmrg 43021debfc3dSmrg@item Nesting levels of @samp{#include} files. 43031debfc3dSmrg 43041debfc3dSmrgWe impose an arbitrary limit of 200 levels, to avoid runaway recursion. 43051debfc3dSmrgThe standard requires at least 15 levels. 43061debfc3dSmrg 43071debfc3dSmrg@item Nesting levels of conditional inclusion. 43081debfc3dSmrg 43091debfc3dSmrgThe C standard mandates this be at least 63. CPP is limited only by 43101debfc3dSmrgavailable memory. 43111debfc3dSmrg 43121debfc3dSmrg@item Levels of parenthesized expressions within a full expression. 43131debfc3dSmrg 43141debfc3dSmrgThe C standard requires this to be at least 63. In preprocessor 43151debfc3dSmrgconditional expressions, it is limited only by available memory. 43161debfc3dSmrg 43171debfc3dSmrg@item Significant initial characters in an identifier or macro name. 43181debfc3dSmrg 43191debfc3dSmrgThe preprocessor treats all characters as significant. The C standard 43201debfc3dSmrgrequires only that the first 63 be significant. 43211debfc3dSmrg 43221debfc3dSmrg@item Number of macros simultaneously defined in a single translation unit. 43231debfc3dSmrg 43241debfc3dSmrgThe standard requires at least 4095 be possible. CPP is limited only 43251debfc3dSmrgby available memory. 43261debfc3dSmrg 43271debfc3dSmrg@item Number of parameters in a macro definition and arguments in a macro call. 43281debfc3dSmrg 43291debfc3dSmrgWe allow @code{USHRT_MAX}, which is no smaller than 65,535. The minimum 43301debfc3dSmrgrequired by the standard is 127. 43311debfc3dSmrg 43321debfc3dSmrg@item Number of characters on a logical source line. 43331debfc3dSmrg 43341debfc3dSmrgThe C standard requires a minimum of 4096 be permitted. CPP places 43351debfc3dSmrgno limits on this, but you may get incorrect column numbers reported in 43361debfc3dSmrgdiagnostics for lines longer than 65,535 characters. 43371debfc3dSmrg 43381debfc3dSmrg@item Maximum size of a source file. 43391debfc3dSmrg 43401debfc3dSmrgThe standard does not specify any lower limit on the maximum size of a 43411debfc3dSmrgsource file. GNU cpp maps files into memory, so it is limited by the 43421debfc3dSmrgavailable address space. This is generally at least two gigabytes. 43431debfc3dSmrgDepending on the operating system, the size of physical memory may or 43441debfc3dSmrgmay not be a limitation. 43451debfc3dSmrg 43461debfc3dSmrg@end itemize 43471debfc3dSmrg 43481debfc3dSmrg@node Obsolete Features 43491debfc3dSmrg@section Obsolete Features 43501debfc3dSmrg 43511debfc3dSmrgCPP has some features which are present mainly for compatibility with 43521debfc3dSmrgolder programs. We discourage their use in new code. In some cases, 43531debfc3dSmrgwe plan to remove the feature in a future version of GCC@. 43541debfc3dSmrg 43551debfc3dSmrg@subsection Assertions 43561debfc3dSmrg@cindex assertions 43571debfc3dSmrg 43581debfc3dSmrg@dfn{Assertions} are a deprecated alternative to macros in writing 43591debfc3dSmrgconditionals to test what sort of computer or system the compiled 43601debfc3dSmrgprogram will run on. Assertions are usually predefined, but you can 43611debfc3dSmrgdefine them with preprocessing directives or command-line options. 43621debfc3dSmrg 43631debfc3dSmrgAssertions were intended to provide a more systematic way to describe 43641debfc3dSmrgthe compiler's target system and we added them for compatibility with 43651debfc3dSmrgexisting compilers. In practice they are just as unpredictable as the 43661debfc3dSmrgsystem-specific predefined macros. In addition, they are not part of 43671debfc3dSmrgany standard, and only a few compilers support them. 43681debfc3dSmrgTherefore, the use of assertions is @strong{less} portable than the use 43691debfc3dSmrgof system-specific predefined macros. We recommend you do not use them at 43701debfc3dSmrgall. 43711debfc3dSmrg 43721debfc3dSmrg@cindex predicates 43731debfc3dSmrgAn assertion looks like this: 43741debfc3dSmrg 43751debfc3dSmrg@smallexample 43761debfc3dSmrg#@var{predicate} (@var{answer}) 43771debfc3dSmrg@end smallexample 43781debfc3dSmrg 43791debfc3dSmrg@noindent 43801debfc3dSmrg@var{predicate} must be a single identifier. @var{answer} can be any 43811debfc3dSmrgsequence of tokens; all characters are significant except for leading 43821debfc3dSmrgand trailing whitespace, and differences in internal whitespace 43831debfc3dSmrgsequences are ignored. (This is similar to the rules governing macro 43841debfc3dSmrgredefinition.) Thus, @code{(x + y)} is different from @code{(x+y)} but 43851debfc3dSmrgequivalent to @code{@w{( x + y )}}. Parentheses do not nest inside an 43861debfc3dSmrganswer. 43871debfc3dSmrg 43881debfc3dSmrg@cindex testing predicates 43891debfc3dSmrgTo test an assertion, you write it in an @samp{#if}. For example, this 43901debfc3dSmrgconditional succeeds if either @code{vax} or @code{ns16000} has been 43911debfc3dSmrgasserted as an answer for @code{machine}. 43921debfc3dSmrg 43931debfc3dSmrg@smallexample 43941debfc3dSmrg#if #machine (vax) || #machine (ns16000) 43951debfc3dSmrg@end smallexample 43961debfc3dSmrg 43971debfc3dSmrg@noindent 43981debfc3dSmrgYou can test whether @emph{any} answer is asserted for a predicate by 43991debfc3dSmrgomitting the answer in the conditional: 44001debfc3dSmrg 44011debfc3dSmrg@smallexample 44021debfc3dSmrg#if #machine 44031debfc3dSmrg@end smallexample 44041debfc3dSmrg 44051debfc3dSmrg@findex #assert 44061debfc3dSmrgAssertions are made with the @samp{#assert} directive. Its sole 44071debfc3dSmrgargument is the assertion to make, without the leading @samp{#} that 44081debfc3dSmrgidentifies assertions in conditionals. 44091debfc3dSmrg 44101debfc3dSmrg@smallexample 44111debfc3dSmrg#assert @var{predicate} (@var{answer}) 44121debfc3dSmrg@end smallexample 44131debfc3dSmrg 44141debfc3dSmrg@noindent 44151debfc3dSmrgYou may make several assertions with the same predicate and different 44161debfc3dSmrganswers. Subsequent assertions do not override previous ones for the 44171debfc3dSmrgsame predicate. All the answers for any given predicate are 44181debfc3dSmrgsimultaneously true. 44191debfc3dSmrg 44201debfc3dSmrg@cindex assertions, canceling 44211debfc3dSmrg@findex #unassert 44221debfc3dSmrgAssertions can be canceled with the @samp{#unassert} directive. It 44231debfc3dSmrghas the same syntax as @samp{#assert}. In that form it cancels only the 44241debfc3dSmrganswer which was specified on the @samp{#unassert} line; other answers 44251debfc3dSmrgfor that predicate remain true. You can cancel an entire predicate by 44261debfc3dSmrgleaving out the answer: 44271debfc3dSmrg 44281debfc3dSmrg@smallexample 44291debfc3dSmrg#unassert @var{predicate} 44301debfc3dSmrg@end smallexample 44311debfc3dSmrg 44321debfc3dSmrg@noindent 44331debfc3dSmrgIn either form, if no such assertion has been made, @samp{#unassert} has 44341debfc3dSmrgno effect. 44351debfc3dSmrg 44361debfc3dSmrgYou can also make or cancel assertions using command-line options. 44371debfc3dSmrg@xref{Invocation}. 44381debfc3dSmrg 44391debfc3dSmrg@node Invocation 44401debfc3dSmrg@chapter Invocation 44411debfc3dSmrg@cindex invocation 44421debfc3dSmrg@cindex command line 44431debfc3dSmrg 44441debfc3dSmrgMost often when you use the C preprocessor you do not have to invoke it 44451debfc3dSmrgexplicitly: the C compiler does so automatically. However, the 44461debfc3dSmrgpreprocessor is sometimes useful on its own. You can invoke the 44471debfc3dSmrgpreprocessor either with the @command{cpp} command, or via @command{gcc -E}. 44481debfc3dSmrgIn GCC, the preprocessor is actually integrated with the compiler 44491debfc3dSmrgrather than a separate program, and both of these commands invoke 44501debfc3dSmrgGCC and tell it to stop after the preprocessing phase. 44511debfc3dSmrg 44521debfc3dSmrgThe @command{cpp} options listed here are also accepted by 44531debfc3dSmrg@command{gcc} and have the same meaning. Likewise the @command{cpp} 44541debfc3dSmrgcommand accepts all the usual @command{gcc} driver options, although those 44551debfc3dSmrgpertaining to compilation phases after preprocessing are ignored. 44561debfc3dSmrg 44571debfc3dSmrgOnly options specific to preprocessing behavior are documented here. 44581debfc3dSmrgRefer to the GCC manual for full documentation of other driver options. 44591debfc3dSmrg 44601debfc3dSmrg@ignore 44611debfc3dSmrg@c man begin SYNOPSIS 44621debfc3dSmrgcpp [@option{-D}@var{macro}[=@var{defn}]@dots{}] [@option{-U}@var{macro}] 44631debfc3dSmrg [@option{-I}@var{dir}@dots{}] [@option{-iquote}@var{dir}@dots{}] 44641debfc3dSmrg [@option{-iremap}@var{src}:@var{dst}] 44651debfc3dSmrg [@option{-W}@var{warn}@dots{}] 44661debfc3dSmrg [@option{-M}|@option{-MM}] [@option{-MG}] [@option{-MF} @var{filename}] 44671debfc3dSmrg [@option{-MP}] [@option{-MQ} @var{target}@dots{}] 44681debfc3dSmrg [@option{-MT} @var{target}@dots{}] 44691debfc3dSmrg @var{infile} [[@option{-o}] @var{outfile}] 44701debfc3dSmrg 44711debfc3dSmrgOnly the most useful options are given above; see below for a more 44721debfc3dSmrgcomplete list of preprocessor-specific options. 44731debfc3dSmrgIn addition, @command{cpp} accepts most @command{gcc} driver options, which 44741debfc3dSmrgare not listed here. Refer to the GCC documentation for details. 44751debfc3dSmrg@c man end 44761debfc3dSmrg@c man begin SEEALSO 44771debfc3dSmrggpl(7), gfdl(7), fsf-funding(7), 44781debfc3dSmrggcc(1), and the Info entries for @file{cpp} and @file{gcc}. 44791debfc3dSmrg@c man end 44801debfc3dSmrg@end ignore 44811debfc3dSmrg 44821debfc3dSmrg@c man begin OPTIONS 44831debfc3dSmrgThe @command{cpp} command expects two file names as arguments, @var{infile} and 44841debfc3dSmrg@var{outfile}. The preprocessor reads @var{infile} together with any 44851debfc3dSmrgother files it specifies with @samp{#include}. All the output generated 44861debfc3dSmrgby the combined input files is written in @var{outfile}. 44871debfc3dSmrg 44881debfc3dSmrgEither @var{infile} or @var{outfile} may be @option{-}, which as 44891debfc3dSmrg@var{infile} means to read from standard input and as @var{outfile} 44901debfc3dSmrgmeans to write to standard output. If either file is omitted, it 44911debfc3dSmrgmeans the same as if @option{-} had been specified for that file. 44921debfc3dSmrgYou can also use the @option{-o @var{outfile}} option to specify the 44931debfc3dSmrgoutput file. 44941debfc3dSmrg 44951debfc3dSmrgUnless otherwise noted, or the option ends in @samp{=}, all options 44961debfc3dSmrgwhich take an argument may have that argument appear either immediately 44971debfc3dSmrgafter the option, or with a space between option and argument: 44981debfc3dSmrg@option{-Ifoo} and @option{-I foo} have the same effect. 44991debfc3dSmrg 45001debfc3dSmrg@cindex grouping options 45011debfc3dSmrg@cindex options, grouping 45021debfc3dSmrgMany options have multi-letter names; therefore multiple single-letter 45031debfc3dSmrgoptions may @emph{not} be grouped: @option{-dM} is very different from 45041debfc3dSmrg@w{@samp{-d -M}}. 45051debfc3dSmrg 45061debfc3dSmrg@cindex options 45071debfc3dSmrg 45081debfc3dSmrg@table @gcctabopt 45091debfc3dSmrg@include cppopts.texi 45101debfc3dSmrg@include cppdiropts.texi 45111debfc3dSmrg@include cppwarnopts.texi 45121debfc3dSmrg@end table 45131debfc3dSmrg@c man end 45141debfc3dSmrg 45151debfc3dSmrg@node Environment Variables 45161debfc3dSmrg@chapter Environment Variables 45171debfc3dSmrg@cindex environment variables 45181debfc3dSmrg@c man begin ENVIRONMENT 45191debfc3dSmrg 45201debfc3dSmrgThis section describes the environment variables that affect how CPP 45211debfc3dSmrgoperates. You can use them to specify directories or prefixes to use 45221debfc3dSmrgwhen searching for include files, or to control dependency output. 45231debfc3dSmrg 45241debfc3dSmrgNote that you can also specify places to search using options such as 45251debfc3dSmrg@option{-I}, and control dependency output with options like 45261debfc3dSmrg@option{-M} (@pxref{Invocation}). These take precedence over 45271debfc3dSmrgenvironment variables, which in turn take precedence over the 45281debfc3dSmrgconfiguration of GCC@. 45291debfc3dSmrg 45301debfc3dSmrg@include cppenv.texi 45311debfc3dSmrg@c man end 45321debfc3dSmrg 45331debfc3dSmrg@page 45341debfc3dSmrg@include fdl.texi 45351debfc3dSmrg 45361debfc3dSmrg@page 45371debfc3dSmrg@node Index of Directives 45381debfc3dSmrg@unnumbered Index of Directives 45391debfc3dSmrg@printindex fn 45401debfc3dSmrg 45411debfc3dSmrg@node Option Index 45421debfc3dSmrg@unnumbered Option Index 45431debfc3dSmrg@noindent 45441debfc3dSmrgCPP's command-line options and environment variables are indexed here 45451debfc3dSmrgwithout any initial @samp{-} or @samp{--}. 45461debfc3dSmrg@printindex op 45471debfc3dSmrg 45481debfc3dSmrg@page 45491debfc3dSmrg@node Concept Index 45501debfc3dSmrg@unnumbered Concept Index 45511debfc3dSmrg@printindex cp 45521debfc3dSmrg 45531debfc3dSmrg@bye 4554