cpp.texi 87 KB

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