OSDN Git Service

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