cpp.texi 169 KB

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