123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523 |
- @c Copyright @copyright{} 2022 Richard Stallman and Free Software Foundation, Inc.
- This is part of the GNU C Intro and Reference Manual
- and covered by its license.
- @node Preprocessing
- @chapter Preprocessing
- @c man begin DESCRIPTION
- @cindex preprocessing
- As the first stage of compiling a C source module, GCC transforms the
- text with text substitutions and file inclusions. This is called
- @dfn{preprocessing}.
- @menu
- * Preproc Overview::
- * Directives::
- * Preprocessing Tokens::
- * Header Files::
- * Macros::
- * Conditionals::
- * Diagnostics::
- * Line Control::
- * Null Directive::
- @end menu
- @node Preproc Overview
- @section Preprocessing Overview
- GNU C performs preprocessing on each line of a C program as the first
- stage of compilation. Preprocessing operates on a line only when it
- contains a @dfn{preprocessing directive} or uses a @dfn{macro}---all
- other lines pass through preprocessing unchanged.
- Here are some jobs that preprocessing does. The rest of
- this chapter gives the details.
- @itemize @bullet
- @item
- Inclusion of header files. These are files (usually containing
- declarations and macro definitions) that can be substituted into your
- program.
- @item
- Macro expansion. You can define @dfn{macros}, which are abbreviations
- for arbitrary fragments of C code. Preprocessing replaces the macros
- with their definitions. Some macros are automatically predefined.
- @item
- Conditional compilation. You can include or exclude parts of the
- program according to various conditions.
- @item
- Line control. If you use a program to combine or rearrange source files
- into an intermediate file that is then compiled, you can use line
- control to inform the compiler where each source line originally came
- from.
- @item
- Compilation control. @code{#pragma} and @code{_Pragma} invoke
- some special compiler features in how to handle certain constructs.
- @item
- Diagnostics. You can detect problems at compile time and issue errors
- or warnings.
- @end itemize
- Except for expansion of predefined macros, all these operations happen
- only if you use preprocessing directives to request them.
- @node Directives
- @section Directives
- @cindex directives
- @cindex preprocessing directives
- @cindex directive line
- @cindex directive name
- @dfn{Preprocessing directives} are lines in the program that start
- with @samp{#}. Whitespace is allowed before and after the @samp{#}.
- The @samp{#} is followed by an identifier, the @dfn{directive name}.
- It specifies the operation to perform. Here are a couple of examples:
- @example
- #define LIMIT 51
- # undef LIMIT
- # error You screwed up!
- @end example
- We usually refer to a directive as @code{#@var{name}} where @var{name}
- is the directive name. For example, @code{#define} means the
- directive that defines a macro.
- The @samp{#} that begins a directive cannot come from a macro
- expansion. Also, the directive name is not macro expanded. Thus, if
- @code{foo} is defined as a macro expanding to @code{define}, that does
- not make @code{#foo} a valid preprocessing directive.
- The set of valid directive names is fixed. Programs cannot define new
- preprocessing directives.
- Some directives require arguments; these make up the rest of the
- directive line and must be separated from the directive name by
- whitespace. For example, @code{#define} must be followed by a macro
- name and the intended expansion of the macro.
- A preprocessing directive cannot cover more than one line. The line
- can, however, be continued with backslash-newline, or by a
- @samp{/*@r{@dots{}}*/}-style comment that extends past the end of the
- line. These will be replaced (by nothing, or by whitespace) before
- the directive is processed.
- @node Preprocessing Tokens
- @section Preprocessing Tokens
- @cindex preprocessing tokens
- Preprocessing divides C code (minus its comments) into
- @dfn{tokens} that are similar to C tokens, but not exactly the same.
- Here are the quirks of preprocessing tokens.
- The main classes of preprocessing tokens are identifiers,
- preprocessing numbers, string constants, character constants, and
- punctuators; there are a few others too.
- @table @asis
- @item identifier
- @cindex identifiers
- An @dfn{identifier} preprocessing token is syntactically like an
- identifier in C: any sequence of letters, digits, or underscores, as
- well as non-ASCII characters represented using @samp{\U} or @samp{\u},
- that doesn't begin with a digit.
- During preprocessing, the keywords of C have no special significance;
- at that stage, they are simply identifiers. Thus, you can define a
- macro whose name is a keyword. The only identifier that is special
- during preprocessing is @code{defined} (@pxref{defined}).
- @item preprocessing number
- @cindex numbers, preprocessing
- @cindex preprocessing numbers
- A @dfn{preprocessing number} is something that preprocessing treats
- textually as a number, including C numeric constants, and other
- sequences of characters which resemble numeric constants.
- Preprocessing does not try to verify that a preprocessing number is a
- valid number in C, and indeed it need not be one.
- More precisely, preprocessing numbers begin with an optional period, a
- required decimal digit, and then continue with any sequence of
- letters, digits, underscores, periods, and exponents. Exponents are
- the two-character sequences @samp{e+}, @samp{e-}, @samp{E+},
- @samp{E-}, @samp{p+}, @samp{p-}, @samp{P+}, and @samp{P-}. (The
- exponents that begin with @samp{p} or @samp{P} are new to C99. They
- are used for hexadecimal floating-point constants.)
- The reason behind this unusual syntactic class is that the full
- complexity of numeric constants is irrelevant during preprocessing.
- The distinction between lexically valid and invalid floating-point
- numbers, for example, doesn't matter at this stage. The use of
- preprocessing numbers makes it possible to split an identifier at any
- position and get exactly two tokens, and reliably paste them together
- using the @code{##} operator (@pxref{Concatenation}).
- @item punctuator
- A @dfn{punctuator} is syntactically like an operator.
- These are the valid punctuators:
- @example
- [ ] ( ) @{ @} . ->
- ++ -- & * + - ~ !
- / % << >> < > <= >= == != ^ | && ||
- ? : ; ...
- = *= /= %= += -= <<= >>= &= ^= |=
- , # ##
- <: :> <% %> %: %:%:
- @end example
- @item string constant
- A string constant in the source code is recognized by preprocessing as
- a single preprocessing token.
- @item character constant
- A character constant in the source code is recognized by preprocessing
- as a single preprocessing token.
- @item header name
- Within the @code{#include} directive, preprocessing recognizes a
- @dfn{header name} token. It consists of @samp{"@var{name}"}, where
- @var{name} is a sequence of source characters other than newline and
- @samp{"}, or @samp{<@var{name}>}, where @var{name} is a sequence of
- source characters other than newline and @samp{>}.
- In practice, it is more convenient to think that the @code{#include} line
- is exempt from tokenization.
- @item other
- Any other character that's valid in a C source program
- is treated as a separate preprocessing token.
- @end table
- Once the program is broken into preprocessing tokens, they remain
- separate until the end of preprocessing. Macros that generate two
- consecutive tokens insert whitespace to keep them separate, if
- necessary. For example,
- @example
- @group
- #define foo() bar
- foo()baz
- @expansion{} bar baz
- @emph{not}
- @expansion{} barbaz
- @end group
- @end example
- The only exception is with the @code{##} preprocessing operator, which
- pastes tokens together (@pxref{Concatenation}).
- Preprocessing treats the null character (code 0) as whitespace, but
- generates a warning for it because it may be invisible to the user
- (many terminals do not display it at all) and its presence in the file
- is probably a mistake.
- @node Header Files
- @section Header Files
- @cindex header file
- A header file is a file of C code, typically containing C declarations
- and macro definitions (@pxref{Macros}), to be shared between several
- source files. You request the use of a header file in your program by
- @dfn{including} it, with the C preprocessing directive
- @code{#include}.
- Header files serve two purposes.
- @itemize @bullet
- @item
- @cindex system header files
- System header files declare the interfaces to parts of the operating
- system. You include them in your program to supply the definitions and
- declarations that you need to invoke system calls and libraries.
- @item
- Program-specific header files contain declarations for interfaces between the
- source files of a particular program. It is a good idea to create a header
- file for related declarations and macro definitions if all or most of them
- are needed in several different source files.
- @end itemize
- Including a header file produces the same results as copying the header
- file into each source file that needs it. Such copying would be
- time-consuming and error-prone. With a header file, the related
- declarations appear in only one place. If they need to be changed, you
- can change them in one place, and programs that include the header file
- will then automatically use the new version when next recompiled. The header
- file eliminates the labor of finding and changing all the copies as well
- as the risk that a failure to change one copy will result in
- inconsistencies within a program.
- In C, the usual convention is to give header files names that end with
- @file{.h}. It is most portable to use only letters, digits, dashes, and
- underscores in header file names, and at most one dot.
- The operation of including another source file isn't actually limited
- to the sort of code we put into header files. You can put any sort of
- C code into a separate file, then use @code{#include} to copy it
- virtually into other C source files. But that is a strange thing to
- do.
- @menu
- * include Syntax::
- * include Operation::
- * Search Path::
- * Once-Only Headers::
- @c * Alternatives to Wrapper #ifndef::
- * Computed Includes::
- @c * Wrapper Headers::
- @c * System Headers::
- @end menu
- @node include Syntax
- @subsection @code{#include} Syntax
- @findex #include
- You can specify inclusion of user and system header files with the
- preprocessing directive @code{#include}. It has two variants:
- @table @code
- @item #include <@var{file}>
- This variant is used for system header files. It searches for a file
- named @var{file} in a standard list of system directories. You can
- prepend directories to this list with the @option{-I} option
- (@pxref{Invocation, Invoking GCC, Invoking GCC, gcc, Using the GNU
- Compiler Collection}).
- @item #include "@var{file}"
- This variant is used for header files of your own program. It
- searches for a file named @var{file} first in the directory containing
- the current file, then in the quote directories, then the same
- directories used for @code{<@var{file}>}. You can prepend directories
- to the list of quote directories with the @option{-iquote} option.
- @end table
- The argument of @code{#include}, whether delimited with quote marks or
- angle brackets, behaves like a string constant in that comments are not
- recognized, and macro names are not expanded. Thus, @code{@w{#include
- <x/*y>}} specifies inclusion of a system header file named @file{x/*y}.
- However, if backslashes occur within @var{file}, they are considered
- ordinary text characters, not escape characters: character escape
- sequences such as used in string constants in C are not meaningful
- here. Thus, @code{@w{#include "x\n\\y"}} specifies a filename
- containing three backslashes. By the same token, there is no way to
- escape @samp{"} or @samp{>} to include it in the header file name if
- it would instead end the file name.
- Some systems interpret @samp{\} as a file name component separator.
- All these systems also interpret @samp{/} the same way. It is most
- portable to use only @samp{/}.
- It is an error to put anything other than comments on the
- @code{#include} line after the file name.
- @node include Operation
- @subsection @code{#include} Operation
- The @code{#include} directive works by scanning the specified header
- file as input before continuing with the rest of the current file.
- The result of preprocessing consists of the text already generated,
- followed by the result of preprocessing the included file, followed by
- whatever results from the text after the @code{#include} directive.
- For example, if you have a header file @file{header.h} as follows,
- @example
- char *test (void);
- @end example
- @noindent
- and a main program called @file{program.c} that uses the header file,
- like this,
- @example
- int x;
- #include "header.h"
- int
- main (void)
- @{
- puts (test ());
- @}
- @end example
- @noindent
- the result is equivalent to putting this text in @file{program.c}:
- @example
- int x;
- char *test (void);
- int
- main (void)
- @{
- puts (test ());
- @}
- @end example
- Included files are not limited to declarations and macro definitions;
- those are merely the typical uses. Any fragment of a C program can be
- included from another file. The include file could even contain the
- beginning of a statement that is concluded in the containing file, or
- the end of a statement that was started in the including file. However,
- an included file must consist of complete tokens. Comments and string
- literals that have not been closed by the end of an included file are
- invalid. For error recovery, the compiler terminates them at the end of
- the file.
- To avoid confusion, it is best if header files contain only complete
- syntactic units---function declarations or definitions, type
- declarations, etc.
- The line following the @code{#include} directive is always treated as
- a separate line, even if the included file lacks a final newline.
- There is no problem putting a preprocessing directive there.
- @node Search Path
- @subsection Search Path
- GCC looks in several different places for header files to be included.
- On the GNU system, and Unix systems, the default directories for
- system header files are:
- @example
- @var{libdir}/gcc/@var{target}/@var{version}/include
- /usr/local/include
- @var{libdir}/gcc/@var{target}/@var{version}/include-fixed
- @var{libdir}/@var{target}/include
- /usr/include/@var{target}
- /usr/include
- @end example
- @noindent
- The list may be different in some operating systems. Other
- directories are added for C++.
- In the above, @var{target} is the canonical name of the system GCC was
- configured to compile code for; often but not always the same as the
- canonical name of the system it runs on. @var{version} is the version
- of GCC in use.
- You can add to this list with the @option{-I@var{dir}} command-line
- option. All the directories named by @option{-I} are searched, in
- left-to-right order, @emph{before} the default directories. The only
- exception is when @file{dir} is already searched by default. In
- this case, the option is ignored and the search order for system
- directories remains unchanged.
- Duplicate directories are removed from the quote and bracket search
- chains before the two chains are merged to make the final search chain.
- Thus, it is possible for a directory to occur twice in the final search
- chain if it was specified in both the quote and bracket chains.
- You can prevent GCC from searching any of the default directories with
- the @option{-nostdinc} option. This is useful when you are compiling an
- operating system kernel or some other program that does not use the
- standard C library facilities, or the standard C library itself.
- @option{-I} options are not ignored as described above when
- @option{-nostdinc} is in effect.
- GCC looks for headers requested with @code{@w{#include "@var{file}"}}
- first in the directory containing the current file, then in the
- @dfn{quote directories} specified by @option{-iquote} options, then in
- the same places it looks for a system header. For example, if
- @file{/usr/include/sys/stat.h} contains @code{@w{#include "types.h"}},
- GCC looks for @file{types.h} first in @file{/usr/include/sys}, then in
- the quote directories and then in its usual search path.
- @code{#line} (@pxref{Line Control}) does not change GCC's idea of the
- directory containing the current file.
- @cindex quote directories
- The @option{-I-} is an old-fashioned, deprecated way to specify the
- quote directories. To look for headers in a directory named @file{-},
- specify @option{-I./-}. There are several more ways to adjust the
- header search path. @xref{invocation, Invoking GCC, Invoking GCC,
- gcc, Using the GNU Compiler Collection}.
- @node Once-Only Headers
- @subsection Once-Only Headers
- @cindex repeated inclusion
- @cindex including just once
- @cindex wrapper @code{#ifndef}
- If a header file happens to be included twice, the compiler will process
- its contents twice. This is very likely to cause an error, e.g.@: when the
- compiler sees the same structure definition twice.
- The standard way to prevent this is to enclose the entire real contents
- of the file in a conditional, like this:
- @example
- @group
- /* File foo. */
- #ifndef FILE_FOO_SEEN
- #define FILE_FOO_SEEN
- @var{the entire file}
- #endif /* !FILE_FOO_SEEN */
- @end group
- @end example
- This construct is commonly known as a @dfn{wrapper #ifndef}. When the
- header is included again, the conditional will be false, because
- @code{FILE_FOO_SEEN} is defined. Preprocessing skips over the entire
- contents of the file, so that compilation will never ``see'' the file
- contents twice in one module.
- GCC optimizes this case even further. It remembers when a header file
- has a wrapper @code{#ifndef}. If a subsequent @code{#include}
- specifies that header, and the macro in the @code{#ifndef} is still
- defined, it does not bother to rescan the file at all.
- You can put comments in the header file outside the wrapper. They
- do not interfere with this optimization.
- @cindex controlling macro
- @cindex guard macro
- The macro @code{FILE_FOO_SEEN} is called the @dfn{controlling macro}
- or @dfn{guard macro}. In a user header file, the macro name should
- not begin with @samp{_}. In a system header file, it should begin
- with @samp{__} (or @samp{_} followed by an upper-case letter) to avoid
- conflicts with user programs. In any kind of header file, the macro
- name should contain the name of the file and some additional text, to
- avoid conflicts with other header files.
- @node Computed Includes
- @subsection Computed Includes
- @cindex computed includes
- @cindex macros in include
- Sometimes it is necessary to select one of several different header
- files to be included into your program. They might specify
- configuration parameters to be used on different sorts of operating
- systems, for instance. You could do this with a series of conditionals,
- @example
- #if SYSTEM_1
- # include "system_1.h"
- #elif SYSTEM_2
- # include "system_2.h"
- #elif SYSTEM_3
- /* @r{@dots{}} */
- #endif
- @end example
- That rapidly becomes tedious. Instead, GNU C offers the ability to use
- a macro for the header name. This is called a @dfn{computed include}.
- Instead of writing a header name as the direct argument of
- @code{#include}, you simply put a macro name there instead:
- @example
- #define SYSTEM_H "system_1.h"
- /* @r{@dots{}} */
- #include SYSTEM_H
- @end example
- @noindent
- @code{SYSTEM_H} is expanded, then @file{system_1.h} is included as if
- the @code{#include} had been written with that name. @code{SYSTEM_H}
- could be defined by your Makefile with a @option{-D} option.
- You must be careful when you define such a macro. @code{#define}
- saves tokens, not text. GCC has no way of knowing that the macro will
- be used as the argument of @code{#include}, so it generates ordinary
- tokens, not a header name. This is unlikely to cause problems if you
- use double-quote includes, which are syntactically similar to string
- constants. If you use angle brackets, however, you may have trouble.
- The syntax of a computed include is actually a bit more general than the
- above. If the first non-whitespace character after @code{#include} is
- not @samp{"} or @samp{<}, then the entire line is macro-expanded
- like running text would be.
- If the line expands to a single string constant, the contents of that
- string constant are the file to be included. Preprocessing does not
- re-examine the string for embedded quotes, but neither does it process
- backslash escapes in the string. Therefore
- @example
- #define HEADER "a\"b"
- #include HEADER
- @end example
- @noindent
- looks for a file named @file{a\"b}. Preprocessing searches for the
- file according to the rules for double-quoted includes.
- If the line expands to a token stream beginning with a @samp{<} token
- and including a @samp{>} token, then the tokens between the @samp{<} and
- the first @samp{>} are combined to form the filename to be included.
- Any whitespace between tokens is reduced to a single space; then any
- space after the initial @samp{<} is retained, but a trailing space
- before the closing @samp{>} is ignored. Preprocessing searches for the file
- according to the rules for angle-bracket includes.
- In either case, if there are any tokens on the line after the file name,
- an error occurs and the directive is not processed. It is also an error
- if the result of expansion does not match either of the two expected
- forms.
- These rules are implementation-defined behavior according to the C
- standard. To minimize the risk of different compilers interpreting your
- computed includes differently, we recommend you use only a single
- object-like macro that expands to a string constant. That also
- makes it clear to people reading your program.
- @node Macros
- @section Macros
- @cindex macros
- A @dfn{macro} is a fragment of code that has been given a name.
- Whenever the name is used, it is replaced by the contents of the macro.
- There are two kinds of macros. They differ mostly in what they look
- like when they are used. @dfn{Object-like} macros resemble data objects
- when used, @dfn{function-like} macros resemble function calls.
- You may define any valid identifier as a macro, even if it is a C
- keyword. In the preprocessing stage, GCC does not know anything about
- keywords. This can be useful if you wish to hide a keyword such as
- @code{const} from an older compiler that does not understand it.
- However, the preprocessing operator @code{defined} (@pxref{defined})
- can never be defined as a macro, and C@code{++}'s named operators
- (@pxref{C++ Named Operators, C++ Named Operators, C++ Named Operators,
- gcc, Using the GNU Compiler Collection}) cannot be macros when
- compiling C@code{++} code.
- The operator @code{#} is used in macros for stringification of an
- argument (@pxref{Stringification}), and @code{##} is used for
- concatenation of arguments into larger tokens (@pxref{Concatenation})
- @menu
- * Object-like Macros::
- * Function-like Macros::
- @c * Macro Pragmas::
- * Macro Arguments::
- * Stringification::
- * Concatenation::
- * Variadic Macros::
- * Predefined Macros::
- * Undefining and Redefining Macros::
- * Directives Within Macro Arguments::
- * Macro Pitfalls::
- @end menu
- @node Object-like Macros
- @subsection Object-like Macros
- @cindex object-like macro
- @cindex symbolic constants
- @cindex manifest constants
- An @dfn{object-like macro} is a simple identifier that will be
- replaced by a code fragment. It is called object-like because in most
- cases the use of the macro looks like reference to a data object in
- code that uses it. These macros are most commonly used to give
- symbolic names to numeric constants.
- @findex #define
- The way to define macros with the @code{#define} directive.
- @code{#define} is followed by the name of the macro and then the token
- sequence it should be an abbreviation for, which is variously referred
- to as the macro's @dfn{body}, @dfn{expansion} or @dfn{replacement
- list}. For example,
- @example
- #define BUFFER_SIZE 1024
- @end example
- @noindent
- defines a macro named @code{BUFFER_SIZE} as an abbreviation for the
- token @code{1024}. If somewhere after this @code{#define} directive
- there comes a C statement of the form
- @example
- foo = (char *) malloc (BUFFER_SIZE);
- @end example
- @noindent
- then preprocessing will recognize and @dfn{expand} the macro
- @code{BUFFER_SIZE}, so that compilation will see the tokens:
- @example
- foo = (char *) malloc (1024);
- @end example
- By convention, macro names are written in upper case. Programs are
- easier to read when it is possible to tell at a glance which names are
- macros. Macro names that start with @samp{__} are reserved for
- internal uses, and many of them are defined automatically, so don't
- define such macro names unless you really know what you're doing.
- Likewise for macro names that start with @samp{_} and an upper-case letter.
- The macro's body ends at the end of the @code{#define} line. You may
- continue the definition onto multiple lines, if necessary, using
- backslash-newline. When the macro is expanded, however, it will all
- come out on one line. For example,
- @example
- #define NUMBERS 1, \
- 2, \
- 3
- int x[] = @{ NUMBERS @};
- @expansion{} int x[] = @{ 1, 2, 3 @};
- @end example
- @noindent
- The most common visible consequence of this is surprising line numbers
- in error messages.
- There is no restriction on what can go in a macro body provided it
- decomposes into valid preprocessing tokens. Parentheses need not
- balance, and the body need not resemble valid C code. (If it does not,
- you may get error messages from the C compiler when you use the macro.)
- Preprocessing scans the program sequentially. A macro definition
- takes effect right after its appearance. Therefore, the following
- input
- @example
- foo = X;
- #define X 4
- bar = X;
- @end example
- @noindent
- produces
- @example
- foo = X;
- bar = 4;
- @end example
- When preprocessing expands a macro name, the macro's expansion
- replaces the macro invocation, then the expansion is examined for more
- macros to expand. For example,
- @example
- @group
- #define TABLESIZE BUFSIZE
- #define BUFSIZE 1024
- TABLESIZE
- @expansion{} BUFSIZE
- @expansion{} 1024
- @end group
- @end example
- @noindent
- @code{TABLESIZE} is expanded first to produce @code{BUFSIZE}, then that
- macro is expanded to produce the final result, @code{1024}.
- Notice that @code{BUFSIZE} was not defined when @code{TABLESIZE} was
- defined. The @code{#define} for @code{TABLESIZE} uses exactly the
- expansion you specify---in this case, @code{BUFSIZE}---and does not
- check to see whether it too contains macro names. Only when you
- @emph{use} @code{TABLESIZE} is the result of its expansion scanned for
- more macro names.
- This makes a difference if you change the definition of @code{BUFSIZE}
- at some point in the source file. @code{TABLESIZE}, defined as shown,
- will always expand using the definition of @code{BUFSIZE} that is
- currently in effect:
- @example
- #define BUFSIZE 1020
- #define TABLESIZE BUFSIZE
- #undef BUFSIZE
- #define BUFSIZE 37
- @end example
- @noindent
- Now @code{TABLESIZE} expands (in two stages) to @code{37}.
- If the expansion of a macro contains its own name, either directly or
- via intermediate macros, it is not expanded again when the expansion is
- examined for more macros. This prevents infinite recursion.
- @xref{Self-Referential Macros}, for the precise details.
- @node Function-like Macros
- @subsection Function-like Macros
- @cindex function-like macros
- You can also define macros whose use looks like a function call.
- These are called @dfn{function-like macros}. To define one, use the
- @code{#define} directive with a pair of parentheses immediately after
- the macro name. For example,
- @example
- #define lang_init() c_init ()
- lang_init ()
- @expansion{} c_init ()
- lang_init ()
- @expansion{} c_init ()
- lang_init()
- @expansion{} c_init ()
- @end example
- There must be no space between the macro name and the following
- open-parenthesis in the the @code{#define} directive; that's what
- indicates you're defining a function-like macro. However, you can add
- unnecessary whitespace around the open-parenthesis (and around the
- close-parenthesis) when you @emph{call} the macro; they don't change
- anything.
- A function-like macro is expanded only when its name appears with a
- pair of parentheses after it. If you write just the name, without
- parentheses, it is left alone. This can be useful when you have a
- function and a macro of the same name, and you wish to use the
- function sometimes. Whitespace and line breaks before or between the
- parentheses are ignored when the macro is called.
- @example
- extern void foo(void);
- #define foo() /* @r{optimized inline version} */
- /* @r{@dots{}} */
- foo();
- funcptr = foo;
- @end example
- Here the call to @code{foo()} expands the macro, but the function
- pointer @code{funcptr} gets the address of the real function
- @code{foo}. If the macro were to be expanded there, it would cause a
- syntax error.
- If you put spaces between the macro name and the parentheses in the
- macro definition, that does not define a function-like macro, it defines
- an object-like macro whose expansion happens to begin with a pair of
- parentheses. Here is an example:
- @example
- #define lang_init () c_init()
- lang_init()
- @expansion{} () c_init()()
- @end example
- The first two pairs of parentheses in this expansion come from the
- macro. The third is the pair that was originally after the macro
- invocation. Since @code{lang_init} is an object-like macro, it does not
- consume those parentheses.
- Any name can have at most one macro definition at a time. Thus,
- you can't define the same name as an object-like macro and a
- function-like macro at once.
- @node Macro Arguments
- @subsection Macro Arguments
- @cindex arguments
- @cindex macros with arguments
- @cindex arguments in macro definitions
- Function-like macros can take @dfn{arguments}, just like true functions.
- To define a macro that uses arguments, you insert @dfn{parameters}
- between the pair of parentheses in the macro definition that make the
- macro function-like. The parameters must be valid C identifiers,
- separated by commas and optionally whitespace.
- To invoke a macro that takes arguments, you write the name of the macro
- followed by a list of @dfn{actual arguments} in parentheses, separated
- by commas. The invocation of the macro need not be restricted to a
- single logical line---it can cross as many lines in the source file as
- you wish. The number of arguments you give must match the number of
- parameters in the macro definition. When the macro is expanded, each
- use of a parameter in its body is replaced by the tokens of the
- corresponding argument. (The macro body is not required to use all of the
- parameters.)
- As an example, here is a macro that computes the minimum of two numeric
- values, as it is defined in many C programs, and some uses.
- @example
- #define min(X, Y) ((X) < (Y) ? (X) : (Y))
- x = min(a, b); @expansion{} x = ((a) < (b) ? (a) : (b));
- y = min(1, 2); @expansion{} y = ((1) < (2) ? (1) : (2));
- z = min(a+28, *p); @expansion{} z = ((a+28) < (*p) ? (a+28) : (*p));
- @end example
- @noindent
- In this small example you can already see several of the dangers of
- macro arguments. @xref{Macro Pitfalls}, for detailed explanations.
- Leading and trailing whitespace in each argument is dropped, and all
- whitespace between the tokens of an argument is reduced to a single
- space. Parentheses within each argument must balance; a comma within
- such parentheses does not end the argument. However, there is no
- requirement for square brackets or braces to balance, and they do not
- prevent a comma from separating arguments. Thus,
- @example
- macro (array[x = y, x + 1])
- @end example
- @noindent
- passes two arguments to @code{macro}: @code{array[x = y} and @code{x +
- 1]}. If you want to supply @code{array[x = y, x + 1]} as an argument,
- you can write it as @code{array[(x = y, x + 1)]}, which is equivalent C
- code. However, putting an assignment inside an array subscript
- is to be avoided anyway.
- All arguments to a macro are completely macro-expanded before they are
- substituted into the macro body. After substitution, the complete text
- is scanned again for macros to expand, including the arguments. This rule
- may seem strange, but it is carefully designed so you need not worry
- about whether any function call is actually a macro invocation. You can
- run into trouble if you try to be too clever, though. @xref{Argument
- Prescan}, for detailed discussion.
- For example, @code{min (min (a, b), c)} is first expanded to
- @example
- min (((a) < (b) ? (a) : (b)), (c))
- @end example
- @noindent
- and then to
- @example
- @group
- ((((a) < (b) ? (a) : (b))) < (c)
- ? (((a) < (b) ? (a) : (b)))
- : (c))
- @end group
- @end example
- @noindent
- (The line breaks shown here for clarity are not actually generated.)
- @cindex empty macro arguments
- You can leave macro arguments empty without error, but many macros
- will then expand to invalid code. You cannot leave out arguments
- entirely; if a macro takes two arguments, there must be exactly one
- comma at the top level of its argument list. Here are some silly
- examples using @code{min}:
- @smallexample
- min(, b) @expansion{} (( ) < (b) ? ( ) : (b))
- min(a, ) @expansion{} ((a ) < ( ) ? (a ) : ( ))
- min(,) @expansion{} (( ) < ( ) ? ( ) : ( ))
- min((,),) @expansion{} (((,)) < ( ) ? ((,)) : ( ))
- min() @error{} macro "min" requires 2 arguments, but only 1 given
- min(,,) @error{} macro "min" passed 3 arguments, but takes just 2
- @end smallexample
- Whitespace is not a preprocessing token, so if a macro @code{foo} takes
- one argument, @code{@w{foo ()}} and @code{@w{foo ( )}} both supply it an
- empty argument.
- @ignore @c How long ago was this?
- Previous GNU preprocessor implementations and
- documentation were incorrect on this point, insisting that a
- function-like macro that takes a single argument be passed a space if an
- empty argument was required.
- @end ignore
- Macro parameters appearing inside string literals are not replaced by
- their corresponding actual arguments.
- @example
- #define foo(x) x, "x"
- foo(bar) @expansion{} bar, "x"
- @end example
- @noindent
- See the next subsection for how to insert macro arguments
- into a string literal.
- The token following the macro call and the last token of the macro
- expansion do not become one token even if it looks like they could:
- @example
- #define foo() abc
- foo()def @expansion{} abc def
- @end example
- @node Stringification
- @subsection Stringification
- @cindex stringification
- @cindex @code{#} operator
- Sometimes you may want to convert a macro argument into a string
- constant. Parameters are not replaced inside string constants, but
- you can use the @code{#} preprocessing operator instead. When a macro
- parameter is used with a leading @code{#}, preprocessing replaces it
- with the literal text of the actual argument, converted to a string
- constant. Unlike normal parameter replacement, the argument is not
- macro-expanded first. This is called @dfn{stringification}.
- There is no way to combine an argument with surrounding text and
- stringify it all together. But you can write a series of string
- constants and stringified arguments. After preprocessing replaces the
- stringified arguments with string constants, the consecutive string
- constants will be concatenated into one long string constant
- (@pxref{String Constants}).
- Here is an example that uses stringification and concatenation of
- string constants:
- @example
- @group
- #define WARN_IF(EXP) \
- do @{ if (EXP) \
- fprintf (stderr, "Warning: " #EXP "\n"); @} \
- while (0)
- WARN_IF (x == 0);
- @expansion{}
- do @{ if (x == 0)
- fprintf (stderr, "Warning: " "x == 0" "\n"); @}
- while (0);
- @end group
- @end example
- @noindent
- The argument for @code{EXP} is substituted once, as is, into the
- @code{if} statement, and once, stringified, into the argument to
- @code{fprintf}. If @code{x} were a macro, it would be expanded in the
- @code{if} statement but not in the string.
- The @code{do} and @code{while (0)} are a kludge to make it possible to
- write @code{WARN_IF (@var{arg});}. The resemblance of @code{WARN_IF}
- to a function makes that a natural way to write it.
- @xref{Swallowing the Semicolon}.
- Stringification in C involves more than putting double-quote
- characters around the fragment. It also backslash-escapes the quotes
- surrounding embedded string constants, and all backslashes within
- string and character constants, in order to get a valid C string
- constant with the proper contents. Thus, stringifying @code{@w{p =
- "foo\n";}} results in @t{@w{"p = \"foo\\n\";"}}. However, backslashes
- that are not inside string or character constants are not duplicated:
- @samp{\n} by itself stringifies to @t{"\n"}.
- All leading and trailing whitespace in text being stringified is
- ignored. Any sequence of whitespace in the middle of the text is
- converted to a single space in the stringified result. Comments are
- replaced by whitespace long before stringification happens, so they
- never appear in stringified text.
- There is no way to convert a macro argument into a character constant.
- To stringify the result of expansion of a macro argument, you have to
- use two levels of macros, like this:
- @example
- #define xstr(S) str(S)
- #define str(s) #s
- #define foo 4
- str (foo)
- @expansion{} "foo"
- xstr (foo)
- @expansion{} xstr (4)
- @expansion{} str (4)
- @expansion{} "4"
- @end example
- @code{s} is stringified when it is used in @code{str}, so it is not
- macro-expanded first. But @code{S} is an ordinary argument to
- @code{xstr}, so it is completely macro-expanded before @code{xstr}
- itself is expanded (@pxref{Argument Prescan}). Therefore, by the time
- @code{str} gets to its argument text, that text already been
- macro-expanded.
- @node Concatenation
- @subsection Concatenation
- @cindex concatenation
- @cindex token pasting
- @cindex token concatenation
- @cindex @code{##} operator
- It is often useful to merge two tokens into one while expanding macros.
- This is called @dfn{token pasting} or @dfn{token concatenation}. The
- @code{##} preprocessing operator performs token pasting. When a macro
- is expanded, the two tokens on either side of each @code{##} operator
- are combined into a single token, which then replaces the @code{##} and
- the two original tokens in the macro expansion. Usually both will be
- identifiers, or one will be an identifier and the other a preprocessing
- number. When pasted, they make a longer identifier.
- Concatenation into an identifier isn't the only valid case. It is
- also possible to concatenate two numbers (or a number and a name, such
- as @code{1.5} and @code{e3}) into a number. Also, multi-character
- operators such as @code{+=} can be formed by token pasting.
- However, two tokens that don't together form a valid token cannot be
- pasted together. For example, you cannot concatenate @code{x} with
- @code{+}, not in either order. Trying this issues a warning and keeps
- the two tokens separate. Whether it puts white space between the
- tokens is undefined. It is common to find unnecessary uses of
- @code{##} in complex macros. If you get this warning, it is likely
- that you can simply remove the @code{##}.
- The tokens combined by @code{##} could both come from the macro body,
- but then you could just as well write them as one token in the first place.
- Token pasting is useful when one or both of the tokens comes from a
- macro argument. If either of the tokens next to an @code{##} is a
- parameter name, it is replaced by its actual argument before @code{##}
- executes. As with stringification, the actual argument is not
- macro-expanded first. If the argument is empty, that @code{##} has no
- effect.
- Keep in mind that preprocessing converts comments to whitespace before
- it looks for uses of macros. Therefore, you cannot create a comment
- by concatenating @samp{/} and @samp{*}. You can put as much
- whitespace between @code{##} and its operands as you like, including
- comments, and you can put comments in arguments that will be
- concatenated.
- It is an error to use @code{##} at the beginning or end of a macro
- body.
- Multiple @code{##} operators are handled left-to-right, so that
- @samp{1 ## e ## -2} pastes into @samp{1e-2}. (Right-to-left
- processing would first generate @samp{e-2}, which is an invalid token.)
- When @code{#} and @code{##} are used together, they are all handled
- left-to-right.
- Consider a C program that interprets named commands. There probably
- needs to be a table of commands, perhaps an array of structures declared
- as follows:
- @example
- @group
- struct command
- @{
- char *name;
- void (*function) (void);
- @};
- @end group
- @group
- struct command commands[] =
- @{
- @{ "quit", quit_command @},
- @{ "help", help_command @},
- /* @r{@dots{}} */
- @};
- @end group
- @end example
- It would be cleaner not to have to write each command name twice, once
- in the string constant and once in the function name. A macro that
- takes the name of a command as an argument can make this unnecessary.
- It can create the string constant with stringification, and the
- function name by concatenating the argument with @samp{_command}.
- Here is how it is done:
- @example
- #define COMMAND(NAME) @{ #NAME, NAME ## _command @}
- struct command commands[] =
- @{
- COMMAND (quit),
- COMMAND (help),
- /* @r{@dots{}} */
- @};
- @end example
- @node Variadic Macros
- @subsection Variadic Macros
- @cindex variable number of arguments
- @cindex macros with variable arguments
- @cindex variadic macros
- A macro can be declared to accept a variable number of arguments much as
- a function can. The syntax for defining the macro is similar to that of
- a function. Here is an example:
- @example
- #define eprintf(@dots{}) fprintf (stderr, __VA_ARGS__)
- @end example
- This kind of macro is called @dfn{variadic}. When the macro is invoked,
- all the tokens in its argument list after the last named argument (this
- macro has none), including any commas, become the @dfn{variable
- argument}. This sequence of tokens replaces the identifier
- @code{@w{__VA_ARGS__}} in the macro body wherever it appears. Thus, we
- have this expansion:
- @example
- eprintf ("%s:%d: ", input_file, lineno)
- @expansion{} fprintf (stderr, "%s:%d: ", input_file, lineno)
- @end example
- The variable argument is completely macro-expanded before it is inserted
- into the macro expansion, just like an ordinary argument. You may use
- the @code{#} and @code{##} operators to stringify the variable argument
- or to paste its leading or trailing token with another token. (But see
- below for an important special case for @code{##}.)
- @strong{Warning:} don't use the identifier @code{@w{__VA_ARGS__}}
- for anything other than this.
- If your macro is complicated, you may want a more descriptive name for
- the variable argument than @code{@w{__VA_ARGS__}}. You can write an
- argument name immediately before the @samp{@dots{}}; that name is used
- for the variable argument.@footnote{GNU C extension.} The
- @code{eprintf} macro above could be written thus:
- @example
- #define eprintf(args@dots{}) fprintf (stderr, args)
- @end example
- A variadic macro can have named arguments as well as variable
- arguments, so @code{eprintf} can be defined like this, instead:
- @example
- #define eprintf(format, @dots{}) \
- fprintf (stderr, format, __VA_ARGS__)
- @end example
- @noindent
- This formulation is more descriptive, but what if you want to specify
- a format string that takes no arguments? In GNU C, you can omit the
- comma before the variable arguments if they are empty, but that puts
- an extra comma in the expansion:
- @example
- eprintf ("success!\n")
- @expansion{} fprintf(stderr, "success!\n", )
- @end example
- @noindent
- That's an error in the call to @code{fprintf}.
- To get rid of that comma, the @code{##} token paste operator has a
- special meaning when placed between a comma and a variable
- argument.@footnote{GNU C extension.} If you write
- @example
- #define eprintf(format, @dots{}) \
- fprintf (stderr, format, ##__VA_ARGS__)
- @end example
- @noindent
- then use the macro @code{eprintf} with empty variable arguments,
- @code{##} deletes the preceding comma.
- @example
- eprintf ("success!\n")
- @expansion{} fprintf(stderr, "success!\n")
- @end example
- @noindent
- This does @emph{not} happen if you pass an empty argument, nor does it
- happen if the token preceding @code{##} is anything other than a
- comma.
- @noindent
- When the only macro parameter is a variable arguments parameter, and
- the macro call has no argument at all, it is not obvious whether that
- means an empty argument or a missing argument. Should the comma be
- kept, or deleted? The C standard says to keep the comma, but the
- preexisting GNU C extension deleted the comma. Nowadays, GNU C
- retains the comma when implementing a specific C standard, and deletes
- it otherwise.
- C99 mandates that the only place the identifier @code{@w{__VA_ARGS__}}
- can appear is in the replacement list of a variadic macro. It may not
- be used as a macro name, macro parameter name, or within a different
- type of macro. It may also be forbidden in open text; the standard is
- ambiguous. We recommend you avoid using that name except for its
- special purpose.
- Variadic macros where you specify the parameter name is a GNU C
- feature that has been supported for a long time. Standard C, as of
- C99, supports only the form where the parameter is called
- @code{@w{__VA_ARGS__}}. For portability to previous versions of GNU C
- you should use only named variable argument parameters. On the other
- hand, for portability to other C99 compilers, you should use only
- @code{@w{__VA_ARGS__}}.
- @node Predefined Macros
- @subsection Predefined Macros
- @cindex predefined macros
- Several object-like macros are predefined; you use them without
- supplying their definitions. Here we explain the ones user programs
- often need to use. Many other macro names starting with @samp{__} are
- predefined; in general, you should not define such macro names
- yourself.
- @table @code
- @item __FILE__
- This macro expands to the name of the current input file, in the form
- of a C string constant. This is the full name by which the GCC opened
- the file, not the short name specified in @code{#include} or as the
- input file name argument. For example,
- @code{"/usr/local/include/myheader.h"} is a possible expansion of this
- macro.
- @item __LINE__
- This macro expands to the current input line number, in the form of a
- decimal integer constant. While we call it a predefined macro, it's
- a pretty strange macro, since its ``definition'' changes with each
- new line of source code.
- @item __func__
- @itemx __FUNCTION__
- These names are like variables that have as value a string containing
- the name of the current function definition. They are not really
- macros, but this is the best place to mention them.
- @code{__FUNCTION__} is the name that has been defined in GNU C since
- time immemorial; @code{__func__} is defined by the C standard.
- With the following conditionals, you can use whichever one is defined.
- @example
- #if __STDC_VERSION__ < 199901L
- # if __GNUC__ >= 2
- # define __func__ __FUNCTION__
- # else
- # define __func__ "<unknown>"
- # endif
- #endif
- @end example
- @item __PRETTY_FUNCTION__
- This is equivalent to @code{__FUNCTION__} in C, but in C@code{++}
- the string includes argument type information as well.
- It is a GNU C extension.
- @end table
- Those features are useful in generating an error message to report an
- inconsistency detected by the program; the message can state the
- source line where the inconsistency was detected. For example,
- @example
- fprintf (stderr, "Internal error: "
- "negative string length "
- "in function %s "
- "%d at %s, line %d.",
- __func__, length, __FILE__, __LINE__);
- @end example
- A @code{#line} directive changes @code{__LINE__}, and may change
- @code{__FILE__} as well. @xref{Line Control}.
- @table @code
- @item __DATE__
- This macro expands to a string constant that describes the date of
- compilation. The string constant contains eleven characters and looks
- like @code{@w{"Feb 12 1996"}}. If the day of the month is just one
- digit, an extra space precedes it so that the date is always eleven
- characters.
- If the compiler cannot determine the current date, it emits a warning messages
- (once per compilation) and @code{__DATE__} expands to
- @code{@w{"??? ?? ????"}}.
- We deprecate the use of @code{__DATE__} for the sake of reproducible
- compilation.
- @item __TIME__
- This macro expands to a string constant that describes the time of
- compilation. The string constant contains eight characters and looks
- like @code{"23:59:01"}.
- If the compiler cannot determine the current time, it emits a warning
- message (once per compilation) and @code{__TIME__} expands to
- @code{"??:??:??"}.
- We deprecate the use of @code{__TIME__} for the sake of reproducible
- compilation.
- @item __STDC__
- In normal operation, this macro expands to the constant 1, to signify
- that this compiler implements ISO Standard C@.
- @item __STDC_VERSION__
- This macro expands to the C Standard's version number, a long integer
- constant of the form @code{@var{yyyy}@var{mm}L} where @var{yyyy} and
- @var{mm} are the year and month of the Standard version. This states
- which version of the C Standard the compiler implements.
- The current default value is @code{201112L}, which signifies the C
- 2011 standard.
- @item __STDC_HOSTED__
- This macro is defined, with value 1, if the compiler's target is a
- @dfn{hosted environment}. A hosted environment provides the full
- facilities of the standard C library.
- @end table
- The rest of the predefined macros are GNU C extensions.
- @table @code
- @item __COUNTER__
- This macro expands to sequential integral values starting from 0. In
- other words, each time the program uses this macro, it generates the
- next successive integer. This, with the @code{##} operator, provides
- a convenient means for macros to generate unique identifiers.
- @item __GNUC__
- @itemx __GNUC_MINOR__
- @itemx __GNUC_PATCHLEVEL__
- These macros expand to the major version, minor version, and patch
- level of the compiler, as integer constants. For example, GCC 3.2.1
- expands @code{__GNUC__} to 3, @code{__GNUC_MINOR__} to 2, and
- @code{__GNUC_PATCHLEVEL__} to 1.
- If all you need to know is whether or not your program is being
- compiled by GCC, or a non-GCC compiler that claims to accept the GNU C
- extensions, you can simply test @code{__GNUC__}. If you need to write
- code that depends on a specific version, you must check more
- carefully. Each change in the minor version resets the patch level to
- zero; each change in the major version (which happens rarely) resets
- the minor version and the patch level to zero. To use the predefined
- macros directly in the conditional, write it like this:
- @example
- /* @r{Test for version 3.2.0 or later.} */
- #if __GNUC__ > 3 || \
- (__GNUC__ == 3 && (__GNUC_MINOR__ > 2 || \
- (__GNUC_MINOR__ == 2 && \
- __GNUC_PATCHLEVEL__ > 0))
- @end example
- @noindent
- Another approach is to use the predefined macros to
- calculate a single number, then compare that against a threshold:
- @example
- #define GCC_VERSION (__GNUC__ * 10000 \
- + __GNUC_MINOR__ * 100 \
- + __GNUC_PATCHLEVEL__)
- /* @r{@dots{}} */
- /* @r{Test for GCC > 3.2.0} */
- #if GCC_VERSION > 30200
- @end example
- @noindent
- Many people find this form easier to understand.
- @item __VERSION__
- This macro expands to a string constant that describes the version of
- the compiler in use. You should not rely on its contents' having any
- particular form, but you can count on it to contain at least the
- release number.
- @item __TIMESTAMP__
- This macro expands to a string constant that describes the date and
- time of the last modification of the current source file. The string
- constant contains abbreviated day of the week, month, day of the
- month, time in hh:mm:ss form, and the year, in the format
- @code{@w{"Sun Sep 16 01:03:52 1973"}}. If the day of the month is
- less than 10, it is padded with a space on the left.
- If GCC cannot determine that information date, it emits a warning
- message (once per compilation) and @code{__TIMESTAMP__} expands to
- @code{@w{"??? ??? ?? ??:??:?? ????"}}.
- We deprecate the use of this macro for the sake of reproducible
- compilation.
- @end table
- @node Undefining and Redefining Macros
- @subsection Undefining and Redefining Macros
- @cindex undefining macros
- @cindex redefining macros
- @findex #undef
- You can @dfn{undefine} a macro with the @code{#undef} directive.
- @code{#undef} takes a single argument, the name of the macro to
- undefine. You use the bare macro name, even if the macro is
- function-like. It is an error if anything appears on the line after
- the macro name. @code{#undef} has no effect if the name is not a
- macro.
- @example
- #define FOO 4
- x = FOO; @expansion{} x = 4;
- #undef FOO
- x = FOO; @expansion{} x = FOO;
- @end example
- Once a macro has been undefined, that identifier may be @dfn{redefined}
- as a macro by a subsequent @code{#define} directive. The new definition
- need not have any resemblance to the old definition.
- You can define a macro again without first undefining it only if
- the new definition is @dfn{effectively the same} as the old one.
- Two macro definitions are effectively the same if:
- @itemize @bullet
- @item Both are the same type of macro (object- or function-like).
- @item All the tokens of the replacement list are the same.
- @item If there are any parameters, they are the same.
- @item Whitespace appears in the same places in both. It need not be
- exactly the same amount of whitespace, though. Remember that comments
- count as whitespace.
- @end itemize
- @noindent
- These definitions are effectively the same:
- @example
- #define FOUR (2 + 2)
- #define FOUR (2 + 2)
- #define FOUR (2 /* @r{two} */ + 2)
- @end example
- @noindent
- but these are not:
- @example
- #define FOUR (2 + 2)
- #define FOUR ( 2+2 )
- #define FOUR (2 * 2)
- #define FOUR(score,and,seven,years,ago) (2 + 2)
- @end example
- This allows two different header files to define a common macro.
- You can redefine an existing macro with #define, but redefining an
- existing macro name with a different definition results in a warning.
- @node Directives Within Macro Arguments
- @subsection Directives Within Macro Arguments
- @cindex macro arguments and directives
- GNU C permits and handles preprocessing directives in the text provided
- as arguments for a macro. That case is undefined in the C standard.
- but in GNU C@ conditional directives in macro arguments
- are clear and valid.
- A paradoxical case is to redefine a macro within the call to that same
- macro. What happens is, the new definition takes effect in time for
- pre-expansion of @emph{all} the arguments, then the original
- definition is expanded to replace the call. Here is a pathological
- example:
- @example
- #define f(x) x x
- f (first f second
- #undef f
- #define f 2
- f)
- @end example
- @noindent
- which expands to
- @example
- first 2 second 2 first 2 second 2
- @end example
- @noindent
- with the semantics described above. We suggest you avoid writing code
- which does this sort of thing.
- @node Macro Pitfalls
- @subsection Macro Pitfalls
- @cindex problems with macros
- @cindex pitfalls of macros
- In this section we describe some special rules that apply to macros and
- macro expansion, and point out certain cases in which the rules have
- counter-intuitive consequences that you must watch out for.
- @menu
- * Misnesting::
- * Operator Precedence Problems::
- * Swallowing the Semicolon::
- * Duplication of Side Effects::
- * Macros and Auto Type::
- * Self-Referential Macros::
- * Argument Prescan::
- @end menu
- @node Misnesting
- @subsubsection Misnesting
- When a macro is called with arguments, the arguments are substituted
- into the macro body and the result is checked, together with the rest of
- the input file, for more macro calls. It is possible to piece together
- a macro call coming partially from the macro body and partially from the
- arguments. For example,
- @example
- #define twice(x) (2*(x))
- #define call_with_1(x) x(1)
- call_with_1 (twice)
- @expansion{} twice(1)
- @expansion{} (2*(1))
- @end example
- Macro definitions do not have to have balanced parentheses. By writing
- an unbalanced open parenthesis in a macro body, it is possible to create
- a macro call that begins inside the macro body but ends outside of it.
- For example,
- @example
- #define strange(file) fprintf (file, "%s %d",
- /* @r{@dots{}} */
- strange(stderr) p, 35)
- @expansion{} fprintf (stderr, "%s %d", p, 35)
- @end example
- The ability to piece together a macro call can be useful, but the use of
- unbalanced open parentheses in a macro body is just confusing, and
- should be avoided.
- @node Operator Precedence Problems
- @subsubsection Operator Precedence Problems
- @cindex parentheses in macro bodies
- You may have noticed that in most of the macro definition examples shown
- above, each occurrence of a macro parameter name had parentheses around
- it. In addition, another pair of parentheses usually surrounds the
- entire macro definition. Here is why it is best to write macros that
- way.
- Suppose you define a macro as follows,
- @example
- #define ceil_div(x, y) (x + y - 1) / y
- @end example
- @noindent
- whose purpose is to divide, rounding up. (One use for this operation is
- to compute how many @code{int} objects are needed to hold a certain
- number of @code{char} objects.) Then suppose it is used as follows:
- @example
- a = ceil_div (b & c, sizeof (int));
- @expansion{} a = (b & c + sizeof (int) - 1) / sizeof (int);
- @end example
- @noindent
- This does not do what is intended. The operator-precedence rules of
- C make it equivalent to this:
- @example
- a = (b & (c + sizeof (int) - 1)) / sizeof (int);
- @end example
- @noindent
- What we want is this:
- @example
- a = ((b & c) + sizeof (int) - 1)) / sizeof (int);
- @end example
- @noindent
- Defining the macro as
- @example
- #define ceil_div(x, y) ((x) + (y) - 1) / (y)
- @end example
- @noindent
- provides the desired result.
- Unintended grouping can result in another way. Consider @code{sizeof
- ceil_div(1, 2)}. That has the appearance of a C expression that would
- compute the size of the type of @code{ceil_div (1, 2)}, but in fact it
- means something very different. Here is what it expands to:
- @example
- sizeof ((1) + (2) - 1) / (2)
- @end example
- @noindent
- This would take the size of an integer and divide it by two. The
- precedence rules have put the division outside the @code{sizeof} when it
- was intended to be inside.
- Parentheses around the entire macro definition prevent such problems.
- Here, then, is the recommended way to define @code{ceil_div}:
- @example
- #define ceil_div(x, y) (((x) + (y) - 1) / (y))
- @end example
- @node Swallowing the Semicolon
- @subsubsection Swallowing the Semicolon
- @cindex semicolons (after macro calls)
- Often it is desirable to define a macro that expands into a compound
- statement. Consider, for example, the following macro, that advances a
- pointer (the parameter @code{p} says where to find it) across whitespace
- characters:
- @example
- #define SKIP_SPACES(p, limit) \
- @{ char *lim = (limit); \
- while (p < lim) @{ \
- if (*p++ != ' ') @{ \
- p--; break; @}@}@}
- @end example
- @noindent
- Here backslash-newline is used to split the macro definition, which must
- be a single logical line, so that it resembles the way such code would
- be laid out if not part of a macro definition.
- A call to this macro might be @code{SKIP_SPACES (p, lim)}. Strictly
- speaking, the call expands to a compound statement, which is a complete
- statement with no need for a semicolon to end it. However, since it
- looks like a function call, it minimizes confusion if you can use it
- like a function call, writing a semicolon afterward, as in
- @code{SKIP_SPACES (p, lim);}
- This can cause trouble before @code{else} statements, because the
- semicolon is actually a null statement. Suppose you write
- @example
- if (*p != 0)
- SKIP_SPACES (p, lim);
- else /* @r{@dots{}} */
- @end example
- @noindent
- The presence of two statements---the compound statement and a null
- statement---in between the @code{if} condition and the @code{else}
- makes invalid C code.
- The definition of the macro @code{SKIP_SPACES} can be altered to solve
- this problem, using a @code{do @r{@dots{}} while} statement. Here is how:
- @example
- #define SKIP_SPACES(p, limit) \
- do @{ char *lim = (limit); \
- while (p < lim) @{ \
- if (*p++ != ' ') @{ \
- p--; break; @}@}@} \
- while (0)
- @end example
- Now @code{SKIP_SPACES (p, lim);} expands into
- @example
- do @{ /* @r{@dots{}} */ @} while (0);
- @end example
- @noindent
- which is one statement. The loop executes exactly once; most compilers
- generate no extra code for it.
- @node Duplication of Side Effects
- @subsubsection Duplication of Side Effects
- @cindex side effects (in macro arguments)
- @cindex unsafe macros
- Many C programs define a macro @code{min}, for ``minimum'', like this:
- @example
- #define min(X, Y) ((X) < (Y) ? (X) : (Y))
- @end example
- When you use this macro with an argument containing a side effect,
- as shown here,
- @example
- next = min (x + y, foo (z));
- @end example
- @noindent
- it expands as follows:
- @example
- next = ((x + y) < (foo (z)) ? (x + y) : (foo (z)));
- @end example
- @noindent
- where @code{x + y} has been substituted for @code{X} and @code{foo (z)}
- for @code{Y}.
- The function @code{foo} is used only once in the statement as it
- appears in the program, but the expression @code{foo (z)} has been
- substituted twice into the macro expansion. As a result, @code{foo}
- might be called twice when the statement is executed. If it has side
- effects or if it takes a long time to compute, that may be
- undesirable. We say that @code{min} is an @dfn{unsafe} macro.
- The best solution to this problem is to define @code{min} in a way that
- computes the value of @code{foo (z)} only once. In general, that requires
- using @code{__auto_type} (@pxref{Auto Type}). How to use it for this
- is described in the following section. @xref{Macros and Auto Type}.
- Otherwise, you will need to be careful when @emph{using} the macro
- @code{min}. For example, you can calculate the value of @code{foo
- (z)}, save it in a variable, and use that variable in @code{min}:
- @example
- @group
- #define min(X, Y) ((X) < (Y) ? (X) : (Y))
- /* @r{@dots{}} */
- @{
- int tem = foo (z);
- next = min (x + y, tem);
- @}
- @end group
- @end example
- @noindent
- (where we assume that @code{foo} returns type @code{int}).
- When the repeated value appears as the condition of the @code{?:}
- operator and again as its @var{iftrue} expression, you can avoid
- repeated execution by omitting the @var{iftrue} expression, like this:
- @example
- #define x_or_y(X, Y) ((X) ? : (Y))
- @end example
- @noindent
- In GNU C, this expands to use the first macro argument's value if that
- isn't zero. If that's zero, it compiles the second argument and uses
- that value. @xref{Conditional Expression}.
- @node Macros and Auto Type
- @subsubsection Using @code{__auto_type} for Local Variables
- @cindex local variables in macros
- @cindex variables, local, in macros
- @cindex macros, local variables in
- The operator @code{__auto_type} makes it possible to
- define macros that can work on any data type even though they need to
- generate local variable declarations. @xref{Auto Type}.
- For instance, here's how to define a safe ``maximum'' macro that
- operates on any arithmetic type and computes each of its arguments
- exactly once:
- @example
- #define max(a,b) \
- (@{ __auto_type _a = (a); \
- __auto_type _b = (b); \
- _a > _b ? _a : _b; @})
- @end example
- The @samp{(@{ @dots{} @})} notation produces @dfn{statement
- expression}---a statement that can be used as an expression
- (@pxref{Statement Exprs}). Its value is the value of its last
- statement. This permits us to define local variables and store each
- argument value into one.
- @cindex underscores in variables in macros
- @cindex @samp{_} in variables in macros
- The reason for using names that start with underscores for the local
- variables is to avoid conflicts with variable names that occur within
- the expressions that are substituted for @code{a} and @code{b}.
- Underscore followed by a lower case letter won't be predefined by the
- system in any way.
- @c We hope someday to extend C with a new form of declaration syntax
- @c which all the newly declared variables' scopes would begin at the end
- @c of the entire declaration, rather than as soon as each variable's
- @c declaration begins. This way, all the variables' initializers would
- @c be interpreted in the context before the declaration. Then we could
- @c use any names whatsoever for the local variables and always get correct
- @c behavior for the macro.
- @node Self-Referential Macros
- @subsubsection Self-Referential Macros
- @cindex self-reference
- A @dfn{self-referential} macro is one whose name appears in its
- definition. Recall that all macro definitions are rescanned for more
- macros to replace. If the self-reference were considered a use of the
- macro, it would produce an infinitely large expansion. To prevent
- this, the self-reference is not considered a macro call: preprocessing
- leaves it unchanged. Consider an example:
- @example
- #define foo (4 + foo)
- @end example
- @noindent
- where @code{foo} is also a variable in your program.
- Following the ordinary rules, each reference to @code{foo} will expand
- into @code{(4 + foo)}; then this will be rescanned and will expand into
- @code{(4 + (4 + foo))}; and so on until the computer runs out of memory.
- The self-reference rule cuts this process short after one step, at
- @code{(4 + foo)}. Therefore, this macro definition has the possibly
- useful effect of causing the program to add 4 to the value of @code{foo}
- wherever @code{foo} is referred to.
- In most cases, it is a bad idea to take advantage of this feature. A
- person reading the program who sees that @code{foo} is a variable will
- not expect that it is a macro as well. The reader will come across the
- identifier @code{foo} in the program and think its value should be that
- of the variable @code{foo}, whereas in fact the value is four greater.
- It is useful to make a macro definition that expands to the macro
- name itself. If you write
- @example
- #define EPERM EPERM
- @end example
- @noindent
- then the macro @code{EPERM} expands to @code{EPERM}. Effectively,
- preprocessing leaves it unchanged in the source code. You can tell
- that it's a macro with @code{#ifdef}. You might do this if you want
- to define numeric constants with an @code{enum}, but have
- @code{#ifdef} be true for each constant.
- If a macro @code{x} expands to use a macro @code{y}, and the expansion of
- @code{y} refers to the macro @code{x}, that is an @dfn{indirect
- self-reference} of @code{x}. @code{x} is not expanded in this case
- either. Thus, if we have
- @example
- #define x (4 + y)
- #define y (2 * x)
- @end example
- @noindent
- then @code{x} and @code{y} expand as follows:
- @example
- @group
- x @expansion{} (4 + y)
- @expansion{} (4 + (2 * x))
- y @expansion{} (2 * x)
- @expansion{} (2 * (4 + y))
- @end group
- @end example
- @noindent
- Each macro is expanded when it appears in the definition of the other
- macro, but not when it indirectly appears in its own definition.
- @node Argument Prescan
- @subsubsection Argument Prescan
- @cindex expansion of arguments
- @cindex macro argument expansion
- @cindex prescan of macro arguments
- Macro arguments are completely macro-expanded before they are
- substituted into a macro body, unless they are stringified or pasted
- with other tokens. After substitution, the entire macro body, including
- the substituted arguments, is scanned again for macros to be expanded.
- The result is that the arguments are scanned @emph{twice} to expand
- macro calls in them.
- Most of the time, this has no effect. If the argument contained any
- macro calls, they were expanded during the first scan. The result
- therefore contains no macro calls, so the second scan does not change
- it. If the argument were substituted as given, with no prescan, the
- single remaining scan would find the same macro calls and produce the
- same results.
- You might expect the double scan to change the results when a
- self-referential macro is used in an argument of another macro
- (@pxref{Self-Referential Macros}): the self-referential macro would be
- expanded once in the first scan, and a second time in the second scan.
- However, this is not what happens. The self-references that do not
- expand in the first scan are marked so that they will not expand in the
- second scan either.
- You might wonder, ``Why mention the prescan, if it makes no difference?
- And why not skip it and make preprocessing go faster?'' The answer is
- that the prescan does make a difference in three special cases:
- @itemize @bullet
- @item
- Nested calls to a macro.
- We say that @dfn{nested} calls to a macro occur when a macro's argument
- contains a call to that very macro. For example, if @code{f} is a macro
- that expects one argument, @code{f (f (1))} is a nested pair of calls to
- @code{f}. The desired expansion is made by expanding @code{f (1)} and
- substituting that into the definition of @code{f}. The prescan causes
- the expected result to happen. Without the prescan, @code{f (1)} itself
- would be substituted as an argument, and the inner use of @code{f} would
- appear during the main scan as an indirect self-reference and would not
- be expanded.
- @item
- Macros that call other macros that stringify or concatenate.
- If an argument is stringified or concatenated, the prescan does not
- occur. If you @emph{want} to expand a macro, then stringify or
- concatenate its expansion, you can do that by causing one macro to call
- another macro that does the stringification or concatenation. For
- instance, if you have
- @example
- #define AFTERX(x) X_ ## x
- #define XAFTERX(x) AFTERX(x)
- #define TABLESIZE 1024
- #define BUFSIZE TABLESIZE
- @end example
- @noindent
- then @code{AFTERX(BUFSIZE)} expands to @code{X_BUFSIZE}, and
- @code{XAFTERX(BUFSIZE)} expands to @code{X_1024}. (Not to
- @code{X_TABLESIZE}. Prescan always does a complete expansion.)
- @item
- Macros used in arguments, whose expansions contain unshielded commas.
- This can cause a macro expanded on the second scan to be called with the
- wrong number of arguments. Here is an example:
- @example
- #define foo a,b
- #define bar(x) lose(x)
- #define lose(x) (1 + (x))
- @end example
- We would like @code{bar(foo)} to turn into @code{(1 + (foo))}, which
- would then turn into @code{(1 + (a,b))}. Instead, @code{bar(foo)}
- expands into @code{lose(a,b)}, which gives an error because @code{lose}
- requires a single argument. In this case, the problem is easily solved
- by the same parentheses that ought to be used to prevent misnesting of
- arithmetic operations:
- @example
- #define foo (a,b)
- @exdent or
- #define bar(x) lose((x))
- @end example
- The extra pair of parentheses prevents the comma in @code{foo}'s
- definition from being interpreted as an argument separator.
- @end itemize
- @ignore
- @c This is commented out because pragmas are not supposed
- @c to alter the meaning of the program.
- @c Microsoft did something stupid in defining these.
- @node Macro Pragmas
- @subsection Macro Pragmas
- A pragma is a way of specifying special directions to the C compiler.
- @xref{Pragmas}, for the basic syntax of pragmas. Here we describe two
- pragmas that save the current definition of a macro on a stack, and
- restore it later. This makes it possible to redefine a macro temporarily
- and later go back to the previous definition.
- @table @code
- @item #pragma push_macro (@var{macro_name})
- @itemx _Pragma ("push_macro (@var{macro_name})")
- The @samp{push_macro} pragma saves the current macro definition of
- @var{macro_name} on the macro definition stack.
- @item #pragma pop_macro (@var{macro_name})
- @itemx _Pragma ("pop_macro (@var{macro_name})")
- The @samp{pop_macro} pragma pops a saved macro definition
- off the macro definition stack and defines @var{macro_name} with
- that definition.
- @end table
- Each macro name has a separate stack, and @samp{pop_macro}
- when the stack is empty has no effect.
- Here's an example of using these to pragmas to override temporarily
- the definition of @code{FOO}.
- @example
- #define FOO 42
- /* @r{Do something with @var{FOO} defined as 42...} */
- _Pragma ("push_macro (\"FOO\")")
- #undef FOO
- #define FOO 47
- /* @r{Do something with @var{FOO} defined as 47...} */
- _Pragma ("pop_macro (\"FOO\")")
- /* @r{@var{FOO} is now restored}
- @r{to its previous definition of 42.} */
- @end example
- @end ignore
- @node Conditionals
- @section Conditionals
- @cindex conditionals
- A @dfn{conditional} is a preprocessing directive that controls whether
- or not to include a chunk of code in the final token stream that is
- compiled. Preprocessing conditionals can test arithmetic expressions,
- or whether a name is defined as a macro, or both together using the
- special @code{defined} operator.
- A preprocessing conditional in C resembles in some ways an @code{if}
- statement in C, but it is important to understand the difference between
- them. The condition in an @code{if} statement is tested during the
- execution of your program. Its purpose is to allow your program to
- behave differently from run to run, depending on the data it is
- operating on. The condition in a preprocessing conditional directive is
- tested when your program is compiled. Its purpose is to allow different
- code to be included in the program depending on the situation at the
- time of compilation.
- Sometimes this distinction makes no practical difference. GCC and
- other modern compilers often
- do test @code{if} statements when a program is compiled, if their
- conditions are known not to vary at run time, and eliminate code that
- can never be executed. If you can count on your compiler to do this,
- you may find that your program is more readable if you use @code{if}
- statements with constant conditions (perhaps determined by macros). Of
- course, you can only use this to exclude code, not type definitions or
- other preprocessing directives, and you can only do it if the file
- remains syntactically valid when that code is not used.
- @menu
- * Conditional Uses::
- * Conditional Syntax::
- * Deleted Code::
- @end menu
- @node Conditional Uses
- @subsection Uses of Conditional Directives
- There are three usual reasons to use a preprocessing conditional.
- @itemize @bullet
- @item
- A program may need to use different code depending on the machine or
- operating system it is to run on. In some cases the code for one
- operating system may be erroneous on another operating system; for
- example, it might refer to data types or constants that do not exist on
- the other system. When this happens, it is not enough to avoid
- executing the invalid code. Its mere presence will cause the compiler
- to reject the program. With a preprocessing conditional, the offending
- code can be effectively excised from the program when it is not valid.
- @item
- You may want to be able to compile the same source file into two
- different programs. One version might make frequent time-consuming
- consistency checks on its intermediate data, or print the values of
- those data for debugging, and the other not.
- @item
- A conditional whose condition is always false is one way to exclude code
- from the program but keep it as a sort of comment for future reference.
- @end itemize
- Simple programs that do not need system-specific logic or complex
- debugging hooks generally will not need to use preprocessing
- conditionals.
- @node Conditional Syntax
- @subsection Syntax of Preprocessing Conditionals
- @findex #if
- A preprocessing conditional begins with a @dfn{conditional
- directive}: @code{#if}, @code{#ifdef} or @code{#ifndef}.
- @menu
- * ifdef::
- * if::
- * defined::
- * else::
- * elif::
- @end menu
- @node ifdef
- @subsubsection The @code{#ifdef} directive
- @findex #ifdef
- @findex #endif
- The simplest sort of conditional is
- @example
- @group
- #ifdef @var{MACRO}
- @var{controlled text}
- #endif /* @var{MACRO} */
- @end group
- @end example
- @cindex conditional group
- This block is called a @dfn{conditional group}. The body,
- @var{controlled text}, will be included in compilation if
- and only if @var{MACRO} is defined. We say that the conditional
- @dfn{succeeds} if @var{MACRO} is defined, @dfn{fails} if it is not.
- The @var{controlled text} inside a conditional can include
- preprocessing directives. They are executed only if the conditional
- succeeds. You can nest conditional groups inside other conditional
- groups, but they must be completely nested. In other words,
- @code{#endif} always matches the nearest @code{#ifdef} (or
- @code{#ifndef}, or @code{#if}). Also, you cannot start a conditional
- group in one file and end it in another.
- Even if a conditional fails, the @var{controlled text} inside it is
- still run through initial transformations and tokenization. Therefore,
- it must all be lexically valid C@. Normally the only way this matters is
- that all comments and string literals inside a failing conditional group
- must still be properly ended.
- The comment following the @code{#endif} is not required, but it is a
- good practice if there is a lot of @var{controlled text}, because it
- helps people match the @code{#endif} to the corresponding @code{#ifdef}.
- Older programs sometimes put @var{macro} directly after the
- @code{#endif} without enclosing it in a comment. This is invalid code
- according to the C standard, but it only causes a warning in GNU C@.
- It never affects which @code{#ifndef} the @code{#endif} matches.
- @findex #ifndef
- Sometimes you wish to use some code if a macro is @emph{not} defined.
- You can do this by writing @code{#ifndef} instead of @code{#ifdef}.
- One common use of @code{#ifndef} is to include code only the first
- time a header file is included. @xref{Once-Only Headers}.
- Macro definitions can vary between compilations for several reasons.
- Here are some samples.
- @itemize @bullet
- @item
- Some macros are predefined on each kind of machine
- (@pxref{System-specific Predefined Macros, System-specific Predefined
- Macros, System-specific Predefined Macros, gcc, Using the GNU Compiler
- Collection}). This allows you to provide code specially tuned for a
- particular machine.
- @item
- System header files define more macros, associated with the features
- they implement. You can test these macros with conditionals to avoid
- using a system feature on a machine where it is not implemented.
- @item
- Macros can be defined or undefined with the @option{-D} and @option{-U}
- command-line options when you compile the program. You can arrange to
- compile the same source file into two different programs by choosing a
- macro name to specify which program you want, writing conditionals to
- test whether or how this macro is defined, and then controlling the
- state of the macro with command-line options, perhaps set in the
- file @file{Makefile}. @xref{Invocation, Invoking GCC, Invoking GCC,
- gcc, Using the GNU Compiler Collection}.
- @item
- Your program might have a special header file (often called
- @file{config.h}) that is adjusted when the program is compiled. It can
- define or not define macros depending on the features of the system and
- the desired capabilities of the program. The adjustment can be
- automated by a tool such as @command{autoconf}, or done by hand.
- @end itemize
- @node if
- @subsubsection The @code{#if} directive
- The @code{#if} directive allows you to test the value of an integer arithmetic
- expression, rather than the mere existence of one macro. Its syntax is
- @example
- @group
- #if @var{expression}
- @var{controlled text}
- #endif /* @var{expression} */
- @end group
- @end example
- @var{expression} is a C expression of integer type, subject to
- stringent restrictions so its value can be computed at compile time.
- It may contain
- @itemize @bullet
- @item
- Integer constants.
- @item
- Character constants, which are interpreted as they would be in normal
- code.
- @item
- Arithmetic operators for addition, subtraction, multiplication,
- division, bitwise operations, shifts, comparisons, and logical
- operations (@code{&&} and @code{||}). The latter two obey the usual
- short-circuiting rules of standard C@.
- @item
- Macros. All macros in the expression are expanded before actual
- computation of the expression's value begins.
- @item
- Uses of the @code{defined} operator, which lets you check whether macros
- are defined in the middle of an @code{#if}.
- @item
- Identifiers that are not macros, which are all considered to be the
- number zero. This allows you to write @code{@w{#if MACRO}} instead of
- @code{@w{#ifdef MACRO}}, if you know that MACRO, when defined, will
- always have a nonzero value. Function-like macros used without their
- function call parentheses are also treated as zero.
- In some contexts this shortcut is undesirable. The @option{-Wundef}
- requests warnings for any identifier in an @code{#if} that is not
- defined as a macro.
- @end itemize
- Preprocessing does not know anything about the data types of C.
- Therefore, @code{sizeof} operators are not recognized in @code{#if};
- @code{sizeof} is simply an identifier, and if it is not a macro, it
- stands for zero. This is likely to make the expression invalid.
- Preprocessing does not recognize @code{enum} constants; they too are
- simply identifiers, so if they are not macros, they stand for zero.
- Preprocessing calculates the value of @var{expression}, and carries
- out all calculations in the widest integer type known to the compiler;
- on most machines supported by GNU C this is 64 bits. This is not the
- same rule as the compiler uses to calculate the value of a constant
- expression, and may give different results in some cases. If the
- value comes out to be nonzero, the @code{#if} succeeds and the
- @var{controlled text} is compiled; otherwise it is skipped.
- @node defined
- @subsubsection The @code{defined} test
- @cindex @code{defined}
- The special operator @code{defined} is used in @code{#if} and
- @code{#elif} expressions to test whether a certain name is defined as a
- macro. @code{defined @var{name}} and @code{defined (@var{name})} are
- both expressions whose value is 1 if @var{name} is defined as a macro at
- the current point in the program, and 0 otherwise. Thus, @code{@w{#if
- defined MACRO}} is precisely equivalent to @code{@w{#ifdef MACRO}}.
- @code{defined} is useful when you wish to test more than one macro for
- existence at once. For example,
- @example
- #if defined (__arm__) || defined (__PPC__)
- @end example
- @noindent
- would succeed if either of the names @code{__arm__} or
- @code{__PPC__} is defined as a macro---in other words,
- when compiling for ARM processors or PowerPC processors.
- Conditionals written like this:
- @example
- #if defined BUFSIZE && BUFSIZE >= 1024
- @end example
- @noindent
- can generally be simplified to just @code{@w{#if BUFSIZE >= 1024}},
- since if @code{BUFSIZE} is not defined, it will be interpreted as having
- the value zero.
- In GCC, you can include @code{defined} as part of another macro definition,
- like this:
- @example
- #define MACRO_DEFINED(X) defined X
- #if MACRO_DEFINED(BUFSIZE)
- @end example
- @noindent
- which would expand the @code{#if} expression to:
- @example
- #if defined BUFSIZE
- @end example
- @noindent
- Generating @code{defined} in this way is a GNU C extension.
- @node else
- @subsubsection The @code{#else} directive
- @findex #else
- The @code{#else} directive can be added to a conditional to provide
- alternative text to be used if the condition fails. This is what it
- looks like:
- @example
- @group
- #if @var{expression}
- @var{text-if-true}
- #else /* Not @var{expression} */
- @var{text-if-false}
- #endif /* Not @var{expression} */
- @end group
- @end example
- @noindent
- If @var{expression} is nonzero, the @var{text-if-true} is included and
- the @var{text-if-false} is skipped. If @var{expression} is zero, the
- opposite happens.
- You can use @code{#else} with @code{#ifdef} and @code{#ifndef}, too.
- @node elif
- @subsubsection The @code{#elif} directive
- @findex #elif
- One common case of nested conditionals is used to check for more than two
- possible alternatives. For example, you might have
- @example
- #if X == 1
- /* @r{@dots{}} */
- #else /* X != 1 */
- #if X == 2
- /* @r{@dots{}} */
- #else /* X != 2 */
- /* @r{@dots{}} */
- #endif /* X != 2 */
- #endif /* X != 1 */
- @end example
- Another conditional directive, @code{#elif}, allows this to be
- abbreviated as follows:
- @example
- #if X == 1
- /* @r{@dots{}} */
- #elif X == 2
- /* @r{@dots{}} */
- #else /* X != 2 and X != 1*/
- /* @r{@dots{}} */
- #endif /* X != 2 and X != 1*/
- @end example
- @code{#elif} stands for ``else if''. Like @code{#else}, it goes in the
- middle of a conditional group and subdivides it; it does not require a
- matching @code{#endif} of its own. Like @code{#if}, the @code{#elif}
- directive includes an expression to be tested. The text following the
- @code{#elif} is processed only if the original @code{#if}-condition
- failed and the @code{#elif} condition succeeds.
- More than one @code{#elif} can go in the same conditional group. Then
- the text after each @code{#elif} is processed only if the @code{#elif}
- condition succeeds after the original @code{#if} and all previous
- @code{#elif} directives within it have failed.
- @code{#else} is allowed after any number of @code{#elif} directives, but
- @code{#elif} may not follow @code{#else}.
- @node Deleted Code
- @subsection Deleted Code
- @cindex commenting out code
- If you replace or delete a part of the program but want to keep the
- old code in the file for future reference, commenting it out is not so
- straightforward in C. Block comments do not nest, so the first
- comment inside the old code will end the commenting-out. The probable
- result is a flood of syntax errors.
- One way to avoid this problem is to use an always-false conditional
- instead. For instance, put @code{#if 0} before the deleted code and
- @code{#endif} after it. This works even if the code being turned
- off contains conditionals, but they must be entire conditionals
- (balanced @code{#if} and @code{#endif}).
- Some people use @code{#ifdef notdef} instead. This is risky, because
- @code{notdef} might be accidentally defined as a macro, and then the
- conditional would succeed. @code{#if 0} can be counted on to fail.
- Do not use @code{#if 0} around text that is not C code. Use a real
- comment, instead. The interior of @code{#if 0} must consist of complete
- tokens; in particular, single-quote characters must balance. Comments
- often contain unbalanced single-quote characters (known in English as
- apostrophes). These confuse @code{#if 0}. They don't confuse
- @samp{/*}.
- @node Diagnostics
- @section Diagnostics
- @cindex diagnostic
- @cindex reporting errors
- @cindex reporting warnings
- @findex #error
- The directive @code{#error} reports a fatal error. The
- tokens forming the rest of the line following @code{#error} are used
- as the error message.
- The usual place to use @code{#error} is inside a conditional that
- detects a combination of parameters that you know the program does not
- properly support. For example,
- @smallexample
- #if !defined(UNALIGNED_INT_ASM_OP) && defined(DWARF2_DEBUGGING_INFO)
- #error "DWARF2_DEBUGGING_INFO requires UNALIGNED_INT_ASM_OP."
- #endif
- @end smallexample
- @findex #warning
- The directive @code{#warning} is like @code{#error}, but it reports a
- warning instead of an error. The tokens following @code{#warning} are
- used as the warning message.
- You might use @code{#warning} in obsolete header files, with a message
- saying which header file to use instead.
- Neither @code{#error} nor @code{#warning} macro-expands its argument.
- Internal whitespace sequences are each replaced with a single space.
- The line must consist of complete tokens. It is wisest to make the
- argument of these directives be a single string constant; this avoids
- problems with apostrophes and the like.
- @node Line Control
- @section Line Control
- @cindex line control
- Due to C's widespread availability and low-level nature, it is often
- used as the target language for translation of other languages, or for
- the output of lexical analyzers and parsers (e.g., lex/flex and
- yacc/bison). Line control enables the user to track diagnostics back
- to the location in the original language.
- The C compiler knows the location in the source file where each token
- came from: file name, starting line and column, and final line and column.
- (Column numbers are used only for error messages.)
- When a program generates C source code, as the Bison parser generator
- does, often it copies some of that C code from another file. For
- instance parts of the output from Bison are generated from scratch or
- come from a standard parser file, but Bison copies the rest from
- Bison's input file. Errors in that code, at compile time or run time,
- should refer to that file, which is the real source code. To make that happen,
- Bison generates line-control directives that the C compiler understands.
- @findex #line
- @code{#line} is a directive that specifies the original line number
- and source file name for subsequent code. @code{#line} has three
- variants:
- @table @code
- @item #line @var{linenum}
- @var{linenum} is a non-negative decimal integer constant. It specifies
- the line number that should be reported for the following line of
- input. Subsequent lines are counted from @var{linenum}.
- @item #line @var{linenum} @var{filename}
- @var{linenum} is the same as for the first form, and has the same
- effect. In addition, @var{filename} is a string constant that
- specifies the source file name. Subsequent source lines are recorded
- as coming from that file, until something else happens to change that.
- @var{filename} is interpreted according to the normal rules for a
- string constant. Backslash escapes are interpreted, in contrast to
- @code{#include}.
- @item #line @var{anything else}
- @var{anything else} is checked for macro calls, which are expanded.
- The result should match one of the above two forms.
- @end table
- @code{#line} directives alter the results of the @code{__FILE__} and
- @code{__LINE__} symbols from that point on. @xref{Predefined Macros}.
- @node Null Directive
- @section Null Directive
- @cindex null directive
- The @dfn{null directive} consists of a @code{#} followed by a newline,
- with only whitespace and comments in between. It has no
- effect on the output of the compiler.
|