cpp.texi 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507
  1. @ignore
  2. @c Copyright @copyright{} 2020 Richard Stallman and Free Software Foundation, Inc.
  3. DRAFT --- DO NOT REDISTRIBUTE --- DRAFT --- DO NOT REDISTRIBUTE --- DRAFT
  4. @end ignore
  5. @node Preprocessing
  6. @chapter Preprocessing
  7. @c man begin DESCRIPTION
  8. @cindex preprocessing
  9. As the first stage of compiling a C source module, GCC transforms the
  10. text with text substitutions and file inclusions. This is called
  11. @dfn{preprocessing}.
  12. @menu
  13. * Preproc Overview::
  14. * Directives::
  15. * Preprocessing Tokens::
  16. * Header Files::
  17. * Macros::
  18. * Conditionals::
  19. * Diagnostics::
  20. * Line Control::
  21. * Null Directive::
  22. @end menu
  23. @node Preproc Overview
  24. @section Preprocessing Overview
  25. GNU C performs preprocessing on each line of a C program as the first
  26. stage of compilation. Preprocessing operates on a line only when it
  27. contains a @dfn{preprocessing directive} or uses a @dfn{macro}---all
  28. other lines pass through preprocessing unchanged.
  29. Here are some jobs that preprocessing does. The rest of
  30. this chapter gives the details.
  31. @itemize @bullet
  32. @item
  33. Inclusion of header files. These are files (usually containing
  34. declarations and macro definitions) that can be substituted into your
  35. program.
  36. @item
  37. Macro expansion. You can define @dfn{macros}, which are abbreviations
  38. for arbitrary fragments of C code. Preprocessing replaces the macros
  39. with their definitions. Some macros are automatically predefined.
  40. @item
  41. Conditional compilation. You can include or exclude parts of the
  42. program according to various conditions.
  43. @item
  44. Line control. If you use a program to combine or rearrange source files
  45. into an intermediate file that is then compiled, you can use line
  46. control to inform the compiler where each source line originally came
  47. from.
  48. @item
  49. Compilation control. @code{#pragma} and @code{_Pragma} invoke
  50. some special compiler features in how to handle certain constructs.
  51. @item
  52. Diagnostics. You can detect problems at compile time and issue errors
  53. or warnings.
  54. @end itemize
  55. Except for expansion of predefined macros, all these operations happen
  56. only if you use preprocessing directives to request them.
  57. @node Directives
  58. @section Directives
  59. @cindex directives
  60. @cindex preprocessing directives
  61. @cindex directive line
  62. @cindex directive name
  63. @dfn{Preprocessing directives} are lines in the program that start
  64. with @samp{#}. Whitespace is allowed before and after the @samp{#}.
  65. The @samp{#} is followed by an identifier, the @dfn{directive name}.
  66. It specifies the operation to perform. Here are a couple of examples:
  67. @example
  68. #define LIMIT 51
  69. # undef LIMIT
  70. # error You screwed up!
  71. @end example
  72. We usually refer to a directive as @code{#@var{name}} where @var{name}
  73. is the directive name. For example, @code{#define} means the
  74. directive that defines a macro.
  75. The @samp{#} that begins a directive cannot come from a macro
  76. expansion. Also, the directive name is not macro expanded. Thus, if
  77. @code{foo} is defined as a macro expanding to @code{define}, that does
  78. not make @code{#foo} a valid preprocessing directive.
  79. The set of valid directive names is fixed. Programs cannot define new
  80. preprocessing directives.
  81. Some directives require arguments; these make up the rest of the
  82. directive line and must be separated from the directive name by
  83. whitespace. For example, @code{#define} must be followed by a macro
  84. name and the intended expansion of the macro.
  85. A preprocessing directive cannot cover more than one line. The line
  86. can, however, be continued with backslash-newline, or by a
  87. @samp{/*@r{@dots{}}*/}-style comment that extends past the end of the
  88. line. These will be replaced (by nothing, or by whitespace) before
  89. the directive is processed.
  90. @node Preprocessing Tokens
  91. @section Preprocessing Tokens
  92. @cindex preprocessing tokens
  93. Preprocessing divides C code (minus its comments) into
  94. @dfn{tokens} that are similar to C tokens, but not exactly the same.
  95. Here are the quirks of preprocessing tokens.
  96. The main classes of preprocessing tokens are identifiers,
  97. preprocessing numbers, string constants, character constants, and
  98. punctuators; there are a few others too.
  99. @table @asis
  100. @item identifier
  101. @cindex identifiers
  102. An @dfn{identifier} preprocessing token is syntactically like an
  103. identifier in C: any sequence of letters, digits, or underscores, as
  104. well as non-ASCII characters represented using @samp{\U} or @samp{\u},
  105. that doesn't begin with a digit.
  106. During preprocessing, the keywords of C have no special significance;
  107. at that stage, they are simply identifiers. Thus, you can define a
  108. macro whose name is a keyword. The only identifier that is special
  109. during preprocessing is @code{defined} (@pxref{defined}).
  110. @item preprocessing number
  111. @cindex numbers, preprocessing
  112. @cindex preprocessing numbers
  113. A @dfn{preprocessing number} is something that preprocessing treats
  114. textually as a number, including C numeric constants, and other
  115. sequences of characters which resemble numeric constants.
  116. Preprocessing does not try to verify that a preprocessing number is a
  117. valid number in C, and indeed it need not be one.
  118. More precisely, preprocessing numbers begin with an optional period, a
  119. required decimal digit, and then continue with any sequence of
  120. letters, digits, underscores, periods, and exponents. Exponents are
  121. the two-character sequences @samp{e+}, @samp{e-}, @samp{E+},
  122. @samp{E-}, @samp{p+}, @samp{p-}, @samp{P+}, and @samp{P-}. (The
  123. exponents that begin with @samp{p} or @samp{P} are new to C99. They
  124. are used for hexadecimal floating-point constants.)
  125. The reason behind this unusual syntactic class is that the full
  126. complexity of numeric constants is irrelevant during preprocessing.
  127. The distinction between lexically valid and invalid floating-point
  128. numbers, for example, doesn't matter at this stage. The use of
  129. preprocessing numbers makes it possible to split an identifier at any
  130. position and get exactly two tokens, and reliably paste them together
  131. using the @code{##} operator (@pxref{Concatenation}).
  132. @item punctuator
  133. A @dfn{punctuator} is syntactically like an operator.
  134. These are the valid punctuators:
  135. @example
  136. [ ] ( ) @{ @} . ->
  137. ++ -- & * + - ~ !
  138. / % << >> < > <= >= == != ^ | && ||
  139. ? : ; ...
  140. = *= /= %= += -= <<= >>= &= ^= |=
  141. , # ##
  142. <: :> <% %> %: %:%:
  143. @end example
  144. @item string constant
  145. A string constant in the source code is recognized by preprocessing as
  146. a single preprocessing token.
  147. @item character constant
  148. A character constant in the source code is recognized by preprocessing
  149. as a single preprocessing token.
  150. @item header name
  151. Within the @code{#include} directive, preprocessing recognizes a
  152. @dfn{header name} token. It consists of @samp{"@var{name}"}, where
  153. @var{name} is a sequence of source characters other than newline and
  154. @samp{"}, or @samp{<@var{name}>}, where @var{name} is a sequence of
  155. source characters other than newline and @samp{>}.
  156. In practice, it is more convenient to think that the @code{#include} line
  157. is exempt from tokenization.
  158. @item other
  159. Any other character that's valid in a C source program
  160. is treated as a separate preprocessing token.
  161. @end table
  162. Once the program is broken into preprocessing tokens, they remain
  163. separate until the end of preprocessing. Macros that generate two
  164. consecutive tokens insert whitespace to keep them separate, if
  165. necessary. For example,
  166. @example
  167. @group
  168. #define foo() bar
  169. foo()baz
  170. @expansion{} bar baz
  171. @emph{not}
  172. @expansion{} barbaz
  173. @end group
  174. @end example
  175. The only exception is with the @code{##} preprocessing operator, which
  176. pastes tokens together (@pxref{Concatenation}).
  177. Preprocessing treats the null character (code 0) as whitespace, but
  178. generates a warning for it because it may be invisible to the user
  179. (many terminals do not display it at all) and its presence in the file
  180. is probably a mistake.
  181. @node Header Files
  182. @section Header Files
  183. @cindex header file
  184. A header file is a file of C code, typically containing C declarations
  185. and macro definitions (@pxref{Macros}), to be shared between several
  186. source files. You request the use of a header file in your program by
  187. @dfn{including} it, with the C preprocessing directive
  188. @code{#include}.
  189. Header files serve two purposes.
  190. @itemize @bullet
  191. @item
  192. @cindex system header files
  193. System header files declare the interfaces to parts of the operating
  194. system. You include them in your program to supply the definitions and
  195. declarations that you need to invoke system calls and libraries.
  196. @item
  197. Program-specific header files contain declarations for interfaces between the
  198. source files of a particular program. It is a good idea to create a header
  199. file for related declarations and macro definitions if all or most of them
  200. are needed in several different source files.
  201. @end itemize
  202. Including a header file produces the same results as copying the header
  203. file into each source file that needs it. Such copying would be
  204. time-consuming and error-prone. With a header file, the related
  205. declarations appear in only one place. If they need to be changed, you
  206. can change them in one place, and programs that include the header file
  207. will then automatically use the new version when next recompiled. The header
  208. file eliminates the labor of finding and changing all the copies as well
  209. as the risk that a failure to change one copy will result in
  210. inconsistencies within a program.
  211. In C, the usual convention is to give header files names that end with
  212. @file{.h}. It is most portable to use only letters, digits, dashes, and
  213. underscores in header file names, and at most one dot.
  214. @menu
  215. * include Syntax::
  216. * include Operation::
  217. * Search Path::
  218. * Once-Only Headers::
  219. @c * Alternatives to Wrapper #ifndef::
  220. * Computed Includes::
  221. @c * Wrapper Headers::
  222. @c * System Headers::
  223. @end menu
  224. @node include Syntax
  225. @subsection @code{#include} Syntax
  226. @findex #include
  227. You can specify inclusion of user and system header files with the
  228. preprocessing directive @code{#include}. It has two variants:
  229. @table @code
  230. @item #include <@var{file}>
  231. This variant is used for system header files. It searches for a file
  232. named @var{file} in a standard list of system directories. You can
  233. prepend directories to this list with the @option{-I} option
  234. (@pxref{Invocation, Invoking GCC, Invoking GCC, gcc, Using the GNU
  235. Compiler Collection}).
  236. @item #include "@var{file}"
  237. This variant is used for header files of your own program. It
  238. searches for a file named @var{file} first in the directory containing
  239. the current file, then in the quote directories, then the same
  240. directories used for @code{<@var{file}>}. You can prepend directories
  241. to the list of quote directories with the @option{-iquote} option.
  242. @end table
  243. The argument of @code{#include}, whether delimited with quote marks or
  244. angle brackets, behaves like a string constant in that comments are not
  245. recognized, and macro names are not expanded. Thus, @code{@w{#include
  246. <x/*y>}} specifies inclusion of a system header file named @file{x/*y}.
  247. However, if backslashes occur within @var{file}, they are considered
  248. ordinary text characters, not escape characters: character escape
  249. sequences such as used in string constants in C are not meaningful
  250. here. Thus, @code{@w{#include "x\n\\y"}} specifies a filename
  251. containing three backslashes. By the same token, there is no way to
  252. escape @samp{"} or @samp{>} to include it in the header file name if
  253. it would instead end the file name.
  254. Some systems interpret @samp{\} as a file name component separator.
  255. All these systems also interpret @samp{/} the same way. It is most
  256. portable to use only @samp{/}.
  257. It is an error to put anything other than comments on the
  258. @code{#include} line after the file name.
  259. @node include Operation
  260. @subsection @code{#include} Operation
  261. The @code{#include} directive works by scanning the specified header
  262. file as input before continuing with the rest of the current file.
  263. The result of preprocessing consists of the text already generated,
  264. followed by the result of preprocessing the included file, followed by
  265. whatever results from the text after the @code{#include} directive.
  266. For example, if you have a header file @file{header.h} as follows,
  267. @example
  268. char *test (void);
  269. @end example
  270. @noindent
  271. and a main program called @file{program.c} that uses the header file,
  272. like this,
  273. @example
  274. int x;
  275. #include "header.h"
  276. int
  277. main (void)
  278. @{
  279. puts (test ());
  280. @}
  281. @end example
  282. @noindent
  283. the result is equivalent to putting this text in @file{program.c}:
  284. @example
  285. int x;
  286. char *test (void);
  287. int
  288. main (void)
  289. @{
  290. puts (test ());
  291. @}
  292. @end example
  293. Included files are not limited to declarations and macro definitions;
  294. those are merely the typical uses. Any fragment of a C program can be
  295. included from another file. The include file could even contain the
  296. beginning of a statement that is concluded in the containing file, or
  297. the end of a statement that was started in the including file. However,
  298. an included file must consist of complete tokens. Comments and string
  299. literals that have not been closed by the end of an included file are
  300. invalid. For error recovery, the compiler terminates them at the end of
  301. the file.
  302. To avoid confusion, it is best if header files contain only complete
  303. syntactic units---function declarations or definitions, type
  304. declarations, etc.
  305. The line following the @code{#include} directive is always treated as
  306. a separate line, even if the included file lacks a final newline.
  307. There is no problem putting a preprocessing directive there.
  308. @node Search Path
  309. @subsection Search Path
  310. GCC looks in several different places for header files to be included.
  311. On the GNU system, and Unix systems, the default directories for
  312. system header files are:
  313. @example
  314. @var{libdir}/gcc/@var{target}/@var{version}/include
  315. /usr/local/include
  316. @var{libdir}/gcc/@var{target}/@var{version}/include-fixed
  317. @var{libdir}/@var{target}/include
  318. /usr/include/@var{target}
  319. /usr/include
  320. @end example
  321. @noindent
  322. The list may be different in some operating systems. Other
  323. directories are added for C++.
  324. In the above, @var{target} is the canonical name of the system GCC was
  325. configured to compile code for; often but not always the same as the
  326. canonical name of the system it runs on. @var{version} is the version
  327. of GCC in use.
  328. You can add to this list with the @option{-I@var{dir}} command-line
  329. option. All the directories named by @option{-I} are searched, in
  330. left-to-right order, @emph{before} the default directories. The only
  331. exception is when @file{dir} is already searched by default. In
  332. this case, the option is ignored and the search order for system
  333. directories remains unchanged.
  334. Duplicate directories are removed from the quote and bracket search
  335. chains before the two chains are merged to make the final search chain.
  336. Thus, it is possible for a directory to occur twice in the final search
  337. chain if it was specified in both the quote and bracket chains.
  338. You can prevent GCC from searching any of the default directories with
  339. the @option{-nostdinc} option. This is useful when you are compiling an
  340. operating system kernel or some other program that does not use the
  341. standard C library facilities, or the standard C library itself.
  342. @option{-I} options are not ignored as described above when
  343. @option{-nostdinc} is in effect.
  344. GCC looks for headers requested with @code{@w{#include "@var{file}"}}
  345. first in the directory containing the current file, then in the
  346. @dfn{quote directories} specified by @option{-iquote} options, then in
  347. the same places it looks for a system header. For example, if
  348. @file{/usr/include/sys/stat.h} contains @code{@w{#include "types.h"}},
  349. GCC looks for @file{types.h} first in @file{/usr/include/sys}, then in
  350. the quote directories and then in its usual search path.
  351. @code{#line} (@pxref{Line Control}) does not change GCC's idea of the
  352. directory containing the current file.
  353. @cindex quote directories
  354. The @option{-I-} is an old-fashioned, deprecated way to specify the
  355. quote directories. To look for headers in a directory named @file{-},
  356. specify @option{-I./-}. There are several more ways to adjust the
  357. header search path. @xref{invocation, Invoking GCC, Invoking GCC,
  358. gcc, Using the GNU Compiler Collection}.
  359. @node Once-Only Headers
  360. @subsection Once-Only Headers
  361. @cindex repeated inclusion
  362. @cindex including just once
  363. @cindex wrapper @code{#ifndef}
  364. If a header file happens to be included twice, the compiler will process
  365. its contents twice. This is very likely to cause an error, e.g.@: when the
  366. compiler sees the same structure definition twice.
  367. The standard way to prevent this is to enclose the entire real contents
  368. of the file in a conditional, like this:
  369. @example
  370. @group
  371. /* File foo. */
  372. #ifndef FILE_FOO_SEEN
  373. #define FILE_FOO_SEEN
  374. @var{the entire file}
  375. #endif /* !FILE_FOO_SEEN */
  376. @end group
  377. @end example
  378. This construct is commonly known as a @dfn{wrapper #ifndef}. When the
  379. header is included again, the conditional will be false, because
  380. @code{FILE_FOO_SEEN} is defined. Preprocessing skips over the entire
  381. contents of the file, so that compilation will never ``see'' the file
  382. contents twice in one module.
  383. GCC optimizes this case even further. It remembers when a header file
  384. has a wrapper @code{#ifndef}. If a subsequent @code{#include}
  385. specifies that header, and the macro in the @code{#ifndef} is still
  386. defined, it does not bother to rescan the file at all.
  387. You can put comments in the header file outside the wrapper. They
  388. do not interfere with this optimization.
  389. @cindex controlling macro
  390. @cindex guard macro
  391. The macro @code{FILE_FOO_SEEN} is called the @dfn{controlling macro}
  392. or @dfn{guard macro}. In a user header file, the macro name should
  393. not begin with @samp{_}. In a system header file, it should begin
  394. with @samp{__} (or @samp{_} followed by an upper-case letter) to avoid
  395. conflicts with user programs. In any kind of header file, the macro
  396. name should contain the name of the file and some additional text, to
  397. avoid conflicts with other header files.
  398. @node Computed Includes
  399. @subsection Computed Includes
  400. @cindex computed includes
  401. @cindex macros in include
  402. Sometimes it is necessary to select one of several different header
  403. files to be included into your program. They might specify
  404. configuration parameters to be used on different sorts of operating
  405. systems, for instance. You could do this with a series of conditionals,
  406. @example
  407. #if SYSTEM_1
  408. # include "system_1.h"
  409. #elif SYSTEM_2
  410. # include "system_2.h"
  411. #elif SYSTEM_3
  412. /* @r{@dots{}} */
  413. #endif
  414. @end example
  415. That rapidly becomes tedious. Instead, GNU C offers the ability to use
  416. a macro for the header name. This is called a @dfn{computed include}.
  417. Instead of writing a header name as the direct argument of
  418. @code{#include}, you simply put a macro name there instead:
  419. @example
  420. #define SYSTEM_H "system_1.h"
  421. /* @r{@dots{}} */
  422. #include SYSTEM_H
  423. @end example
  424. @noindent
  425. @code{SYSTEM_H} is expanded, then @file{system_1.h} is included as if
  426. the @code{#include} had been written with that name. @code{SYSTEM_H}
  427. could be defined by your Makefile with a @option{-D} option.
  428. You must be careful when you define such a macro. @code{#define}
  429. saves tokens, not text. GCC has no way of knowing that the macro will
  430. be used as the argument of @code{#include}, so it generates ordinary
  431. tokens, not a header name. This is unlikely to cause problems if you
  432. use double-quote includes, which are syntactically similar to string
  433. constants. If you use angle brackets, however, you may have trouble.
  434. The syntax of a computed include is actually a bit more general than the
  435. above. If the first non-whitespace character after @code{#include} is
  436. not @samp{"} or @samp{<}, then the entire line is macro-expanded
  437. like running text would be.
  438. If the line expands to a single string constant, the contents of that
  439. string constant are the file to be included. Preprocessing does not
  440. re-examine the string for embedded quotes, but neither does it process
  441. backslash escapes in the string. Therefore
  442. @example
  443. #define HEADER "a\"b"
  444. #include HEADER
  445. @end example
  446. @noindent
  447. looks for a file named @file{a\"b}. Preprocessing searches for the
  448. file according to the rules for double-quoted includes.
  449. If the line expands to a token stream beginning with a @samp{<} token
  450. and including a @samp{>} token, then the tokens between the @samp{<} and
  451. the first @samp{>} are combined to form the filename to be included.
  452. Any whitespace between tokens is reduced to a single space; then any
  453. space after the initial @samp{<} is retained, but a trailing space
  454. before the closing @samp{>} is ignored. Preprocessing searches for the file
  455. according to the rules for angle-bracket includes.
  456. In either case, if there are any tokens on the line after the file name,
  457. an error occurs and the directive is not processed. It is also an error
  458. if the result of expansion does not match either of the two expected
  459. forms.
  460. These rules are implementation-defined behavior according to the C
  461. standard. To minimize the risk of different compilers interpreting your
  462. computed includes differently, we recommend you use only a single
  463. object-like macro that expands to a string constant. That also
  464. makes it clear to people reading your program.
  465. @node Macros
  466. @section Macros
  467. @cindex macros
  468. A @dfn{macro} is a fragment of code that has been given a name.
  469. Whenever the name is used, it is replaced by the contents of the macro.
  470. There are two kinds of macros. They differ mostly in what they look
  471. like when they are used. @dfn{Object-like} macros resemble data objects
  472. when used, @dfn{function-like} macros resemble function calls.
  473. You may define any valid identifier as a macro, even if it is a C
  474. keyword. In the preprocessing stage, GCC does not know anything about
  475. keywords. This can be useful if you wish to hide a keyword such as
  476. @code{const} from an older compiler that does not understand it.
  477. However, the preprocessing operator @code{defined} (@pxref{defined})
  478. can never be defined as a macro, and C@code{++}'s named operators
  479. (@pxref{C++ Named Operators, C++ Named Operators, C++ Named Operators,
  480. gcc, Using the GNU Compiler Collection}) cannot be macros when
  481. compiling C@code{++} code.
  482. The operator @code{#} is used in macros for stringification of an
  483. argument (@pxref{Stringification}), and @code{##} is used for
  484. concatenation of arguments into larger tokens (@pxref{Concatenation})
  485. @menu
  486. * Object-like Macros::
  487. * Function-like Macros::
  488. @c * Macro Pragmas::
  489. * Macro Arguments::
  490. * Stringification::
  491. * Concatenation::
  492. * Variadic Macros::
  493. * Predefined Macros::
  494. * Undefining and Redefining Macros::
  495. * Directives Within Macro Arguments::
  496. * Macro Pitfalls::
  497. @end menu
  498. @node Object-like Macros
  499. @subsection Object-like Macros
  500. @cindex object-like macro
  501. @cindex symbolic constants
  502. @cindex manifest constants
  503. An @dfn{object-like macro} is a simple identifier that will be
  504. replaced by a code fragment. It is called object-like because in most
  505. cases the use of the macro looks like reference to a data object in
  506. code that uses it. These macros are most commonly used to give
  507. symbolic names to numeric constants.
  508. @findex #define
  509. The way to define macros with the @code{#define} directive.
  510. @code{#define} is followed by the name of the macro and then the token
  511. sequence it should be an abbreviation for, which is variously referred
  512. to as the macro's @dfn{body}, @dfn{expansion} or @dfn{replacement
  513. list}. For example,
  514. @example
  515. #define BUFFER_SIZE 1024
  516. @end example
  517. @noindent
  518. defines a macro named @code{BUFFER_SIZE} as an abbreviation for the
  519. token @code{1024}. If somewhere after this @code{#define} directive
  520. there comes a C statement of the form
  521. @example
  522. foo = (char *) malloc (BUFFER_SIZE);
  523. @end example
  524. @noindent
  525. then preprocessing will recognize and @dfn{expand} the macro
  526. @code{BUFFER_SIZE}, so that compilation will see the tokens:
  527. @example
  528. foo = (char *) malloc (1024);
  529. @end example
  530. By convention, macro names are written in upper case. Programs are
  531. easier to read when it is possible to tell at a glance which names are
  532. macros. Macro names that start with @samp{__} are reserved for
  533. internal uses, and many of them are defined automatically, so don't
  534. define such macro names unless you really know what you're doing.
  535. Likewise for macro names that start with @samp{_} and an upper-case letter.
  536. The macro's body ends at the end of the @code{#define} line. You may
  537. continue the definition onto multiple lines, if necessary, using
  538. backslash-newline. When the macro is expanded, however, it will all
  539. come out on one line. For example,
  540. @example
  541. #define NUMBERS 1, \
  542. 2, \
  543. 3
  544. int x[] = @{ NUMBERS @};
  545. @expansion{} int x[] = @{ 1, 2, 3 @};
  546. @end example
  547. @noindent
  548. The most common visible consequence of this is surprising line numbers
  549. in error messages.
  550. There is no restriction on what can go in a macro body provided it
  551. decomposes into valid preprocessing tokens. Parentheses need not
  552. balance, and the body need not resemble valid C code. (If it does not,
  553. you may get error messages from the C compiler when you use the macro.)
  554. Preprocessing scans the program sequentially. A macro definition
  555. takes effect right after its appearance. Therefore, the following
  556. input
  557. @example
  558. foo = X;
  559. #define X 4
  560. bar = X;
  561. @end example
  562. @noindent
  563. produces
  564. @example
  565. foo = X;
  566. bar = 4;
  567. @end example
  568. When preprocessing expands a macro name, the macro's expansion
  569. replaces the macro invocation, then the expansion is examined for more
  570. macros to expand. For example,
  571. @example
  572. @group
  573. #define TABLESIZE BUFSIZE
  574. #define BUFSIZE 1024
  575. TABLESIZE
  576. @expansion{} BUFSIZE
  577. @expansion{} 1024
  578. @end group
  579. @end example
  580. @noindent
  581. @code{TABLESIZE} is expanded first to produce @code{BUFSIZE}, then that
  582. macro is expanded to produce the final result, @code{1024}.
  583. Notice that @code{BUFSIZE} was not defined when @code{TABLESIZE} was
  584. defined. The @code{#define} for @code{TABLESIZE} uses exactly the
  585. expansion you specify---in this case, @code{BUFSIZE}---and does not
  586. check to see whether it too contains macro names. Only when you
  587. @emph{use} @code{TABLESIZE} is the result of its expansion scanned for
  588. more macro names.
  589. This makes a difference if you change the definition of @code{BUFSIZE}
  590. at some point in the source file. @code{TABLESIZE}, defined as shown,
  591. will always expand using the definition of @code{BUFSIZE} that is
  592. currently in effect:
  593. @example
  594. #define BUFSIZE 1020
  595. #define TABLESIZE BUFSIZE
  596. #undef BUFSIZE
  597. #define BUFSIZE 37
  598. @end example
  599. @noindent
  600. Now @code{TABLESIZE} expands (in two stages) to @code{37}.
  601. If the expansion of a macro contains its own name, either directly or
  602. via intermediate macros, it is not expanded again when the expansion is
  603. examined for more macros. This prevents infinite recursion.
  604. @xref{Self-Referential Macros}, for the precise details.
  605. @node Function-like Macros
  606. @subsection Function-like Macros
  607. @cindex function-like macros
  608. You can also define macros whose use looks like a function call.
  609. These are called @dfn{function-like macros}. To define one, use the
  610. @code{#define} directive with a pair of parentheses immediately after
  611. the macro name. For example,
  612. @example
  613. #define lang_init() c_init()
  614. lang_init()
  615. @expansion{} c_init()
  616. @end example
  617. A function-like macro is expanded only when its name appears with a
  618. pair of parentheses after it. If you write just the name, without
  619. parentheses, it is left alone. This can be useful when you have a
  620. function and a macro of the same name, and you wish to use the
  621. function sometimes. Whitespace and line breaks before or between the
  622. parentheses are ignored when the macro is called.
  623. @example
  624. extern void foo(void);
  625. #define foo() /* @r{optimized inline version} */
  626. /* @r{@dots{}} */
  627. foo();
  628. funcptr = foo;
  629. @end example
  630. Here the call to @code{foo()} expands the macro, but the function
  631. pointer @code{funcptr} gets the address of the real function
  632. @code{foo}. If the macro were to be expanded there, it would cause a
  633. syntax error.
  634. If you put spaces between the macro name and the parentheses in the
  635. macro definition, that does not define a function-like macro, it defines
  636. an object-like macro whose expansion happens to begin with a pair of
  637. parentheses. Here is an example:
  638. @example
  639. #define lang_init () c_init()
  640. lang_init()
  641. @expansion{} () c_init()()
  642. @end example
  643. The first two pairs of parentheses in this expansion come from the
  644. macro. The third is the pair that was originally after the macro
  645. invocation. Since @code{lang_init} is an object-like macro, it does not
  646. consume those parentheses.
  647. Any name can have at most one macro definition at a time. Thus,
  648. you can't define the same name as an object-like macro and a
  649. function-like macro at once.
  650. @node Macro Arguments
  651. @subsection Macro Arguments
  652. @cindex arguments
  653. @cindex macros with arguments
  654. @cindex arguments in macro definitions
  655. Function-like macros can take @dfn{arguments}, just like true functions.
  656. To define a macro that uses arguments, you insert @dfn{parameters}
  657. between the pair of parentheses in the macro definition that make the
  658. macro function-like. The parameters must be valid C identifiers,
  659. separated by commas and optionally whitespace.
  660. To invoke a macro that takes arguments, you write the name of the macro
  661. followed by a list of @dfn{actual arguments} in parentheses, separated
  662. by commas. The invocation of the macro need not be restricted to a
  663. single logical line---it can cross as many lines in the source file as
  664. you wish. The number of arguments you give must match the number of
  665. parameters in the macro definition. When the macro is expanded, each
  666. use of a parameter in its body is replaced by the tokens of the
  667. corresponding argument. (The macro body is not required to use all of the
  668. parameters.)
  669. As an example, here is a macro that computes the minimum of two numeric
  670. values, as it is defined in many C programs, and some uses.
  671. @example
  672. #define min(X, Y) ((X) < (Y) ? (X) : (Y))
  673. x = min(a, b); @expansion{} x = ((a) < (b) ? (a) : (b));
  674. y = min(1, 2); @expansion{} y = ((1) < (2) ? (1) : (2));
  675. z = min(a+28, *p); @expansion{} z = ((a+28) < (*p) ? (a+28) : (*p));
  676. @end example
  677. @noindent
  678. In this small example you can already see several of the dangers of
  679. macro arguments. @xref{Macro Pitfalls}, for detailed explanations.
  680. Leading and trailing whitespace in each argument is dropped, and all
  681. whitespace between the tokens of an argument is reduced to a single
  682. space. Parentheses within each argument must balance; a comma within
  683. such parentheses does not end the argument. However, there is no
  684. requirement for square brackets or braces to balance, and they do not
  685. prevent a comma from separating arguments. Thus,
  686. @example
  687. macro (array[x = y, x + 1])
  688. @end example
  689. @noindent
  690. passes two arguments to @code{macro}: @code{array[x = y} and @code{x +
  691. 1]}. If you want to supply @code{array[x = y, x + 1]} as an argument,
  692. you can write it as @code{array[(x = y, x + 1)]}, which is equivalent C
  693. code. However, putting an assignment inside an array subscript
  694. is to be avoided anyway.
  695. All arguments to a macro are completely macro-expanded before they are
  696. substituted into the macro body. After substitution, the complete text
  697. is scanned again for macros to expand, including the arguments. This rule
  698. may seem strange, but it is carefully designed so you need not worry
  699. about whether any function call is actually a macro invocation. You can
  700. run into trouble if you try to be too clever, though. @xref{Argument
  701. Prescan}, for detailed discussion.
  702. For example, @code{min (min (a, b), c)} is first expanded to
  703. @example
  704. min (((a) < (b) ? (a) : (b)), (c))
  705. @end example
  706. @noindent
  707. and then to
  708. @example
  709. @group
  710. ((((a) < (b) ? (a) : (b))) < (c)
  711. ? (((a) < (b) ? (a) : (b)))
  712. : (c))
  713. @end group
  714. @end example
  715. @noindent
  716. (The line breaks shown here for clarity are not actually generated.)
  717. @cindex empty macro arguments
  718. You can leave macro arguments empty without error, but many macros
  719. will then expand to invalid code. You cannot leave out arguments
  720. entirely; if a macro takes two arguments, there must be exactly one
  721. comma at the top level of its argument list. Here are some silly
  722. examples using @code{min}:
  723. @smallexample
  724. min(, b) @expansion{} (( ) < (b) ? ( ) : (b))
  725. min(a, ) @expansion{} ((a ) < ( ) ? (a ) : ( ))
  726. min(,) @expansion{} (( ) < ( ) ? ( ) : ( ))
  727. min((,),) @expansion{} (((,)) < ( ) ? ((,)) : ( ))
  728. min() @error{} macro "min" requires 2 arguments, but only 1 given
  729. min(,,) @error{} macro "min" passed 3 arguments, but takes just 2
  730. @end smallexample
  731. Whitespace is not a preprocessing token, so if a macro @code{foo} takes
  732. one argument, @code{@w{foo ()}} and @code{@w{foo ( )}} both supply it an
  733. empty argument.
  734. @ignore @c How long ago was this?
  735. Previous GNU preprocessor implementations and
  736. documentation were incorrect on this point, insisting that a
  737. function-like macro that takes a single argument be passed a space if an
  738. empty argument was required.
  739. @end ignore
  740. Macro parameters appearing inside string literals are not replaced by
  741. their corresponding actual arguments.
  742. @example
  743. #define foo(x) x, "x"
  744. foo(bar) @expansion{} bar, "x"
  745. @end example
  746. @noindent
  747. See the next subsection for how to insert macro arguments
  748. into a string literal.
  749. The token following the macro call and the last token of the macro
  750. expansion do not become one token even if it looks like they could:
  751. @example
  752. #define foo() abc
  753. foo()def @expansion{} abc def
  754. @end example
  755. @node Stringification
  756. @subsection Stringification
  757. @cindex stringification
  758. @cindex @code{#} operator
  759. Sometimes you may want to convert a macro argument into a string
  760. constant. Parameters are not replaced inside string constants, but
  761. you can use the @code{#} preprocessing operator instead. When a macro
  762. parameter is used with a leading @code{#}, preprocessing replaces it
  763. with the literal text of the actual argument, converted to a string
  764. constant. Unlike normal parameter replacement, the argument is not
  765. macro-expanded first. This is called @dfn{stringification}.
  766. There is no way to combine an argument with surrounding text and
  767. stringify it all together. But you can write a series of string
  768. constants and stringified arguments. After preprocessing replaces the
  769. stringified arguments with string constants, the consecutive string
  770. constants will be concatenated into one long string constant
  771. (@pxref{String Constants}).
  772. Here is an example that uses stringification and concatenation of
  773. string constants:
  774. @example
  775. @group
  776. #define WARN_IF(EXP) \
  777. do @{ if (EXP) \
  778. fprintf (stderr, "Warning: " #EXP "\n"); @} \
  779. while (0)
  780. WARN_IF (x == 0);
  781. @expansion{}
  782. do @{ if (x == 0)
  783. fprintf (stderr, "Warning: " "x == 0" "\n"); @}
  784. while (0);
  785. @end group
  786. @end example
  787. @noindent
  788. The argument for @code{EXP} is substituted once, as is, into the
  789. @code{if} statement, and once, stringified, into the argument to
  790. @code{fprintf}. If @code{x} were a macro, it would be expanded in the
  791. @code{if} statement but not in the string.
  792. The @code{do} and @code{while (0)} are a kludge to make it possible to
  793. write @code{WARN_IF (@var{arg});}. The resemblance of @code{WARN_IF}
  794. to a function makes that a natural way to write it.
  795. @xref{Swallowing the Semicolon}.
  796. Stringification in C involves more than putting double-quote
  797. characters around the fragment. It also backslash-escapes the quotes
  798. surrounding embedded string constants, and all backslashes within
  799. string and character constants, in order to get a valid C string
  800. constant with the proper contents. Thus, stringifying @code{@w{p =
  801. "foo\n";}} results in @t{@w{"p = \"foo\\n\";"}}. However, backslashes
  802. that are not inside string or character constants are not duplicated:
  803. @samp{\n} by itself stringifies to @t{"\n"}.
  804. All leading and trailing whitespace in text being stringified is
  805. ignored. Any sequence of whitespace in the middle of the text is
  806. converted to a single space in the stringified result. Comments are
  807. replaced by whitespace long before stringification happens, so they
  808. never appear in stringified text.
  809. There is no way to convert a macro argument into a character constant.
  810. To stringify the result of expansion of a macro argument, you have to
  811. use two levels of macros, like this:
  812. @example
  813. #define xstr(S) str(S)
  814. #define str(s) #s
  815. #define foo 4
  816. str (foo)
  817. @expansion{} "foo"
  818. xstr (foo)
  819. @expansion{} xstr (4)
  820. @expansion{} str (4)
  821. @expansion{} "4"
  822. @end example
  823. @code{s} is stringified when it is used in @code{str}, so it is not
  824. macro-expanded first. But @code{S} is an ordinary argument to
  825. @code{xstr}, so it is completely macro-expanded before @code{xstr}
  826. itself is expanded (@pxref{Argument Prescan}). Therefore, by the time
  827. @code{str} gets to its argument text, that text already been
  828. macro-expanded.
  829. @node Concatenation
  830. @subsection Concatenation
  831. @cindex concatenation
  832. @cindex token pasting
  833. @cindex token concatenation
  834. @cindex @code{##} operator
  835. It is often useful to merge two tokens into one while expanding macros.
  836. This is called @dfn{token pasting} or @dfn{token concatenation}. The
  837. @code{##} preprocessing operator performs token pasting. When a macro
  838. is expanded, the two tokens on either side of each @code{##} operator
  839. are combined into a single token, which then replaces the @code{##} and
  840. the two original tokens in the macro expansion. Usually both will be
  841. identifiers, or one will be an identifier and the other a preprocessing
  842. number. When pasted, they make a longer identifier.
  843. Concatenation into an identifier isn't the only valid case. It is
  844. also possible to concatenate two numbers (or a number and a name, such
  845. as @code{1.5} and @code{e3}) into a number. Also, multi-character
  846. operators such as @code{+=} can be formed by token pasting.
  847. However, two tokens that don't together form a valid token cannot be
  848. pasted together. For example, you cannot concatenate @code{x} with
  849. @code{+}, not in either order. Trying this issues a warning and keeps
  850. the two tokens separate. Whether it puts white space between the
  851. tokens is undefined. It is common to find unnecessary uses of
  852. @code{##} in complex macros. If you get this warning, it is likely
  853. that you can simply remove the @code{##}.
  854. The tokens combined by @code{##} could both come from the macro body,
  855. but then you could just as well write them as one token in the first place.
  856. Token pasting is useful when one or both of the tokens comes from a
  857. macro argument. If either of the tokens next to an @code{##} is a
  858. parameter name, it is replaced by its actual argument before @code{##}
  859. executes. As with stringification, the actual argument is not
  860. macro-expanded first. If the argument is empty, that @code{##} has no
  861. effect.
  862. Keep in mind that preprocessing converts comments to whitespace before
  863. it looks for uses of macros. Therefore, you cannot create a comment
  864. by concatenating @samp{/} and @samp{*}. You can put as much
  865. whitespace between @code{##} and its operands as you like, including
  866. comments, and you can put comments in arguments that will be
  867. concatenated.
  868. It is an error to use @code{##} at the beginning or end of a macro
  869. body.
  870. Multiple @code{##} operators are handled left-to-right, so that
  871. @samp{1 ## e ## -2} pastes into @samp{1e-2}. (Right-to-left
  872. processing would first generate @samp{e-2}, which is an invalid token.)
  873. When @code{#} and @code{##} are used together, they are all handled
  874. left-to-right.
  875. Consider a C program that interprets named commands. There probably
  876. needs to be a table of commands, perhaps an array of structures declared
  877. as follows:
  878. @example
  879. @group
  880. struct command
  881. @{
  882. char *name;
  883. void (*function) (void);
  884. @};
  885. @end group
  886. @group
  887. struct command commands[] =
  888. @{
  889. @{ "quit", quit_command @},
  890. @{ "help", help_command @},
  891. /* @r{@dots{}} */
  892. @};
  893. @end group
  894. @end example
  895. It would be cleaner not to have to write each command name twice, once
  896. in the string constant and once in the function name. A macro that
  897. takes the name of a command as an argument can make this unnecessary.
  898. It can create the string constant with stringification, and the
  899. function name by concatenating the argument with @samp{_command}.
  900. Here is how it is done:
  901. @example
  902. #define COMMAND(NAME) @{ #NAME, NAME ## _command @}
  903. struct command commands[] =
  904. @{
  905. COMMAND (quit),
  906. COMMAND (help),
  907. /* @r{@dots{}} */
  908. @};
  909. @end example
  910. @node Variadic Macros
  911. @subsection Variadic Macros
  912. @cindex variable number of arguments
  913. @cindex macros with variable arguments
  914. @cindex variadic macros
  915. A macro can be declared to accept a variable number of arguments much as
  916. a function can. The syntax for defining the macro is similar to that of
  917. a function. Here is an example:
  918. @example
  919. #define eprintf(@dots{}) fprintf (stderr, __VA_ARGS__)
  920. @end example
  921. This kind of macro is called @dfn{variadic}. When the macro is invoked,
  922. all the tokens in its argument list after the last named argument (this
  923. macro has none), including any commas, become the @dfn{variable
  924. argument}. This sequence of tokens replaces the identifier
  925. @code{@w{__VA_ARGS__}} in the macro body wherever it appears. Thus, we
  926. have this expansion:
  927. @example
  928. eprintf ("%s:%d: ", input_file, lineno)
  929. @expansion{} fprintf (stderr, "%s:%d: ", input_file, lineno)
  930. @end example
  931. The variable argument is completely macro-expanded before it is inserted
  932. into the macro expansion, just like an ordinary argument. You may use
  933. the @code{#} and @code{##} operators to stringify the variable argument
  934. or to paste its leading or trailing token with another token. (But see
  935. below for an important special case for @code{##}.)
  936. @strong{Warning:} don't use the identifier @code{@w{__VA_ARGS__}}
  937. for anything other than this.
  938. If your macro is complicated, you may want a more descriptive name for
  939. the variable argument than @code{@w{__VA_ARGS__}}. You can write an
  940. argument name immediately before the @samp{@dots{}}; that name is used
  941. for the variable argument.@footnote{GNU C extension.} The
  942. @code{eprintf} macro above could be written thus:
  943. @example
  944. #define eprintf(args@dots{}) fprintf (stderr, args)
  945. @end example
  946. A variadic macro can have named arguments as well as variable
  947. arguments, so @code{eprintf} can be defined like this, instead:
  948. @example
  949. #define eprintf(format, @dots{}) \
  950. fprintf (stderr, format, __VA_ARGS__)
  951. @end example
  952. @noindent
  953. This formulation is more descriptive, but what if you want to specify
  954. a format string that takes no arguments? In GNU C, you can omit the
  955. comma before the variable arguments if they are empty, but that puts
  956. an extra comma in the expansion:
  957. @example
  958. eprintf ("success!\n")
  959. @expansion{} fprintf(stderr, "success!\n", );
  960. @end example
  961. @noindent
  962. That's an error in the call to @code{fprintf}.
  963. To get rid of that comma, the @code{##} token paste operator has a
  964. special meaning when placed between a comma and a variable
  965. argument.@footnote{GNU C extension.} If you write
  966. @example
  967. #define eprintf(format, @dots{}) \
  968. fprintf (stderr, format, ##__VA_ARGS__)
  969. @end example
  970. @noindent
  971. then use the macro @code{eprintf} with empty variable arguments,
  972. @code{##} deletes the preceding comma.
  973. @example
  974. eprintf ("success!\n")
  975. @expansion{} fprintf(stderr, "success!\n");
  976. @end example
  977. @noindent
  978. This does @emph{not} happen if you pass an empty argument, nor does it
  979. happen if the token preceding @code{##} is anything other than a
  980. comma.
  981. @noindent
  982. When the only macro parameter is a variable arguments parameter, and
  983. the macro call has no argument at all, it is not obvious whether that
  984. means an empty argument or a missing argument. Should the comma be
  985. kept, or deleted? The C standard says to keep the comma, but the
  986. preexisting GNU C extension deleted the comma. Nowadays, GNU C
  987. retains the comma when implementing a specific C standard, and deletes
  988. it otherwise.
  989. C99 mandates that the only place the identifier @code{@w{__VA_ARGS__}}
  990. can appear is in the replacement list of a variadic macro. It may not
  991. be used as a macro name, macro parameter name, or within a different
  992. type of macro. It may also be forbidden in open text; the standard is
  993. ambiguous. We recommend you avoid using that name except for its
  994. special purpose.
  995. Variadic macros where you specify the parameter name is a GNU C
  996. feature that has been supported for a long time. Standard C, as of
  997. C99, supports only the form where the parameter is called
  998. @code{@w{__VA_ARGS__}}. For portability to previous versions of GNU C
  999. you should use only named variable argument parameters. On the other
  1000. hand, for portability to other C99 compilers, you should use only
  1001. @code{@w{__VA_ARGS__}}.
  1002. @node Predefined Macros
  1003. @subsection Predefined Macros
  1004. @cindex predefined macros
  1005. Several object-like macros are predefined; you use them without
  1006. supplying their definitions. Here we explain the ones user programs
  1007. often need to use. Many other macro names starting with @samp{__} are
  1008. predefined; in general, you should not define such macro names
  1009. yourself.
  1010. @table @code
  1011. @item __FILE__
  1012. This macro expands to the name of the current input file, in the form
  1013. of a C string constant. This is the full name by which the GCC opened
  1014. the file, not the short name specified in @code{#include} or as the
  1015. input file name argument. For example,
  1016. @code{"/usr/local/include/myheader.h"} is a possible expansion of this
  1017. macro.
  1018. @item __LINE__
  1019. This macro expands to the current input line number, in the form of a
  1020. decimal integer constant. While we call it a predefined macro, it's
  1021. a pretty strange macro, since its ``definition'' changes with each
  1022. new line of source code.
  1023. @item __func__
  1024. @itemx __FUNCTION__
  1025. These names are like variables that have as value a string containing
  1026. the name of the current function definition. They are not really
  1027. macros, but this is the best place to mention them.
  1028. @code{__FUNCTION__} is the name that has been defined in GNU C since
  1029. time immemorial; @code{__func__} is defined by the C standard.
  1030. With the following conditionals, you can use whichever one is defined.
  1031. @example
  1032. #if __STDC_VERSION__ < 199901L
  1033. # if __GNUC__ >= 2
  1034. # define __func__ __FUNCTION__
  1035. # else
  1036. # define __func__ "<unknown>"
  1037. # endif
  1038. #endif
  1039. @end example
  1040. @item __PRETTY_FUNCTION__
  1041. This is equivalent to @code{__FUNCTION__} in C, but in C@code{++}
  1042. the string includes argument type information as well.
  1043. It is a GNU C extension.
  1044. @end table
  1045. Those features are useful in generating an error message to report an
  1046. inconsistency detected by the program; the message can state the
  1047. source line where the inconsistency was detected. For example,
  1048. @example
  1049. fprintf (stderr, "Internal error: "
  1050. "negative string length "
  1051. "in function %s "
  1052. "%d at %s, line %d.",
  1053. __func__, length, __FILE__, __LINE__);
  1054. @end example
  1055. A @code{#line} directive changes @code{__LINE__}, and may change
  1056. @code{__FILE__} as well. @xref{Line Control}.
  1057. @table @code
  1058. @item __DATE__
  1059. This macro expands to a string constant that describes the date of
  1060. compilation. The string constant contains eleven characters and looks
  1061. like @code{@w{"Feb 12 1996"}}. If the day of the month is just one
  1062. digit, an extra space precedes it so that the date is always eleven
  1063. characters.
  1064. If the compiler cannot determine the current date, it emits a warning messages
  1065. (once per compilation) and @code{__DATE__} expands to
  1066. @code{@w{"??? ?? ????"}}.
  1067. We deprecate the use of @code{__DATE__} for the sake of reproducible
  1068. compilation.
  1069. @item __TIME__
  1070. This macro expands to a string constant that describes the time of
  1071. compilation. The string constant contains eight characters and looks
  1072. like @code{"23:59:01"}.
  1073. If the compiler cannot determine the current time, it emits a warning
  1074. message (once per compilation) and @code{__TIME__} expands to
  1075. @code{"??:??:??"}.
  1076. We deprecate the use of @code{__TIME__} for the sake of reproducible
  1077. compilation.
  1078. @item __STDC__
  1079. In normal operation, this macro expands to the constant 1, to signify
  1080. that this compiler implements ISO Standard C@.
  1081. @item __STDC_VERSION__
  1082. This macro expands to the C Standard's version number, a long integer
  1083. constant of the form @code{@var{yyyy}@var{mm}L} where @var{yyyy} and
  1084. @var{mm} are the year and month of the Standard version. This states
  1085. which version of the C Standard the compiler implements.
  1086. The current default value is @code{201112L}, which signifies the C
  1087. 2011 standard.
  1088. @item __STDC_HOSTED__
  1089. This macro is defined, with value 1, if the compiler's target is a
  1090. @dfn{hosted environment}. A hosted environment provides the full
  1091. facilities of the standard C library.
  1092. @end table
  1093. The rest of the predefined macros are GNU C extensions.
  1094. @table @code
  1095. @item __COUNTER__
  1096. This macro expands to sequential integral values starting from 0. In
  1097. other words, each time the program uses this acro, it generates the
  1098. next successive integer. This, with the @code{##} operator, provides
  1099. a convenient means for macros to generate unique identifiers.
  1100. @item __GNUC__
  1101. @itemx __GNUC_MINOR__
  1102. @itemx __GNUC_PATCHLEVEL__
  1103. These macros expand to the major version, minor version, and patch
  1104. level of the compiler, as integer constants. For example, GCC 3.2.1
  1105. expands @code{__GNUC__} to 3, @code{__GNUC_MINOR__} to 2, and
  1106. @code{__GNUC_PATCHLEVEL__} to 1.
  1107. If all you need to know is whether or not your program is being
  1108. compiled by GCC, or a non-GCC compiler that claims to accept the GNU C
  1109. extensions, you can simply test @code{__GNUC__}. If you need to write
  1110. code that depends on a specific version, you must check more
  1111. carefully. Each change in the minor version resets the patch level to
  1112. zero; each change in the major version (which happens rarely) resets
  1113. the minor version and the patch level to zero. To use the predefined
  1114. macros directly in the conditional, write it like this:
  1115. @example
  1116. /* @r{Test for version 3.2.0 or later.} */
  1117. #if __GNUC__ > 3 || \
  1118. (__GNUC__ == 3 && (__GNUC_MINOR__ > 2 || \
  1119. (__GNUC_MINOR__ == 2 && \
  1120. __GNUC_PATCHLEVEL__ > 0))
  1121. @end example
  1122. @noindent
  1123. Another approach is to use the predefined macros to
  1124. calculate a single number, then compare that against a threshold:
  1125. @example
  1126. #define GCC_VERSION (__GNUC__ * 10000 \
  1127. + __GNUC_MINOR__ * 100 \
  1128. + __GNUC_PATCHLEVEL__)
  1129. /* @r{@dots{}} */
  1130. /* @r{Test for GCC > 3.2.0} */
  1131. #if GCC_VERSION > 30200
  1132. @end example
  1133. @noindent
  1134. Many people find this form easier to understand.
  1135. @item __VERSION__
  1136. This macro expands to a string constant that describes the version of
  1137. the compiler in use. You should not rely on its contents' having any
  1138. particular form, but you can count on it to contain at least the
  1139. release number.
  1140. @item __TIMESTAMP__
  1141. This macro expands to a string constant that describes the date and
  1142. time of the last modification of the current source file. The string
  1143. constant contains abbreviated day of the week, month, day of the
  1144. month, time in hh:mm:ss form, and the year, in the format
  1145. @code{@w{"Sun Sep 16 01:03:52 1973"}}. If the day of the month is
  1146. less than 10, it is padded with a space on the left.
  1147. If GCC cannot determine that information date, it emits a warning
  1148. message (once per compilation) and @code{__TIMESTAMP__} expands to
  1149. @code{@w{"??? ??? ?? ??:??:?? ????"}}.
  1150. We deprecate the use of this macro for the sake of reproducible
  1151. compilation.
  1152. @end table
  1153. @node Undefining and Redefining Macros
  1154. @subsection Undefining and Redefining Macros
  1155. @cindex undefining macros
  1156. @cindex redefining macros
  1157. @findex #undef
  1158. You can @dfn{undefine} a macro with the @code{#undef} directive.
  1159. @code{#undef} takes a single argument, the name of the macro to
  1160. undefine. You use the bare macro name, even if the macro is
  1161. function-like. It is an error if anything appears on the line after
  1162. the macro name. @code{#undef} has no effect if the name is not a
  1163. macro.
  1164. @example
  1165. #define FOO 4
  1166. x = FOO; @expansion{} x = 4;
  1167. #undef FOO
  1168. x = FOO; @expansion{} x = FOO;
  1169. @end example
  1170. Once a macro has been undefined, that identifier may be @dfn{redefined}
  1171. as a macro by a subsequent @code{#define} directive. The new definition
  1172. need not have any resemblance to the old definition.
  1173. You can define a macro again without first undefining it only if
  1174. the new definition is @dfn{effectively the same} as the old one.
  1175. Two macro definitions are effectively the same if:
  1176. @itemize @bullet
  1177. @item Both are the same type of macro (object- or function-like).
  1178. @item All the tokens of the replacement list are the same.
  1179. @item If there are any parameters, they are the same.
  1180. @item Whitespace appears in the same places in both. It need not be
  1181. exactly the same amount of whitespace, though. Remember that comments
  1182. count as whitespace.
  1183. @end itemize
  1184. @noindent
  1185. These definitions are effectively the same:
  1186. @example
  1187. #define FOUR (2 + 2)
  1188. #define FOUR (2 + 2)
  1189. #define FOUR (2 /* @r{two} */ + 2)
  1190. @end example
  1191. @noindent
  1192. but these are not:
  1193. @example
  1194. #define FOUR (2 + 2)
  1195. #define FOUR ( 2+2 )
  1196. #define FOUR (2 * 2)
  1197. #define FOUR(score,and,seven,years,ago) (2 + 2)
  1198. @end example
  1199. This allows two different header files to define a common macro.
  1200. You can redefine an existing macro with #define, but redefining an
  1201. existing macro name with a different definition results in a warning.
  1202. @node Directives Within Macro Arguments
  1203. @subsection Directives Within Macro Arguments
  1204. @cindex macro arguments and directives
  1205. GNU C permits and handles preprocessing directives in the text provided
  1206. as arguments for a macro. That case is undefined in the C standard.
  1207. but in GNU C@ conditional directives in macro arguments
  1208. are clear and valid.
  1209. A paradoxical case is to redefine a macro within the call to that same
  1210. macro. What happens is, the new definition takes effect in time for
  1211. pre-expansion of @emph{all} the arguments, then the original
  1212. definition is expanded to replace the call. Here is a pathological
  1213. example:
  1214. @example
  1215. #define f(x) x x
  1216. f (first f second
  1217. #undef f
  1218. #define f 2
  1219. f)
  1220. @end example
  1221. @noindent
  1222. which expands to
  1223. @example
  1224. first 2 second 2 first 2 second 2
  1225. @end example
  1226. @noindent
  1227. with the semantics described above. We suggest you avoid writing code
  1228. which does this sort of thing.
  1229. @node Macro Pitfalls
  1230. @subsection Macro Pitfalls
  1231. @cindex problems with macros
  1232. @cindex pitfalls of macros
  1233. In this section we describe some special rules that apply to macros and
  1234. macro expansion, and point out certain cases in which the rules have
  1235. counter-intuitive consequences that you must watch out for.
  1236. @menu
  1237. * Misnesting::
  1238. * Operator Precedence Problems::
  1239. * Swallowing the Semicolon::
  1240. * Duplication of Side Effects::
  1241. * Macros and Auto Type::
  1242. * Self-Referential Macros::
  1243. * Argument Prescan::
  1244. @end menu
  1245. @node Misnesting
  1246. @subsubsection Misnesting
  1247. When a macro is called with arguments, the arguments are substituted
  1248. into the macro body and the result is checked, together with the rest of
  1249. the input file, for more macro calls. It is possible to piece together
  1250. a macro call coming partially from the macro body and partially from the
  1251. arguments. For example,
  1252. @example
  1253. #define twice(x) (2*(x))
  1254. #define call_with_1(x) x(1)
  1255. call_with_1 (twice)
  1256. @expansion{} twice(1)
  1257. @expansion{} (2*(1))
  1258. @end example
  1259. Macro definitions do not have to have balanced parentheses. By writing
  1260. an unbalanced open parenthesis in a macro body, it is possible to create
  1261. a macro call that begins inside the macro body but ends outside of it.
  1262. For example,
  1263. @example
  1264. #define strange(file) fprintf (file, "%s %d",
  1265. /* @r{@dots{}} */
  1266. strange(stderr) p, 35)
  1267. @expansion{} fprintf (stderr, "%s %d", p, 35)
  1268. @end example
  1269. The ability to piece together a macro call can be useful, but the use of
  1270. unbalanced open parentheses in a macro body is just confusing, and
  1271. should be avoided.
  1272. @node Operator Precedence Problems
  1273. @subsubsection Operator Precedence Problems
  1274. @cindex parentheses in macro bodies
  1275. You may have noticed that in most of the macro definition examples shown
  1276. above, each occurrence of a macro parameter name had parentheses around
  1277. it. In addition, another pair of parentheses usually surrounds the
  1278. entire macro definition. Here is why it is best to write macros that
  1279. way.
  1280. Suppose you define a macro as follows,
  1281. @example
  1282. #define ceil_div(x, y) (x + y - 1) / y
  1283. @end example
  1284. @noindent
  1285. whose purpose is to divide, rounding up. (One use for this operation is
  1286. to compute how many @code{int} objects are needed to hold a certain
  1287. number of @code{char} objects.) Then suppose it is used as follows:
  1288. @example
  1289. a = ceil_div (b & c, sizeof (int));
  1290. @expansion{} a = (b & c + sizeof (int) - 1) / sizeof (int);
  1291. @end example
  1292. @noindent
  1293. This does not do what is intended. The operator-precedence rules of
  1294. C make it equivalent to this:
  1295. @example
  1296. a = (b & (c + sizeof (int) - 1)) / sizeof (int);
  1297. @end example
  1298. @noindent
  1299. What we want is this:
  1300. @example
  1301. a = ((b & c) + sizeof (int) - 1)) / sizeof (int);
  1302. @end example
  1303. @noindent
  1304. Defining the macro as
  1305. @example
  1306. #define ceil_div(x, y) ((x) + (y) - 1) / (y)
  1307. @end example
  1308. @noindent
  1309. provides the desired result.
  1310. Unintended grouping can result in another way. Consider @code{sizeof
  1311. ceil_div(1, 2)}. That has the appearance of a C expression that would
  1312. compute the size of the type of @code{ceil_div (1, 2)}, but in fact it
  1313. means something very different. Here is what it expands to:
  1314. @example
  1315. sizeof ((1) + (2) - 1) / (2)
  1316. @end example
  1317. @noindent
  1318. This would take the size of an integer and divide it by two. The
  1319. precedence rules have put the division outside the @code{sizeof} when it
  1320. was intended to be inside.
  1321. Parentheses around the entire macro definition prevent such problems.
  1322. Here, then, is the recommended way to define @code{ceil_div}:
  1323. @example
  1324. #define ceil_div(x, y) (((x) + (y) - 1) / (y))
  1325. @end example
  1326. @node Swallowing the Semicolon
  1327. @subsubsection Swallowing the Semicolon
  1328. @cindex semicolons (after macro calls)
  1329. Often it is desirable to define a macro that expands into a compound
  1330. statement. Consider, for example, the following macro, that advances a
  1331. pointer (the parameter @code{p} says where to find it) across whitespace
  1332. characters:
  1333. @example
  1334. #define SKIP_SPACES(p, limit) \
  1335. @{ char *lim = (limit); \
  1336. while (p < lim) @{ \
  1337. if (*p++ != ' ') @{ \
  1338. p--; break; @}@}@}
  1339. @end example
  1340. @noindent
  1341. Here backslash-newline is used to split the macro definition, which must
  1342. be a single logical line, so that it resembles the way such code would
  1343. be laid out if not part of a macro definition.
  1344. A call to this macro might be @code{SKIP_SPACES (p, lim)}. Strictly
  1345. speaking, the call expands to a compound statement, which is a complete
  1346. statement with no need for a semicolon to end it. However, since it
  1347. looks like a function call, it minimizes confusion if you can use it
  1348. like a function call, writing a semicolon afterward, as in
  1349. @code{SKIP_SPACES (p, lim);}
  1350. This can cause trouble before @code{else} statements, because the
  1351. semicolon is actually a null statement. Suppose you write
  1352. @example
  1353. if (*p != 0)
  1354. SKIP_SPACES (p, lim);
  1355. else /* @r{@dots{}} */
  1356. @end example
  1357. @noindent
  1358. The presence of two statements---the compound statement and a null
  1359. statement---in between the @code{if} condition and the @code{else}
  1360. makes invalid C code.
  1361. The definition of the macro @code{SKIP_SPACES} can be altered to solve
  1362. this problem, using a @code{do @r{@dots{}} while} statement. Here is how:
  1363. @example
  1364. #define SKIP_SPACES(p, limit) \
  1365. do @{ char *lim = (limit); \
  1366. while (p < lim) @{ \
  1367. if (*p++ != ' ') @{ \
  1368. p--; break; @}@}@} \
  1369. while (0)
  1370. @end example
  1371. Now @code{SKIP_SPACES (p, lim);} expands into
  1372. @example
  1373. do @{ /* @r{@dots{}} */ @} while (0);
  1374. @end example
  1375. @noindent
  1376. which is one statement. The loop executes exactly once; most compilers
  1377. generate no extra code for it.
  1378. @node Duplication of Side Effects
  1379. @subsubsection Duplication of Side Effects
  1380. @cindex side effects (in macro arguments)
  1381. @cindex unsafe macros
  1382. Many C programs define a macro @code{min}, for ``minimum'', like this:
  1383. @example
  1384. #define min(X, Y) ((X) < (Y) ? (X) : (Y))
  1385. @end example
  1386. When you use this macro with an argument containing a side effect,
  1387. as shown here,
  1388. @example
  1389. next = min (x + y, foo (z));
  1390. @end example
  1391. @noindent
  1392. it expands as follows:
  1393. @example
  1394. next = ((x + y) < (foo (z)) ? (x + y) : (foo (z)));
  1395. @end example
  1396. @noindent
  1397. where @code{x + y} has been substituted for @code{X} and @code{foo (z)}
  1398. for @code{Y}.
  1399. The function @code{foo} is used only once in the statement as it
  1400. appears in the program, but the expression @code{foo (z)} has been
  1401. substituted twice into the macro expansion. As a result, @code{foo}
  1402. might be called twice when the statement is executed. If it has side
  1403. effects or if it takes a long time to compute, that may be
  1404. undesirable. We say that @code{min} is an @dfn{unsafe} macro.
  1405. The best solution to this problem is to define @code{min} in a way that
  1406. computes the value of @code{foo (z)} only once. In general, that requires
  1407. using @code{__auto_type} (@pxref{Auto Type}). How to use it for this
  1408. is described in the following section. @xref{Macros and Auto Type}.
  1409. Otherwise, you will need to be careful when @emph{using} the macro
  1410. @code{min}. For example, you can calculate the value of @code{foo
  1411. (z)}, save it in a variable, and use that variable in @code{min}:
  1412. @example
  1413. @group
  1414. #define min(X, Y) ((X) < (Y) ? (X) : (Y))
  1415. /* @r{@dots{}} */
  1416. @{
  1417. int tem = foo (z);
  1418. next = min (x + y, tem);
  1419. @}
  1420. @end group
  1421. @end example
  1422. @noindent
  1423. (where we assume that @code{foo} returns type @code{int}).
  1424. When the repeated value appears as the condition of the @code{?:}
  1425. operator and again as its @var{iftrue} expression, you can avoid
  1426. repeated execution by omitting the @var{iftrue} expression, like this:
  1427. @example
  1428. #define x_or_y(X, Y) ((X) ? : (Y))
  1429. @end example
  1430. @noindent
  1431. In GNU C, this expands to use the first macro argument's value if that
  1432. isn't zero. If that's zero, it compiles the second argument and uses
  1433. that value. @xref{Conditional Expression}.
  1434. @node Macros and Auto Type
  1435. @subsubsection Using @code{__auto_type} for Local Variables
  1436. @cindex local variables in macros
  1437. @cindex variables, local, in macros
  1438. @cindex macros, local variables in
  1439. The operator @code{__auto_type} makes it possible to
  1440. define macros that can work on any data type even though they need to
  1441. generate local variable declarations. @xref{Auto Type}.
  1442. For instance, here's how to define a safe ``maximum'' macro that
  1443. operates on any arithmetic type and computes each of its arguments
  1444. exactly once:
  1445. @example
  1446. #define max(a,b) \
  1447. (@{ __auto_type _a = (a); \
  1448. __auto_type _b = (b); \
  1449. _a > _b ? _a : _b; @})
  1450. @end example
  1451. The @samp{(@{ @dots{} @})} notation produces @dfn{statement
  1452. expression}---a statement that can be used as an expression
  1453. (@pxref{Statement Exprs}). Its value is the value of its last
  1454. statement. This permits us to define local variables and store each
  1455. argument value into one.
  1456. @cindex underscores in variables in macros
  1457. @cindex @samp{_} in variables in macros
  1458. The reason for using names that start with underscores for the local
  1459. variables is to avoid conflicts with variable names that occur within
  1460. the expressions that are substituted for @code{a} and @code{b}.
  1461. Underscore followed by a lower case letter won't be predefined by the
  1462. system in any way.
  1463. @c We hope someday to extend C with a new form of declaration syntax
  1464. @c which all the newly declared variables' scopes would begin at the end
  1465. @c of the entire declaration, rather than as soon as each variable's
  1466. @c declaration begins. This way, all the variables' initializers would
  1467. @c be interpreted in the context before the declaration. Then we could
  1468. @c use any names whatsoever for the local variables and always get correct
  1469. @c behavior for the macro.
  1470. @node Self-Referential Macros
  1471. @subsubsection Self-Referential Macros
  1472. @cindex self-reference
  1473. A @dfn{self-referential} macro is one whose name appears in its
  1474. definition. Recall that all macro definitions are rescanned for more
  1475. macros to replace. If the self-reference were considered a use of the
  1476. macro, it would produce an infinitely large expansion. To prevent
  1477. this, the self-reference is not considered a macro call: preprocessing
  1478. leaves it unchanged. Consider an example:
  1479. @example
  1480. #define foo (4 + foo)
  1481. @end example
  1482. @noindent
  1483. where @code{foo} is also a variable in your program.
  1484. Following the ordinary rules, each reference to @code{foo} will expand
  1485. into @code{(4 + foo)}; then this will be rescanned and will expand into
  1486. @code{(4 + (4 + foo))}; and so on until the computer runs out of memory.
  1487. The self-reference rule cuts this process short after one step, at
  1488. @code{(4 + foo)}. Therefore, this macro definition has the possibly
  1489. useful effect of causing the program to add 4 to the value of @code{foo}
  1490. wherever @code{foo} is referred to.
  1491. In most cases, it is a bad idea to take advantage of this feature. A
  1492. person reading the program who sees that @code{foo} is a variable will
  1493. not expect that it is a macro as well. The reader will come across the
  1494. identifier @code{foo} in the program and think its value should be that
  1495. of the variable @code{foo}, whereas in fact the value is four greater.
  1496. It is useful to make a macro definition that expands to the macro
  1497. name itself. If you write
  1498. @example
  1499. #define EPERM EPERM
  1500. @end example
  1501. @noindent
  1502. then the macro @code{EPERM} expands to @code{EPERM}. Effectively,
  1503. preprocessing leaves it unchanged in the source code. You can tell
  1504. that it's a macro with @code{#ifdef}. You might do this if you want
  1505. to define numeric constants with an @code{enum}, but have
  1506. @code{#ifdef} be true for each constant.
  1507. If a macro @code{x} expands to use a macro @code{y}, and the expansion of
  1508. @code{y} refers to the macro @code{x}, that is an @dfn{indirect
  1509. self-reference} of @code{x}. @code{x} is not expanded in this case
  1510. either. Thus, if we have
  1511. @example
  1512. #define x (4 + y)
  1513. #define y (2 * x)
  1514. @end example
  1515. @noindent
  1516. then @code{x} and @code{y} expand as follows:
  1517. @example
  1518. @group
  1519. x @expansion{} (4 + y)
  1520. @expansion{} (4 + (2 * x))
  1521. y @expansion{} (2 * x)
  1522. @expansion{} (2 * (4 + y))
  1523. @end group
  1524. @end example
  1525. @noindent
  1526. Each macro is expanded when it appears in the definition of the other
  1527. macro, but not when it indirectly appears in its own definition.
  1528. @node Argument Prescan
  1529. @subsubsection Argument Prescan
  1530. @cindex expansion of arguments
  1531. @cindex macro argument expansion
  1532. @cindex prescan of macro arguments
  1533. Macro arguments are completely macro-expanded before they are
  1534. substituted into a macro body, unless they are stringified or pasted
  1535. with other tokens. After substitution, the entire macro body, including
  1536. the substituted arguments, is scanned again for macros to be expanded.
  1537. The result is that the arguments are scanned @emph{twice} to expand
  1538. macro calls in them.
  1539. Most of the time, this has no effect. If the argument contained any
  1540. macro calls, they were expanded during the first scan. The result
  1541. therefore contains no macro calls, so the second scan does not change
  1542. it. If the argument were substituted as given, with no prescan, the
  1543. single remaining scan would find the same macro calls and produce the
  1544. same results.
  1545. You might expect the double scan to change the results when a
  1546. self-referential macro is used in an argument of another macro
  1547. (@pxref{Self-Referential Macros}): the self-referential macro would be
  1548. expanded once in the first scan, and a second time in the second scan.
  1549. However, this is not what happens. The self-references that do not
  1550. expand in the first scan are marked so that they will not expand in the
  1551. second scan either.
  1552. You might wonder, ``Why mention the prescan, if it makes no difference?
  1553. And why not skip it and make preprocessing go faster?'' The answer is
  1554. that the prescan does make a difference in three special cases:
  1555. @itemize @bullet
  1556. @item
  1557. Nested calls to a macro.
  1558. We say that @dfn{nested} calls to a macro occur when a macro's argument
  1559. contains a call to that very macro. For example, if @code{f} is a macro
  1560. that expects one argument, @code{f (f (1))} is a nested pair of calls to
  1561. @code{f}. The desired expansion is made by expanding @code{f (1)} and
  1562. substituting that into the definition of @code{f}. The prescan causes
  1563. the expected result to happen. Without the prescan, @code{f (1)} itself
  1564. would be substituted as an argument, and the inner use of @code{f} would
  1565. appear during the main scan as an indirect self-reference and would not
  1566. be expanded.
  1567. @item
  1568. Macros that call other macros that stringify or concatenate.
  1569. If an argument is stringified or concatenated, the prescan does not
  1570. occur. If you @emph{want} to expand a macro, then stringify or
  1571. concatenate its expansion, you can do that by causing one macro to call
  1572. another macro that does the stringification or concatenation. For
  1573. instance, if you have
  1574. @example
  1575. #define AFTERX(x) X_ ## x
  1576. #define XAFTERX(x) AFTERX(x)
  1577. #define TABLESIZE 1024
  1578. #define BUFSIZE TABLESIZE
  1579. @end example
  1580. @noindent
  1581. then @code{AFTERX(BUFSIZE)} expands to @code{X_BUFSIZE}, and
  1582. @code{XAFTERX(BUFSIZE)} expands to @code{X_1024}. (Not to
  1583. @code{X_TABLESIZE}. Prescan always does a complete expansion.)
  1584. @item
  1585. Macros used in arguments, whose expansions contain unshielded commas.
  1586. This can cause a macro expanded on the second scan to be called with the
  1587. wrong number of arguments. Here is an example:
  1588. @example
  1589. #define foo a,b
  1590. #define bar(x) lose(x)
  1591. #define lose(x) (1 + (x))
  1592. @end example
  1593. We would like @code{bar(foo)} to turn into @code{(1 + (foo))}, which
  1594. would then turn into @code{(1 + (a,b))}. Instead, @code{bar(foo)}
  1595. expands into @code{lose(a,b)}, which gives an error because @code{lose}
  1596. requires a single argument. In this case, the problem is easily solved
  1597. by the same parentheses that ought to be used to prevent misnesting of
  1598. arithmetic operations:
  1599. @example
  1600. #define foo (a,b)
  1601. @exdent or
  1602. #define bar(x) lose((x))
  1603. @end example
  1604. The extra pair of parentheses prevents the comma in @code{foo}'s
  1605. definition from being interpreted as an argument separator.
  1606. @end itemize
  1607. @ignore
  1608. @c This is commented out because pragmas are not supposed
  1609. @c to alter the meaning of the program.
  1610. @c Microsoft did something stupid in defining these.
  1611. @node Macro Pragmas
  1612. @subsection Macro Pragmas
  1613. A pragma is a way of specifying special directions to the C compiler.
  1614. @xref{Pragmas}, for the basic syntax of pragmas. Here we describe two
  1615. pragmas that save the current definition of a macro on a stack, and
  1616. restore it later. This makes it possible to redefine a macro temporarily
  1617. and later go back to the previous definition.
  1618. @table @code
  1619. @item #pragma push_macro (@var{macro_name})
  1620. @itemx _Pragma ("push_macro (@var{macro_name})")
  1621. The @samp{push_macro} pragma saves the current macro definition of
  1622. @var{macro_name} on the macro definition stack.
  1623. @item #pragma pop_macro (@var{macro_name})
  1624. @itemx _Pragma ("pop_macro (@var{macro_name})")
  1625. The @samp{pop_macro} pragma pops a saved macro definition
  1626. off the macro definition stack and defines @var{macro_name} with
  1627. that definition.
  1628. @end table
  1629. Each macro name has a separate stack, and @samp{pop_macro}
  1630. when the stack is empty has no effect.
  1631. Here's an example of using these to pragmas to override temporarily
  1632. the definition of @code{FOO}.
  1633. @example
  1634. #define FOO 42
  1635. /* @r{Do something with @var{FOO} defined as 42...} */
  1636. _Pragma ("push_macro (\"FOO\")")
  1637. #undef FOO
  1638. #define FOO 47
  1639. /* @r{Do something with @var{FOO} defined as 47...} */
  1640. _Pragma ("pop_macro (\"FOO\")")
  1641. /* @r{@var{FOO} is now restored}
  1642. @r{to its previous definition of 42.} */
  1643. @end example
  1644. @end ignore
  1645. @node Conditionals
  1646. @section Conditionals
  1647. @cindex conditionals
  1648. A @dfn{conditional} is a preprocessing directive that controls whether
  1649. or not to include a chunk of code in the final token stream that is
  1650. compiled. Preprocessing conditionals can test arithmetic expressions,
  1651. or whether a name is defined as a macro, or both together using the
  1652. special @code{defined} operator.
  1653. A preprocessing conditional in C resembles in some ways an @code{if}
  1654. statement in C, but it is important to understand the difference between
  1655. them. The condition in an @code{if} statement is tested during the
  1656. execution of your program. Its purpose is to allow your program to
  1657. behave differently from run to run, depending on the data it is
  1658. operating on. The condition in a preprocessing conditional directive is
  1659. tested when your program is compiled. Its purpose is to allow different
  1660. code to be included in the program depending on the situation at the
  1661. time of compilation.
  1662. Sometimes this distinction makes no practical difference. GCC and
  1663. other modern compilers often
  1664. do test @code{if} statements when a program is compiled, if their
  1665. conditions are known not to vary at run time, and eliminate code that
  1666. can never be executed. If you can count on your compiler to do this,
  1667. you may find that your program is more readable if you use @code{if}
  1668. statements with constant conditions (perhaps determined by macros). Of
  1669. course, you can only use this to exclude code, not type definitions or
  1670. other preprocessing directives, and you can only do it if the file
  1671. remains syntactically valid when that code is not used.
  1672. @menu
  1673. * Conditional Uses::
  1674. * Conditional Syntax::
  1675. * Deleted Code::
  1676. @end menu
  1677. @node Conditional Uses
  1678. @subsection Uses of Conditional Directives
  1679. There are three usual reasons to use a preprocessing conditional.
  1680. @itemize @bullet
  1681. @item
  1682. A program may need to use different code depending on the machine or
  1683. operating system it is to run on. In some cases the code for one
  1684. operating system may be erroneous on another operating system; for
  1685. example, it might refer to data types or constants that do not exist on
  1686. the other system. When this happens, it is not enough to avoid
  1687. executing the invalid code. Its mere presence will cause the compiler
  1688. to reject the program. With a preprocessing conditional, the offending
  1689. code can be effectively excised from the program when it is not valid.
  1690. @item
  1691. You may want to be able to compile the same source file into two
  1692. different programs. One version might make frequent time-consuming
  1693. consistency checks on its intermediate data, or print the values of
  1694. those data for debugging, and the other not.
  1695. @item
  1696. A conditional whose condition is always false is one way to exclude code
  1697. from the program but keep it as a sort of comment for future reference.
  1698. @end itemize
  1699. Simple programs that do not need system-specific logic or complex
  1700. debugging hooks generally will not need to use preprocessing
  1701. conditionals.
  1702. @node Conditional Syntax
  1703. @subsection Syntax of Preprocessing Conditionals
  1704. @findex #if
  1705. A preprocessing conditional begins with a @dfn{conditional
  1706. directive}: @code{#if}, @code{#ifdef} or @code{#ifndef}.
  1707. @menu
  1708. * ifdef::
  1709. * if::
  1710. * defined::
  1711. * else::
  1712. * elif::
  1713. @end menu
  1714. @node ifdef
  1715. @subsubsection The @code{#ifdef} directive
  1716. @findex #ifdef
  1717. @findex #endif
  1718. The simplest sort of conditional is
  1719. @example
  1720. @group
  1721. #ifdef @var{MACRO}
  1722. @var{controlled text}
  1723. #endif /* @var{MACRO} */
  1724. @end group
  1725. @end example
  1726. @cindex conditional group
  1727. This block is called a @dfn{conditional group}. The body,
  1728. @var{controlled text}, will be included in compilation if
  1729. and only if @var{MACRO} is defined. We say that the conditional
  1730. @dfn{succeeds} if @var{MACRO} is defined, @dfn{fails} if it is not.
  1731. The @var{controlled text} inside a conditional can include
  1732. preprocessing directives. They are executed only if the conditional
  1733. succeeds. You can nest conditional groups inside other conditional
  1734. groups, but they must be completely nested. In other words,
  1735. @code{#endif} always matches the nearest @code{#ifdef} (or
  1736. @code{#ifndef}, or @code{#if}). Also, you cannot start a conditional
  1737. group in one file and end it in another.
  1738. Even if a conditional fails, the @var{controlled text} inside it is
  1739. still run through initial transformations and tokenization. Therefore,
  1740. it must all be lexically valid C@. Normally the only way this matters is
  1741. that all comments and string literals inside a failing conditional group
  1742. must still be properly ended.
  1743. The comment following the @code{#endif} is not required, but it is a
  1744. good practice if there is a lot of @var{controlled text}, because it
  1745. helps people match the @code{#endif} to the corresponding @code{#ifdef}.
  1746. Older programs sometimes put @var{macro} directly after the
  1747. @code{#endif} without enclosing it in a comment. This is invalid code
  1748. according to the C standard, but it only causes a warning in GNU C@.
  1749. It never affects which @code{#ifndef} the @code{#endif} matches.
  1750. @findex #ifndef
  1751. Sometimes you wish to use some code if a macro is @emph{not} defined.
  1752. You can do this by writing @code{#ifndef} instead of @code{#ifdef}.
  1753. One common use of @code{#ifndef} is to include code only the first
  1754. time a header file is included. @xref{Once-Only Headers}.
  1755. Macro definitions can vary between compilations for several reasons.
  1756. Here are some samples.
  1757. @itemize @bullet
  1758. @item
  1759. Some macros are predefined on each kind of machine
  1760. (@pxref{System-specific Predefined Macros, System-specific Predefined
  1761. Macros, System-specific Predefined Macros, gcc, Using the GNU Compiler
  1762. Collection}). This allows you to provide code specially tuned for a
  1763. particular machine.
  1764. @item
  1765. System header files define more macros, associated with the features
  1766. they implement. You can test these macros with conditionals to avoid
  1767. using a system feature on a machine where it is not implemented.
  1768. @item
  1769. Macros can be defined or undefined with the @option{-D} and @option{-U}
  1770. command-line options when you compile the program. You can arrange to
  1771. compile the same source file into two different programs by choosing a
  1772. macro name to specify which program you want, writing conditionals to
  1773. test whether or how this macro is defined, and then controlling the
  1774. state of the macro with command-line options, perhaps set in the
  1775. file @file{Makefile}. @xref{Invocation, Invoking GCC, Invoking GCC,
  1776. gcc, Using the GNU Compiler Collection}.
  1777. @item
  1778. Your program might have a special header file (often called
  1779. @file{config.h}) that is adjusted when the program is compiled. It can
  1780. define or not define macros depending on the features of the system and
  1781. the desired capabilities of the program. The adjustment can be
  1782. automated by a tool such as @command{autoconf}, or done by hand.
  1783. @end itemize
  1784. @node if
  1785. @subsubsection The @code{#if} directive
  1786. The @code{#if} directive allows you to test the value of an integer arithmetic
  1787. expression, rather than the mere existence of one macro. Its syntax is
  1788. @example
  1789. @group
  1790. #if @var{expression}
  1791. @var{controlled text}
  1792. #endif /* @var{expression} */
  1793. @end group
  1794. @end example
  1795. @var{expression} is a C expression of integer type, subject to
  1796. stringent restrictions so its value can be computed at compile time.
  1797. It may contain
  1798. @itemize @bullet
  1799. @item
  1800. Integer constants.
  1801. @item
  1802. Character constants, which are interpreted as they would be in normal
  1803. code.
  1804. @item
  1805. Arithmetic operators for addition, subtraction, multiplication,
  1806. division, bitwise operations, shifts, comparisons, and logical
  1807. operations (@code{&&} and @code{||}). The latter two obey the usual
  1808. short-circuiting rules of standard C@.
  1809. @item
  1810. Macros. All macros in the expression are expanded before actual
  1811. computation of the expression's value begins.
  1812. @item
  1813. Uses of the @code{defined} operator, which lets you check whether macros
  1814. are defined in the middle of an @code{#if}.
  1815. @item
  1816. Identifiers that are not macros, which are all considered to be the
  1817. number zero. This allows you to write @code{@w{#if MACRO}} instead of
  1818. @code{@w{#ifdef MACRO}}, if you know that MACRO, when defined, will
  1819. always have a nonzero value. Function-like macros used without their
  1820. function call parentheses are also treated as zero.
  1821. In some contexts this shortcut is undesirable. The @option{-Wundef}
  1822. requests warnings for any identifier in an @code{#if} that is not
  1823. defined as a macro.
  1824. @end itemize
  1825. Preprocessing does not know anything about the data types of C.
  1826. Therefore, @code{sizeof} operators are not recognized in @code{#if};
  1827. @code{sizeof} is simply an identifier, and if it is not a macro, it
  1828. stands for zero. This is likely to make the expression invalid.
  1829. Preprocessing does not recognize @code{enum} constants; they too are
  1830. simply identifiers, so if they are not macros, they stand for zero.
  1831. Preprocessing calculates the value of @var{expression}, and carries
  1832. out all calculations in the widest integer type known to the compiler;
  1833. on most machines supported by GNU C this is 64 bits. This is not the
  1834. same rule as the compiler uses to calculate the value of a constant
  1835. expression, and may give different results in some cases. If the
  1836. value comes out to be nonzero, the @code{#if} succeeds and the
  1837. @var{controlled text} is compiled; otherwise it is skipped.
  1838. @node defined
  1839. @subsubsection The @code{defined} test
  1840. @cindex @code{defined}
  1841. The special operator @code{defined} is used in @code{#if} and
  1842. @code{#elif} expressions to test whether a certain name is defined as a
  1843. macro. @code{defined @var{name}} and @code{defined (@var{name})} are
  1844. both expressions whose value is 1 if @var{name} is defined as a macro at
  1845. the current point in the program, and 0 otherwise. Thus, @code{@w{#if
  1846. defined MACRO}} is precisely equivalent to @code{@w{#ifdef MACRO}}.
  1847. @code{defined} is useful when you wish to test more than one macro for
  1848. existence at once. For example,
  1849. @example
  1850. #if defined (__arm__) || defined (__PPC__)
  1851. @end example
  1852. @noindent
  1853. would succeed if either of the names @code{__arm__} or
  1854. @code{__PPC__} is defined as a macro---in other words,
  1855. when compiling for ARM processors or PowerPC processors.
  1856. Conditionals written like this:
  1857. @example
  1858. #if defined BUFSIZE && BUFSIZE >= 1024
  1859. @end example
  1860. @noindent
  1861. can generally be simplified to just @code{@w{#if BUFSIZE >= 1024}},
  1862. since if @code{BUFSIZE} is not defined, it will be interpreted as having
  1863. the value zero.
  1864. In GCC, you can include @code{defined} as part of another macro definition,
  1865. like this:
  1866. @example
  1867. #define MACRO_DEFINED(X) defined X
  1868. #if MACRO_DEFINED(BUFSIZE)
  1869. @end example
  1870. @noindent
  1871. which would expand the @code{#if} expression to:
  1872. @example
  1873. #if defined BUFSIZE
  1874. @end example
  1875. @noindent
  1876. Generating @code{defined} in this way is a GNU C extension.
  1877. @node else
  1878. @subsubsection The @code{#else} directive
  1879. @findex #else
  1880. The @code{#else} directive can be added to a conditional to provide
  1881. alternative text to be used if the condition fails. This is what it
  1882. looks like:
  1883. @example
  1884. @group
  1885. #if @var{expression}
  1886. @var{text-if-true}
  1887. #else /* Not @var{expression} */
  1888. @var{text-if-false}
  1889. #endif /* Not @var{expression} */
  1890. @end group
  1891. @end example
  1892. @noindent
  1893. If @var{expression} is nonzero, the @var{text-if-true} is included and
  1894. the @var{text-if-false} is skipped. If @var{expression} is zero, the
  1895. opposite happens.
  1896. You can use @code{#else} with @code{#ifdef} and @code{#ifndef}, too.
  1897. @node elif
  1898. @subsubsection The @code{#elif} directive
  1899. @findex #elif
  1900. One common case of nested conditionals is used to check for more than two
  1901. possible alternatives. For example, you might have
  1902. @example
  1903. #if X == 1
  1904. /* @r{@dots{}} */
  1905. #else /* X != 1 */
  1906. #if X == 2
  1907. /* @r{@dots{}} */
  1908. #else /* X != 2 */
  1909. /* @r{@dots{}} */
  1910. #endif /* X != 2 */
  1911. #endif /* X != 1 */
  1912. @end example
  1913. Another conditional directive, @code{#elif}, allows this to be
  1914. abbreviated as follows:
  1915. @example
  1916. #if X == 1
  1917. /* @r{@dots{}} */
  1918. #elif X == 2
  1919. /* @r{@dots{}} */
  1920. #else /* X != 2 and X != 1*/
  1921. /* @r{@dots{}} */
  1922. #endif /* X != 2 and X != 1*/
  1923. @end example
  1924. @code{#elif} stands for ``else if''. Like @code{#else}, it goes in the
  1925. middle of a conditional group and subdivides it; it does not require a
  1926. matching @code{#endif} of its own. Like @code{#if}, the @code{#elif}
  1927. directive includes an expression to be tested. The text following the
  1928. @code{#elif} is processed only if the original @code{#if}-condition
  1929. failed and the @code{#elif} condition succeeds.
  1930. More than one @code{#elif} can go in the same conditional group. Then
  1931. the text after each @code{#elif} is processed only if the @code{#elif}
  1932. condition succeeds after the original @code{#if} and all previous
  1933. @code{#elif} directives within it have failed.
  1934. @code{#else} is allowed after any number of @code{#elif} directives, but
  1935. @code{#elif} may not follow @code{#else}.
  1936. @node Deleted Code
  1937. @subsection Deleted Code
  1938. @cindex commenting out code
  1939. If you replace or delete a part of the program but want to keep the
  1940. old code in the file for future reference, commenting it out is not so
  1941. straightforward in C. Block comments do not nest, so the first
  1942. comment inside the old code will end the commenting-out. The probable
  1943. result is a flood of syntax errors.
  1944. One way to avoid this problem is to use an always-false conditional
  1945. instead. For instance, put @code{#if 0} before the deleted code and
  1946. @code{#endif} after it. This works even if the code being turned
  1947. off contains conditionals, but they must be entire conditionals
  1948. (balanced @code{#if} and @code{#endif}).
  1949. Some people use @code{#ifdef notdef} instead. This is risky, because
  1950. @code{notdef} might be accidentally defined as a macro, and then the
  1951. conditional would succeed. @code{#if 0} can be counted on to fail.
  1952. Do not use @code{#if 0} around text that is not C code. Use a real
  1953. comment, instead. The interior of @code{#if 0} must consist of complete
  1954. tokens; in particular, single-quote characters must balance. Comments
  1955. often contain unbalanced single-quote characters (known in English as
  1956. apostrophes). These confuse @code{#if 0}. They don't confuse
  1957. @samp{/*}.
  1958. @node Diagnostics
  1959. @section Diagnostics
  1960. @cindex diagnostic
  1961. @cindex reporting errors
  1962. @cindex reporting warnings
  1963. @findex #error
  1964. The directive @code{#error} reports a fatal error. The
  1965. tokens forming the rest of the line following @code{#error} are used
  1966. as the error message.
  1967. The usual place to use @code{#error} is inside a conditional that
  1968. detects a combination of parameters that you know the program does not
  1969. properly support. For example,
  1970. @smallexample
  1971. #if !defined(UNALIGNED_INT_ASM_OP) && defined(DWARF2_DEBUGGING_INFO)
  1972. #error "DWARF2_DEBUGGING_INFO requires UNALIGNED_INT_ASM_OP."
  1973. #endif
  1974. @end smallexample
  1975. @findex #warning
  1976. The directive @code{#warning} is like @code{#error}, but it reports a
  1977. warning instead of an error. The tokens following @code{#warning} are
  1978. used as the warning message.
  1979. You might use @code{#warning} in obsolete header files, with a message
  1980. saying which header file to use instead.
  1981. Neither @code{#error} nor @code{#warning} macro-expands its argument.
  1982. Internal whitespace sequences are each replaced with a single space.
  1983. The line must consist of complete tokens. It is wisest to make the
  1984. argument of these directives be a single string constant; this avoids
  1985. problems with apostrophes and the like.
  1986. @node Line Control
  1987. @section Line Control
  1988. @cindex line control
  1989. Due to C's widespread availability and low-level nature, it is often
  1990. used as the target language for translation of other languages, or for
  1991. the output of lexical analyzers and parsers (e.g., lex/flex and
  1992. yacc/bison). Line control enables the user to track diagnostics back
  1993. to the location in the original language.
  1994. The C compiler knows the location in the source file where each token
  1995. came from: file name, starting line and column, and final line and column.
  1996. (Column numbers are used only for error messages.)
  1997. When a program generates C source code, as the Bison parser generator
  1998. does, often it copies some of that C code from another file. For
  1999. instance parts of the output from Bison are generated from scratch or
  2000. come from a standard parser file, but Bison copies the rest from
  2001. Bison's input file. Errors in that code, at compile time or run time,
  2002. should refer to that file, which is the real source code. To make that happen,
  2003. Bison generates line-control directives that the C compiler understands.
  2004. @findex #line
  2005. @code{#line} is a directive that specifies the original line number
  2006. and source file name for subsequent code. @code{#line} has three
  2007. variants:
  2008. @table @code
  2009. @item #line @var{linenum}
  2010. @var{linenum} is a non-negative decimal integer constant. It specifies
  2011. the line number that should be reported for the following line of
  2012. input. Subsequent lines are counted from @var{linenum}.
  2013. @item #line @var{linenum} @var{filename}
  2014. @var{linenum} is the same as for the first form, and has the same
  2015. effect. In addition, @var{filename} is a string constant that
  2016. specifies the source file name. Subsequent source lines are recorded
  2017. as coming from that file, until something else happens to change that.
  2018. @var{filename} is interpreted according to the normal rules for a
  2019. string constant. Backslash escapes are interpreted, in contrast to
  2020. @code{#include}.
  2021. @item #line @var{anything else}
  2022. @var{anything else} is checked for macro calls, which are expanded.
  2023. The result should match one of the above two forms.
  2024. @end table
  2025. @code{#line} directives alter the results of the @code{__FILE__} and
  2026. @code{__LINE__} symbols from that point on. @xref{Predefined Macros}.
  2027. @node Null Directive
  2028. @section Null Directive
  2029. @cindex null directive
  2030. The @dfn{null directive} consists of a @code{#} followed by a newline,
  2031. with only whitespace and comments in between. It has no
  2032. effect on the output of the compiler.