cpp.texi 87 KB

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