OSDN Git Service

* cpp.texi: Update.
[pf3gnuchains/gcc-fork.git] / gcc / cpp.texi
1 \input texinfo
2 @setfilename cpp.info
3 @settitle The C Preprocessor
4
5 @ifinfo
6 @dircategory Programming
7 @direntry
8 * Cpp: (cpp).                  The GNU C preprocessor.
9 @end direntry
10 @end ifinfo
11
12 @c @smallbook
13 @c @cropmarks
14 @c @finalout
15 @setchapternewpage odd
16 @ifinfo
17 This file documents the GNU C Preprocessor.
18
19 Copyright 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
20 1999, 2000 Free Software Foundation, Inc.
21
22 Permission is granted to make and distribute verbatim copies of
23 this manual provided the copyright notice and this permission notice
24 are preserved on all copies.
25
26 @ignore
27 Permission is granted to process this file through Tex and print the
28 results, provided the printed document carries copying permission
29 notice identical to this one except for the removal of this paragraph
30 (this paragraph not being relevant to the printed manual).
31
32 @end ignore
33 Permission is granted to copy and distribute modified versions of this
34 manual under the conditions for verbatim copying, provided also that
35 the entire resulting derived work is distributed under the terms of a
36 permission notice identical to this one.
37
38 Permission is granted to copy and distribute translations of this manual
39 into another language, under the above conditions for modified versions.
40 @end ifinfo
41
42 @titlepage
43 @c @finalout
44 @title The C Preprocessor
45 @subtitle Last revised July 2000
46 @subtitle for GCC version 2
47 @author Richard M. Stallman
48 @page
49 @vskip 2pc
50 This booklet is eventually intended to form the first chapter of a GNU 
51 C Language manual.
52
53 @vskip 0pt plus 1filll
54 @c man begin COPYRIGHT
55 Copyright @copyright{} 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996,
56 1997, 1998, 1999, 2000
57 Free Software Foundation, Inc.
58
59 Permission is granted to make and distribute verbatim copies of
60 this manual provided the copyright notice and this permission notice
61 are preserved on all copies.
62
63 Permission is granted to copy and distribute modified versions of this
64 manual under the conditions for verbatim copying, provided also that
65 the entire resulting derived work is distributed under the terms of a
66 permission notice identical to this one.
67
68 Permission is granted to copy and distribute translations of this manual
69 into another language, under the above conditions for modified versions.
70 @c man end
71 @end titlepage
72 @page
73
74 @node Top, Global Actions,, (DIR)
75 @chapter The C Preprocessor
76 @c man begin DESCRIPTION
77
78 The C preprocessor is a @dfn{macro processor} that is used automatically
79 by the C compiler to transform your program before actual compilation.
80 It is called a macro processor because it allows you to define
81 @dfn{macros}, which are brief abbreviations for longer constructs.
82
83 The C preprocessor is intended only for macro processing of C, C++ and
84 Objective C source files.  For macro processing of other files, you are
85 strongly encouraged to use alternatives like M4, which will likely give
86 you better results and avoid many problems.  For example, normally the C
87 preprocessor does not preserve arbitrary whitespace verbatim, but
88 instead replaces each sequence with a single space.
89
90 For use on C-like source files, the C preprocessor provides four
91 separate facilities that you can use as you see fit:
92
93 @itemize @bullet
94 @item
95 Inclusion of header files.  These are files of declarations that can be
96 substituted into your program.
97
98 @item
99 Macro expansion.  You can define @dfn{macros}, which are abbreviations
100 for arbitrary fragments of C code, and then the C preprocessor will
101 replace the macros with their definitions throughout the program.
102
103 @item
104 Conditional compilation.  Using special preprocessing directives, you
105 can include or exclude parts of the program according to various
106 conditions.
107
108 @item
109 Line control.  If you use a program to combine or rearrange source files
110 into an intermediate file which is then compiled, you can use line
111 control to inform the compiler of where each source line originally came
112 from.
113 @end itemize
114
115 C preprocessors vary in some details.  This manual discusses the GNU C
116 preprocessor, which provides a small superset of the features of ISO
117 Standard C@.
118
119 ISO Standard C requires the rejection of many harmless constructs
120 commonly used by today's C programs.  Such incompatibility would be
121 inconvenient for users, so the GNU C preprocessor is configured to
122 accept these constructs by default.  Strictly speaking, to get ISO
123 Standard C, you must use the options @samp{-trigraphs}, @samp{-undef}
124 and @samp{-pedantic}, but in practice the consequences of having strict
125 ISO Standard C make it undesirable to do this.  @xref{Invocation}.
126
127 @c man end
128
129 @menu
130 * Global Actions::       Actions made uniformly on all input files.
131 * Directives::           General syntax of preprocessing directives.
132 * Header Files::         How and why to use header files.
133 * Macros::               How and why to use macros.
134 * Conditionals::         How and why to use conditionals.
135 * Assertions::           How and why to use assertions.
136 * Line Control::         Use of line control when you combine source files.
137 * Other Directives::     Miscellaneous preprocessing directives.
138 * Output::               Format of output from the C preprocessor.
139 * Unreliable Features::  Undefined behavior and deprecated features.
140 * Invocation::           How to invoke the preprocessor; command options.
141 * Concept Index::        Index of concepts and terms.
142 * Index::                Index of directives, predefined macros and options.
143 @end menu
144
145 @node Global Actions, Directives, Top, Top
146 @section Transformations Made Globally
147 @cindex ASCII NUL handling
148
149 Most C preprocessor features are inactive unless you give specific
150 directives to request their use.  (Preprocessing directives are lines
151 starting with a @samp{#} token, possibly preceded by whitespace;
152 @pxref{Directives}).  However, there are three transformations that the
153 preprocessor always makes on all the input it receives, even in the
154 absence of directives.
155
156 @itemize @bullet
157 @item
158 Trigraphs, if enabled, are replaced with the character they represent.
159 Conceptually, this is the very first action undertaken, just before
160 backslash-newline deletion.
161
162 @item
163 Backslash-newline sequences are deleted, no matter where.  This
164 feature allows you to break long lines for cosmetic purposes without
165 changing their meaning.
166
167 @item
168 All C comments are replaced with single spaces.
169
170 @item
171 Predefined macro names are replaced with their expansions
172 (@pxref{Predefined}).
173 @end itemize
174
175 The first three transformations are done @emph{before} nearly all other
176 parsing and before preprocessing directives are recognized.  Thus, for
177 example, you can split a line cosmetically with backslash-newline
178 anywhere (except within trigraphs since they are replaced first; see
179 below).
180
181 @example
182 /*
183 */ # /*
184 */ defi\
185 ne FO\
186 O 10\
187 20
188 @end example
189
190 @noindent
191 is equivalent into @samp{#define FOO 1020}.  You can split even an
192 escape sequence with backslash-newline.  For example, you can split
193 @code{"foo\bar"} between the @samp{\} and the @samp{b} to get
194
195 @example
196 "foo\\
197 bar"
198 @end example
199
200 @noindent
201 This behavior can be confusing: in all other contexts, a backslash can
202 be inserted in a string constant as an ordinary character by writing a
203 double backslash.  This is an exception, but the ISO C standard requires
204 it.  (Strict ISO C does not allow string constants to extend to more
205 than one logical line, so they do not consider this a problem.)
206
207 There are a few exceptions to all three transformations.
208
209 @itemize @bullet
210 @item
211 C comments and predefined macro names are not recognized inside a
212 @samp{#include} directive in which the file name is delimited with
213 @samp{<} and @samp{>}.  What lies in-between is read literally.
214
215 @item
216 C comments and predefined macro names are never recognized within a
217 character or string constant.  (Strictly speaking, this is the rule,
218 not an exception, but it is worth noting here anyway.)
219
220 @item
221 Backslash-newline may not safely be used within an ISO ``trigraph'',
222 since trigraphs are converted before backslash-newlines are deleted.  If
223 you write what looks like a trigraph with a backslash-newline inside,
224 the backslash-newline is deleted as usual, but it is then too late to
225 recognize the trigraph.
226
227 This is relevant only if you use the @samp{-trigraphs} option to enable
228 trigraph processing.  @xref{Invocation}.
229 @end itemize
230
231 The preprocessor handles null characters embedded in the input file
232 depending upon the context in which the null appears.  Note that here we
233 are referring not to the two-character escape sequence "\0", but to the
234 single character ASCII NUL.
235
236 There are three different contexts in which a null character may
237 appear:-
238
239 @itemize @bullet
240 @item
241 Within comments.  Here, null characters are silently ignored.
242
243 @item
244 Within a string or character constant.  Here the preprocessor emits a
245 warning, but preserves the null character and passes it through to the
246 output file or compiler front-end.
247
248 @item
249 In any other context, the preprocessor issues a warning, and discards
250 the null character.  The preprocessor treats it like whitespace,
251 combining it with any surrounding whitespace to become a single
252 whitespace block.  Representing the null character by "^@@", this means
253 that code like
254
255 @example
256 #define X^@@1
257 @end example
258
259 is equivalent to
260
261 @example
262 #define X 1
263 @end example
264
265 and X is defined with replacement text "1".
266 @end itemize
267
268 @node Directives, Header Files, Global Actions, Top
269 @section Preprocessing Directives
270
271 @cindex preprocessing directives
272 @cindex directives
273 Most preprocessor features are active only if you use preprocessing
274 directives to request their use.
275
276 Preprocessing directives are lines in your program that start with
277 @samp{#}.  Whitespace is allowed before and after the @samp{#}.  The
278 @samp{#} is followed by an identifier that is the @dfn{directive name}.
279 For example, @samp{#define} is the directive that defines a macro.
280
281 Since the @samp{#} must be the first token on the line, it cannot come
282 from a macro expansion if you wish it to begin a directive.  Also, the
283 directive name is not macro expanded.  Thus, if @samp{foo} is defined as
284 a macro expanding to @samp{define}, that does not make @samp{#foo} a
285 valid preprocessing directive.
286
287 The set of valid directive names is fixed.  Programs cannot define new
288 preprocessing directives.
289
290 Some directive names require arguments; these make up the rest of the
291 directive line and must be separated from the directive name by
292 whitespace.  For example, @samp{#define} must be followed by a macro
293 name and the intended expansion of the macro.  @xref{Object-like
294 Macros}.
295
296 A preprocessing directive cannot cover more than one line.  It may be
297 logically extended with backslash-newline, but that has no effect on its
298 meaning.  Comments containing newlines can also divide the directive
299 into multiple lines, but a comment is replaced by a single space before
300 the directive is interpreted.
301
302 @node Header Files, Macros, Directives, Top
303 @section Header Files
304
305 @cindex header file
306 A header file is a file containing C declarations and macro definitions
307 (@pxref{Macros}) to be shared between several source files.  You request
308 the use of a header file in your program with the C preprocessing
309 directive @samp{#include}.
310
311 @menu
312 * Header Uses::         What header files are used for.
313 * Include Syntax::      How to write @samp{#include} directives.
314 * Include Operation::   What @samp{#include} does.
315 * Once-Only::           Preventing multiple inclusion of one header file.
316 * Inheritance::         Including one header file in another header file.
317 * System Headers::      Special treatment for some header files.
318 @end menu
319
320 @node Header Uses, Include Syntax, Header Files, Header Files
321 @subsection Uses of Header Files
322
323 Header files serve two kinds of purposes.
324
325 @itemize @bullet
326 @item
327 @cindex system header files
328 System header files declare the interfaces to parts of the operating
329 system.  You include them in your program to supply the definitions and
330 declarations you need to invoke system calls and libraries.
331
332 @item
333 Your own header files contain declarations for interfaces between the
334 source files of your program.  Each time you have a group of related
335 declarations and macro definitions all or most of which are needed in
336 several different source files, it is a good idea to create a header
337 file for them.
338 @end itemize
339
340 Including a header file produces the same results in C compilation as
341 copying the header file into each source file that needs it.  Such
342 copying would be time-consuming and error-prone.  With a header file,
343 the related declarations appear in only one place.  If they need to be
344 changed, they can be changed in one place, and programs that include the
345 header file will automatically use the new version when next recompiled.
346 The header file eliminates the labor of finding and changing all the
347 copies as well as the risk that a failure to find one copy will result
348 in inconsistencies within a program.
349
350 The usual convention is to give header files names that end with
351 @file{.h}.  Avoid unusual characters in header file names, as they
352 reduce portability.
353
354 @node Include Syntax, Include Operation, Header Uses, Header Files
355 @subsection The @samp{#include} Directive
356
357 @findex #include
358 Both user and system header files are included using the preprocessing
359 directive @samp{#include}.  It has three variants:
360
361 @table @code
362 @item #include <@var{file}>
363 This variant is used for system header files.  It searches for a file
364 named @var{file} in a list of directories specified by you, then in a
365 standard list of system directories.  You specify directories to search
366 for header files with the command option @samp{-I} (@pxref{Invocation}).
367 The option @samp{-nostdinc} inhibits searching the standard system
368 directories; in this case only the directories you specify are searched.
369
370 The parsing of this form of @samp{#include} is slightly special because
371 comments are not recognized within the @samp{<@dots{}>}.  Thus, in
372 @samp{#include <x/*y>} the @samp{/*} does not start a comment and the
373 directive specifies inclusion of a system header file named @file{x/*y}.
374 Of course, a header file with such a name is unlikely to exist on Unix,
375 where shell wildcard features would make it hard to manipulate.@refill
376
377 The first @samp{>} character terminates the file name.  The file name
378 may contain a @samp{<} character.
379
380 @item #include "@var{file}"
381 This variant is used for header files of your own program.  It searches
382 for a file named @var{file} first in the current directory, then in the
383 same directories used for system header files.  The current directory is
384 the directory of the current input file.  It is tried first because it
385 is presumed to be the location of the files that the current input file
386 refers to.  (If the @samp{-I-} option is used, the special treatment of
387 the current directory is inhibited.)
388
389 The first @samp{"} character terminates the file name.  If backslashes
390 occur within @var{file}, they are considered ordinary text characters,
391 not escape characters.  None of the character escape sequences
392 appropriate to string constants in C are processed.  Thus,
393 @samp{#include "x\n\\y"} specifies a filename containing three
394 backslashes.
395
396 @item #include @var{anything else}
397 @cindex computed @samp{#include}
398 This variant is called a @dfn{computed #include}.  Any @samp{#include}
399 directive whose argument does not fit the above two forms is a computed
400 include.  The text @var{anything else} is checked for macro calls, which
401 are expanded (@pxref{Macros}).  When this is done, the result must match
402 one of the above two variants --- in particular, the expansion must form
403 a string literal token, or a sequence of tokens surrounded by angle
404 braces.  @xref{Unreliable Features}
405
406 This feature allows you to define a macro which controls the file name
407 to be used at a later point in the program.  One application of this is
408 to allow a site-specific configuration file for your program to specify
409 the names of the system include files to be used.  This can help in
410 porting the program to various operating systems in which the necessary
411 system header files are found in different places.
412 @end table
413
414 @node Include Operation, Once-Only, Include Syntax, Header Files
415 @subsection How @samp{#include} Works
416
417 The @samp{#include} directive works by directing the C preprocessor to
418 scan the specified file as input before continuing with the rest of the
419 current file.  The output from the preprocessor contains the output
420 already generated, followed by the output resulting from the included
421 file, followed by the output that comes from the text after the
422 @samp{#include} directive.  For example, given a header file
423 @file{header.h} as follows,
424
425 @example
426 char *test ();
427 @end example
428
429 @noindent
430 and a main program called @file{program.c} that uses the header file,
431 like this,
432
433 @example
434 int x;
435 #include "header.h"
436
437 main ()
438 @{
439   printf (test ());
440 @}
441 @end example
442
443 @noindent
444 the output generated by the C preprocessor for @file{program.c} as input
445 would be
446
447 @example
448 int x;
449 char *test ();
450
451 main ()
452 @{
453   printf (test ());
454 @}
455 @end example
456
457 Included files are not limited to declarations and macro definitions;
458 those are merely the typical uses.  Any fragment of a C program can be
459 included from another file.  The include file could even contain the
460 beginning of a statement that is concluded in the containing file, or
461 the end of a statement that was started in the including file.  However,
462 a comment or a string or character constant may not start in the
463 included file and finish in the including file.  An unterminated
464 comment, string constant or character constant in an included file is
465 considered to end (with an error message) at the end of the file.
466
467 It is possible for a header file to begin or end a syntactic unit such
468 as a function definition, but that would be very confusing, so don't do
469 it.
470
471 The line following the @samp{#include} directive is always treated as a
472 separate line by the C preprocessor, even if the included file lacks a
473 final newline.
474
475 @node Once-Only, Inheritance, Include Operation, Header Files
476 @subsection Once-Only Include Files
477 @cindex repeated inclusion
478 @cindex including just once
479
480 Very often, one header file includes another.  It can easily result that
481 a certain header file is included more than once.  This may lead to
482 errors, if the header file defines structure types or typedefs, and is
483 certainly wasteful.  Therefore, we often wish to prevent multiple
484 inclusion of a header file.
485
486 The standard way to do this is to enclose the entire real contents of the
487 file in a conditional, like this:
488
489 @example
490 #ifndef FILE_FOO_SEEN
491 #define FILE_FOO_SEEN
492
493 @var{the entire file}
494
495 #endif /* FILE_FOO_SEEN */
496 @end example
497
498 The macro @code{FILE_FOO_SEEN} indicates that the file has been included
499 once already.  In a user header file, the macro name should not begin
500 with @samp{_}.  In a system header file, this name should begin with
501 @samp{__} to avoid conflicts with user programs.  In any kind of header
502 file, the macro name should contain the name of the file and some
503 additional text, to avoid conflicts with other header files.
504
505 The GNU C preprocessor is programmed to notice when a header file uses
506 this particular construct and handle it efficiently.  If a header file
507 is contained entirely in a @samp{#ifndef} conditional, modulo whitespace
508 and comments, then it remembers that fact.  If a subsequent
509 @samp{#include} specifies the same file, and the macro in the
510 @samp{#ifndef} is already defined, then the directive is skipped without
511 processing the specified file at all.
512
513 @findex #import
514 In the Objective C language, there is a variant of @samp{#include}
515 called @samp{#import} which includes a file, but does so at most once.
516 If you use @samp{#import} @emph{instead of} @samp{#include}, then you
517 don't need the conditionals inside the header file to prevent multiple
518 execution of the contents.
519
520 @samp{#import} is obsolete because it is not a well designed feature.
521 It requires the users of a header file --- the applications programmers
522 --- to know that a certain header file should only be included once.  It
523 is much better for the header file's implementor to write the file so
524 that users don't need to know this.  Using @samp{#ifndef} accomplishes
525 this goal.
526
527 @node Inheritance, System Headers, Once-Only, Header Files
528 @subsection Inheritance and Header Files
529 @cindex inheritance
530 @cindex overriding a header file
531
532 @dfn{Inheritance} is what happens when one object or file derives some
533 of its contents by virtual copying from another object or file.  In
534 the case of C header files, inheritance means that one header file 
535 includes another header file and then replaces or adds something.
536
537 If the inheriting header file and the base header file have different
538 names, then inheritance is straightforward: simply write @samp{#include
539 "@var{base}"} in the inheriting file.
540
541 Sometimes it is necessary to give the inheriting file the same name as
542 the base file.  This is less straightforward.
543
544 For example, suppose an application program uses the system header
545 @file{sys/signal.h}, but the version of @file{/usr/include/sys/signal.h}
546 on a particular system doesn't do what the application program expects.
547 It might be convenient to define a ``local'' version, perhaps under the
548 name @file{/usr/local/include/sys/signal.h}, to override or add to the
549 one supplied by the system.
550
551 You can do this by compiling with the option @samp{-I.}, and writing a
552 file @file{sys/signal.h} that does what the application program expects.
553 Making this file include the standard @file{sys/signal.h} is not so easy
554 --- writing @samp{#include <sys/signal.h>} in that file doesn't work,
555 because it includes your own version of the file, not the standard
556 system version.  Used in that file itself, this leads to an infinite
557 recursion and a fatal error in compilation.
558
559 @samp{#include </usr/include/sys/signal.h>} would find the proper file,
560 but that is not clean, since it makes an assumption about where the
561 system header file is found.  This is bad for maintenance, since it
562 means that any change in where the system's header files are kept
563 requires a change somewhere else.
564
565 @findex #include_next
566 The clean way to solve this problem is to use 
567 @samp{#include_next}, which means, ``Include the @emph{next} file with
568 this name.''  This directive works like @samp{#include} except in
569 searching for the specified file: it starts searching the list of header
570 file directories @emph{after} the directory in which the current file
571 was found.
572
573 Suppose you specify @samp{-I /usr/local/include}, and the list of
574 directories to search also includes @file{/usr/include}; and suppose
575 both directories contain @file{sys/signal.h}.  Ordinary @samp{#include
576 <sys/signal.h>} finds the file under @file{/usr/local/include}.  If that
577 file contains @samp{#include_next <sys/signal.h>}, it starts searching
578 after that directory, and finds the file in @file{/usr/include}.
579
580 @samp{#include_next} is a GCC extension and should not be used in
581 programs intended to be portable to other compilers.
582
583 @node System Headers,, Inheritance, Header Files
584 @subsection System Headers
585 @cindex system header files
586
587 The header files declaring interfaces to the operating system and
588 runtime libraries often cannot be written in strictly conforming C.
589 Therefore, GNU C gives code found in @dfn{system headers} special
590 treatment.  Certain categories of warnings are suppressed, notably those
591 enabled by @samp{-pedantic}.
592
593 Normally, only the headers found in specific directories are considered
594 system headers.  The set of these directories is determined when GCC is
595 compiled.  There are, however, two ways to add to the set.
596
597 @findex -isystem
598 The @samp{-isystem} command line option adds its argument to the list of
599 directories to search for headers, just like @samp{-I}.  In addition,
600 any headers found in that directory will be considered system headers.
601 Note that unlike @samp{-I}, you must put a space between @samp{-isystem}
602 and its argument.
603
604 All directories named by @samp{-isystem} are searched @strong{after} all
605 directories named by @samp{-I}, no matter what their order was on the
606 command line.  If the same directory is named by both @samp{-I} and
607 @samp{-isystem}, @samp{-I} wins; it is as if the @samp{-isystem} option
608 had never been specified at all.
609
610 @findex #pragma GCC system_header
611 There is also a directive, @samp{#pragma GCC system_header}, which tells
612 GCC to consider the rest of the current include file a system header, no
613 matter where it was found.  Code that comes before the @samp{#pragma} in
614 the file will not be affected.
615
616 @samp{#pragma GCC system_header} has no effect in the primary source file.
617
618 @node Macros, Conditionals, Header Files, Top
619 @section Macros
620
621 A macro is a sort of abbreviation which you can define once and then
622 use later.  There are many complicated features associated with macros
623 in the C preprocessor.
624
625 @menu
626 * Object-like Macros::   Macros that always expand the same way.
627 * Function-like Macros:: Macros that accept arguments that are substituted
628                          into the macro expansion.
629 * Macro Varargs::        Macros with variable number of arguments.
630 * Predefined::           Predefined macros that are always available.
631 * Stringification::      Macro arguments converted into string constants.
632 * Concatenation::        Building tokens from parts taken from macro arguments.
633 * Undefining::           Cancelling a macro's definition.
634 * Redefining::           Changing a macro's definition.
635 * Poisoning::            Ensuring a macro is never defined or used.
636 * Macro Pitfalls::       Macros can confuse the unwary.  Here we explain
637                            several common problems and strange features.
638 @end menu
639
640 @node Object-like Macros, Function-like Macros, Macros, Macros
641 @subsection Object-like Macros
642 @cindex object-like macro
643 @cindex manifest constant
644
645 An @dfn{object-like macro} is a kind of abbreviation.  It is a name
646 which stands for a fragment of code.  Some people refer to these as
647 @dfn{manifest constants}.
648
649 Before you can use a macro, you must @dfn{define} it explicitly with the
650 @samp{#define} directive.  @samp{#define} is followed by the name of the
651 macro and then the token sequence it should be an abbreviation for,
652 which is variously referred to as the macro's @dfn{body},
653 @dfn{expansion} or @dfn{replacement list}.  For example,
654
655 @example
656 #define BUFFER_SIZE 1020
657 @end example
658
659 @noindent
660 defines a macro named @samp{BUFFER_SIZE} as an abbreviation for the
661 token @samp{1020}.  If somewhere after this @samp{#define} directive
662 there comes a C statement of the form
663
664 @example
665 foo = (char *) xmalloc (BUFFER_SIZE);
666 @end example
667
668 @noindent
669 then the C preprocessor will recognize and @dfn{expand} the macro
670 @samp{BUFFER_SIZE}, resulting in
671
672 @example
673 foo = (char *) xmalloc (1020);
674 @end example
675
676 The use of all upper case for macro names is a standard convention.
677 Programs are easier to read when it is possible to tell at a glance
678 which names are macros.
679
680 Normally, a macro definition can only span a single logical line, like
681 all C preprocessing directives.  Comments within a macro definition may
682 contain newlines, which make no difference since each comment is
683 replaced by a space regardless of its contents.
684
685 Apart from this, there is no restriction on what can go in a macro body
686 provided it decomposes into valid preprocessing tokens.  In particular,
687 parentheses need not balance, and the body need not resemble valid C
688 code.  (If it does not, you may get error messages from the C
689 compiler when you use the macro.)
690
691 The C preprocessor scans your program sequentially, so macro definitions
692 take effect at the place you write them.  Therefore, the following input
693 to the C preprocessor
694
695 @example
696 foo = X;
697 #define X 4
698 bar = X;
699 @end example
700
701 @noindent
702 produces as output
703
704 @example
705 foo = X;
706
707 bar = 4;
708 @end example
709
710 When the preprocessor expands a macro name, the macro's expansion
711 replaces the macro invocation, and the result is re-scanned for more
712 macros to expand.  For example, after
713
714 @example
715 #define BUFSIZE 1020
716 #define TABLESIZE BUFSIZE
717 @end example
718
719 @noindent
720 the name @samp{TABLESIZE} when used in the program would go through two
721 stages of expansion, resulting ultimately in @samp{1020}.
722
723 This is not the same as defining @samp{TABLESIZE} to be @samp{1020}.
724 The @samp{#define} for @samp{TABLESIZE} uses exactly the expansion you
725 specify --- in this case, @samp{BUFSIZE} --- and does not check to see
726 whether it too contains macro names.  Only when you @emph{use}
727 @samp{TABLESIZE} is the result of its expansion scanned for more macro
728 names.  @xref{Cascaded Macros}.
729
730 @node Function-like Macros, Macro Varargs, Object-like Macros, Macros
731 @subsection Macros with Arguments
732 @cindex macros with argument
733 @cindex arguments in macro definitions
734 @cindex function-like macro
735
736 An object-like macro is always replaced by exactly the same tokens each
737 time it is used.  Macros can be made more flexible by taking
738 @dfn{arguments}.  Arguments are fragments of code that you supply each
739 time the macro is used.  These fragments are included in the expansion
740 of the macro according to the directions in the macro definition.  A
741 macro that accepts arguments is called a @dfn{function-like macro}
742 because the syntax for using it looks like a function call.
743
744 @findex #define
745 To define a macro that uses arguments, you write a @samp{#define}
746 directive with a list of @dfn{parameters} in parentheses after the name
747 of the macro.  The parameters must be valid C identifiers, separated by
748 commas and optionally whitespace.  The @samp{(} must follow the macro
749 name immediately, with no space in between.  If you leave a space, you
750 instead define an object-like macro whose expansion begins with a
751 @samp{(}, and often leads to confusing errors at compile time.
752
753 As an example, here is a macro that computes the minimum of two numeric
754 values, as it is defined in many C programs:
755
756 @example
757 #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
758 @end example
759
760 @noindent
761 (This is not the best way to define a ``minimum'' macro in GNU C@.
762 @xref{Side Effects}, for more information.)
763
764 To invoke a function-like macro, you write the name of the macro
765 followed by a list of @dfn{arguments} in parentheses, separated by
766 commas.  The invocation of the macro need not be restricted to a single
767 logical line - it can cross as many lines in the source file as you
768 wish.  The number of arguments you give must match the number of
769 parameters in the macro definition; empty arguments are fine.  Examples
770 of use of the macro @samp{min} include @samp{min (1, 2)} and @samp{min
771 (x + 28, *p)}.
772
773 The expansion text of the macro depends on the arguments you use.  Each
774 macro parameter is replaced throughout the macro expansion with the
775 tokens of the corresponding argument.  Leading and trailing argument
776 whitespace is dropped, and all whitespace between the tokens of an
777 argument is reduced to a single space.  Using the same macro @samp{min}
778 defined above, @samp{min (1, 2)} expands into
779
780 @example
781 ((1) < (2) ? (1) : (2))
782 @end example
783
784 @noindent
785 where @samp{1} has been substituted for @samp{X} and @samp{2} for @samp{Y}.
786
787 Likewise, @samp{min (x + 28, *p)} expands into
788
789 @example
790 ((x + 28) < (*p) ? (x + 28) : (*p))
791 @end example
792
793 Parentheses within each argument must balance; a comma within such
794 parentheses does not end the argument.  However, there is no requirement
795 for square brackets or braces to balance, and they do not prevent a
796 comma from separating arguments.  Thus,
797
798 @example
799 macro (array[x = y, x + 1])
800 @end example
801
802 @noindent
803 passes two arguments to @code{macro}: @samp{array[x = y} and @samp{x +
804 1]}.  If you want to supply @samp{array[x = y, x + 1]} as an argument,
805 you must write it as @samp{array[(x = y, x + 1)]}, which is equivalent C
806 code.
807
808 After the arguments have been substituted into the macro body, the
809 resulting expansion replaces the macro invocation, and re-scanned for
810 more macro calls.  Therefore even arguments can contain calls to other
811 macros, either with or without arguments, and even to the same macro.
812 For example, @samp{min (min (a, b), c)} expands into this text:
813
814 @example
815 ((((a) < (b) ? (a) : (b))) < (c)
816  ? (((a) < (b) ? (a) : (b)))
817  : (c))
818 @end example
819
820 @noindent
821 (Line breaks shown here for clarity would not actually be generated.)
822
823 @cindex empty macro arguments
824 If a macro @code{foo} takes one argument, and you want to supply an
825 empty argument, simply supply no preprocessing tokens.  Since whitespace
826 does not form a preprocessing token, it is optional.  For example,
827 @samp{foo ()}, @samp{foo ( )} and @samp{bar (, arg2)}.
828
829 Previous GNU preprocessor implementations and documentation were
830 incorrect on this point, insisting that a function-like macro that takes
831 a single argument be passed a space if an empty argument was required.
832
833 If you use a macro name followed by something other than a @samp{(}
834 (after ignoring any whitespace that might follow), it does not form an
835 invocation of the macro, and the preprocessor does not change what you
836 have written.  Therefore, it is possible for the same identifier to be a
837 variable or function in your program as well as a macro, and you can
838 choose in each instance whether to refer to the macro (if an actual
839 argument list follows) or the variable or function (if an argument list
840 does not follow).  For example,
841
842 @example
843 #define foo(X) X
844 foo bar foo(baz)
845 @end example
846
847 expands to @samp{foo bar baz}.  Such dual use of one name could be
848 confusing and should be avoided except when the two meanings are
849 effectively synonymous: that is, when the name is both a macro and a
850 function and the two have similar effects.  You can think of the name
851 simply as a function; use of the name for purposes other than calling it
852 (such as, to take the address) will refer to the function, while calls
853 will expand the macro and generate better but equivalent code.
854
855 For example, you can use a function named @samp{min} in the same source
856 file that defines the macro.  If you write @samp{&min} with no argument
857 list, you refer to the function.  If you write @samp{min (x, bb)}, with
858 an argument list, the macro is expanded.  If you write @samp{(min) (a,
859 bb)}, where the name @samp{min} is not followed by an open-parenthesis,
860 the macro is not expanded, so you wind up with a call to the function
861 @samp{min}.
862
863 In the definition of a macro with arguments, the list of argument names
864 must follow the macro name immediately with no space in between.  If
865 there is a space after the macro name, the macro is defined as taking no
866 arguments, and all the rest of the line is taken to be the expansion.
867 The reason for this is that it is often useful to define a macro that
868 takes no arguments and whose definition begins with an identifier in
869 parentheses.  This rule makes it possible for you to do either this:
870
871 @example
872 #define FOO(x) - 1 / (x)
873 @end example
874
875 @noindent
876 (which defines @samp{FOO} to take an argument and expand into minus the
877 reciprocal of that argument) or this:
878
879 @example
880 #define BAR (x) - 1 / (x)
881 @end example
882
883 @noindent
884 (which defines @samp{BAR} to take no argument and always expand into
885 @samp{(x) - 1 / (x)}).
886
887 Note that the @emph{uses} of a macro with arguments can have spaces
888 before the left parenthesis; it's the @emph{definition} where it matters
889 whether there is a space.
890
891 @node Macro Varargs, Predefined, Function-like Macros, Macros
892 @subsection Macros with Variable Numbers of Arguments
893 @cindex variable number of arguments
894 @cindex macro with variable arguments
895 @cindex rest argument (in macro)
896
897 In the ISO C standard of 1999, a macro can be declared to accept a
898 variable number of arguments much as a function can.  The syntax for
899 defining the macro is similar to that of a function.  Here is an
900 example:
901
902 @example
903 #define eprintf(...) fprintf (stderr, __VA_ARGS__)
904 @end example
905
906 Here @samp{@dots{}} is a @dfn{variable argument}.  In the invocation of
907 such a macro, it represents the zero or more tokens until the closing
908 parenthesis that ends the invocation, including any commas.  This set of
909 tokens replaces the identifier @code{__VA_ARGS__} in the macro body
910 wherever it appears.  Thus, we have this expansion:
911
912 @example
913 eprintf ("%s:%d: ", input_file_name, line_number)
914 @expansion{}
915 fprintf (stderr, "%s:%d: " , input_file_name, line_number)
916 @end example
917
918 We might instead have defined eprintf as follows:-
919
920 @example
921 #define eprintf(format, ...) fprintf (stderr, format, __VA_ARGS__)
922 @end example
923
924 This formulation looks more descriptive, but unfortunately causes
925 problems if fprintf wants no arguments the format.  There is no way to
926 produce expanded output of
927
928 @example
929 fprintf (stderr, "success!\n")
930 @end example
931
932 @noindent
933 since passing an empty argument for the variable arguments part like this
934
935 @example
936 eprintf ("success!\n", )
937 @end example
938
939 @noindent
940 produces
941
942 @example
943 fprintf (stderr, "success!\n",)
944 @end example
945
946 @noindent
947 where the extra comma originates from the replacement list and not from
948 the arguments to eprintf.
949
950 Within a @samp{#define} directive, ISO C mandates that the only place
951 the identifier @code{__VA_ARGS__} can appear is in the replacement list
952 of a variable-argument macro.  Using it as a macro name, macro argument
953 or within a different type of macro is illegal.
954
955 Before standardization, previous GNU preprocessors implemented a
956 slightly different syntax for defining variable-argument macros.  The
957 macros were called ``rest args macros''.  You could assign a name to the
958 variable arguments, by contrast the standardized method leaves them
959 anonymous.  For example, the eprintf macro could have been defined like
960 this
961
962 @example
963 #define eprintf(format...) fprintf (stderr, format)
964 @end example
965
966 Now that there is a standardized construct, you are encouraged to use
967 that instead.  It is unlikely that support for named variable arguments
968 will be removed in future revisions of CPP, since being able to assign a
969 name is descriptive, and there is a wide base of legacy code.  However,
970 two obscure features of the GNU style are deprecated and likely to be
971 dropped in future.  @xref{Unreliable Features}.
972
973 @node Predefined, Stringification, Macro Varargs, Macros
974 @subsection Predefined Macros
975
976 @cindex predefined macros
977 Several object-like macros are predefined; you use them without
978 supplying their definitions.  They fall into two classes: standard
979 macros and system-specific macros.
980
981 @menu
982 * Standard Predefined::     Standard predefined macros.
983 * Nonstandard Predefined::  Nonstandard predefined macros.
984 @end menu
985
986 @node Standard Predefined, Nonstandard Predefined, Predefined, Predefined
987 @subsubsection Standard Predefined Macros
988 @cindex standard predefined macros
989
990 The standard predefined macros are available with the same meanings
991 regardless of the machine or operating system on which you are using GNU
992 C@.  Their names all start and end with double underscores.  Those
993 preceding @code{__GNUC__} in this table are standardized by ISO C; the
994 rest are GNU C extensions.
995
996 @table @code
997 @item __FILE__
998 @findex __FILE__
999 This macro expands to the name of the current input file, in the form of
1000 a C string constant.  The precise name returned is the one that was
1001 specified in @samp{#include} or as the input file name argument.  For
1002 example, @samp{"/usr/local/include/myheader.h"} is a possible expansion
1003 of this macro.
1004
1005 @item __LINE__
1006 @findex __LINE__
1007 This macro expands to the current input line number, in the form of a
1008 decimal integer constant.  While we call it a predefined macro, it's
1009 a pretty strange macro, since its ``definition'' changes with each
1010 new line of source code.
1011
1012 This and @samp{__FILE__} are useful in generating an error message to
1013 report an inconsistency detected by the program; the message can state
1014 the source line at which the inconsistency was detected.  For example,
1015
1016 @smallexample
1017 fprintf (stderr, "Internal error: "
1018                  "negative string length "
1019                  "%d at %s, line %d.",
1020          length, __FILE__, __LINE__);
1021 @end smallexample
1022
1023 A @samp{#include} directive changes the expansions of @samp{__FILE__}
1024 and @samp{__LINE__} to correspond to the included file.  At the end of
1025 that file, when processing resumes on the input file that contained
1026 the @samp{#include} directive, the expansions of @samp{__FILE__} and
1027 @samp{__LINE__} revert to the values they had before the
1028 @samp{#include} (but @samp{__LINE__} is then incremented by one as
1029 processing moves to the line after the @samp{#include}).
1030
1031 The expansions of both @samp{__FILE__} and @samp{__LINE__} are altered
1032 if a @samp{#line} directive is used.  @xref{Line Control}.
1033
1034 @item __DATE__
1035 @findex __DATE__
1036 This macro expands to a string constant that describes the date on
1037 which the preprocessor is being run.  The string constant contains
1038 eleven characters and looks like @w{@samp{"Feb  1 1996"}}.
1039 @c After reformatting the above, check that the date remains `Feb  1 1996',
1040 @c all on one line, with two spaces between the `Feb' and the `1'.
1041
1042 @item __TIME__
1043 @findex __TIME__
1044 This macro expands to a string constant that describes the time at
1045 which the preprocessor is being run.  The string constant contains
1046 eight characters and looks like @samp{"23:59:01"}.
1047
1048 @item __STDC__
1049 @findex __STDC__
1050 This macro expands to the constant 1, to signify that this is ISO
1051 Standard C@.  (Whether that is actually true depends on what C compiler
1052 will operate on the output from the preprocessor.)
1053
1054 On some hosts, system include files use a different convention, where
1055 @samp{__STDC__} is normally 0, but is 1 if the user specifies strict
1056 conformance to the C Standard.  The preprocessor follows the host
1057 convention when processing system include files, but when processing
1058 user files it follows the usual GNU C convention.
1059
1060 This macro is not defined if the @samp{-traditional} option is used.
1061
1062 @item __STDC_VERSION__
1063 @findex __STDC_VERSION__
1064 This macro expands to the C Standard's version number, a long integer
1065 constant of the form @samp{@var{yyyy}@var{mm}L} where @var{yyyy} and
1066 @var{mm} are the year and month of the Standard version.  This signifies
1067 which version of the C Standard the preprocessor conforms to.  Like
1068 @samp{__STDC__}, whether this version number is accurate for the entire
1069 implementation depends on what C compiler will operate on the output
1070 from the preprocessor.
1071
1072 This macro is not defined if the @samp{-traditional} option is used.
1073
1074 @item __GNUC__
1075 @findex __GNUC__
1076 This macro is defined if and only if this is GNU C@.  This macro is
1077 defined only when the entire GNU C compiler is in use; if you invoke the
1078 preprocessor directly, @samp{__GNUC__} is undefined.  The value
1079 identifies the major version number of GNU CC (@samp{1} for GNU CC
1080 version 1, which is now obsolete, and @samp{2} for version 2).
1081
1082 @item __GNUC_MINOR__
1083 @findex __GNUC_MINOR__
1084 The macro contains the minor version number of the compiler.  This can
1085 be used to work around differences between different releases of the
1086 compiler (for example, if GCC 2.6.3 is known to support a feature, you
1087 can test for @code{__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 6)}).
1088
1089 @item __GNUC_PATCHLEVEL__
1090 @findex __GNUC_PATCHLEVEL__
1091 This macro contains the patch level of the compiler.  This can be
1092 used to work around differences between different patch level releases
1093 of the compiler (for example, if GCC 2.6.2 is known to contain a bug,
1094 whereas GCC 2.6.3 contains a fix, and you have code which can workaround
1095 the problem depending on whether the bug is fixed or not, you can test for
1096 @code{__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 6) || 
1097 (__GNUC__ == 2 && __GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 3)}).
1098
1099 @item __GNUG__
1100 @findex __GNUG__
1101 The GNU C compiler defines this when the compilation language is
1102 C++; use @samp{__GNUG__} to distinguish between GNU C and GNU
1103 C++.
1104
1105 @item __cplusplus 
1106 @findex __cplusplus 
1107 The ISO standard for C++ requires predefining this variable.  You can
1108 use @samp{__cplusplus} to test whether a header is compiled by a C
1109 compiler or a C++ compiler. The compiler currently uses a value of
1110 @samp{1}, instead of the value @samp{199711L}, which would indicate full
1111 conformance with the standard.
1112
1113 @item __STRICT_ANSI__
1114 @findex __STRICT_ANSI__
1115 GNU C defines this macro if and only if the @samp{-ansi} switch was
1116 specified when GNU C was invoked.  Its definition is the null string.
1117 This macro exists primarily to direct certain GNU header files not to
1118 define certain traditional Unix constructs which are incompatible with
1119 ISO C@.
1120
1121 @item __BASE_FILE__
1122 @findex __BASE_FILE__
1123 This macro expands to the name of the main input file, in the form
1124 of a C string constant.  This is the source file that was specified
1125 on the command line of the preprocessor or C compiler.
1126
1127 @item __INCLUDE_LEVEL__
1128 @findex __INCLUDE_LEVEL_
1129 This macro expands to a decimal integer constant that represents the
1130 depth of nesting in include files.  The value of this macro is
1131 incremented on every @samp{#include} directive and decremented at the
1132 end of every included file.  It starts out at 0, it's value within the
1133 base file specified on the command line.
1134
1135 @item __VERSION__
1136 @findex __VERSION__
1137 This macro expands to a string constant which describes the version
1138 number of GNU C@.  The string is normally a sequence of decimal numbers
1139 separated by periods, such as @samp{"2.6.0"}.
1140
1141 @item __OPTIMIZE__
1142 @findex __OPTIMIZE__
1143 GNU CC defines this macro in optimizing compilations.  It causes certain
1144 GNU header files to define alternative macro definitions for some system
1145 library functions.  You should not refer to or test the definition of
1146 this macro unless you make very sure that programs will execute with the
1147 same effect regardless.
1148
1149 @item __CHAR_UNSIGNED__
1150 @findex __CHAR_UNSIGNED__
1151 GNU C defines this macro if and only if the data type @code{char} is
1152 unsigned on the target machine.  It exists to cause the standard header
1153 file @file{limits.h} to work correctly.  You should not refer to this
1154 macro yourself; instead, refer to the standard macros defined in
1155 @file{limits.h}.  The preprocessor uses this macro to determine whether
1156 or not to sign-extend large character constants written in octal; see
1157 @ref{#if Directive,,The @samp{#if} Directive}.
1158
1159 @item __REGISTER_PREFIX__
1160 @findex __REGISTER_PREFIX__
1161 This macro expands to a string (not a string constant) describing the
1162 prefix applied to CPU registers in assembler code.  You can use it to
1163 write assembler code that is usable in multiple environments.  For
1164 example, in the @samp{m68k-aout} environment it expands to the null
1165 string, but in the @samp{m68k-coff} environment it expands to the string
1166 @samp{%}.
1167
1168 @item __USER_LABEL_PREFIX__
1169 @findex __USER_LABEL_PREFIX__
1170 Similar to @code{__REGISTER_PREFIX__}, but describes the prefix applied
1171 to user generated labels in assembler code.  For example, in the
1172 @samp{m68k-aout} environment it expands to the string @samp{_}, but in
1173 the @samp{m68k-coff} environment it expands to the null string.  This
1174 does not work with the @samp{-mno-underscores} option that the i386
1175 OSF/rose and m88k targets provide nor with the @samp{-mcall*} options of
1176 the rs6000 System V Release 4 target.
1177 @end table
1178
1179 @node Nonstandard Predefined,, Standard Predefined, Predefined
1180 @subsubsection Nonstandard Predefined Macros
1181
1182 The C preprocessor normally has several predefined macros that vary
1183 between machines because their purpose is to indicate what type of
1184 system and machine is in use.  This manual, being for all systems and
1185 machines, cannot tell you exactly what their names are; instead, we
1186 offer a list of some typical ones.  You can use @samp{cpp -dM} to see
1187 the values of predefined macros; see @ref{Invocation}.
1188
1189 Some nonstandard predefined macros describe the operating system in use,
1190 with more or less specificity.  For example,
1191
1192 @table @code
1193 @item unix
1194 @findex unix
1195 @samp{unix} is normally predefined on all Unix systems.
1196
1197 @item BSD
1198 @findex BSD
1199 @samp{BSD} is predefined on recent versions of Berkeley Unix
1200 (perhaps only in version 4.3).
1201 @end table
1202
1203 Other nonstandard predefined macros describe the kind of CPU, with more or
1204 less specificity.  For example,
1205
1206 @table @code
1207 @item vax
1208 @findex vax
1209 @samp{vax} is predefined on Vax computers.
1210
1211 @item mc68000
1212 @findex mc68000
1213 @samp{mc68000} is predefined on most computers whose CPU is a Motorola
1214 68000, 68010 or 68020.
1215
1216 @item m68k
1217 @findex m68k
1218 @samp{m68k} is also predefined on most computers whose CPU is a 68000,
1219 68010 or 68020; however, some makers use @samp{mc68000} and some use
1220 @samp{m68k}.  Some predefine both names.  What happens in GNU C
1221 depends on the system you are using it on.
1222
1223 @item M68020
1224 @findex M68020
1225 @samp{M68020} has been observed to be predefined on some systems that
1226 use 68020 CPUs --- in addition to @samp{mc68000} and @samp{m68k}, which
1227 are less specific.
1228
1229 @item _AM29K
1230 @findex _AM29K
1231 @itemx _AM29000
1232 @findex _AM29000
1233 Both @samp{_AM29K} and @samp{_AM29000} are predefined for the AMD 29000
1234 CPU family.
1235
1236 @item ns32000
1237 @findex ns32000
1238 @samp{ns32000} is predefined on computers which use the National
1239 Semiconductor 32000 series CPU.
1240 @end table
1241
1242 Yet other nonstandard predefined macros describe the manufacturer of
1243 the system.  For example,
1244
1245 @table @code
1246 @item sun
1247 @findex sun
1248 @samp{sun} is predefined on all models of Sun computers.
1249
1250 @item pyr
1251 @findex pyr
1252 @samp{pyr} is predefined on all models of Pyramid computers.
1253
1254 @item sequent
1255 @findex sequent
1256 @samp{sequent} is predefined on all models of Sequent computers.
1257 @end table
1258
1259 These predefined symbols are not only nonstandard, they are contrary to the
1260 ISO standard because their names do not start with underscores.
1261 Therefore, the option @samp{-ansi} inhibits the definition of these
1262 symbols.
1263
1264 This tends to make @samp{-ansi} useless, since many programs depend on
1265 the customary nonstandard predefined symbols.  Even system header files
1266 check them and will generate incorrect declarations if they do not find
1267 the names that are expected.  You might think that the header files
1268 supplied for the Uglix computer would not need to test what machine they
1269 are running on, because they can simply assume it is the Uglix; but
1270 often they do, and they do so using the customary names.  As a result,
1271 very few C programs will compile with @samp{-ansi}.  We intend to avoid
1272 such problems on the GNU system.
1273
1274 What, then, should you do in an ISO C program to test the type of machine
1275 it will run on?
1276
1277 GNU C offers a parallel series of symbols for this purpose, whose names
1278 are made from the customary ones by adding @samp{__} at the beginning
1279 and end.  Thus, the symbol @code{__vax__} would be available on a Vax,
1280 and so on.
1281
1282 The set of nonstandard predefined names in the GNU C preprocessor is
1283 controlled (when @code{cpp} is itself compiled) by the macro
1284 @samp{CPP_PREDEFINES}, which should be a string containing @samp{-D}
1285 options, separated by spaces.  For example, on the Sun 3, we use the
1286 following definition:
1287
1288 @example
1289 #define CPP_PREDEFINES "-Dmc68000 -Dsun -Dunix -Dm68k"
1290 @end example
1291
1292 @noindent 
1293 This macro is usually specified in @file{tm.h}.
1294
1295 @node Stringification, Concatenation, Predefined, Macros
1296 @subsection Stringification
1297
1298 @cindex stringification
1299 @dfn{Stringification} means turning a sequence of preprocessing tokens
1300 into a string literal.  For example, stringifying @samp{foo (z)} results
1301 in @samp{"foo (z)"}.
1302
1303 In the C preprocessor, stringification is possible when macro arguments
1304 are substituted during macro expansion.  When a parameter appears
1305 preceded by a @samp{#} token in the replacement list of a function-like
1306 macro, it indicates that both tokens should be replaced with the
1307 stringification of the corresponding argument during expansion.  The
1308 same argument may be substituted in other places in the definition
1309 without stringification if the argument name appears in those places
1310 with no preceding @samp{#}.
1311
1312 Here is an example of a macro definition that uses stringification:
1313
1314 @smallexample
1315 @group
1316 #define WARN_IF(EXP) \
1317 do @{ if (EXP) \
1318         fprintf (stderr, "Warning: " #EXP "\n"); @} \
1319 while (0)
1320 @end group
1321 @end smallexample
1322
1323 @noindent
1324 Here the argument for @samp{EXP} is substituted once, as-is, into the
1325 @samp{if} statement, and once, stringified, into the argument to
1326 @samp{fprintf}.  The @samp{do} and @samp{while (0)} are a kludge to make
1327 it possible to write @samp{WARN_IF (@var{arg});}, which the resemblance
1328 of @samp{WARN_IF} to a function would make C programmers want to do; see
1329 @ref{Swallow Semicolon}.
1330
1331 The stringification feature is limited to transforming the tokens of a
1332 macro argument into a string constant: there is no way to combine the
1333 argument with surrounding text and stringify it all together.  The
1334 example above shows how an equivalent result can be obtained in ISO
1335 Standard C, using the fact that adjacent string constants are
1336 concatenated by the C compiler to form a single string constant.  The
1337 preprocessor stringifies the actual value of @samp{EXP} into a separate
1338 string constant, resulting in text like
1339
1340 @smallexample
1341 @group
1342 do @{ if (x == 0) \
1343         fprintf (stderr, "Warning: " "x == 0" "\n"); @} \
1344 while (0)
1345 @end group
1346 @end smallexample
1347
1348 @noindent
1349 but the compiler then sees three consecutive string constants and
1350 concatenates them into one, producing effectively
1351
1352 @smallexample
1353 do @{ if (x == 0) \
1354         fprintf (stderr, "Warning: x == 0\n"); @} \
1355 while (0)
1356 @end smallexample
1357
1358 Stringification in C involves more than putting double-quote characters
1359 around the fragment.  The preprocessor backslash-escapes the surrounding
1360 quotes of string literals, and all backslashes within string and
1361 character constants, in order to get a valid C string constant with the
1362 proper contents.  Thus, stringifying @samp{p = "foo\n";} results in
1363 @samp{"p = \"foo\\n\";"}.  However, backslashes that are not inside
1364 string or character constants are not duplicated: @samp{\n} by itself
1365 stringifies to @samp{"\n"}.
1366
1367 Whitespace (including comments) in the text being stringified is handled
1368 according to precise rules.  All leading and trailing whitespace is
1369 ignored.  Any sequence of whitespace in the middle of the text is
1370 converted to a single space in the stringified result.
1371
1372 @node Concatenation, Undefining, Stringification, Macros
1373 @subsection Concatenation
1374 @cindex concatenation
1375 @cindex @samp{##}
1376 @dfn{Concatenation} means joining two strings into one.  In the context
1377 of macro expansion, concatenation refers to joining two preprocessing
1378 tokens to form one.  In particular, a token of a macro argument can be
1379 concatenated with another argument's token or with fixed text to produce
1380 a longer name.  The longer name might be the name of a function,
1381 variable, type, or a C keyword; it might even be the name of another
1382 macro, in which case it will be expanded.
1383
1384 When you define a function-like or object-like macro, you request
1385 concatenation with the special operator @samp{##} in the macro's
1386 replacement list.  When the macro is called, any arguments are
1387 substituted without performing macro expansion, every @samp{##} operator
1388 is deleted, and the two tokens on either side of it are concatenated to
1389 form a single token.
1390
1391 Consider a C program that interprets named commands.  There probably needs
1392 to be a table of commands, perhaps an array of structures declared as
1393 follows:
1394
1395 @example
1396 struct command
1397 @{
1398   char *name;
1399   void (*function) ();
1400 @};
1401
1402 struct command commands[] =
1403 @{
1404   @{ "quit", quit_command@},
1405   @{ "help", help_command@},
1406   @dots{}
1407 @};
1408 @end example
1409
1410 It would be cleaner not to have to give each command name twice, once in
1411 the string constant and once in the function name.  A macro which takes the
1412 name of a command as an argument can make this unnecessary.  The string
1413 constant can be created with stringification, and the function name by
1414 concatenating the argument with @samp{_command}.  Here is how it is done:
1415
1416 @example
1417 #define COMMAND(NAME)  @{ #NAME, NAME ## _command @}
1418
1419 struct command commands[] =
1420 @{
1421   COMMAND (quit),
1422   COMMAND (help),
1423   @dots{}
1424 @};
1425 @end example
1426
1427 The usual case of concatenation is concatenating two names (or a name
1428 and a number) into a longer name.  This isn't the only valid case.
1429 It is also possible to concatenate two numbers (or a number and a name,
1430 such as @samp{1.5} and @samp{e3}) into a number.  Also, multi-character
1431 operators such as @samp{+=} can be formed by concatenation.  However,
1432 two tokens that don't together form a valid token cannot be
1433 concatenated.  For example, concatenation of @samp{x} on one side and
1434 @samp{+} on the other is not meaningful because those two tokens do not
1435 form a valid preprocessing token when concatenated.  UNDEFINED
1436
1437 Keep in mind that the C preprocessor converts comments to whitespace
1438 before macros are even considered.  Therefore, you cannot create a
1439 comment by concatenating @samp{/} and @samp{*}: the @samp{/*} sequence
1440 that starts a comment is not a token, but rather the beginning of a
1441 comment.  You can freely use comments next to @samp{##} in a macro
1442 definition, or in arguments that will be concatenated, because the
1443 comments will be converted to spaces at first sight, and concatenation
1444 operates on tokens and so ignores whitespace.
1445
1446 @node Undefining, Redefining, Concatenation, Macros
1447 @subsection Undefining Macros
1448
1449 @cindex undefining macros
1450 To @dfn{undefine} a macro means to cancel its definition.  This is done
1451 with the @samp{#undef} directive.  @samp{#undef} is followed by the macro
1452 name to be undefined.
1453
1454 Like definition, undefinition occurs at a specific point in the source
1455 file, and it applies starting from that point.  The name ceases to be a
1456 macro name, and from that point on it is treated by the preprocessor as
1457 if it had never been a macro name.
1458
1459 For example,
1460
1461 @example
1462 #define FOO 4
1463 x = FOO;
1464 #undef FOO
1465 x = FOO;
1466 @end example
1467
1468 @noindent
1469 expands into
1470
1471 @example
1472 x = 4;
1473
1474 x = FOO;
1475 @end example
1476
1477 @noindent
1478 In this example, @samp{FOO} had better be a variable or function as well
1479 as (temporarily) a macro, in order for the result of the expansion to be
1480 valid C code.
1481
1482 The same form of @samp{#undef} directive will cancel definitions with
1483 arguments or definitions that don't expect arguments.  The @samp{#undef}
1484 directive has no effect when used on a name not currently defined as a
1485 macro.
1486
1487 @node Redefining, Poisoning, Undefining, Macros
1488 @subsection Redefining Macros
1489
1490 @cindex redefining macros
1491 @dfn{Redefining} a macro means defining (with @samp{#define}) a name that
1492 is already defined as a macro.
1493
1494 A redefinition is trivial if the new definition is transparently
1495 identical to the old one.  You probably wouldn't deliberately write a
1496 trivial redefinition, but they can happen automatically when a header
1497 file is included more than once (@pxref{Header Files}), so they are
1498 accepted silently and without effect.
1499
1500 Nontrivial redefinition is considered likely to be an error, so it
1501 provokes a warning message from the preprocessor.  However, sometimes it
1502 is useful to change the definition of a macro in mid-compilation.  You
1503 can inhibit the warning by undefining the macro with @samp{#undef}
1504 before the second definition.
1505
1506 In order for a redefinition to be trivial, the parameter names must
1507 match and be in the same order, and the new replacement list must
1508 exactly match the one already in effect, with two possible exceptions:
1509
1510 @itemize @bullet
1511 @item
1512 Whitespace may be added or deleted at the beginning or the end of the
1513 replacement list.  In a sense this is vacuous, since strictly such
1514 whitespace doesn't form part of the macro's expansion.
1515
1516 @item
1517 Between tokens in the expansion, any two forms of whitespace are
1518 considered equivalent.  In particular, whitespace may not be eliminated
1519 entirely, nor may it be added where there previously wasn't any.
1520 @end itemize
1521
1522 Recall that a comment counts as whitespace.
1523
1524 As a particular case of the above, you may not redefine an object-like
1525 macro as a function-like macro, and vice-versa.
1526
1527 @node Poisoning, Macro Pitfalls, Redefining, Macros
1528 @subsection Poisoning Macros
1529 @cindex poisoning macros
1530 @findex #pragma GCC poison
1531
1532 Sometimes, there is an identifier that you want to remove completely
1533 from your program, and make sure that it never creeps back in.  To
1534 enforce this, the @samp{#pragma GCC poison} directive can be used.
1535 @samp{#pragma GCC poison} is followed by a list of identifiers to
1536 poison, and takes effect for the rest of the source.  You cannot
1537 @samp{#undef} a poisoned identifier or test to see if it's defined with
1538 @samp{#ifdef}.
1539
1540 For example,
1541
1542 @example
1543 #pragma GCC poison printf sprintf fprintf
1544 sprintf(some_string, "hello");
1545 @end example
1546
1547 @noindent
1548 will produce an error.
1549
1550 @node Macro Pitfalls,, Poisoning, Macros
1551 @subsection Pitfalls and Subtleties of Macros
1552 @cindex problems with macros
1553 @cindex pitfalls of macros
1554
1555 In this section we describe some special rules that apply to macros and
1556 macro expansion, and point out certain cases in which the rules have
1557 counterintuitive consequences that you must watch out for.
1558
1559 @menu
1560 * Misnesting::        Macros can contain unmatched parentheses.
1561 * Macro Parentheses:: Why apparently superfluous parentheses
1562                          may be necessary to avoid incorrect grouping.
1563 * Swallow Semicolon:: Macros that look like functions
1564                          but expand into compound statements.
1565 * Side Effects::      Unsafe macros that cause trouble when
1566                          arguments contain side effects.
1567 * Self-Reference::    Macros whose definitions use the macros' own names.
1568 * Argument Prescan::  Arguments are checked for macro calls before they
1569                          are substituted.
1570 * Cascaded Macros::   Macros whose definitions use other macros.
1571 * Newlines in Args::  Sometimes line numbers get confused.
1572 @end menu
1573
1574 @node Misnesting, Macro Parentheses, Macro Pitfalls, Macro Pitfalls
1575 @subsubsection Improperly Nested Constructs
1576
1577 Recall that when a macro is called with arguments, the arguments are
1578 substituted into the macro body and the result is checked, together with
1579 the rest of the input file, for more macro calls.
1580
1581 It is possible to piece together a macro call coming partially from the
1582 macro body and partially from the arguments.  For example,
1583
1584 @example
1585 #define double(x) (2*(x))
1586 #define call_with_1(x) x(1)
1587 @end example
1588
1589 @noindent
1590 would expand @samp{call_with_1 (double)} into @samp{(2*(1))}.
1591
1592 Macro definitions do not have to have balanced parentheses.  By writing
1593 an unbalanced open parenthesis in a macro body, it is possible to create
1594 a macro call that begins inside the macro body but ends outside of it.
1595 For example,
1596
1597 @example
1598 #define strange(file) fprintf (file, "%s %d",
1599 @dots{}
1600 strange(stderr) p, 35)
1601 @end example
1602
1603 @noindent
1604 This bizarre example expands to @samp{fprintf (stderr, "%s %d", p, 35)}!
1605
1606 @node Macro Parentheses, Swallow Semicolon, Misnesting, Macro Pitfalls
1607 @subsubsection Unintended Grouping of Arithmetic
1608 @cindex parentheses in macro bodies
1609
1610 You may have noticed that in most of the macro definition examples shown
1611 above, each occurrence of a macro argument name had parentheses around
1612 it.  In addition, another pair of parentheses usually surround the
1613 entire macro definition.  Here is why it is best to write macros that
1614 way.
1615
1616 Suppose you define a macro as follows,
1617
1618 @example
1619 #define ceil_div(x, y) (x + y - 1) / y
1620 @end example
1621
1622 @noindent
1623 whose purpose is to divide, rounding up.  (One use for this operation is
1624 to compute how many @samp{int} objects are needed to hold a certain
1625 number of @samp{char} objects.)  Then suppose it is used as follows:
1626
1627 @example
1628 a = ceil_div (b & c, sizeof (int));
1629 @end example
1630
1631 @noindent
1632 This expands into
1633
1634 @example
1635 a = (b & c + sizeof (int) - 1) / sizeof (int);
1636 @end example
1637
1638 @noindent
1639 which does not do what is intended.  The operator-precedence rules of
1640 C make it equivalent to this:
1641
1642 @example
1643 a = (b & (c + sizeof (int) - 1)) / sizeof (int);
1644 @end example
1645
1646 @noindent
1647 What we want is this:
1648
1649 @example
1650 a = ((b & c) + sizeof (int) - 1)) / sizeof (int);
1651 @end example
1652
1653 @noindent
1654 Defining the macro as
1655
1656 @example
1657 #define ceil_div(x, y) ((x) + (y) - 1) / (y)
1658 @end example
1659
1660 @noindent
1661 provides the desired result.
1662
1663 Unintended grouping can result in another way.  Consider @samp{sizeof
1664 ceil_div(1, 2)}.  That has the appearance of a C expression that would
1665 compute the size of the type of @samp{ceil_div (1, 2)}, but in fact it
1666 means something very different.  Here is what it expands to:
1667
1668 @example
1669 sizeof ((1) + (2) - 1) / (2)
1670 @end example
1671
1672 @noindent
1673 This would take the size of an integer and divide it by two.  The
1674 precedence rules have put the division outside the @samp{sizeof} when it
1675 was intended to be inside.
1676
1677 Parentheses around the entire macro definition can prevent such
1678 problems.  Here, then, is the recommended way to define @samp{ceil_div}:
1679
1680 @example
1681 #define ceil_div(x, y) (((x) + (y) - 1) / (y))
1682 @end example
1683
1684 @node Swallow Semicolon, Side Effects, Macro Parentheses, Macro Pitfalls
1685 @subsubsection Swallowing the Semicolon
1686
1687 @cindex semicolons (after macro calls)
1688 Often it is desirable to define a macro that expands into a compound
1689 statement.  Consider, for example, the following macro, that advances a
1690 pointer (the argument @samp{p} says where to find it) across whitespace
1691 characters:
1692
1693 @example
1694 #define SKIP_SPACES(p, limit)  \
1695 @{ register char *lim = (limit); \
1696   while (p != lim) @{            \
1697     if (*p++ != ' ') @{          \
1698       p--; break; @}@}@}
1699 @end example
1700
1701 @noindent
1702 Here backslash-newline is used to split the macro definition, which must
1703 be a single logical line, so that it resembles the way such C code would
1704 be laid out if not part of a macro definition.
1705
1706 A call to this macro might be @samp{SKIP_SPACES (p, lim)}.  Strictly
1707 speaking, the call expands to a compound statement, which is a complete
1708 statement with no need for a semicolon to end it.  However, since it
1709 looks like a function call, it minimizes confusion if you can use it
1710 like a function call, writing a semicolon afterward, as in
1711 @samp{SKIP_SPACES (p, lim);}
1712
1713 This can cause trouble before @samp{else} statements, because the
1714 semicolon is actually a null statement.  Suppose you write
1715
1716 @example
1717 if (*p != 0)
1718   SKIP_SPACES (p, lim);
1719 else @dots{}
1720 @end example
1721
1722 @noindent
1723 The presence of two statements --- the compound statement and a null
1724 statement --- in between the @samp{if} condition and the @samp{else}
1725 makes invalid C code.
1726
1727 The definition of the macro @samp{SKIP_SPACES} can be altered to solve
1728 this problem, using a @samp{do @dots{} while} statement.  Here is how:
1729
1730 @example
1731 #define SKIP_SPACES(p, limit)     \
1732 do @{ register char *lim = (limit); \
1733      while (p != lim) @{            \
1734        if (*p++ != ' ') @{          \
1735          p--; break; @}@}@}           \
1736 while (0)
1737 @end example
1738
1739 Now @samp{SKIP_SPACES (p, lim);} expands into
1740
1741 @example
1742 do @{@dots{}@} while (0);
1743 @end example
1744
1745 @noindent
1746 which is one statement.
1747
1748 @node Side Effects, Self-Reference, Swallow Semicolon, Macro Pitfalls
1749 @subsubsection Duplication of Side Effects
1750
1751 @cindex side effects (in macro arguments)
1752 @cindex unsafe macros
1753 Many C programs define a macro @samp{min}, for ``minimum'', like this:
1754
1755 @example
1756 #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
1757 @end example
1758
1759 When you use this macro with an argument containing a side effect,
1760 as shown here,
1761
1762 @example
1763 next = min (x + y, foo (z));
1764 @end example
1765
1766 @noindent
1767 it expands as follows:
1768
1769 @example
1770 next = ((x + y) < (foo (z)) ? (x + y) : (foo (z)));
1771 @end example
1772
1773 @noindent
1774 where @samp{x + y} has been substituted for @samp{X} and @samp{foo (z)}
1775 for @samp{Y}.
1776
1777 The function @samp{foo} is used only once in the statement as it appears
1778 in the program, but the expression @samp{foo (z)} has been substituted
1779 twice into the macro expansion.  As a result, @samp{foo} might be called
1780 two times when the statement is executed.  If it has side effects or if
1781 it takes a long time to compute, the results might not be what you
1782 intended.  We say that @samp{min} is an @dfn{unsafe} macro.
1783
1784 The best solution to this problem is to define @samp{min} in a way that
1785 computes the value of @samp{foo (z)} only once.  The C language offers
1786 no standard way to do this, but it can be done with GNU C extensions as
1787 follows:
1788
1789 @example
1790 #define min(X, Y)                     \
1791 (@{ typeof (X) __x = (X), __y = (Y);   \
1792    (__x < __y) ? __x : __y; @})
1793 @end example
1794
1795 If you do not wish to use GNU C extensions, the only solution is to be
1796 careful when @emph{using} the macro @samp{min}.  For example, you can
1797 calculate the value of @samp{foo (z)}, save it in a variable, and use
1798 that variable in @samp{min}:
1799
1800 @example
1801 #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
1802 @dots{}
1803 @{
1804   int tem = foo (z);
1805   next = min (x + y, tem);
1806 @}
1807 @end example
1808
1809 @noindent
1810 (where we assume that @samp{foo} returns type @samp{int}).
1811
1812 @node Self-Reference, Argument Prescan, Side Effects, Macro Pitfalls
1813 @subsubsection Self-Referential Macros
1814
1815 @cindex self-reference
1816 A @dfn{self-referential} macro is one whose name appears in its
1817 definition.  A special feature of ISO Standard C is that the
1818 self-reference is not considered a macro call.  It is passed into the
1819 preprocessor output unchanged.
1820
1821 Let's consider an example:
1822
1823 @example
1824 #define foo (4 + foo)
1825 @end example
1826
1827 @noindent
1828 where @samp{foo} is also a variable in your program.
1829
1830 Following the ordinary rules, each reference to @samp{foo} will expand
1831 into @samp{(4 + foo)}; then this will be rescanned and will expand into
1832 @samp{(4 + (4 + foo))}; and so on until it causes a fatal error (memory
1833 full) in the preprocessor.
1834
1835 However, the special rule about self-reference cuts this process short
1836 after one step, at @samp{(4 + foo)}.  Therefore, this macro definition
1837 has the possibly useful effect of causing the program to add 4 to the
1838 value of @samp{foo} wherever @samp{foo} is referred to.
1839
1840 In most cases, it is a bad idea to take advantage of this feature.  A
1841 person reading the program who sees that @samp{foo} is a variable will
1842 not expect that it is a macro as well.  The reader will come across the
1843 identifier @samp{foo} in the program and think its value should be that
1844 of the variable @samp{foo}, whereas in fact the value is four greater.
1845
1846 The special rule for self-reference applies also to @dfn{indirect}
1847 self-reference.  This is the case where a macro @var{x} expands to use a
1848 macro @samp{y}, and the expansion of @samp{y} refers to the macro
1849 @samp{x}.  The resulting reference to @samp{x} comes indirectly from the
1850 expansion of @samp{x}, so it is a self-reference and is not further
1851 expanded.  Thus, after
1852
1853 @example
1854 #define x (4 + y)
1855 #define y (2 * x)
1856 @end example
1857
1858 @noindent
1859 @samp{x} would expand into @samp{(4 + (2 * x))}.  Clear?
1860
1861 Suppose @samp{y} is used elsewhere, not from the definition of @samp{x}.
1862 Then the use of @samp{x} in the expansion of @samp{y} is not a
1863 self-reference because @samp{x} is not ``in progress''.  So it does
1864 expand.  However, the expansion of @samp{x} contains a reference to
1865 @samp{y}, and that is an indirect self-reference now because @samp{y} is
1866 ``in progress''.  The result is that @samp{y} expands to @samp{(2 * (4 +
1867 y))}.
1868
1869 This behavior is specified by the ISO C standard, so you may need to
1870 understand it.
1871
1872 @node Argument Prescan, Cascaded Macros, Self-Reference, Macro Pitfalls
1873 @subsubsection Separate Expansion of Macro Arguments
1874 @cindex expansion of arguments
1875 @cindex macro argument expansion
1876 @cindex prescan of macro arguments
1877
1878 We have explained that the expansion of a macro, including the substituted
1879 arguments, is re-scanned for macro calls to be expanded.
1880
1881 What really happens is more subtle: first each argument is scanned
1882 separately for macro calls.  Then the resulting tokens are substituted
1883 into the macro body to produce the macro expansion, and the macro
1884 expansion is scanned again for macros to expand.
1885
1886 The result is that the arguments are scanned @emph{twice} to expand
1887 macro calls in them.
1888
1889 Most of the time, this has no effect.  If the argument contained any
1890 macro calls, they are expanded during the first scan.  The result
1891 therefore contains no macro calls, so the second scan does not change
1892 it.  If the argument were substituted as given, with no prescan, the
1893 single remaining scan would find the same macro calls and produce the
1894 same results.
1895
1896 You might expect the double scan to change the results when a
1897 self-referential macro is used in an argument of another macro
1898 (@pxref{Self-Reference}): the self-referential macro would be expanded
1899 once in the first scan, and a second time in the second scan.  However,
1900 this is not what happens.  The self-references that do not expand in the
1901 first scan are marked so that they will not expand in the second scan
1902 either.
1903
1904 The prescan is not done when an argument is stringified or concatenated.
1905 Thus,
1906
1907 @example
1908 #define str(s) #s
1909 #define foo 4
1910 str (foo)
1911 @end example
1912
1913 @noindent
1914 expands to @samp{"foo"}.  Once more, prescan has been prevented from
1915 having any noticeable effect.
1916
1917 More precisely, stringification and concatenation use the argument
1918 tokens as given without initially scanning for macros.  The same
1919 argument would be used in expanded form if it is substituted elsewhere
1920 without stringification or concatenation.
1921
1922 @example
1923 #define str(s) #s lose(s)
1924 #define foo 4
1925 str (foo)
1926 @end example
1927
1928 expands to @samp{"foo" lose(4)}.
1929
1930 You might now ask, ``Why mention the prescan, if it makes no difference?
1931 And why not skip it and make the preprocessor faster?''  The answer is
1932 that the prescan does make a difference in three special cases:
1933
1934 @itemize @bullet
1935 @item
1936 Nested calls to a macro.
1937
1938 @item
1939 Macros that call other macros that stringify or concatenate.
1940
1941 @item
1942 Macros whose expansions contain unshielded commas.
1943 @end itemize
1944
1945 We say that @dfn{nested} calls to a macro occur when a macro's argument
1946 contains a call to that very macro.  For example, if @samp{f} is a macro
1947 that expects one argument, @samp{f (f (1))} is a nested pair of calls to
1948 @samp{f}.  The desired expansion is made by expanding @samp{f (1)} and
1949 substituting that into the definition of @samp{f}.  The prescan causes
1950 the expected result to happen.  Without the prescan, @samp{f (1)} itself
1951 would be substituted as an argument, and the inner use of @samp{f} would
1952 appear during the main scan as an indirect self-reference and would not
1953 be expanded.  Here, the prescan cancels an undesirable side effect (in
1954 the medical, not computational, sense of the term) of the special rule
1955 for self-referential macros.
1956
1957 Prescan causes trouble in certain other cases of nested macro calls.
1958 Here is an example:
1959
1960 @example
1961 #define foo  a,b
1962 #define bar(x) lose(x)
1963 #define lose(x) (1 + (x))
1964
1965 bar(foo)
1966 @end example
1967
1968 @noindent
1969 We would like @samp{bar(foo)} to turn into @samp{(1 + (foo))}, which
1970 would then turn into @samp{(1 + (a,b))}.  Instead, @samp{bar(foo)}
1971 expands into @samp{lose(a,b)}, and you get an error because @code{lose}
1972 requires a single argument.  In this case, the problem is easily solved
1973 by the same parentheses that ought to be used to prevent misnesting of
1974 arithmetic operations:
1975
1976 @example
1977 #define foo (a,b)
1978 #define bar(x) lose((x))
1979 @end example
1980
1981 The problem is more serious when the operands of the macro are not
1982 expressions; for example, when they are statements.  Then parentheses
1983 are unacceptable because they would make for invalid C code:
1984
1985 @example
1986 #define foo @{ int a, b; @dots{} @}
1987 @end example
1988
1989 @noindent
1990 In GNU C you can shield the commas using the @samp{(@{@dots{}@})}
1991 construct which turns a compound statement into an expression:
1992
1993 @example
1994 #define foo (@{ int a, b; @dots{} @})
1995 @end example
1996
1997 Or you can rewrite the macro definition to avoid such commas:
1998
1999 @example
2000 #define foo @{ int a; int b; @dots{} @}
2001 @end example
2002
2003 There is also one case where prescan is useful.  It is possible to use
2004 prescan to expand an argument and then stringify it --- if you use two
2005 levels of macros.  Let's add a new macro @samp{xstr} to the example
2006 shown above:
2007
2008 @example
2009 #define xstr(s) str(s)
2010 #define str(s) #s
2011 #define foo 4
2012 xstr (foo)
2013 @end example
2014
2015 This expands into @samp{"4"}, not @samp{"foo"}.  The reason for the
2016 difference is that the argument of @samp{xstr} is expanded at prescan
2017 (because @samp{xstr} does not specify stringification or concatenation
2018 of the argument).  The result of prescan then forms the argument for
2019 @samp{str}.  @samp{str} uses its argument without prescan because it
2020 performs stringification; but it cannot prevent or undo the prescanning
2021 already done by @samp{xstr}.
2022
2023 @node Cascaded Macros, Newlines in Args, Argument Prescan, Macro Pitfalls
2024 @subsubsection Cascaded Use of Macros
2025
2026 @cindex cascaded macros
2027 @cindex macro body uses macro
2028 A @dfn{cascade} of macros is when one macro's body contains a reference
2029 to another macro.  This is very common practice.  For example,
2030
2031 @example
2032 #define BUFSIZE 1020
2033 #define TABLESIZE BUFSIZE
2034 @end example
2035
2036 This is not at all the same as defining @samp{TABLESIZE} to be
2037 @samp{1020}.  The @samp{#define} for @samp{TABLESIZE} uses exactly the
2038 body you specify --- in this case, @samp{BUFSIZE} --- and does not check
2039 to see whether it too is the name of a macro.
2040
2041 It's only when you @emph{use} @samp{TABLESIZE} that the result of its
2042 expansion is checked for more macro names.
2043
2044 This makes a difference if you change the definition of @samp{BUFSIZE}
2045 at some point in the source file.  @samp{TABLESIZE}, defined as shown,
2046 will always expand using the definition of @samp{BUFSIZE} that is
2047 currently in effect:
2048
2049 @example
2050 #define BUFSIZE 1020
2051 #define TABLESIZE BUFSIZE
2052 #undef BUFSIZE
2053 #define BUFSIZE 37
2054 @end example
2055
2056 @noindent
2057 Now @samp{TABLESIZE} expands (in two stages) to @samp{37}.  (The
2058 @samp{#undef} is to prevent any warning about the nontrivial
2059 redefinition of @code{BUFSIZE}.)
2060
2061 @node Newlines in Args,, Cascaded Macros, Macro Pitfalls
2062 @subsection Newlines in Macro Arguments
2063 @cindex newlines in macro arguments
2064
2065 The invocation of a function-like macro can extend over many logical
2066 lines.  The ISO C standard requires that newlines within a macro
2067 invocation be treated as ordinary whitespace.  This means that when the
2068 expansion of a function-like macro replaces its invocation, it appears
2069 on the same line as the macro name did.  Thus line numbers emitted by
2070 the compiler or debugger refer to the line the invocation started on,
2071 which might be different to the line containing the argument causing the
2072 problem.
2073
2074 Here is an example illustrating this:
2075
2076 @example
2077 #define ignore_second_arg(a,b,c) a; c
2078
2079 ignore_second_arg (foo (),
2080                    ignored (),
2081                    syntax error);
2082 @end example
2083
2084 @noindent
2085 The syntax error triggered by the tokens @samp{syntax error} results in
2086 an error message citing line three --- the line of ignore_second_arg ---
2087 even though the problematic code comes from line five.
2088
2089 @node Conditionals, Assertions, Macros, Top
2090 @section Conditionals
2091
2092 @cindex conditionals
2093 In a macro processor, a @dfn{conditional} is a directive that allows a
2094 part of the program to be ignored during compilation, on some
2095 conditions.  In the C preprocessor, a conditional can test either an
2096 arithmetic expression or whether a name is defined as a macro.
2097
2098 A conditional in the C preprocessor resembles in some ways an @samp{if}
2099 statement in C, but it is important to understand the difference between
2100 them.  The condition in an @samp{if} statement is tested during the
2101 execution of your program.  Its purpose is to allow your program to
2102 behave differently from run to run, depending on the data it is
2103 operating on.  The condition in a preprocessing conditional directive is
2104 tested when your program is compiled.  Its purpose is to allow different
2105 code to be included in the program depending on the situation at the
2106 time of compilation.
2107
2108 @menu
2109 * Uses: Conditional Uses.       What conditionals are for.
2110 * Syntax: Conditional Syntax.   How conditionals are written.
2111 * Deletion: Deleted Code.       Making code into a comment.
2112 * Macros: Conditionals-Macros.  Why conditionals are used with macros.
2113 * Errors: #error Directive.     Detecting inconsistent compilation parameters.
2114 @end menu
2115
2116 @node Conditional Uses
2117 @subsection Why Conditionals are Used
2118
2119 Generally there are three kinds of reason to use a conditional.
2120
2121 @itemize @bullet
2122 @item
2123 A program may need to use different code depending on the machine or
2124 operating system it is to run on.  In some cases the code for one
2125 operating system may be erroneous on another operating system; for
2126 example, it might refer to library routines that do not exist on the
2127 other system.  When this happens, it is not enough to avoid executing
2128 the invalid code: merely having it in the program makes it impossible to
2129 link the program and run it.  With a preprocessing conditional, the
2130 offending code can be effectively excised from the program when it is
2131 not valid.
2132
2133 @item
2134 You may want to be able to compile the same source file into two
2135 different programs.  Sometimes the difference between the programs is
2136 that one makes frequent time-consuming consistency checks on its
2137 intermediate data, or prints the values of those data for debugging,
2138 while the other does not.
2139
2140 @item
2141 A conditional whose condition is always false is a good way to exclude
2142 code from the program but keep it as a sort of comment for future
2143 reference.
2144 @end itemize
2145
2146 Most simple programs that are intended to run on only one machine will
2147 not need to use preprocessing conditionals.
2148
2149 @node Conditional Syntax
2150 @subsection Syntax of Conditionals
2151
2152 @findex #if
2153 A conditional in the C preprocessor begins with a @dfn{conditional
2154 directive}: @samp{#if}, @samp{#ifdef} or @samp{#ifndef}.
2155 @xref{Conditionals-Macros}, for information on @samp{#ifdef} and
2156 @samp{#ifndef}; only @samp{#if} is explained here.
2157
2158 @menu
2159 * If: #if Directive.     Basic conditionals using @samp{#if} and @samp{#endif}.
2160 * Else: #else Directive. Including some text if the condition fails.
2161 * Elif: #elif Directive. Testing several alternative possibilities.
2162 @end menu
2163
2164 @node #if Directive
2165 @subsubsection The @samp{#if} Directive
2166
2167 The @samp{#if} directive in its simplest form consists of
2168
2169 @example
2170 #if @var{expression}
2171 @var{controlled text}
2172 #endif /* @var{expression} */
2173 @end example
2174
2175 The comment following the @samp{#endif} is not required, but it is a
2176 good practice because it helps people match the @samp{#endif} to the
2177 corresponding @samp{#if}.  Such comments should always be used, except
2178 in short conditionals that are not nested.  In fact, you can put
2179 anything at all after the @samp{#endif} and it will be ignored by the
2180 GNU C preprocessor, but only comments are acceptable in ISO Standard C@.
2181
2182 @var{expression} is a C expression of integer type, subject to stringent
2183 restrictions.  It may contain
2184
2185 @itemize @bullet
2186 @item
2187 Integer constants, which are all regarded as @code{long} or
2188 @code{unsigned long}.
2189
2190 @item
2191 Character constants, which are interpreted according to the character
2192 set and conventions of the machine and operating system on which the
2193 preprocessor is running.  The GNU C preprocessor uses the C data type
2194 @samp{char} for these character constants; therefore, whether some
2195 character codes are negative is determined by the C compiler used to
2196 compile the preprocessor.  If it treats @samp{char} as signed, then
2197 character codes large enough to set the sign bit will be considered
2198 negative; otherwise, no character code is considered negative.
2199
2200 @item
2201 Arithmetic operators for addition, subtraction, multiplication,
2202 division, bitwise operations, shifts, comparisons, and logical
2203 operations (@samp{&&} and @samp{||}).  The latter two obey the usual
2204 short-circuiting rules of standard C.
2205
2206 @item
2207 Identifiers that are not macros, which are all treated as zero(!).
2208
2209 @item
2210 Macro calls.  All macro calls in the expression are expanded before
2211 actual computation of the expression's value begins.
2212 @end itemize
2213
2214 Note that @samp{sizeof} operators and @code{enum}-type values are not
2215 allowed.  @code{enum}-type values, like all other identifiers that are
2216 not taken as macro calls and expanded, are treated as zero.
2217
2218 The @var{controlled text} inside of a conditional can include
2219 preprocessing directives.  Then the directives inside the conditional
2220 are obeyed only if that branch of the conditional succeeds.  The text
2221 can also contain other conditional groups.  However, the @samp{#if} and
2222 @samp{#endif} directives must balance.
2223
2224 @node #else Directive
2225 @subsubsection The @samp{#else} Directive
2226
2227 @findex #else
2228 The @samp{#else} directive can be added to a conditional to provide
2229 alternative text to be used if the condition is false.  This is what
2230 it looks like:
2231
2232 @example
2233 #if @var{expression}
2234 @var{text-if-true}
2235 #else /* Not @var{expression} */
2236 @var{text-if-false}
2237 #endif /* Not @var{expression} */
2238 @end example
2239
2240 If @var{expression} is nonzero, and thus the @var{text-if-true} is 
2241 active, then @samp{#else} acts like a failing conditional and the
2242 @var{text-if-false} is ignored.  Conversely, if the @samp{#if}
2243 conditional fails, the @var{text-if-false} is considered included.
2244
2245 @node #elif Directive
2246 @subsubsection The @samp{#elif} Directive
2247
2248 @findex #elif
2249 One common case of nested conditionals is used to check for more than two
2250 possible alternatives.  For example, you might have
2251
2252 @example
2253 #if X == 1
2254 @dots{}
2255 #else /* X != 1 */
2256 #if X == 2
2257 @dots{}
2258 #else /* X != 2 */
2259 @dots{}
2260 #endif /* X != 2 */
2261 #endif /* X != 1 */
2262 @end example
2263
2264 Another conditional directive, @samp{#elif}, allows this to be
2265 abbreviated as follows:
2266
2267 @example
2268 #if X == 1
2269 @dots{}
2270 #elif X == 2
2271 @dots{}
2272 #else /* X != 2 and X != 1*/
2273 @dots{}
2274 #endif /* X != 2 and X != 1*/
2275 @end example
2276
2277 @samp{#elif} stands for ``else if''.  Like @samp{#else}, it goes in the
2278 middle of a @samp{#if}-@samp{#endif} pair and subdivides it; it does not
2279 require a matching @samp{#endif} of its own.  Like @samp{#if}, the
2280 @samp{#elif} directive includes an expression to be tested.
2281
2282 The text following the @samp{#elif} is processed only if the original
2283 @samp{#if}-condition failed and the @samp{#elif} condition succeeds.
2284 More than one @samp{#elif} can go in the same @samp{#if}-@samp{#endif}
2285 group.  Then the text after each @samp{#elif} is processed only if the
2286 @samp{#elif} condition succeeds after the original @samp{#if} and any
2287 previous @samp{#elif} directives within it have failed.  @samp{#else} is
2288 equivalent to @samp{#elif 1}, and @samp{#else} is allowed after any
2289 number of @samp{#elif} directives, but @samp{#elif} may not follow
2290 @samp{#else}.
2291
2292 @node Deleted Code
2293 @subsection Keeping Deleted Code for Future Reference
2294 @cindex commenting out code
2295
2296 If you replace or delete a part of the program but want to keep the old
2297 code around as a comment for future reference, the easy way to do this
2298 is to put @samp{#if 0} before it and @samp{#endif} after it.  This is
2299 better than using comment delimiters @samp{/*} and @samp{*/} since those
2300 won't work if the code already contains comments (C comments do not
2301 nest).
2302
2303 This works even if the code being turned off contains conditionals, but
2304 they must be entire conditionals (balanced @samp{#if} and @samp{#endif}).
2305
2306 Conversely, do not use @samp{#if 0} for comments which are not C code.
2307 Use the comment delimiters @samp{/*} and @samp{*/} instead.  The
2308 interior of @samp{#if 0} must consist of complete tokens; in particular,
2309 single-quote characters must balance.  Comments often contain unbalanced
2310 single-quote characters (known in English as apostrophes).  These
2311 confuse @samp{#if 0}.  They do not confuse @samp{/*}.
2312
2313 @node Conditionals-Macros
2314 @subsection Conditionals and Macros
2315
2316 Conditionals are useful in connection with macros or assertions, because
2317 those are the only ways that an expression's value can vary from one
2318 compilation to another.  A @samp{#if} directive whose expression uses no
2319 macros or assertions is equivalent to @samp{#if 1} or @samp{#if 0}; you
2320 might as well determine which one, by computing the value of the
2321 expression yourself, and then simplify the program.
2322
2323 For example, here is a conditional that tests the expression
2324 @samp{BUFSIZE == 1020}, where @samp{BUFSIZE} must be a macro.
2325
2326 @example
2327 #if BUFSIZE == 1020
2328   printf ("Large buffers!\n");
2329 #endif /* BUFSIZE is large */
2330 @end example
2331
2332 (Programmers often wish they could test the size of a variable or data
2333 type in @samp{#if}, but this does not work.  The preprocessor does not
2334 understand @code{sizeof}, or typedef names, or even the type keywords
2335 such as @code{int}.)
2336
2337 @findex defined
2338 The special operator @samp{defined} is used in @samp{#if} expressions to
2339 test whether a certain name is defined as a macro.  Either @samp{defined
2340 @var{name}} or @samp{defined (@var{name})} is an expression whose value
2341 is 1 if @var{name} is defined as macro at the current point in the
2342 program, and 0 otherwise.  For the @samp{defined} operator it makes no
2343 difference what the definition of the macro is; all that matters is
2344 whether there is a definition.  Thus, for example,@refill
2345
2346 @example
2347 #if defined (vax) || defined (ns16000)
2348 @end example
2349
2350 @noindent
2351 would succeed if either of the names @samp{vax} and @samp{ns16000} is
2352 defined as a macro.  You can test the same condition using assertions
2353 (@pxref{Assertions}), like this:
2354
2355 @example
2356 #if #cpu (vax) || #cpu (ns16000)
2357 @end example
2358
2359 If a macro is defined and later undefined with @samp{#undef}, subsequent
2360 use of the @samp{defined} operator returns 0, because the name is no
2361 longer defined.  If the macro is defined again with another
2362 @samp{#define}, @samp{defined} will recommence returning 1.
2363
2364 @findex #ifdef
2365 @findex #ifndef
2366 Conditionals that test whether a single macro is defined are very common,
2367 so there are two special short conditional directives for this case.
2368
2369 @table @code
2370 @item #ifdef @var{name}
2371 is equivalent to @samp{#if defined (@var{name})}.
2372
2373 @item #ifndef @var{name}
2374 is equivalent to @samp{#if ! defined (@var{name})}.
2375 @end table
2376
2377 Macro definitions can vary between compilations for several reasons.
2378
2379 @itemize @bullet
2380 @item
2381 Some macros are predefined on each kind of machine.  For example, on a
2382 Vax, the name @samp{vax} is a predefined macro.  On other machines, it
2383 would not be defined.
2384
2385 @item
2386 Many more macros are defined by system header files.  Different systems
2387 and machines define different macros, or give them different values.  It
2388 is useful to test these macros with conditionals to avoid using a system
2389 feature on a machine where it is not implemented.
2390
2391 @item
2392 Macros are a common way of allowing users to customize a program for
2393 different machines or applications.  For example, the macro
2394 @samp{BUFSIZE} might be defined in a configuration file for your program
2395 that is included as a header file in each source file.  You would use
2396 @samp{BUFSIZE} in a preprocessing conditional in order to generate
2397 different code depending on the chosen configuration.
2398
2399 @item
2400 Macros can be defined or undefined with @samp{-D} and @samp{-U} command
2401 options when you compile the program.  You can arrange to compile the
2402 same source file into two different programs by choosing a macro name to
2403 specify which program you want, writing conditionals to test whether or
2404 how this macro is defined, and then controlling the state of the macro
2405 with compiler command options.  @xref{Invocation}.
2406 @end itemize
2407
2408 @ifinfo
2409 Assertions are usually predefined, but can be defined with preprocessor
2410 directives or command-line options.
2411 @end ifinfo
2412
2413 @node #error Directive
2414 @subsection The @samp{#error} and @samp{#warning} Directives
2415
2416 @findex #error
2417 The directive @samp{#error} causes the preprocessor to report a fatal
2418 error.  The tokens forming the rest of the line following @samp{#error}
2419 are used as the error message, and not macro-expanded.  Internal
2420 whitespace sequences are each replaced with a single space.  The line
2421 must consist of complete tokens.
2422
2423 You would use @samp{#error} inside of a conditional that detects a
2424 combination of parameters which you know the program does not properly
2425 support.  For example, if you know that the program will not run
2426 properly on a Vax, you might write
2427
2428 @smallexample
2429 @group
2430 #ifdef __vax__
2431 #error "Won't work on Vaxen.  See comments at get_last_object."
2432 #endif
2433 @end group
2434 @end smallexample
2435
2436 @noindent
2437 @xref{Nonstandard Predefined}, for why this works.
2438
2439 If you have several configuration parameters that must be set up by
2440 the installation in a consistent way, you can use conditionals to detect
2441 an inconsistency and report it with @samp{#error}.  For example,
2442
2443 @smallexample
2444 #if HASH_TABLE_SIZE % 2 == 0 || HASH_TABLE_SIZE % 3 == 0 \
2445     || HASH_TABLE_SIZE % 5 == 0
2446 #error HASH_TABLE_SIZE should not be divisible by a small prime
2447 #endif
2448 @end smallexample
2449
2450 @findex #warning
2451 The directive @samp{#warning} is like the directive @samp{#error}, but
2452 causes the preprocessor to issue a warning and continue preprocessing.
2453 The tokens following @samp{#warning} are used as the warning message,
2454 and not macro-expanded.
2455
2456 You might use @samp{#warning} in obsolete header files, with a message
2457 directing the user to the header file which should be used instead.
2458
2459 @node Assertions, Line Control, Conditionals, Top
2460
2461 @cindex assertions
2462 @dfn{Assertions} are a more systematic alternative to macros in writing
2463 conditionals to test what sort of computer or system the compiled
2464 program will run on.  Assertions are usually predefined, but you can
2465 define them with preprocessing directives or command-line options.
2466
2467 @cindex predicates
2468 The macros traditionally used to describe the type of target are not
2469 classified in any way according to which question they answer; they may
2470 indicate a hardware architecture, a particular hardware model, an
2471 operating system, a particular version of an operating system, or
2472 specific configuration options.  These are jumbled together in a single
2473 namespace.  In contrast, each assertion consists of a named question and
2474 an answer.  The question is usually called the @dfn{predicate}.  An
2475 assertion looks like this:
2476
2477 @example
2478 #@var{predicate} (@var{answer})
2479 @end example
2480
2481 @noindent
2482 You must use a properly formed identifier for @var{predicate}.  The
2483 value of @var{answer} can be any sequence of words; all characters are
2484 significant except for leading and trailing whitespace, and differences
2485 in internal whitespace sequences are ignored.  (This is similar to the
2486 rules governing macro redefinition.)  Thus, @samp{x + y} is different
2487 from @samp{x+y} but equivalent to @samp{ x + y }.  @samp{)} is not
2488 allowed in an answer.
2489
2490 @cindex testing predicates
2491 Here is a conditional to test whether the answer @var{answer} is asserted
2492 for the predicate @var{predicate}:
2493
2494 @example
2495 #if #@var{predicate} (@var{answer})
2496 @end example
2497
2498 @noindent
2499 There may be more than one answer asserted for a given predicate.  If
2500 you omit the answer, you can test whether @emph{any} answer is asserted
2501 for @var{predicate}:
2502
2503 @example
2504 #if #@var{predicate}
2505 @end example
2506
2507 @findex #system
2508 @findex #machine
2509 @findex #cpu
2510 Most of the time, the assertions you test will be predefined assertions.
2511 GNU C provides three predefined predicates: @code{system}, @code{cpu},
2512 and @code{machine}.  @code{system} is for assertions about the type of
2513 software, @code{cpu} describes the type of computer architecture, and
2514 @code{machine} gives more information about the computer.  For example,
2515 on a GNU system, the following assertions would be true:
2516
2517 @example
2518 #system (gnu)
2519 #system (mach)
2520 #system (mach 3)
2521 #system (mach 3.@var{subversion})
2522 #system (hurd)
2523 #system (hurd @var{version})
2524 @end example
2525
2526 @noindent
2527 and perhaps others.  The alternatives with
2528 more or less version information let you ask more or less detailed
2529 questions about the type of system software.
2530
2531 On a Unix system, you would find @code{#system (unix)} and perhaps one of:
2532 @code{#system (aix)}, @code{#system (bsd)}, @code{#system (hpux)},
2533 @code{#system (lynx)}, @code{#system (mach)}, @code{#system (posix)},
2534 @code{#system (svr3)}, @code{#system (svr4)}, or @code{#system (xpg4)}
2535 with possible version numbers following.
2536
2537 Other values for @code{system} are @code{#system (mvs)}
2538 and @code{#system (vms)}.
2539
2540 @strong{Portability note:} Many Unix C compilers provide only one answer
2541 for the @code{system} assertion: @code{#system (unix)}, if they support
2542 assertions at all.  This is less than useful.
2543
2544 An assertion with a multi-word answer is completely different from several
2545 assertions with individual single-word answers.  For example, the presence
2546 of @code{system (mach 3.0)} does not mean that @code{system (3.0)} is true.
2547 It also does not directly imply @code{system (mach)}, but in GNU C, that
2548 last will normally be asserted as well.
2549
2550 The current list of possible assertion values for @code{cpu} is:
2551 @code{#cpu (a29k)}, @code{#cpu (alpha)}, @code{#cpu (arm)}, @code{#cpu
2552 (clipper)}, @code{#cpu (convex)}, @code{#cpu (elxsi)}, @code{#cpu
2553 (tron)}, @code{#cpu (h8300)}, @code{#cpu (i370)}, @code{#cpu (i386)},
2554 @code{#cpu (i860)}, @code{#cpu (i960)}, @code{#cpu (m68k)}, @code{#cpu
2555 (m88k)}, @code{#cpu (mips)}, @code{#cpu (ns32k)}, @code{#cpu (hppa)},
2556 @code{#cpu (pyr)}, @code{#cpu (ibm032)}, @code{#cpu (rs6000)},
2557 @code{#cpu (sh)}, @code{#cpu (sparc)}, @code{#cpu (spur)}, @code{#cpu
2558 (tahoe)}, @code{#cpu (vax)}, @code{#cpu (we32000)}.
2559
2560 @findex #assert
2561 You can create assertions within a C program using @samp{#assert}, like
2562 this:
2563
2564 @example
2565 #assert @var{predicate} (@var{answer})
2566 @end example
2567
2568 @noindent
2569 (Note the absence of a @samp{#} before @var{predicate}.)
2570
2571 @cindex unassert
2572 @cindex assertions, undoing
2573 @cindex retracting assertions
2574 @findex #unassert
2575 Each time you do this, you assert a new true answer for @var{predicate}.
2576 Asserting one answer does not invalidate previously asserted answers;
2577 they all remain true.  The only way to remove an answer is with
2578 @samp{#unassert}.  @samp{#unassert} has the same syntax as
2579 @samp{#assert}.  You can also remove all answers to a @var{predicate}
2580 like this:
2581
2582 @example
2583 #unassert @var{predicate}
2584 @end example
2585
2586 You can also add or cancel assertions using command options
2587 when you run @code{gcc} or @code{cpp}.  @xref{Invocation}.
2588
2589 @node Line Control, Other Directives, Assertions, Top
2590 @section Combining Source Files
2591
2592 @cindex line control
2593 One of the jobs of the C preprocessor is to inform the C compiler of where
2594 each line of C code came from: which source file and which line number.
2595
2596 C code can come from multiple source files if you use @samp{#include};
2597 both @samp{#include} and the use of conditionals and macros can cause
2598 the line number of a line in the preprocessor output to be different
2599 from the line's number in the original source file.  You will appreciate
2600 the value of making both the C compiler (in error messages) and symbolic
2601 debuggers such as GDB use the line numbers in your source file.
2602
2603 The C preprocessor builds on this feature by offering a directive by
2604 which you can control the feature explicitly.  This is useful when a
2605 file for input to the C preprocessor is the output from another program
2606 such as the @code{bison} parser generator, which operates on another
2607 file that is the true source file.  Parts of the output from
2608 @code{bison} are generated from scratch, other parts come from a
2609 standard parser file.  The rest are copied nearly verbatim from the
2610 source file, but their line numbers in the @code{bison} output are not
2611 the same as their original line numbers.  Naturally you would like
2612 compiler error messages and symbolic debuggers to know the original
2613 source file and line number of each line in the @code{bison} input.
2614
2615 @findex #line
2616 @code{bison} arranges this by writing @samp{#line} directives into the output
2617 file.  @samp{#line} is a directive that specifies the original line number
2618 and source file name for subsequent input in the current preprocessor input
2619 file.  @samp{#line} has three variants:
2620
2621 @table @code
2622 @item #line @var{linenum}
2623 Here @var{linenum} is a decimal integer constant.  This specifies that
2624 the line number of the following line of input, in its original source file,
2625 was @var{linenum}.
2626
2627 @item #line @var{linenum} @var{filename}
2628 Here @var{linenum} is a decimal integer constant and @var{filename} is a
2629 string constant.  This specifies that the following line of input came
2630 originally from source file @var{filename} and its line number there was
2631 @var{linenum}.  Keep in mind that @var{filename} is not just a file
2632 name; it is surrounded by double-quote characters so that it looks like
2633 a string constant.
2634
2635 @item #line @var{anything else}
2636 @var{anything else} is checked for macro calls, which are expanded.
2637 The result should be a decimal integer constant followed optionally
2638 by a string constant, as described above.
2639 @end table
2640
2641 @samp{#line} directives alter the results of the @samp{__FILE__} and
2642 @samp{__LINE__} predefined macros from that point on.  @xref{Standard
2643 Predefined}.
2644
2645 The output of the preprocessor (which is the input for the rest of the
2646 compiler) contains directives that look much like @samp{#line}
2647 directives.  They start with just @samp{#} instead of @samp{#line}, but
2648 this is followed by a line number and file name as in @samp{#line}.
2649 @xref{Output}.
2650
2651 @node Other Directives, Output, Line Control, Top
2652 @section Miscellaneous Preprocessing Directives
2653
2654 @cindex null directive
2655 This section describes three additional preprocessing directives.  They
2656 are not very useful, but are mentioned for completeness.
2657
2658 The @dfn{null directive} consists of a @samp{#} followed by a newline,
2659 with only whitespace (including comments) in between.  A null directive
2660 is understood as a preprocessing directive but has no effect on the
2661 preprocessor output.  The primary significance of the existence of the
2662 null directive is that an input line consisting of just a @samp{#} will
2663 produce no output, rather than a line of output containing just a
2664 @samp{#}.  Supposedly some old C programs contain such lines.
2665
2666 @findex #pragma
2667 @findex #pragma GCC
2668
2669 The ISO standard specifies that the effect of the @samp{#pragma}
2670 directive is implementation-defined.  The GNU C preprocessor recognizes
2671 some pragmas, and passes unrecognized ones through to the preprocessor
2672 output, so they are available to the compilation pass.
2673
2674 In line with the C99 standard, which introduces a STDC namespace for C99
2675 pragmas, the preprocessor introduces a GCC namespace for GCC pragmas.
2676 Supported GCC preprocessor pragmas are of the form @samp{#pragma GCC
2677 ...}.  For backwards compatibility previously supported pragmas are also
2678 recognized without the @samp{GCC} prefix, however that use is
2679 deprecated.  Pragmas that are already deprecated are not recognized with
2680 a @samp{GCC} prefix.
2681
2682 @findex #ident
2683 The @samp{#ident} directive is supported for compatibility with certain
2684 other systems.  It is followed by a line of text.  On some systems, the
2685 text is copied into a special place in the object file; on most systems,
2686 the text is ignored and this directive has no effect.  Typically
2687 @samp{#ident} is only used in header files supplied with those systems
2688 where it is meaningful.
2689
2690 @findex #pragma GCC dependency
2691 The @samp{#pragma GCC dependency} allows you to check the relative dates
2692 of the current file and another file. If the other file is more recent
2693 than the current file, a warning is issued. This is useful if the
2694 include file is derived from the other file, and should be regenerated.
2695 The other file is searched for using the normal include search path.
2696 Optional trailing text can be used to give more information in the
2697 warning message.
2698
2699 @smallexample
2700 #pragma GCC dependency "parse.y"
2701 #pragma GCC dependency "/usr/include/time.h" rerun /path/to/fixincludes
2702 @end smallexample
2703
2704 @node Output, Unreliable Features, Other Directives, Top
2705 @section C Preprocessor Output
2706
2707 @cindex output format
2708 The output from the C preprocessor looks much like the input, except
2709 that all preprocessing directive lines have been replaced with blank
2710 lines and all comments with spaces.
2711
2712 The ISO standard specifies that it is implementation defined whether a
2713 preprocessor preserves whitespace between tokens, or replaces it with
2714 e.g. a single space.  In the GNU C preprocessor, whitespace between
2715 tokens is collapsed to become a single space, with the exception that
2716 the first token on a non-directive line is preceded with sufficient
2717 spaces that it appears in the same column in the preprocessed output
2718 that it appeared in in the original source file.  This is so the output
2719 is easy to read.  @xref{Unreliable Features}.
2720
2721 Source file name and line number information is conveyed by lines
2722 of the form
2723
2724 @example
2725 # @var{linenum} @var{filename} @var{flags}
2726 @end example
2727
2728 @noindent
2729 which are inserted as needed into the output (but never within a string
2730 or character constant), and in place of long sequences of empty lines.
2731 Such a line means that the following line originated in file
2732 @var{filename} at line @var{linenum}.
2733
2734 After the file name comes zero or more flags, which are @samp{1},
2735 @samp{2}, @samp{3}, or @samp{4}.  If there are multiple flags, spaces
2736 separate them.  Here is what the flags mean:
2737
2738 @table @samp
2739 @item 1
2740 This indicates the start of a new file.
2741 @item 2
2742 This indicates returning to a file (after having included another file).
2743 @item 3
2744 This indicates that the following text comes from a system header file,
2745 so certain warnings should be suppressed.
2746 @item 4
2747 This indicates that the following text should be treated as C@.
2748 @c maybe cross reference NO_IMPLICIT_EXTERN_C
2749 @end table
2750
2751 @node Unreliable Features, Invocation, Output, Top
2752 @section Undefined Behavior and Deprecated Features
2753 @cindex undefined behavior
2754 @cindex deprecated features
2755
2756 This section details GNU C preprocessor behavior that is subject to
2757 change or deprecated.  You are @emph{strongly advised} to write your
2758 software so it does not rely on anything described here; future versions
2759 of the preprocessor may subtly change such behavior or even remove the
2760 feature altogether.
2761
2762 Preservation of the form of whitespace between tokens is unlikely to
2763 change from current behavior (see @ref{Output}), but you are advised not
2764 to rely on it.
2765
2766 The following are undocumented and subject to change:-
2767
2768 @itemize @bullet
2769
2770 @item Interpretation of the filename between @samp{<} and @samp{>} tokens
2771  resulting from a macro-expanded @samp{#include} directive
2772
2773 The text between the @samp{<} and @samp{>} is taken literally if given
2774 directly within a @samp{#include} or similar directive.  If a directive
2775 of this form is obtained through macro expansion, however, behavior like
2776 preservation of whitespace, and interpretation of backslashes and quotes
2777 is undefined.  @xref{Include Syntax}
2778
2779 @item Precedence of ## operators with respect to each other
2780
2781 It is not defined whether a sequence of ## operators are evaluated
2782 left-to-right, right-to-left or indeed in a consistent direction at all.
2783 An example of where this might matter is pasting the arguments @samp{1},
2784 @samp{e} and @samp{-2}.  This would be fine for left-to-right pasting,
2785 but right-to-left pasting would produce an invalid token @samp{e-2}.
2786
2787 @item Precedence of # operator with respect to the ## operator
2788
2789 It is undefined which of these two operators is evaluated first.
2790
2791 @end itemize
2792
2793 The following features are deprecated and will likely be removed at some
2794 point in the future:-
2795
2796 @itemize @bullet
2797
2798 @item ## swallowing the previous token in GNU rest argument macros
2799
2800 In a macro expansion, if ## appeared before a GNU named variable arguments
2801 parameter, and the set of tokens specified for that argument in the
2802 macro invocation was empty, previous versions of the GNU C preprocessor
2803 would back up and remove the token appearing before the ##.  This
2804 behavior was not well-defined, and alternative ways of achieving its
2805 intended use are available.  Since the ISO C standard now provides for
2806 variable-argument macros, and since this old behavior potentially
2807 conflicts with behavior mandated by the standard, this feature is now
2808 deprecated and will be removed in future.
2809
2810 The current preprocessor still supports it for reasons of code
2811 migration, and warns at each use of the feature.
2812
2813 @item Optional argument when invoking GNU rest argument macros
2814
2815 In the invocation of a GNU named variable arguments macro, the variable
2816 arguments were optional.  For example, the following two invocations are
2817 both legal for GNU rest args.  The first is illegal in the equivalent
2818 formulation using ISO C anonymous variable arguments and
2819 @code{__VA_ARGS__}:-
2820
2821 @smallexample
2822 #define debug(format, args...) printf (format, args)
2823 debug("string");       /* Illegal in ISO C equivalent.  */
2824 debug("string",);      /* OK for both.  */
2825 @end smallexample
2826
2827 The current preprocessor still supports it for reasons of code
2828 migration, and warns at each use of the feature.
2829
2830 @item Attempting to paste two tokens which together do not form a valid
2831 preprocessing token
2832
2833 The preprocessor currently warns about this and outputs the two tokens
2834 adjacently, which is probably the behavior the programmer intends.  It
2835 may not work in future, though.
2836
2837 @findex #pragma once
2838 @item #pragma once
2839
2840 This pragma was once used to tell the preprocessor that it need not
2841 include a file more than once.  It is now obsolete and should not be
2842 used at all.
2843
2844 @item #pragma poison
2845
2846 This pragma has been superceded by @samp{#pragma GCC poison}.
2847 @xref{Poisoning}
2848
2849 @item Multi-line string literals in directives
2850
2851 The GNU C preprocessor currently allows newlines in string literals
2852 within a directive.
2853
2854 @end itemize
2855
2856 @node Invocation, Concept Index, Unreliable Features, Top
2857 @section Invoking the C Preprocessor
2858 @cindex invocation of the preprocessor
2859
2860 Most often when you use the C preprocessor you will not have to invoke it
2861 explicitly: the C compiler will do so automatically.  However, the
2862 preprocessor is sometimes useful on its own.
2863
2864 @ignore
2865 @c man begin SYNOPSIS
2866 cpp [@samp{-P}] [@samp{-C}] [@samp{-gcc}] [@samp{-traditional}]
2867     [@samp{-undef}] [@samp{-trigraphs}] [@samp{-pedantic}]
2868     [@samp{-W}@var{warn}...] [@samp{-I}@var{dir}...]
2869     [@samp{-D}@var{macro}[=@var{defn}]...] [@samp{-U}@var{macro}]
2870     [@samp{-A}@var{predicate}(@var{answer})]
2871     [@samp{-M}|@samp{-MM}|@samp{-MD}|@samp{-MMD} [@samp{-MG}]]
2872     [@samp{-x} @var{language}] [@samp{-std=}@var{standard}]
2873     @var{infile} @var{outfile}
2874
2875 Only the most useful options are listed here; see below for the remainder.
2876 @c man end
2877 @c man begin SEEALSO
2878 gcc(1), as(1), ld(1), and the Info entries for @file{cpp}, @file{gcc}, and
2879 @file{binutils}.
2880 @c man end
2881 @end ignore
2882
2883 @c man begin OPTIONS
2884 The C preprocessor expects two file names as arguments, @var{infile} and
2885 @var{outfile}.  The preprocessor reads @var{infile} together with any
2886 other files it specifies with @samp{#include}.  All the output generated
2887 by the combined input files is written in @var{outfile}.
2888
2889 Either @var{infile} or @var{outfile} may be @samp{-}, which as
2890 @var{infile} means to read from standard input and as @var{outfile}
2891 means to write to standard output.  Also, if either file is omitted, it
2892 means the same as if @samp{-} had been specified for that file.
2893
2894 @cindex options
2895 Here is a table of command options accepted by the C preprocessor.
2896 These options can also be given when compiling a C program; they are
2897 passed along automatically to the preprocessor when it is invoked by the
2898 compiler.
2899
2900 @table @samp
2901 @item -P
2902 @findex -P
2903 Inhibit generation of @samp{#}-lines with line-number information in
2904 the output from the preprocessor (@pxref{Output}).  This might be
2905 useful when running the preprocessor on something that is not C code
2906 and will be sent to a program which might be confused by the
2907 @samp{#}-lines.
2908
2909 @item -C
2910 @findex -C
2911 Do not discard comments.  All comments are passed through to the output
2912 file, except for comments in processed directives, which are deleted
2913 along with the directive.  Comments appearing in the expansion list of a
2914 macro will be preserved, and appear in place wherever the macro is
2915 invoked.
2916
2917 You should be prepared for side effects when using -C; it causes the
2918 preprocessor to treat comments as tokens in their own right.  For
2919 example, macro redefinitions that were trivial when comments were
2920 replaced by a single space might become significant when comments are
2921 retained.  Also, comments appearing at the start of what would be a
2922 directive line have the effect of turning that line into an ordinary
2923 source line, since the first token on the line is no longer a @samp{#}.
2924
2925 @item -traditional
2926 @findex -traditional
2927 Try to imitate the behavior of old-fashioned C, as opposed to ISO C@.
2928 Note: support for this option is currently fairly broken.
2929
2930 @itemize @bullet
2931 @item
2932 Traditional macro expansion pays no attention to single-quote or
2933 double-quote characters; macro argument symbols are replaced by the
2934 argument values even when they appear within apparent string or
2935 character constants.
2936
2937 @item
2938 Traditionally, it is permissible for a macro expansion to end in the
2939 middle of a string or character constant.  The constant continues into
2940 the text surrounding the macro call.
2941
2942 @item
2943 However, traditionally the end of the line terminates a string or
2944 character constant, with no error.
2945
2946 @item
2947 In traditional C, a comment is equivalent to no text at all.  (In ISO
2948 C, a comment counts as whitespace.)
2949
2950 @item
2951 Traditional C does not have the concept of a ``preprocessing number''.
2952 It considers @samp{1.0e+4} to be three tokens: @samp{1.0e}, @samp{+},
2953 and @samp{4}.
2954
2955 @item
2956 A macro is not suppressed within its own definition, in traditional C@.
2957 Thus, any macro that is used recursively inevitably causes an error.
2958
2959 @item
2960 The character @samp{#} has no special meaning within a macro definition
2961 in traditional C@.
2962
2963 @item
2964 In traditional C, the text at the end of a macro expansion can run
2965 together with the text after the macro call, to produce a single token.
2966 (This is impossible in ISO C@.)
2967
2968 @item
2969 Traditionally, @samp{\} inside a macro argument suppresses the syntactic
2970 significance of the following character.
2971 @end itemize
2972
2973 @cindex Fortran
2974 @cindex unterminated
2975 Use the @samp{-traditional} option when preprocessing Fortran code, so
2976 that single-quotes and double-quotes within Fortran comment lines (which
2977 are generally not recognized as such by the preprocessor) do not cause
2978 diagnostics about unterminated character or string constants.
2979
2980 However, this option does not prevent diagnostics about unterminated
2981 comments when a C-style comment appears to start, but not end, within
2982 Fortran-style commentary.
2983
2984 So, the following Fortran comment lines are accepted with
2985 @samp{-traditional}:
2986
2987 @smallexample
2988 C This isn't an unterminated character constant
2989 C Neither is "20000000000, an octal constant
2990 C in some dialects of Fortran
2991 @end smallexample
2992
2993 However, this type of comment line will likely produce a diagnostic, or
2994 at least unexpected output from the preprocessor, due to the
2995 unterminated comment:
2996
2997 @smallexample
2998 C Some Fortran compilers accept /* as starting
2999 C an inline comment.
3000 @end smallexample
3001
3002 @cindex g77
3003 Note that @code{g77} automatically supplies the @samp{-traditional}
3004 option when it invokes the preprocessor.  However, a future version of
3005 @code{g77} might use a different, more-Fortran-aware preprocessor in
3006 place of @code{cpp}.
3007
3008 @item -trigraphs
3009 @findex -trigraphs
3010 Process ISO standard trigraph sequences.  These are three-character
3011 sequences, all starting with @samp{??}, that are defined by ISO C to
3012 stand for single characters.  For example, @samp{??/} stands for
3013 @samp{\}, so @samp{'??/n'} is a character constant for a newline.
3014 Strictly speaking, the GNU C preprocessor does not conform to ISO
3015 Standard C unless @samp{-trigraphs} is used, but if you ever notice the
3016 difference it will be with relief.
3017
3018 The nine trigraph sequences are
3019 @samp{??(} -> @samp{[},
3020 @samp{??)} -> @samp{]},
3021 @samp{??<} -> @samp{@{},
3022 @samp{??>} -> @samp{@}},
3023 @samp{??=} -> @samp{#},
3024 @samp{??/} -> @samp{\},
3025 @samp{??'} -> @samp{^},
3026 @samp{??!} -> @samp{|},
3027 @samp{??-} -> @samp{~}
3028
3029 @item -pedantic
3030 @findex -pedantic
3031 Issue warnings required by the ISO C standard in certain cases such
3032 as when text other than a comment follows @samp{#else} or @samp{#endif}.
3033
3034 @item -pedantic-errors
3035 @findex -pedantic-errors
3036 Like @samp{-pedantic}, except that errors are produced rather than
3037 warnings.
3038
3039 @item -Wcomment
3040 @findex -Wcomment
3041 @ignore
3042 @c "Not worth documenting" both singular and plural forms of this
3043 @c option, per RMS.  Also unclear which is better; hence may need to
3044 @c switch this at some future date.  pesch@cygnus.com, 2jan92.
3045 @itemx -Wcomments
3046 (Both forms have the same effect).
3047 @end ignore
3048 Warn whenever a comment-start sequence @samp{/*} appears in a @samp{/*}
3049 comment, or whenever a backslash-newline appears in a @samp{//} comment.
3050
3051 @item -Wtrigraphs
3052 @findex -Wtrigraphs
3053 Warn if any trigraphs are encountered.
3054
3055 @item -Wwhite-space
3056 @findex -Wwhite-space
3057 Warn about possible white space confusion, e.g. white space between a
3058 backslash and a newline.
3059
3060 @item -Wall
3061 @findex -Wall
3062 Requests @samp{-Wcomment}, @samp{-Wtrigraphs}, and @samp{-Wwhite-space}
3063 (but not @samp{-Wtraditional} or @samp{-Wundef}).
3064
3065 @item -Wtraditional
3066 @findex -Wtraditional
3067 Warn about certain constructs that behave differently in traditional and
3068 ISO C@.
3069
3070 @item -Wundef
3071 @findex -Wundef
3072 Warn if an undefined identifier is evaluated in an @samp{#if} directive.
3073
3074 @item -I @var{directory}
3075 @findex -I
3076 Add the directory @var{directory} to the head of the list of
3077 directories to be searched for header files (@pxref{Include Syntax}).
3078 This can be used to override a system header file, substituting your
3079 own version, since these directories are searched before the system
3080 header file directories.  If you use more than one @samp{-I} option,
3081 the directories are scanned in left-to-right order; the standard
3082 system directories come after.
3083
3084 @item -I-
3085 Any directories specified with @samp{-I} options before the @samp{-I-}
3086 option are searched only for the case of @samp{#include "@var{file}"};
3087 they are not searched for @samp{#include <@var{file}>}.
3088
3089 If additional directories are specified with @samp{-I} options after
3090 the @samp{-I-}, these directories are searched for all @samp{#include}
3091 directives.
3092
3093 In addition, the @samp{-I-} option inhibits the use of the current
3094 directory as the first search directory for @samp{#include "@var{file}"}.
3095 Therefore, the current directory is searched only if it is requested
3096 explicitly with @samp{-I.}.  Specifying both @samp{-I-} and @samp{-I.}
3097 allows you to control precisely which directories are searched before
3098 the current one and which are searched after.
3099
3100 @item -nostdinc
3101 @findex -nostdinc
3102 Do not search the standard system directories for header files.
3103 Only the directories you have specified with @samp{-I} options
3104 (and the current directory, if appropriate) are searched.
3105
3106 @item -nostdinc++
3107 @findex -nostdinc++
3108 Do not search for header files in the C++-specific standard directories,
3109 but do still search the other standard directories.  (This option is
3110 used when building the C++ library.)
3111
3112 @item -remap
3113 @findex -remap
3114 When searching for a header file in a directory, remap file names if a
3115 file named @file{header.gcc} exists in that directory.  This can be used
3116 to work around limitations of file systems with file name restrictions.
3117 The @file{header.gcc} file should contain a series of lines with two
3118 tokens on each line: the first token is the name to map, and the second
3119 token is the actual name to use.
3120
3121 @item -D @var{name}
3122 @findex -D
3123 Predefine @var{name} as a macro, with definition @samp{1}.
3124
3125 @item -D @var{name}=@var{definition}
3126 Predefine @var{name} as a macro, with definition @var{definition}.
3127 There are no restrictions on the contents of @var{definition}, but if
3128 you are invoking the preprocessor from a shell or shell-like program you
3129 may need to use the shell's quoting syntax to protect characters such as
3130 spaces that have a meaning in the shell syntax.  If you use more than
3131 one @samp{-D} for the same @var{name}, the rightmost definition takes
3132 effect.
3133
3134 @item -U @var{name}
3135 @findex -U
3136 Do not predefine @var{name}.  If both @samp{-U} and @samp{-D} are
3137 specified for one name, whichever one appears later on the command line
3138 wins.
3139
3140 @item -undef
3141 @findex -undef
3142 Do not predefine any nonstandard macros.
3143
3144 @item -gcc
3145 @findex -gcc
3146 Define the macros @var{__GNUC__}, @var{__GNUC_MINOR__} and
3147 @var{__GNUC_PATCHLEVEL__}. These are defined automatically when you use
3148 @samp{gcc -E}; you can turn them off in that case with @samp{-no-gcc}.
3149
3150 @item -A @var{predicate}(@var{answer})
3151 @findex -A
3152 Make an assertion with the predicate @var{predicate} and answer
3153 @var{answer}.  @xref{Assertions}.
3154
3155 @item -A -@var{predicate}(@var{answer})
3156 Disable an assertion with the predicate @var{predicate} and answer
3157 @var{answer}.  Specifying no predicate, by @samp{-A-} or @samp{-A -},
3158 disables all predefined assertions and all assertions preceding it on
3159 the command line; and also undefines all predefined macros and all
3160 macros preceding it on the command line.
3161
3162 @item -dM
3163 @findex -dM
3164 Instead of outputting the result of preprocessing, output a list of
3165 @samp{#define} directives for all the macros defined during the
3166 execution of the preprocessor, including predefined macros.  This gives
3167 you a way of finding out what is predefined in your version of the
3168 preprocessor; assuming you have no file @samp{foo.h}, the command
3169
3170 @example
3171 touch foo.h; cpp -dM foo.h
3172 @end example
3173
3174 @noindent 
3175 will show the values of any predefined macros.
3176
3177 @item -dD
3178 @findex -dD
3179 Like @samp{-dM} except in two respects: it does @emph{not} include the
3180 predefined macros, and it outputs @emph{both} the @samp{#define}
3181 directives and the result of preprocessing.  Both kinds of output go to
3182 the standard output file.
3183
3184 @item -dI
3185 @findex -dI
3186 Output @samp{#include} directives in addition to the result of
3187 preprocessing.
3188
3189 @item -M [-MG]
3190 @findex -M
3191 Instead of outputting the result of preprocessing, output a rule
3192 suitable for @code{make} describing the dependencies of the main source
3193 file.  The preprocessor outputs one @code{make} rule containing the
3194 object file name for that source file, a colon, and the names of all the
3195 included files.  If there are many included files then the rule is split
3196 into several lines using @samp{\}-newline.
3197
3198 @samp{-MG} says to treat missing header files as generated files and
3199 assume they live in the same directory as the source file.  It must be
3200 specified in addition to @samp{-M}.
3201
3202 This feature is used in automatic updating of makefiles.
3203
3204 @item -MM [-MG]
3205 @findex -MM
3206 Like @samp{-M} but mention only the files included with @samp{#include
3207 "@var{file}"}.  System header files included with @samp{#include
3208 <@var{file}>} are omitted.
3209
3210 @item -MD @var{file}
3211 @findex -MD
3212 Like @samp{-M} but the dependency information is written to @var{file}.
3213 This is in addition to compiling the file as specified --- @samp{-MD}
3214 does not inhibit ordinary compilation the way @samp{-M} does.
3215
3216 When invoking @code{gcc}, do not specify the @var{file} argument.
3217 @code{gcc} will create file names made by replacing ".c" with ".d" at
3218 the end of the input file names.
3219
3220 In Mach, you can use the utility @code{md} to merge multiple dependency
3221 files into a single dependency file suitable for using with the
3222 @samp{make} command.
3223
3224 @item -MMD @var{file}
3225 @findex -MMD
3226 Like @samp{-MD} except mention only user header files, not system
3227 header files.
3228
3229 @item -H
3230 @findex -H
3231 Print the name of each header file used, in addition to other normal
3232 activities.
3233
3234 @item -imacros @var{file}
3235 @findex -imacros
3236 Process @var{file} as input, discarding the resulting output, before
3237 processing the regular input file.  Because the output generated from
3238 @var{file} is discarded, the only effect of @samp{-imacros @var{file}}
3239 is to make the macros defined in @var{file} available for use in the
3240 main input.
3241
3242 @item -include @var{file}
3243 @findex -include
3244 Process @var{file} as input, and include all the resulting output,
3245 before processing the regular input file.  
3246
3247 @item -idirafter @var{dir}
3248 @findex -idirafter
3249 @cindex second include path
3250 Add the directory @var{dir} to the second include path.  The directories
3251 on the second include path are searched when a header file is not found
3252 in any of the directories in the main include path (the one that
3253 @samp{-I} adds to).
3254
3255 @item -iprefix @var{prefix}
3256 @findex -iprefix
3257 Specify @var{prefix} as the prefix for subsequent @samp{-iwithprefix}
3258 options.  If the prefix represents a directory, you should include the
3259 final @samp{/}.
3260
3261 @item -iwithprefix @var{dir}
3262 @findex -iwithprefix
3263 Add a directory to the second include path.  The directory's name is
3264 made by concatenating @var{prefix} and @var{dir}, where @var{prefix} was
3265 specified previously with @samp{-iprefix}.
3266
3267 @item -isystem @var{dir}
3268 @findex -isystem
3269 Add a directory to the beginning of the second include path, marking it
3270 as a system directory, so that it gets the same special treatment as
3271 is applied to the standard system directories.  @xref{System Headers}.
3272
3273 @item -x c
3274 @itemx -x c++
3275 @itemx -x objective-c
3276 @itemx -x assembler-with-cpp
3277 @findex -x c
3278 @findex -x objective-c
3279 @findex -x assembler-with-cpp
3280 Specify the source language: C, C++, Objective-C, or assembly.  This has
3281 nothing to do with standards conformance or extensions; it merely
3282 selects which base syntax to expect.  If you give none of these options,
3283 cpp will deduce the language from the extension of the source file:
3284 @samp{.c}, @samp{.cc}, @samp{.m}, or @samp{.S}.  Some other common
3285 extensions for C++ and assembly are also recognized.  If cpp does not
3286 recognize the extension, it will treat the file as C; this is the most
3287 generic mode.
3288
3289 @strong{Note:} Previous versions of cpp accepted a @samp{-lang} option
3290 which selected both the language and the standards conformance level.
3291 This option has been removed, because it conflicts with the @samp{-l}
3292 option.
3293
3294 @item -std=@var{standard}
3295 @itemx -ansi
3296 @findex -std
3297 @findex -ansi
3298 Specify the standard to which the code should conform.  Currently cpp
3299 only knows about the standards for C; other language standards will be
3300 added in the future.
3301
3302 @var{standard}
3303 may be one of:
3304 @table @code
3305 @item iso9899:1990
3306 The ISO C standard from 1990.
3307
3308 @item iso9899:199409
3309 @itemx c89
3310 The 1990 C standard, as amended in 1994.  @samp{c89} is the customary
3311 shorthand for this version of the standard.
3312
3313 The @samp{-ansi} option is equivalent to @samp{-std=c89}.
3314
3315 @item iso9899:199x
3316 @itemx c9x
3317 The revised ISO C standard, which is expected to be promulgated some
3318 time in 1999.  It has not been approved yet, hence the @samp{x}.
3319
3320 @item gnu89
3321 The 1990 C standard plus GNU extensions.  This is the default.
3322
3323 @item gnu9x
3324 The 199x C standard plus GNU extensions.
3325 @end table
3326
3327 @item -Wp,-lint
3328 @findex -lint
3329 Look for commands to the program checker @code{lint} embedded in
3330 comments, and emit them preceded by @samp{#pragma lint}.  For example,
3331 the comment @samp{/* NOTREACHED */} becomes @samp{#pragma lint
3332 NOTREACHED}.
3333
3334 Because of the clash with @samp{-l}, you must use the awkward syntax
3335 above.  In a future release, this option will be replaced by
3336 @samp{-flint} or @samp{-Wlint}; we are not sure which yet.
3337
3338 @item -ftabstop=NUMBER
3339 @findex -ftabstop
3340 Indicates the distance between tabstops.  This helps the preprocessor
3341 report correct column numbers in warnings or errors, even if tabs appear
3342 on the line.  Values less than 1 or greater than 100 are ignored.  The
3343 default is 8.
3344
3345 @item -$
3346 @findex -$
3347 Forbid the use of @samp{$} in identifiers.  The C standard allows
3348 implementations to define extra characters that can appear in
3349 identifiers.  By default the GNU C preprocessor permits @samp{$}, a
3350 common extension.
3351 @end table
3352 @c man end
3353
3354 @node Concept Index, Index, Invocation, Top
3355 @unnumbered Concept Index
3356 @printindex cp
3357
3358 @node Index,, Concept Index, Top
3359 @unnumbered Index of Directives, Macros and Options
3360 @printindex fn
3361
3362 @contents
3363 @bye