import of gcc-2.8
[official-gcc.git] / gcc / cpp.texi
blob9424b48f9850b6bc72c0e3696cf94867e22e4dde
1 \input texinfo
2 @setfilename cpp.info
3 @settitle The C Preprocessor
5 @ignore
6 @ifinfo
7 @format
8 START-INFO-DIR-ENTRY
9 * Cpp: (cpp).                   The C preprocessor.
10 END-INFO-DIR-ENTRY
11 @end format
12 @end ifinfo
13 @end ignore
15 @c @smallbook
16 @c @cropmarks
17 @c @finalout
18 @setchapternewpage odd
19 @ifinfo
20 This file documents the GNU C Preprocessor.
22 Copyright 1987, 1989, 1991, 1992, 1993, 1994, 1995 Free Software
23 Foundation, Inc.
25 Permission is granted to make and distribute verbatim copies of
26 this manual provided the copyright notice and this permission notice
27 are preserved on all copies.
29 @ignore
30 Permission is granted to process this file through Tex and print the
31 results, provided the printed document carries copying permission
32 notice identical to this one except for the removal of this paragraph
33 (this paragraph not being relevant to the printed manual).
35 @end ignore
36 Permission is granted to copy and distribute modified versions of this
37 manual under the conditions for verbatim copying, provided also that
38 the entire resulting derived work is distributed under the terms of a
39 permission notice identical to this one.
41 Permission is granted to copy and distribute translations of this manual
42 into another language, under the above conditions for modified versions.
43 @end ifinfo
45 @titlepage
46 @c @finalout
47 @title The C Preprocessor
48 @subtitle Last revised March 1997
49 @subtitle for GCC version 2
50 @author Richard M. Stallman
51 @page
52 @vskip 2pc
53 This booklet is eventually intended to form the first chapter of a GNU 
54 C Language manual.
56 @vskip 0pt plus 1filll
57 Copyright @copyright{} 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997 Free
58 Software Foundation, Inc.
60 Permission is granted to make and distribute verbatim copies of
61 this manual provided the copyright notice and this permission notice
62 are preserved on all copies.
64 Permission is granted to copy and distribute modified versions of this
65 manual under the conditions for verbatim copying, provided also that
66 the entire resulting derived work is distributed under the terms of a
67 permission notice identical to this one.
69 Permission is granted to copy and distribute translations of this manual
70 into another language, under the above conditions for modified versions.
71 @end titlepage
72 @page
74 @node Top, Global Actions,, (DIR)
75 @chapter The C Preprocessor
77 The C preprocessor is a @dfn{macro processor} that is used automatically by
78 the C compiler to transform your program before actual compilation.  It is
79 called a macro processor because it allows you to define @dfn{macros},
80 which are brief abbreviations for longer constructs.
82 The C preprocessor provides four separate facilities that you can use as
83 you see fit:
85 @itemize @bullet
86 @item
87 Inclusion of header files.  These are files of declarations that can be
88 substituted into your program.
90 @item
91 Macro expansion.  You can define @dfn{macros}, which are abbreviations
92 for arbitrary fragments of C code, and then the C preprocessor will
93 replace the macros with their definitions throughout the program.
95 @item
96 Conditional compilation.  Using special preprocessing directives, you
97 can include or exclude parts of the program according to various
98 conditions.
100 @item
101 Line control.  If you use a program to combine or rearrange source files into
102 an intermediate file which is then compiled, you can use line control
103 to inform the compiler of where each source line originally came from.
104 @end itemize
106 C preprocessors vary in some details.  This manual discusses the GNU C
107 preprocessor, the C Compatible Compiler Preprocessor.  The GNU C
108 preprocessor provides a superset of the features of ANSI Standard C@.
110 ANSI Standard C requires the rejection of many harmless constructs commonly
111 used by today's C programs.  Such incompatibility would be inconvenient for
112 users, so the GNU C preprocessor is configured to accept these constructs
113 by default.  Strictly speaking, to get ANSI Standard C, you must use the
114 options @samp{-trigraphs}, @samp{-undef} and @samp{-pedantic}, but in
115 practice the consequences of having strict ANSI Standard C make it
116 undesirable to do this.  @xref{Invocation}.
118 The C preprocessor is designed for C-like languages; you may run into
119 problems if you apply it to other kinds of languages, because it assumes
120 that it is dealing with C@.  For example, the C preprocessor sometimes
121 outputs extra white space to avoid inadvertent C token concatenation,
122 and this may cause problems with other languages.
124 @menu
125 * Global Actions::    Actions made uniformly on all input files.
126 * Directives::        General syntax of preprocessing directives.
127 * Header Files::      How and why to use header files.
128 * Macros::            How and why to use macros.
129 * Conditionals::      How and why to use conditionals.
130 * Combining Sources:: Use of line control when you combine source files.
131 * Other Directives::  Miscellaneous preprocessing directives.
132 * Output::            Format of output from the C preprocessor.
133 * Invocation::        How to invoke the preprocessor; command options.
134 * Concept Index::     Index of concepts and terms.
135 * Index::             Index of directives, predefined macros and options.
136 @end menu
138 @node Global Actions, Directives, Top, Top
139 @section Transformations Made Globally
141 Most C preprocessor features are inactive unless you give specific directives
142 to request their use.  (Preprocessing directives are lines starting with
143 @samp{#}; @pxref{Directives}).  But there are three transformations that the
144 preprocessor always makes on all the input it receives, even in the absence
145 of directives.
147 @itemize @bullet
148 @item
149 All C comments are replaced with single spaces.
151 @item
152 Backslash-Newline sequences are deleted, no matter where.  This
153 feature allows you to break long lines for cosmetic purposes without
154 changing their meaning.
156 @item
157 Predefined macro names are replaced with their expansions
158 (@pxref{Predefined}).
159 @end itemize
161 The first two transformations are done @emph{before} nearly all other parsing
162 and before preprocessing directives are recognized.  Thus, for example, you
163 can split a line cosmetically with Backslash-Newline anywhere (except
164 when trigraphs are in use; see below).
166 @example
168 */ # /*
169 */ defi\
170 ne FO\
171 O 10\
173 @end example
175 @noindent
176 is equivalent into @samp{#define FOO 1020}.  You can split even an escape
177 sequence with Backslash-Newline.  For example, you can split @code{"foo\bar"}
178 between the @samp{\} and the @samp{b} to get
180 @example
181 "foo\\
182 bar"
183 @end example
185 @noindent
186 This behavior is unclean: in all other contexts, a Backslash can be
187 inserted in a string constant as an ordinary character by writing a double
188 Backslash, and this creates an exception.  But the ANSI C standard requires
189 it.  (Strict ANSI C does not allow Newlines in string constants, so they
190 do not consider this a problem.)
192 But there are a few exceptions to all three transformations.
194 @itemize @bullet
195 @item
196 C comments and predefined macro names are not recognized inside a
197 @samp{#include} directive in which the file name is delimited with
198 @samp{<} and @samp{>}.
200 @item
201 C comments and predefined macro names are never recognized within a
202 character or string constant.  (Strictly speaking, this is the rule,
203 not an exception, but it is worth noting here anyway.)
205 @item
206 Backslash-Newline may not safely be used within an ANSI ``trigraph''.
207 Trigraphs are converted before Backslash-Newline is deleted.  If you
208 write what looks like a trigraph with a Backslash-Newline inside, the
209 Backslash-Newline is deleted as usual, but it is then too late to
210 recognize the trigraph.
212 This exception is relevant only if you use the @samp{-trigraphs}
213 option to enable trigraph processing.  @xref{Invocation}.
214 @end itemize
216 @node Directives, Header Files, Global Actions, Top
217 @section Preprocessing Directives
219 @cindex preprocessing directives
220 @cindex directives
221 Most preprocessor features are active only if you use preprocessing directives
222 to request their use.
224 Preprocessing directives are lines in your program that start with @samp{#}.
225 The @samp{#} is followed by an identifier that is the @dfn{directive name}.
226 For example, @samp{#define} is the directive that defines a macro.
227 Whitespace is also allowed before and after the @samp{#}.
229 The set of valid directive names is fixed.  Programs cannot define new
230 preprocessing directives.
232 Some directive names require arguments; these make up the rest of the directive
233 line and must be separated from the directive name by whitespace.  For example,
234 @samp{#define} must be followed by a macro name and the intended expansion
235 of the macro.  @xref{Simple Macros}.
237 A preprocessing directive cannot be more than one line in normal circumstances.
238 It may be split cosmetically with Backslash-Newline, but that has no effect
239 on its meaning.  Comments containing Newlines can also divide the
240 directive into multiple lines, but the comments are changed to Spaces
241 before the directive is interpreted.  The only way a significant Newline
242 can occur in a preprocessing directive is within a string constant or
243 character constant.  Note that
244 most C compilers that might be applied to the output from the preprocessor
245 do not accept string or character constants containing Newlines.
247 The @samp{#} and the directive name cannot come from a macro expansion.  For
248 example, if @samp{foo} is defined as a macro expanding to @samp{define},
249 that does not make @samp{#foo} a valid preprocessing directive.
251 @node Header Files, Macros, Directives, Top
252 @section Header Files
254 @cindex header file
255 A header file is a file containing C declarations and macro definitions
256 (@pxref{Macros}) to be shared between several source files.  You request
257 the use of a header file in your program with the C preprocessing directive
258 @samp{#include}.
260 @menu
261 * Header Uses::         What header files are used for.
262 * Include Syntax::      How to write @samp{#include} directives.
263 * Include Operation::   What @samp{#include} does.
264 * Once-Only::           Preventing multiple inclusion of one header file.
265 * Inheritance::         Including one header file in another header file.
266 @end menu
268 @node Header Uses, Include Syntax, Header Files, Header Files
269 @subsection Uses of Header Files
271 Header files serve two kinds of purposes.
273 @itemize @bullet
274 @item
275 @findex system header files
276 System header files declare the interfaces to parts of the operating
277 system.  You include them in your program to supply the definitions and
278 declarations you need to invoke system calls and libraries.
280 @item
281 Your own header files contain declarations for interfaces between the
282 source files of your program.  Each time you have a group of related
283 declarations and macro definitions all or most of which are needed in
284 several different source files, it is a good idea to create a header
285 file for them.
286 @end itemize
288 Including a header file produces the same results in C compilation as
289 copying the header file into each source file that needs it.  But such
290 copying would be time-consuming and error-prone.  With a header file, the
291 related declarations appear in only one place.  If they need to be changed,
292 they can be changed in one place, and programs that include the header file
293 will automatically use the new version when next recompiled.  The header
294 file eliminates the labor of finding and changing all the copies as well as
295 the risk that a failure to find one copy will result in inconsistencies
296 within a program.
298 The usual convention is to give header files names that end with
299 @file{.h}.  Avoid unusual characters in header file names, as they
300 reduce portability.
302 @node Include Syntax, Include Operation, Header Uses, Header Files
303 @subsection The @samp{#include} Directive
305 @findex #include
306 Both user and system header files are included using the preprocessing
307 directive @samp{#include}.  It has three variants:
309 @table @code
310 @item #include <@var{file}>
311 This variant is used for system header files.  It searches for a file
312 named @var{file} in a list of directories specified by you, then in a
313 standard list of system directories.  You specify directories to
314 search for header files with the command option @samp{-I}
315 (@pxref{Invocation}).  The option @samp{-nostdinc} inhibits searching
316 the standard system directories; in this case only the directories
317 you specify are searched.
319 The parsing of this form of @samp{#include} is slightly special
320 because comments are not recognized within the @samp{<@dots{}>}.
321 Thus, in @samp{#include <x/*y>} the @samp{/*} does not start a comment
322 and the directive specifies inclusion of a system header file named
323 @file{x/*y}.  Of course, a header file with such a name is unlikely to
324 exist on Unix, where shell wildcard features would make it hard to
325 manipulate.@refill
327 The argument @var{file} may not contain a @samp{>} character.  It may,
328 however, contain a @samp{<} character.
330 @item #include "@var{file}"
331 This variant is used for header files of your own program.  It
332 searches for a file named @var{file} first in the current directory,
333 then in the same directories used for system header files.  The
334 current directory is the directory of the current input file.  It is
335 tried first because it is presumed to be the location of the files
336 that the current input file refers to.  (If the @samp{-I-} option is
337 used, the special treatment of the current directory is inhibited.)
339 The argument @var{file} may not contain @samp{"} characters.  If
340 backslashes occur within @var{file}, they are considered ordinary text
341 characters, not escape characters.  None of the character escape
342 sequences appropriate to string constants in C are processed.  Thus,
343 @samp{#include "x\n\\y"} specifies a filename containing three
344 backslashes.  It is not clear why this behavior is ever useful, but
345 the ANSI standard specifies it.
347 @item #include @var{anything else}
348 @cindex computed @samp{#include}
349 This variant is called a @dfn{computed #include}.  Any @samp{#include}
350 directive whose argument does not fit the above two forms is a computed
351 include.  The text @var{anything else} is checked for macro calls,
352 which are expanded (@pxref{Macros}).  When this is done, the result
353 must fit one of the above two variants---in particular, the expanded
354 text must in the end be surrounded by either quotes or angle braces.
356 This feature allows you to define a macro which controls the file name
357 to be used at a later point in the program.  One application of this is
358 to allow a site-specific configuration file for your program to specify
359 the names of the system include files to be used.  This can help in
360 porting the program to various operating systems in which the necessary
361 system header files are found in different places.
362 @end table
364 @node Include Operation, Once-Only, Include Syntax, Header Files
365 @subsection How @samp{#include} Works
367 The @samp{#include} directive works by directing the C preprocessor to scan
368 the specified file as input before continuing with the rest of the current
369 file.  The output from the preprocessor contains the output already
370 generated, followed by the output resulting from the included file,
371 followed by the output that comes from the text after the @samp{#include}
372 directive.  For example, given a header file @file{header.h} as follows,
374 @example
375 char *test ();
376 @end example
378 @noindent
379 and a main program called @file{program.c} that uses the header file,
380 like this,
382 @example
383 int x;
384 #include "header.h"
386 main ()
388   printf (test ());
390 @end example
392 @noindent
393 the output generated by the C preprocessor for @file{program.c} as input
394 would be
396 @example
397 int x;
398 char *test ();
400 main ()
402   printf (test ());
404 @end example
406 Included files are not limited to declarations and macro definitions; those
407 are merely the typical uses.  Any fragment of a C program can be included
408 from another file.  The include file could even contain the beginning of a
409 statement that is concluded in the containing file, or the end of a
410 statement that was started in the including file.  However, a comment or a
411 string or character constant may not start in the included file and finish
412 in the including file.  An unterminated comment, string constant or
413 character constant in an included file is considered to end (with an error
414 message) at the end of the file.
416 It is possible for a header file to begin or end a syntactic unit such
417 as a function definition, but that would be very confusing, so don't do
420 The line following the @samp{#include} directive is always treated as a
421 separate line by the C preprocessor even if the included file lacks a final
422 newline.
424 @node Once-Only, Inheritance, Include Operation, Header Files
425 @subsection Once-Only Include Files
426 @cindex repeated inclusion
427 @cindex including just once
429 Very often, one header file includes another.  It can easily result that a
430 certain header file is included more than once.  This may lead to errors,
431 if the header file defines structure types or typedefs, and is certainly
432 wasteful.  Therefore, we often wish to prevent multiple inclusion of a
433 header file.
435 The standard way to do this is to enclose the entire real contents of the
436 file in a conditional, like this:
438 @example
439 #ifndef FILE_FOO_SEEN
440 #define FILE_FOO_SEEN
442 @var{the entire file}
444 #endif /* FILE_FOO_SEEN */
445 @end example
447 The macro @code{FILE_FOO_SEEN} indicates that the file has been included
448 once already.  In a user header file, the macro name should not begin
449 with @samp{_}.  In a system header file, this name should begin with
450 @samp{__} to avoid conflicts with user programs.  In any kind of header
451 file, the macro name should contain the name of the file and some
452 additional text, to avoid conflicts with other header files.
454 The GNU C preprocessor is programmed to notice when a header file uses
455 this particular construct and handle it efficiently.  If a header file
456 is contained entirely in a @samp{#ifndef} conditional, then it records
457 that fact.  If a subsequent @samp{#include} specifies the same file,
458 and the macro in the @samp{#ifndef} is already defined, then the file
459 is entirely skipped, without even reading it.
461 @findex #pragma once
462 There is also an explicit directive to tell the preprocessor that it need
463 not include a file more than once.  This is called @samp{#pragma once},
464 and was used @emph{in addition to} the @samp{#ifndef} conditional around
465 the contents of the header file.  @samp{#pragma once} is now obsolete
466 and should not be used at all.
468 @findex #import
469 In the Objective C language, there is a variant of @samp{#include}
470 called @samp{#import} which includes a file, but does so at most once.
471 If you use @samp{#import} @emph{instead of} @samp{#include}, then you
472 don't need the conditionals inside the header file to prevent multiple
473 execution of the contents.
475 @samp{#import} is obsolete because it is not a well designed feature.
476 It requires the users of a header file---the applications
477 programmers---to know that a certain header file should only be included
478 once.  It is much better for the header file's implementor to write the
479 file so that users don't need to know this.  Using @samp{#ifndef}
480 accomplishes this goal.
482 @node Inheritance,, Once-Only, Header Files
483 @subsection Inheritance and Header Files
484 @cindex inheritance
485 @cindex overriding a header file
487 @dfn{Inheritance} is what happens when one object or file derives some
488 of its contents by virtual copying from another object or file.  In
489 the case of C header files, inheritance means that one header file 
490 includes another header file and then replaces or adds something.
492 If the inheriting header file and the base header file have different
493 names, then inheritance is straightforward: simply write @samp{#include
494 "@var{base}"} in the inheriting file.
496 Sometimes it is necessary to give the inheriting file the same name as
497 the base file.  This is less straightforward.
499 For example, suppose an application program uses the system header
500 @file{sys/signal.h}, but the version of @file{/usr/include/sys/signal.h}
501 on a particular system doesn't do what the application program expects.
502 It might be convenient to define a ``local'' version, perhaps under the
503 name @file{/usr/local/include/sys/signal.h}, to override or add to the
504 one supplied by the system.
506 You can do this by compiling with the option @samp{-I.}, and
507 writing a file @file{sys/signal.h} that does what the application
508 program expects.  But making this file include the standard
509 @file{sys/signal.h} is not so easy---writing @samp{#include
510 <sys/signal.h>} in that file doesn't work, because it includes your own
511 version of the file, not the standard system version.  Used in that file
512 itself, this leads to an infinite recursion and a fatal error in
513 compilation.
515 @samp{#include </usr/include/sys/signal.h>} would find the proper file,
516 but that is not clean, since it makes an assumption about where the
517 system header file is found.  This is bad for maintenance, since it
518 means that any change in where the system's header files are kept
519 requires a change somewhere else.
521 @findex #include_next
522 The clean way to solve this problem is to use 
523 @samp{#include_next}, which means, ``Include the @emph{next} file with
524 this name.''  This directive works like @samp{#include} except in
525 searching for the specified file: it starts searching the list of header
526 file directories @emph{after} the directory in which the current file
527 was found.
529 Suppose you specify @samp{-I /usr/local/include}, and the list of
530 directories to search also includes @file{/usr/include}; and suppose
531 both directories contain @file{sys/signal.h}.  Ordinary
532 @samp{#include <sys/signal.h>} finds the file under
533 @file{/usr/local/include}.  If that file contains @samp{#include_next
534 <sys/signal.h>}, it starts searching after that directory, and finds the
535 file in @file{/usr/include}.
537 @node Macros, Conditionals, Header Files, Top
538 @section Macros
540 A macro is a sort of abbreviation which you can define once and then
541 use later.  There are many complicated features associated with macros
542 in the C preprocessor.
544 @menu
545 * Simple Macros::    Macros that always expand the same way.
546 * Argument Macros::  Macros that accept arguments that are substituted
547                        into the macro expansion.
548 * Predefined::       Predefined macros that are always available.
549 * Stringification::  Macro arguments converted into string constants.
550 * Concatenation::    Building tokens from parts taken from macro arguments.
551 * Undefining::       Cancelling a macro's definition.
552 * Redefining::       Changing a macro's definition.
553 * Macro Pitfalls::   Macros can confuse the unwary.  Here we explain
554                        several common problems and strange features.
555 @end menu
557 @node Simple Macros, Argument Macros, Macros, Macros
558 @subsection Simple Macros
559 @cindex simple macro
560 @cindex manifest constant
562 A @dfn{simple macro} is a kind of abbreviation.  It is a name which
563 stands for a fragment of code.  Some people refer to these as
564 @dfn{manifest constants}.
566 Before you can use a macro, you must @dfn{define} it explicitly with the
567 @samp{#define} directive.  @samp{#define} is followed by the name of the
568 macro and then the code it should be an abbreviation for.  For example,
570 @example
571 #define BUFFER_SIZE 1020
572 @end example
574 @noindent
575 defines a macro named @samp{BUFFER_SIZE} as an abbreviation for the text
576 @samp{1020}.  If somewhere after this @samp{#define} directive there comes
577 a C statement of the form
579 @example
580 foo = (char *) xmalloc (BUFFER_SIZE);
581 @end example
583 @noindent
584 then the C preprocessor will recognize and @dfn{expand} the macro
585 @samp{BUFFER_SIZE}, resulting in
587 @example
588 foo = (char *) xmalloc (1020);
589 @end example
591 The use of all upper case for macro names is a standard convention.
592 Programs are easier to read when it is possible to tell at a glance which
593 names are macros.
595 Normally, a macro definition must be a single line, like all C
596 preprocessing directives.  (You can split a long macro definition
597 cosmetically with Backslash-Newline.)  There is one exception: Newlines
598 can be included in the macro definition if within a string or character
599 constant.  This is because it is not possible for a macro definition to
600 contain an unbalanced quote character; the definition automatically
601 extends to include the matching quote character that ends the string or
602 character constant.  Comments within a macro definition may contain
603 Newlines, which make no difference since the comments are entirely
604 replaced with Spaces regardless of their contents.
606 Aside from the above, there is no restriction on what can go in a macro
607 body.  Parentheses need not balance.  The body need not resemble valid C
608 code.  (But if it does not, you may get error messages from the C
609 compiler when you use the macro.)
611 The C preprocessor scans your program sequentially, so macro definitions
612 take effect at the place you write them.  Therefore, the following input to
613 the C preprocessor
615 @example
616 foo = X;
617 #define X 4
618 bar = X;
619 @end example
621 @noindent
622 produces as output
624 @example
625 foo = X;
627 bar = 4;
628 @end example
630 After the preprocessor expands a macro name, the macro's definition body is
631 appended to the front of the remaining input, and the check for macro calls
632 continues.  Therefore, the macro body can contain calls to other macros.
633 For example, after
635 @example
636 #define BUFSIZE 1020
637 #define TABLESIZE BUFSIZE
638 @end example
640 @noindent
641 the name @samp{TABLESIZE} when used in the program would go through two
642 stages of expansion, resulting ultimately in @samp{1020}.
644 This is not at all the same as defining @samp{TABLESIZE} to be @samp{1020}.
645 The @samp{#define} for @samp{TABLESIZE} uses exactly the body you
646 specify---in this case, @samp{BUFSIZE}---and does not check to see whether
647 it too is the name of a macro.  It's only when you @emph{use} @samp{TABLESIZE}
648 that the result of its expansion is checked for more macro names.
649 @xref{Cascaded Macros}.
651 @node Argument Macros, Predefined, Simple Macros, Macros
652 @subsection Macros with Arguments
653 @cindex macros with argument
654 @cindex arguments in macro definitions
655 @cindex function-like macro
657 A simple macro always stands for exactly the same text, each time it is
658 used.  Macros can be more flexible when they accept @dfn{arguments}.
659 Arguments are fragments of code that you supply each time the macro is
660 used.  These fragments are included in the expansion of the macro
661 according to the directions in the macro definition.  A macro that
662 accepts arguments is called a @dfn{function-like macro} because the
663 syntax for using it looks like a function call.
665 @findex #define
666 To define a macro that uses arguments, you write a @samp{#define} directive
667 with a list of @dfn{argument names} in parentheses after the name of the
668 macro.  The argument names may be any valid C identifiers, separated by
669 commas and optionally whitespace.  The open-parenthesis must follow the
670 macro name immediately, with no space in between.
672 For example, here is a macro that computes the minimum of two numeric
673 values, as it is defined in many C programs:
675 @example
676 #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
677 @end example
679 @noindent
680 (This is not the best way to define a ``minimum'' macro in GNU C@.
681 @xref{Side Effects}, for more information.)
683 To use a macro that expects arguments, you write the name of the macro
684 followed by a list of @dfn{actual arguments} in parentheses, separated by
685 commas.  The number of actual arguments you give must match the number of
686 arguments the macro expects.   Examples of use of the macro @samp{min}
687 include @samp{min (1, 2)} and @samp{min (x + 28, *p)}.
689 The expansion text of the macro depends on the arguments you use.
690 Each of the argument names of the macro is replaced, throughout the
691 macro definition, with the corresponding actual argument.  Using the
692 same macro @samp{min} defined above, @samp{min (1, 2)} expands into
694 @example
695 ((1) < (2) ? (1) : (2))
696 @end example
698 @noindent
699 where @samp{1} has been substituted for @samp{X} and @samp{2} for @samp{Y}.
701 Likewise, @samp{min (x + 28, *p)} expands into
703 @example
704 ((x + 28) < (*p) ? (x + 28) : (*p))
705 @end example
707 Parentheses in the actual arguments must balance; a comma within
708 parentheses does not end an argument.  However, there is no requirement
709 for brackets or braces to balance, and they do not prevent a comma from
710 separating arguments.  Thus,
712 @example
713 macro (array[x = y, x + 1])
714 @end example
716 @noindent
717 passes two arguments to @code{macro}: @samp{array[x = y} and @samp{x +
718 1]}.  If you want to supply @samp{array[x = y, x + 1]} as an argument,
719 you must write it as @samp{array[(x = y, x + 1)]}, which is equivalent C
720 code.
722 After the actual arguments are substituted into the macro body, the entire
723 result is appended to the front of the remaining input, and the check for
724 macro calls continues.  Therefore, the actual arguments can contain calls
725 to other macros, either with or without arguments, or even to the same
726 macro.  The macro body can also contain calls to other macros.  For
727 example, @samp{min (min (a, b), c)} expands into this text:
729 @example
730 ((((a) < (b) ? (a) : (b))) < (c)
731  ? (((a) < (b) ? (a) : (b)))
732  : (c))
733 @end example
735 @noindent
736 (Line breaks shown here for clarity would not actually be generated.)
738 @cindex blank macro arguments
739 @cindex space as macro argument
740 If a macro @code{foo} takes one argument, and you want to supply an
741 empty argument, you must write at least some whitespace between the
742 parentheses, like this: @samp{foo ( )}.  Just @samp{foo ()} is providing
743 no arguments, which is an error if @code{foo} expects an argument.  But
744 @samp{foo0 ()} is the correct way to call a macro defined to take zero
745 arguments, like this:
747 @example
748 #define foo0() @dots{}
749 @end example
751 If you use the macro name followed by something other than an
752 open-parenthesis (after ignoring any spaces, tabs and comments that
753 follow), it is not a call to the macro, and the preprocessor does not
754 change what you have written.  Therefore, it is possible for the same name
755 to be a variable or function in your program as well as a macro, and you
756 can choose in each instance whether to refer to the macro (if an actual
757 argument list follows) or the variable or function (if an argument list
758 does not follow).
760 Such dual use of one name could be confusing and should be avoided
761 except when the two meanings are effectively synonymous: that is, when the
762 name is both a macro and a function and the two have similar effects.  You
763 can think of the name simply as a function; use of the name for purposes
764 other than calling it (such as, to take the address) will refer to the
765 function, while calls will expand the macro and generate better but
766 equivalent code.  For example, you can use a function named @samp{min} in
767 the same source file that defines the macro.  If you write @samp{&min} with
768 no argument list, you refer to the function.  If you write @samp{min (x,
769 bb)}, with an argument list, the macro is expanded.  If you write
770 @samp{(min) (a, bb)}, where the name @samp{min} is not followed by an
771 open-parenthesis, the macro is not expanded, so you wind up with a call to
772 the function @samp{min}.
774 You may not define the same name as both a simple macro and a macro with
775 arguments.
777 In the definition of a macro with arguments, the list of argument names
778 must follow the macro name immediately with no space in between.  If there
779 is a space after the macro name, the macro is defined as taking no
780 arguments, and all the rest of the line is taken to be the expansion.  The
781 reason for this is that it is often useful to define a macro that takes no
782 arguments and whose definition begins with an identifier in parentheses.
783 This rule about spaces makes it possible for you to do either this:
785 @example
786 #define FOO(x) - 1 / (x)
787 @end example
789 @noindent
790 (which defines @samp{FOO} to take an argument and expand into minus the
791 reciprocal of that argument) or this:
793 @example
794 #define BAR (x) - 1 / (x)
795 @end example
797 @noindent
798 (which defines @samp{BAR} to take no argument and always expand into
799 @samp{(x) - 1 / (x)}).
801 Note that the @emph{uses} of a macro with arguments can have spaces before
802 the left parenthesis; it's the @emph{definition} where it matters whether
803 there is a space.
805 @node Predefined, Stringification, Argument Macros, Macros
806 @subsection Predefined Macros
808 @cindex predefined macros
809 Several simple macros are predefined.  You can use them without giving
810 definitions for them.  They fall into two classes: standard macros and
811 system-specific macros.
813 @menu
814 * Standard Predefined::     Standard predefined macros.
815 * Nonstandard Predefined::  Nonstandard predefined macros.
816 @end menu
818 @node Standard Predefined, Nonstandard Predefined, Predefined, Predefined
819 @subsubsection Standard Predefined Macros
820 @cindex standard predefined macros
822 The standard predefined macros are available with the same meanings
823 regardless of the machine or operating system on which you are using GNU C@.
824 Their names all start and end with double underscores.  Those preceding
825 @code{__GNUC__} in this table are standardized by ANSI C; the rest are
826 GNU C extensions.
828 @table @code
829 @item __FILE__
830 @findex __FILE__
831 This macro expands to the name of the current input file, in the form of
832 a C string constant.  The precise name returned is the one that was
833 specified in @samp{#include} or as the input file name argument.
835 @item __LINE__
836 @findex __LINE__
837 This macro expands to the current input line number, in the form of a
838 decimal integer constant.  While we call it a predefined macro, it's
839 a pretty strange macro, since its ``definition'' changes with each
840 new line of source code.
842 This and @samp{__FILE__} are useful in generating an error message to
843 report an inconsistency detected by the program; the message can state
844 the source line at which the inconsistency was detected.  For example,
846 @smallexample
847 fprintf (stderr, "Internal error: "
848                  "negative string length "
849                  "%d at %s, line %d.",
850          length, __FILE__, __LINE__);
851 @end smallexample
853 A @samp{#include} directive changes the expansions of @samp{__FILE__}
854 and @samp{__LINE__} to correspond to the included file.  At the end of
855 that file, when processing resumes on the input file that contained
856 the @samp{#include} directive, the expansions of @samp{__FILE__} and
857 @samp{__LINE__} revert to the values they had before the
858 @samp{#include} (but @samp{__LINE__} is then incremented by one as
859 processing moves to the line after the @samp{#include}).
861 The expansions of both @samp{__FILE__} and @samp{__LINE__} are altered
862 if a @samp{#line} directive is used.  @xref{Combining Sources}.
864 @item __DATE__
865 @findex __DATE__
866 This macro expands to a string constant that describes the date on
867 which the preprocessor is being run.  The string constant contains
868 eleven characters and looks like @w{@samp{"Feb  1 1996"}}.
869 @c After reformatting the above, check that the date remains `Feb  1 1996',
870 @c all on one line, with two spaces between the `Feb' and the `1'.
872 @item __TIME__
873 @findex __TIME__
874 This macro expands to a string constant that describes the time at
875 which the preprocessor is being run.  The string constant contains
876 eight characters and looks like @samp{"23:59:01"}.
878 @item __STDC__
879 @findex __STDC__
880 This macro expands to the constant 1, to signify that this is ANSI
881 Standard C@.  (Whether that is actually true depends on what C compiler
882 will operate on the output from the preprocessor.)
884 On some hosts, system include files use a different convention, where
885 @samp{__STDC__} is normally 0, but is 1 if the user specifies strict
886 conformance to the C Standard.  The preprocessor follows the host convention
887 when processing system include files, but when processing user files it follows
888 the usual GNU C convention.
890 This macro is not defined if the @samp{-traditional} option is used.
892 @item __STDC_VERSION__
893 @findex __STDC_VERSION__
894 This macro expands to the C Standard's version number,
895 a long integer constant of the form @samp{@var{yyyy}@var{mm}L}
896 where @var{yyyy} and @var{mm} are the year and month of the Standard version.
897 This signifies which version of the C Standard the preprocessor conforms to.
898 Like @samp{__STDC__}, whether this version number is accurate
899 for the entire implementation depends on what C compiler
900 will operate on the output from the preprocessor.
902 This macro is not defined if the @samp{-traditional} option is used.
904 @item __GNUC__
905 @findex __GNUC__
906 This macro is defined if and only if this is GNU C@.  This macro is
907 defined only when the entire GNU C compiler is in use; if you invoke the
908 preprocessor directly, @samp{__GNUC__} is undefined.  The value
909 identifies the major version number of GNU CC (@samp{1} for GNU CC
910 version 1, which is now obsolete, and @samp{2} for version 2).
912 @item __GNUC_MINOR__
913 @findex __GNUC_MINOR__
914 The macro contains the minor version number of the compiler.  This can
915 be used to work around differences between different releases of the
916 compiler (for example, if gcc 2.6.3 is known to support a feature, you
917 can test for @code{__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 6)}).
918 The last number, @samp{3} in the
919 example above, denotes the bugfix level of the compiler; no macro
920 contains this value.
922 @item __GNUG__
923 @findex __GNUG__
924 The GNU C compiler defines this when the compilation language is
925 C++; use @samp{__GNUG__} to distinguish between GNU C and GNU
926 C++.
928 @item __cplusplus 
929 @findex __cplusplus 
930 The draft ANSI standard for C++ used to require predefining this
931 variable.  Though it is no longer required, GNU C++ continues to define
932 it, as do other popular C++ compilers.  You can use @samp{__cplusplus}
933 to test whether a header is compiled by a C compiler or a C++ compiler.
935 @item __STRICT_ANSI__
936 @findex __STRICT_ANSI__
937 This macro is defined if and only if the @samp{-ansi} switch was
938 specified when GNU C was invoked.  Its definition is the null string.
939 This macro exists primarily to direct certain GNU header files not to
940 define certain traditional Unix constructs which are incompatible with
941 ANSI C@.
943 @item __BASE_FILE__
944 @findex __BASE_FILE__
945 This macro expands to the name of the main input file, in the form
946 of a C string constant.  This is the source file that was specified
947 as an argument when the C compiler was invoked.
949 @item __INCLUDE_LEVEL__
950 @findex __INCLUDE_LEVEL_
951 This macro expands to a decimal integer constant that represents the
952 depth of nesting in include files.  The value of this macro is
953 incremented on every @samp{#include} directive and decremented at every
954 end of file.  For input files specified by command line arguments,
955 the nesting level is zero.
957 @item __VERSION__
958 @findex __VERSION__
959 This macro expands to a string which describes the version number of
960 GNU C@.  The string is normally a sequence of decimal numbers separated
961 by periods, such as @samp{"2.6.0"}.  The only reasonable use of this
962 macro is to incorporate it into a string constant.
964 @item __OPTIMIZE__
965 @findex __OPTIMIZE__
966 This macro is defined in optimizing compilations.  It causes certain
967 GNU header files to define alternative macro definitions for some
968 system library functions.  It is unwise to refer to or test the
969 definition of this macro unless you make very sure that programs will
970 execute with the same effect regardless.
972 @item __CHAR_UNSIGNED__
973 @findex __CHAR_UNSIGNED__
974 This macro is defined if and only if the data type @code{char} is
975 unsigned on the target machine.  It exists to cause the standard
976 header file @file{limits.h} to work correctly.  It is bad practice
977 to refer to this macro yourself; instead, refer to the standard
978 macros defined in @file{limits.h}.  The preprocessor uses
979 this macro to determine whether or not to sign-extend large character
980 constants written in octal; see @ref{#if Directive,,The @samp{#if} Directive}.
982 @item __REGISTER_PREFIX__
983 @findex __REGISTER_PREFIX__
984 This macro expands to a string describing the prefix applied to cpu
985 registers in assembler code.  It can be used to write assembler code
986 that is usable in multiple environments.  For example, in the
987 @samp{m68k-aout} environment it expands to the string @samp{""},
988 but in the @samp{m68k-coff} environment it expands to the string
989 @samp{"%"}.
991 @item __USER_LABEL_PREFIX__
992 @findex __USER_LABEL_PREFIX__
993 This macro expands to a string describing the prefix applied to user
994 generated labels in assembler code.  It can be used to write assembler
995 code that is usable in multiple environments.  For example, in the
996 @samp{m68k-aout} environment it expands to the string @samp{"_"}, but in
997 the @samp{m68k-coff} environment it expands to the string @samp{""}.
998 This does not work with the @samp{-mno-underscores} option that the
999 i386 OSF/rose and m88k targets provide nor with the @samp{-mcall*} options
1000 of the rs6000 System V Release 4 target.
1001 @end table
1003 @node Nonstandard Predefined,, Standard Predefined, Predefined
1004 @subsubsection Nonstandard Predefined Macros
1006 The C preprocessor normally has several predefined macros that vary between
1007 machines because their purpose is to indicate what type of system and
1008 machine is in use.  This manual, being for all systems and machines, cannot
1009 tell you exactly what their names are; instead, we offer a list of some
1010 typical ones.  You can use @samp{cpp -dM} to see the values of
1011 predefined macros; see @ref{Invocation}.
1013 Some nonstandard predefined macros describe the operating system in use,
1014 with more or less specificity.  For example,
1016 @table @code
1017 @item unix
1018 @findex unix
1019 @samp{unix} is normally predefined on all Unix systems.
1021 @item BSD
1022 @findex BSD
1023 @samp{BSD} is predefined on recent versions of Berkeley Unix
1024 (perhaps only in version 4.3).
1025 @end table
1027 Other nonstandard predefined macros describe the kind of CPU, with more or
1028 less specificity.  For example,
1030 @table @code
1031 @item vax
1032 @findex vax
1033 @samp{vax} is predefined on Vax computers.
1035 @item mc68000
1036 @findex mc68000
1037 @samp{mc68000} is predefined on most computers whose CPU is a Motorola
1038 68000, 68010 or 68020.
1040 @item m68k
1041 @findex m68k
1042 @samp{m68k} is also predefined on most computers whose CPU is a 68000,
1043 68010 or 68020; however, some makers use @samp{mc68000} and some use
1044 @samp{m68k}.  Some predefine both names.  What happens in GNU C
1045 depends on the system you are using it on.
1047 @item M68020
1048 @findex M68020
1049 @samp{M68020} has been observed to be predefined on some systems that
1050 use 68020 CPUs---in addition to @samp{mc68000} and @samp{m68k}, which
1051 are less specific.
1053 @item _AM29K
1054 @findex _AM29K
1055 @itemx _AM29000
1056 @findex _AM29000
1057 Both @samp{_AM29K} and @samp{_AM29000} are predefined for the AMD 29000
1058 CPU family.
1060 @item ns32000
1061 @findex ns32000
1062 @samp{ns32000} is predefined on computers which use the National
1063 Semiconductor 32000 series CPU.
1064 @end table
1066 Yet other nonstandard predefined macros describe the manufacturer of
1067 the system.  For example,
1069 @table @code
1070 @item sun
1071 @findex sun
1072 @samp{sun} is predefined on all models of Sun computers.
1074 @item pyr
1075 @findex pyr
1076 @samp{pyr} is predefined on all models of Pyramid computers.
1078 @item sequent
1079 @findex sequent
1080 @samp{sequent} is predefined on all models of Sequent computers.
1081 @end table
1083 These predefined symbols are not only nonstandard, they are contrary to the
1084 ANSI standard because their names do not start with underscores.
1085 Therefore, the option @samp{-ansi} inhibits the definition of these
1086 symbols.
1088 This tends to make @samp{-ansi} useless, since many programs depend on the
1089 customary nonstandard predefined symbols.  Even system header files check
1090 them and will generate incorrect declarations if they do not find the names
1091 that are expected.  You might think that the header files supplied for the
1092 Uglix computer would not need to test what machine they are running on,
1093 because they can simply assume it is the Uglix; but often they do, and they
1094 do so using the customary names.  As a result, very few C programs will
1095 compile with @samp{-ansi}.  We intend to avoid such problems on the GNU
1096 system.
1098 What, then, should you do in an ANSI C program to test the type of machine
1099 it will run on?
1101 GNU C offers a parallel series of symbols for this purpose, whose names
1102 are made from the customary ones by adding @samp{__} at the beginning
1103 and end.  Thus, the symbol @code{__vax__} would be available on a Vax,
1104 and so on.
1106 The set of nonstandard predefined names in the GNU C preprocessor is
1107 controlled (when @code{cpp} is itself compiled) by the macro
1108 @samp{CPP_PREDEFINES}, which should be a string containing @samp{-D}
1109 options, separated by spaces.  For example, on the Sun 3, we use the
1110 following definition:
1112 @example
1113 #define CPP_PREDEFINES "-Dmc68000 -Dsun -Dunix -Dm68k"
1114 @end example
1116 @noindent 
1117 This macro is usually specified in @file{tm.h}.
1119 @node Stringification, Concatenation, Predefined, Macros
1120 @subsection Stringification
1122 @cindex stringification
1123 @dfn{Stringification} means turning a code fragment into a string constant
1124 whose contents are the text for the code fragment.  For example,
1125 stringifying @samp{foo (z)} results in @samp{"foo (z)"}.
1127 In the C preprocessor, stringification is an option available when macro
1128 arguments are substituted into the macro definition.  In the body of the
1129 definition, when an argument name appears, the character @samp{#} before
1130 the name specifies stringification of the corresponding actual argument
1131 when it is substituted at that point in the definition.  The same argument
1132 may be substituted in other places in the definition without
1133 stringification if the argument name appears in those places with no
1134 @samp{#}.
1136 Here is an example of a macro definition that uses stringification:
1138 @smallexample
1139 @group
1140 #define WARN_IF(EXP) \
1141 do @{ if (EXP) \
1142         fprintf (stderr, "Warning: " #EXP "\n"); @} \
1143 while (0)
1144 @end group
1145 @end smallexample
1147 @noindent
1148 Here the actual argument for @samp{EXP} is substituted once as given,
1149 into the @samp{if} statement, and once as stringified, into the
1150 argument to @samp{fprintf}.  The @samp{do} and @samp{while (0)} are
1151 a kludge to make it possible to write @samp{WARN_IF (@var{arg});},
1152 which the resemblance of @samp{WARN_IF} to a function would make
1153 C programmers want to do; see @ref{Swallow Semicolon}.
1155 The stringification feature is limited to transforming one macro argument
1156 into one string constant: there is no way to combine the argument with
1157 other text and then stringify it all together.  But the example above shows
1158 how an equivalent result can be obtained in ANSI Standard C using the
1159 feature that adjacent string constants are concatenated as one string
1160 constant.  The preprocessor stringifies the actual value of @samp{EXP} 
1161 into a separate string constant, resulting in text like
1163 @smallexample
1164 @group
1165 do @{ if (x == 0) \
1166         fprintf (stderr, "Warning: " "x == 0" "\n"); @} \
1167 while (0)
1168 @end group
1169 @end smallexample
1171 @noindent
1172 but the C compiler then sees three consecutive string constants and
1173 concatenates them into one, producing effectively
1175 @smallexample
1176 do @{ if (x == 0) \
1177         fprintf (stderr, "Warning: x == 0\n"); @} \
1178 while (0)
1179 @end smallexample
1181 Stringification in C involves more than putting doublequote characters
1182 around the fragment; it is necessary to put backslashes in front of all
1183 doublequote characters, and all backslashes in string and character
1184 constants, in order to get a valid C string constant with the proper
1185 contents.  Thus, stringifying @samp{p = "foo\n";} results in @samp{"p =
1186 \"foo\\n\";"}.  However, backslashes that are not inside of string or
1187 character constants are not duplicated: @samp{\n} by itself stringifies to
1188 @samp{"\n"}.
1190 Whitespace (including comments) in the text being stringified is handled
1191 according to precise rules.  All leading and trailing whitespace is ignored.
1192 Any sequence of whitespace in the middle of the text is converted to
1193 a single space in the stringified result.
1195 @node Concatenation, Undefining, Stringification, Macros
1196 @subsection Concatenation
1197 @cindex concatenation
1198 @cindex @samp{##}
1199 @dfn{Concatenation} means joining two strings into one.  In the context
1200 of macro expansion, concatenation refers to joining two lexical units
1201 into one longer one.  Specifically, an actual argument to the macro can be
1202 concatenated with another actual argument or with fixed text to produce
1203 a longer name.  The longer name might be the name of a function,
1204 variable or type, or a C keyword; it might even be the name of another
1205 macro, in which case it will be expanded.
1207 When you define a macro, you request concatenation with the special
1208 operator @samp{##} in the macro body.  When the macro is called,
1209 after actual arguments are substituted, all @samp{##} operators are
1210 deleted, and so is any whitespace next to them (including whitespace
1211 that was part of an actual argument).  The result is to concatenate
1212 the syntactic tokens on either side of the @samp{##}.
1214 Consider a C program that interprets named commands.  There probably needs
1215 to be a table of commands, perhaps an array of structures declared as
1216 follows:
1218 @example
1219 struct command
1221   char *name;
1222   void (*function) ();
1225 struct command commands[] =
1227   @{ "quit", quit_command@},
1228   @{ "help", help_command@},
1229   @dots{}
1231 @end example
1233 It would be cleaner not to have to give each command name twice, once in
1234 the string constant and once in the function name.  A macro which takes the
1235 name of a command as an argument can make this unnecessary.  The string
1236 constant can be created with stringification, and the function name by
1237 concatenating the argument with @samp{_command}.  Here is how it is done:
1239 @example
1240 #define COMMAND(NAME)  @{ #NAME, NAME ## _command @}
1242 struct command commands[] =
1244   COMMAND (quit),
1245   COMMAND (help),
1246   @dots{}
1248 @end example
1250 The usual case of concatenation is concatenating two names (or a name and a
1251 number) into a longer name.  But this isn't the only valid case.  It is
1252 also possible to concatenate two numbers (or a number and a name, such as
1253 @samp{1.5} and @samp{e3}) into a number.  Also, multi-character operators
1254 such as @samp{+=} can be formed by concatenation.  In some cases it is even
1255 possible to piece together a string constant.  However, two pieces of text
1256 that don't together form a valid lexical unit cannot be concatenated.  For
1257 example, concatenation with @samp{x} on one side and @samp{+} on the other
1258 is not meaningful because those two characters can't fit together in any
1259 lexical unit of C@.  The ANSI standard says that such attempts at
1260 concatenation are undefined, but in the GNU C preprocessor it is well
1261 defined: it puts the @samp{x} and @samp{+} side by side with no particular
1262 special results.
1264 Keep in mind that the C preprocessor converts comments to whitespace before
1265 macros are even considered.  Therefore, you cannot create a comment by
1266 concatenating @samp{/} and @samp{*}: the @samp{/*} sequence that starts a
1267 comment is not a lexical unit, but rather the beginning of a ``long'' space
1268 character.  Also, you can freely use comments next to a @samp{##} in a
1269 macro definition, or in actual arguments that will be concatenated, because
1270 the comments will be converted to spaces at first sight, and concatenation
1271 will later discard the spaces.
1273 @node Undefining, Redefining, Concatenation, Macros
1274 @subsection Undefining Macros
1276 @cindex undefining macros
1277 To @dfn{undefine} a macro means to cancel its definition.  This is done
1278 with the @samp{#undef} directive.  @samp{#undef} is followed by the macro
1279 name to be undefined.
1281 Like definition, undefinition occurs at a specific point in the source
1282 file, and it applies starting from that point.  The name ceases to be a
1283 macro name, and from that point on it is treated by the preprocessor as if
1284 it had never been a macro name.
1286 For example,
1288 @example
1289 #define FOO 4
1290 x = FOO;
1291 #undef FOO
1292 x = FOO;
1293 @end example
1295 @noindent
1296 expands into
1298 @example
1299 x = 4;
1301 x = FOO;
1302 @end example
1304 @noindent
1305 In this example, @samp{FOO} had better be a variable or function as well
1306 as (temporarily) a macro, in order for the result of the expansion to be
1307 valid C code.
1309 The same form of @samp{#undef} directive will cancel definitions with
1310 arguments or definitions that don't expect arguments.  The @samp{#undef}
1311 directive has no effect when used on a name not currently defined as a macro.
1313 @node Redefining, Macro Pitfalls, Undefining, Macros
1314 @subsection Redefining Macros
1316 @cindex redefining macros
1317 @dfn{Redefining} a macro means defining (with @samp{#define}) a name that
1318 is already defined as a macro.
1320 A redefinition is trivial if the new definition is transparently identical
1321 to the old one.  You probably wouldn't deliberately write a trivial
1322 redefinition, but they can happen automatically when a header file is
1323 included more than once (@pxref{Header Files}), so they are accepted
1324 silently and without effect.
1326 Nontrivial redefinition is considered likely to be an error, so
1327 it provokes a warning message from the preprocessor.  However, sometimes it
1328 is useful to change the definition of a macro in mid-compilation.  You can
1329 inhibit the warning by undefining the macro with @samp{#undef} before the
1330 second definition.
1332 In order for a redefinition to be trivial, the new definition must
1333 exactly match the one already in effect, with two possible exceptions:
1335 @itemize @bullet
1336 @item
1337 Whitespace may be added or deleted at the beginning or the end.
1339 @item
1340 Whitespace may be changed in the middle (but not inside strings).
1341 However, it may not be eliminated entirely, and it may not be added
1342 where there was no whitespace at all.
1343 @end itemize
1345 Recall that a comment counts as whitespace.
1347 @node Macro Pitfalls,, Redefining, Macros
1348 @subsection Pitfalls and Subtleties of Macros
1349 @cindex problems with macros
1350 @cindex pitfalls of macros
1352 In this section we describe some special rules that apply to macros and
1353 macro expansion, and point out certain cases in which the rules have
1354 counterintuitive consequences that you must watch out for.
1356 @menu
1357 * Misnesting::        Macros can contain unmatched parentheses.
1358 * Macro Parentheses:: Why apparently superfluous parentheses
1359                          may be necessary to avoid incorrect grouping.
1360 * Swallow Semicolon:: Macros that look like functions
1361                          but expand into compound statements.
1362 * Side Effects::      Unsafe macros that cause trouble when
1363                          arguments contain side effects.
1364 * Self-Reference::    Macros whose definitions use the macros' own names.
1365 * Argument Prescan::  Actual arguments are checked for macro calls
1366                          before they are substituted.
1367 * Cascaded Macros::   Macros whose definitions use other macros.
1368 * Newlines in Args::  Sometimes line numbers get confused.
1369 @end menu
1371 @node Misnesting, Macro Parentheses, Macro Pitfalls, Macro Pitfalls
1372 @subsubsection Improperly Nested Constructs
1374 Recall that when a macro is called with arguments, the arguments are
1375 substituted into the macro body and the result is checked, together with
1376 the rest of the input file, for more macro calls.
1378 It is possible to piece together a macro call coming partially from the
1379 macro body and partially from the actual arguments.  For example,
1381 @example
1382 #define double(x) (2*(x))
1383 #define call_with_1(x) x(1)
1384 @end example
1386 @noindent
1387 would expand @samp{call_with_1 (double)} into @samp{(2*(1))}.
1389 Macro definitions do not have to have balanced parentheses.  By writing an
1390 unbalanced open parenthesis in a macro body, it is possible to create a
1391 macro call that begins inside the macro body but ends outside of it.  For
1392 example,
1394 @example
1395 #define strange(file) fprintf (file, "%s %d",
1396 @dots{}
1397 strange(stderr) p, 35)
1398 @end example
1400 @noindent
1401 This bizarre example expands to @samp{fprintf (stderr, "%s %d", p, 35)}!
1403 @node Macro Parentheses, Swallow Semicolon, Misnesting, Macro Pitfalls
1404 @subsubsection Unintended Grouping of Arithmetic
1405 @cindex parentheses in macro bodies
1407 You may have noticed that in most of the macro definition examples shown
1408 above, each occurrence of a macro argument name had parentheses around it.
1409 In addition, another pair of parentheses usually surround the entire macro
1410 definition.  Here is why it is best to write macros that way.
1412 Suppose you define a macro as follows,
1414 @example
1415 #define ceil_div(x, y) (x + y - 1) / y
1416 @end example
1418 @noindent
1419 whose purpose is to divide, rounding up.  (One use for this operation is
1420 to compute how many @samp{int} objects are needed to hold a certain
1421 number of @samp{char} objects.)  Then suppose it is used as follows:
1423 @example
1424 a = ceil_div (b & c, sizeof (int));
1425 @end example
1427 @noindent
1428 This expands into
1430 @example
1431 a = (b & c + sizeof (int) - 1) / sizeof (int);
1432 @end example
1434 @noindent
1435 which does not do what is intended.  The operator-precedence rules of
1436 C make it equivalent to this:
1438 @example
1439 a = (b & (c + sizeof (int) - 1)) / sizeof (int);
1440 @end example
1442 @noindent
1443 But what we want is this:
1445 @example
1446 a = ((b & c) + sizeof (int) - 1)) / sizeof (int);
1447 @end example
1449 @noindent
1450 Defining the macro as
1452 @example
1453 #define ceil_div(x, y) ((x) + (y) - 1) / (y)
1454 @end example
1456 @noindent
1457 provides the desired result.
1459 Unintended grouping can result in another way.  Consider
1460 @samp{sizeof ceil_div(1, 2)}.  That has the appearance of a C expression
1461 that would compute the size of the type of @samp{ceil_div (1, 2)}, but in
1462 fact it means something very different.  Here is what it expands to:
1464 @example
1465 sizeof ((1) + (2) - 1) / (2)
1466 @end example
1468 @noindent
1469 This would take the size of an integer and divide it by two.  The precedence
1470 rules have put the division outside the @samp{sizeof} when it was intended
1471 to be inside.
1473 Parentheses around the entire macro definition can prevent such problems.
1474 Here, then, is the recommended way to define @samp{ceil_div}:
1476 @example
1477 #define ceil_div(x, y) (((x) + (y) - 1) / (y))
1478 @end example
1480 @node Swallow Semicolon, Side Effects, Macro Parentheses, Macro Pitfalls
1481 @subsubsection Swallowing the Semicolon
1483 @cindex semicolons (after macro calls)
1484 Often it is desirable to define a macro that expands into a compound
1485 statement.  Consider, for example, the following macro, that advances a
1486 pointer (the argument @samp{p} says where to find it) across whitespace
1487 characters:
1489 @example
1490 #define SKIP_SPACES (p, limit)  \
1491 @{ register char *lim = (limit); \
1492   while (p != lim) @{            \
1493     if (*p++ != ' ') @{          \
1494       p--; break; @}@}@}
1495 @end example
1497 @noindent
1498 Here Backslash-Newline is used to split the macro definition, which must
1499 be a single line, so that it resembles the way such C code would be
1500 laid out if not part of a macro definition.
1502 A call to this macro might be @samp{SKIP_SPACES (p, lim)}.  Strictly
1503 speaking, the call expands to a compound statement, which is a complete
1504 statement with no need for a semicolon to end it.  But it looks like a
1505 function call.  So it minimizes confusion if you can use it like a function
1506 call, writing a semicolon afterward, as in @samp{SKIP_SPACES (p, lim);}
1508 But this can cause trouble before @samp{else} statements, because the
1509 semicolon is actually a null statement.  Suppose you write
1511 @example
1512 if (*p != 0)
1513   SKIP_SPACES (p, lim);
1514 else @dots{}
1515 @end example
1517 @noindent
1518 The presence of two statements---the compound statement and a null
1519 statement---in between the @samp{if} condition and the @samp{else}
1520 makes invalid C code.
1522 The definition of the macro @samp{SKIP_SPACES} can be altered to solve
1523 this problem, using a @samp{do @dots{} while} statement.  Here is how:
1525 @example
1526 #define SKIP_SPACES (p, limit)     \
1527 do @{ register char *lim = (limit); \
1528      while (p != lim) @{            \
1529        if (*p++ != ' ') @{          \
1530          p--; break; @}@}@}           \
1531 while (0)
1532 @end example
1534 Now @samp{SKIP_SPACES (p, lim);} expands into
1536 @example
1537 do @{@dots{}@} while (0);
1538 @end example
1540 @noindent
1541 which is one statement.
1543 @node Side Effects, Self-Reference, Swallow Semicolon, Macro Pitfalls
1544 @subsubsection Duplication of Side Effects
1546 @cindex side effects (in macro arguments)
1547 @cindex unsafe macros
1548 Many C programs define a macro @samp{min}, for ``minimum'', like this:
1550 @example
1551 #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
1552 @end example
1554 When you use this macro with an argument containing a side effect,
1555 as shown here,
1557 @example
1558 next = min (x + y, foo (z));
1559 @end example
1561 @noindent
1562 it expands as follows:
1564 @example
1565 next = ((x + y) < (foo (z)) ? (x + y) : (foo (z)));
1566 @end example
1568 @noindent
1569 where @samp{x + y} has been substituted for @samp{X} and @samp{foo (z)}
1570 for @samp{Y}.
1572 The function @samp{foo} is used only once in the statement as it appears
1573 in the program, but the expression @samp{foo (z)} has been substituted
1574 twice into the macro expansion.  As a result, @samp{foo} might be called
1575 two times when the statement is executed.  If it has side effects or
1576 if it takes a long time to compute, the results might not be what you
1577 intended.  We say that @samp{min} is an @dfn{unsafe} macro.
1579 The best solution to this problem is to define @samp{min} in a way that
1580 computes the value of @samp{foo (z)} only once.  The C language offers no
1581 standard way to do this, but it can be done with GNU C extensions as
1582 follows:
1584 @example
1585 #define min(X, Y)                     \
1586 (@{ typeof (X) __x = (X), __y = (Y);   \
1587    (__x < __y) ? __x : __y; @})
1588 @end example
1590 If you do not wish to use GNU C extensions, the only solution is to be
1591 careful when @emph{using} the macro @samp{min}.  For example, you can
1592 calculate the value of @samp{foo (z)}, save it in a variable, and use that
1593 variable in @samp{min}:
1595 @example
1596 #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
1597 @dots{}
1599   int tem = foo (z);
1600   next = min (x + y, tem);
1602 @end example
1604 @noindent
1605 (where we assume that @samp{foo} returns type @samp{int}).
1607 @node Self-Reference, Argument Prescan, Side Effects, Macro Pitfalls
1608 @subsubsection Self-Referential Macros
1610 @cindex self-reference
1611 A @dfn{self-referential} macro is one whose name appears in its definition.
1612 A special feature of ANSI Standard C is that the self-reference is not
1613 considered a macro call.  It is passed into the preprocessor output
1614 unchanged.
1616 Let's consider an example:
1618 @example
1619 #define foo (4 + foo)
1620 @end example
1622 @noindent
1623 where @samp{foo} is also a variable in your program.
1625 Following the ordinary rules, each reference to @samp{foo} will expand into
1626 @samp{(4 + foo)}; then this will be rescanned and will expand into @samp{(4
1627 + (4 + foo))}; and so on until it causes a fatal error (memory full) in the
1628 preprocessor.
1630 However, the special rule about self-reference cuts this process short
1631 after one step, at @samp{(4 + foo)}.  Therefore, this macro definition
1632 has the possibly useful effect of causing the program to add 4 to
1633 the value of @samp{foo} wherever @samp{foo} is referred to.
1635 In most cases, it is a bad idea to take advantage of this feature.  A
1636 person reading the program who sees that @samp{foo} is a variable will
1637 not expect that it is a macro as well.  The reader will come across the
1638 identifier @samp{foo} in the program and think its value should be that
1639 of the variable @samp{foo}, whereas in fact the value is four greater.
1641 The special rule for self-reference applies also to @dfn{indirect}
1642 self-reference.  This is the case where a macro @var{x} expands to use a
1643 macro @samp{y}, and the expansion of @samp{y} refers to the macro
1644 @samp{x}.  The resulting reference to @samp{x} comes indirectly from the
1645 expansion of @samp{x}, so it is a self-reference and is not further
1646 expanded.  Thus, after
1648 @example
1649 #define x (4 + y)
1650 #define y (2 * x)
1651 @end example
1653 @noindent
1654 @samp{x} would expand into @samp{(4 + (2 * x))}.  Clear?
1656 But suppose @samp{y} is used elsewhere, not from the definition of @samp{x}.
1657 Then the use of @samp{x} in the expansion of @samp{y} is not a self-reference
1658 because @samp{x} is not ``in progress''.  So it does expand.  However,
1659 the expansion of @samp{x} contains a reference to @samp{y}, and that
1660 is an indirect self-reference now because @samp{y} is ``in progress''.
1661 The result is that @samp{y} expands to @samp{(2 * (4 + y))}.
1663 It is not clear that this behavior would ever be useful, but it is specified
1664 by the ANSI C standard, so you may need to understand it.
1666 @node Argument Prescan, Cascaded Macros, Self-Reference, Macro Pitfalls
1667 @subsubsection Separate Expansion of Macro Arguments
1668 @cindex expansion of arguments
1669 @cindex macro argument expansion
1670 @cindex prescan of macro arguments
1672 We have explained that the expansion of a macro, including the substituted
1673 actual arguments, is scanned over again for macro calls to be expanded.
1675 What really happens is more subtle: first each actual argument text is scanned
1676 separately for macro calls.  Then the results of this are substituted into
1677 the macro body to produce the macro expansion, and the macro expansion
1678 is scanned again for macros to expand.
1680 The result is that the actual arguments are scanned @emph{twice} to expand
1681 macro calls in them.
1683 Most of the time, this has no effect.  If the actual argument contained
1684 any macro calls, they are expanded during the first scan.  The result
1685 therefore contains no macro calls, so the second scan does not change it.
1686 If the actual argument were substituted as given, with no prescan,
1687 the single remaining scan would find the same macro calls and produce
1688 the same results.
1690 You might expect the double scan to change the results when a
1691 self-referential macro is used in an actual argument of another macro
1692 (@pxref{Self-Reference}): the self-referential macro would be expanded once
1693 in the first scan, and a second time in the second scan.  But this is not
1694 what happens.  The self-references that do not expand in the first scan are
1695 marked so that they will not expand in the second scan either.
1697 The prescan is not done when an argument is stringified or concatenated.
1698 Thus,
1700 @example
1701 #define str(s) #s
1702 #define foo 4
1703 str (foo)
1704 @end example
1706 @noindent
1707 expands to @samp{"foo"}.  Once more, prescan has been prevented from
1708 having any noticeable effect.
1710 More precisely, stringification and concatenation use the argument as
1711 written, in un-prescanned form.  The same actual argument would be used in
1712 prescanned form if it is substituted elsewhere without stringification or
1713 concatenation.
1715 @example
1716 #define str(s) #s lose(s)
1717 #define foo 4
1718 str (foo)
1719 @end example
1721 expands to @samp{"foo" lose(4)}.
1723 You might now ask, ``Why mention the prescan, if it makes no difference?
1724 And why not skip it and make the preprocessor faster?''  The answer is
1725 that the prescan does make a difference in three special cases:
1727 @itemize @bullet
1728 @item
1729 Nested calls to a macro.
1731 @item
1732 Macros that call other macros that stringify or concatenate.
1734 @item
1735 Macros whose expansions contain unshielded commas.
1736 @end itemize
1738 We say that @dfn{nested} calls to a macro occur when a macro's actual
1739 argument contains a call to that very macro.  For example, if @samp{f}
1740 is a macro that expects one argument, @samp{f (f (1))} is a nested
1741 pair of calls to @samp{f}.  The desired expansion is made by
1742 expanding @samp{f (1)} and substituting that into the definition of
1743 @samp{f}.  The prescan causes the expected result to happen.
1744 Without the prescan, @samp{f (1)} itself would be substituted as
1745 an actual argument, and the inner use of @samp{f} would appear
1746 during the main scan as an indirect self-reference and would not
1747 be expanded.  Here, the prescan cancels an undesirable side effect
1748 (in the medical, not computational, sense of the term) of the special
1749 rule for self-referential macros.
1751 But prescan causes trouble in certain other cases of nested macro calls.
1752 Here is an example:
1754 @example
1755 #define foo  a,b
1756 #define bar(x) lose(x)
1757 #define lose(x) (1 + (x))
1759 bar(foo)
1760 @end example
1762 @noindent
1763 We would like @samp{bar(foo)} to turn into @samp{(1 + (foo))}, which
1764 would then turn into @samp{(1 + (a,b))}.  But instead, @samp{bar(foo)}
1765 expands into @samp{lose(a,b)}, and you get an error because @code{lose}
1766 requires a single argument.  In this case, the problem is easily solved
1767 by the same parentheses that ought to be used to prevent misnesting of
1768 arithmetic operations:
1770 @example
1771 #define foo (a,b)
1772 #define bar(x) lose((x))
1773 @end example
1775 The problem is more serious when the operands of the macro are not
1776 expressions; for example, when they are statements.  Then parentheses
1777 are unacceptable because they would make for invalid C code:
1779 @example
1780 #define foo @{ int a, b; @dots{} @}
1781 @end example
1783 @noindent
1784 In GNU C you can shield the commas using the @samp{(@{@dots{}@})}
1785 construct which turns a compound statement into an expression:
1787 @example
1788 #define foo (@{ int a, b; @dots{} @})
1789 @end example
1791 Or you can rewrite the macro definition to avoid such commas:
1793 @example
1794 #define foo @{ int a; int b; @dots{} @}
1795 @end example
1797 There is also one case where prescan is useful.  It is possible
1798 to use prescan to expand an argument and then stringify it---if you use
1799 two levels of macros.  Let's add a new macro @samp{xstr} to the
1800 example shown above:
1802 @example
1803 #define xstr(s) str(s)
1804 #define str(s) #s
1805 #define foo 4
1806 xstr (foo)
1807 @end example
1809 This expands into @samp{"4"}, not @samp{"foo"}.  The reason for the
1810 difference is that the argument of @samp{xstr} is expanded at prescan
1811 (because @samp{xstr} does not specify stringification or concatenation of
1812 the argument).  The result of prescan then forms the actual argument for
1813 @samp{str}.  @samp{str} uses its argument without prescan because it
1814 performs stringification; but it cannot prevent or undo the prescanning
1815 already done by @samp{xstr}.
1817 @node Cascaded Macros, Newlines in Args, Argument Prescan, Macro Pitfalls
1818 @subsubsection Cascaded Use of Macros
1820 @cindex cascaded macros
1821 @cindex macro body uses macro
1822 A @dfn{cascade} of macros is when one macro's body contains a reference
1823 to another macro.  This is very common practice.  For example,
1825 @example
1826 #define BUFSIZE 1020
1827 #define TABLESIZE BUFSIZE
1828 @end example
1830 This is not at all the same as defining @samp{TABLESIZE} to be @samp{1020}.
1831 The @samp{#define} for @samp{TABLESIZE} uses exactly the body you
1832 specify---in this case, @samp{BUFSIZE}---and does not check to see whether
1833 it too is the name of a macro.
1835 It's only when you @emph{use} @samp{TABLESIZE} that the result of its expansion
1836 is checked for more macro names.
1838 This makes a difference if you change the definition of @samp{BUFSIZE}
1839 at some point in the source file.  @samp{TABLESIZE}, defined as shown,
1840 will always expand using the definition of @samp{BUFSIZE} that is
1841 currently in effect:
1843 @example
1844 #define BUFSIZE 1020
1845 #define TABLESIZE BUFSIZE
1846 #undef BUFSIZE
1847 #define BUFSIZE 37
1848 @end example
1850 @noindent
1851 Now @samp{TABLESIZE} expands (in two stages) to @samp{37}.  (The
1852 @samp{#undef} is to prevent any warning about the nontrivial
1853 redefinition of @code{BUFSIZE}.)
1855 @node Newlines in Args,, Cascaded Macros, Macro Pitfalls
1856 @subsection Newlines in Macro Arguments
1857 @cindex newlines in macro arguments
1859 Traditional macro processing carries forward all newlines in macro
1860 arguments into the expansion of the macro.  This means that, if some of
1861 the arguments are substituted more than once, or not at all, or out of
1862 order, newlines can be duplicated, lost, or moved around within the
1863 expansion.  If the expansion consists of multiple statements, then the
1864 effect is to distort the line numbers of some of these statements.  The
1865 result can be incorrect line numbers, in error messages or displayed in
1866 a debugger.
1868 The GNU C preprocessor operating in ANSI C mode adjusts appropriately
1869 for multiple use of an argument---the first use expands all the
1870 newlines, and subsequent uses of the same argument produce no newlines.
1871 But even in this mode, it can produce incorrect line numbering if
1872 arguments are used out of order, or not used at all.
1874 Here is an example illustrating this problem:
1876 @example
1877 #define ignore_second_arg(a,b,c) a; c
1879 ignore_second_arg (foo (),
1880                    ignored (),
1881                    syntax error);
1882 @end example
1884 @noindent
1885 The syntax error triggered by the tokens @samp{syntax error} results
1886 in an error message citing line four, even though the statement text
1887 comes from line five.
1889 @node Conditionals, Combining Sources, Macros, Top
1890 @section Conditionals
1892 @cindex conditionals
1893 In a macro processor, a @dfn{conditional} is a directive that allows a part
1894 of the program to be ignored during compilation, on some conditions.
1895 In the C preprocessor, a conditional can test either an arithmetic expression
1896 or whether a name is defined as a macro.
1898 A conditional in the C preprocessor resembles in some ways an @samp{if}
1899 statement in C, but it is important to understand the difference between
1900 them.  The condition in an @samp{if} statement is tested during the execution
1901 of your program.  Its purpose is to allow your program to behave differently
1902 from run to run, depending on the data it is operating on.  The condition
1903 in a preprocessing conditional directive is tested when your program is compiled.
1904 Its purpose is to allow different code to be included in the program depending
1905 on the situation at the time of compilation.
1907 @menu
1908 * Uses: Conditional Uses.       What conditionals are for.
1909 * Syntax: Conditional Syntax.   How conditionals are written.
1910 * Deletion: Deleted Code.       Making code into a comment.
1911 * Macros: Conditionals-Macros.  Why conditionals are used with macros.
1912 * Assertions::                  How and why to use assertions.
1913 * Errors: #error Directive.     Detecting inconsistent compilation parameters.
1914 @end menu
1916 @node Conditional Uses
1917 @subsection Why Conditionals are Used
1919 Generally there are three kinds of reason to use a conditional.
1921 @itemize @bullet
1922 @item
1923 A program may need to use different code depending on the machine or
1924 operating system it is to run on.  In some cases the code for one
1925 operating system may be erroneous on another operating system; for
1926 example, it might refer to library routines that do not exist on the
1927 other system.  When this happens, it is not enough to avoid executing
1928 the invalid code: merely having it in the program makes it impossible
1929 to link the program and run it.  With a preprocessing conditional, the
1930 offending code can be effectively excised from the program when it is
1931 not valid.
1933 @item
1934 You may want to be able to compile the same source file into two
1935 different programs.  Sometimes the difference between the programs is
1936 that one makes frequent time-consuming consistency checks on its
1937 intermediate data, or prints the values of those data for debugging,
1938 while the other does not.
1940 @item
1941 A conditional whose condition is always false is a good way to exclude
1942 code from the program but keep it as a sort of comment for future
1943 reference.
1944 @end itemize
1946 Most simple programs that are intended to run on only one machine will
1947 not need to use preprocessing conditionals.
1949 @node Conditional Syntax
1950 @subsection Syntax of Conditionals
1952 @findex #if
1953 A conditional in the C preprocessor begins with a @dfn{conditional
1954 directive}: @samp{#if}, @samp{#ifdef} or @samp{#ifndef}.
1955 @xref{Conditionals-Macros}, for information on @samp{#ifdef} and
1956 @samp{#ifndef}; only @samp{#if} is explained here.
1958 @menu
1959 * If: #if Directive.     Basic conditionals using @samp{#if} and @samp{#endif}.
1960 * Else: #else Directive. Including some text if the condition fails.
1961 * Elif: #elif Directive. Testing several alternative possibilities.
1962 @end menu
1964 @node #if Directive
1965 @subsubsection The @samp{#if} Directive
1967 The @samp{#if} directive in its simplest form consists of
1969 @example
1970 #if @var{expression}
1971 @var{controlled text}
1972 #endif /* @var{expression} */
1973 @end example
1975 The comment following the @samp{#endif} is not required, but it is a good
1976 practice because it helps people match the @samp{#endif} to the
1977 corresponding @samp{#if}.  Such comments should always be used, except in
1978 short conditionals that are not nested.  In fact, you can put anything at
1979 all after the @samp{#endif} and it will be ignored by the GNU C preprocessor,
1980 but only comments are acceptable in ANSI Standard C@.
1982 @var{expression} is a C expression of integer type, subject to stringent
1983 restrictions.  It may contain
1985 @itemize @bullet
1986 @item
1987 Integer constants, which are all regarded as @code{long} or
1988 @code{unsigned long}.
1990 @item
1991 Character constants, which are interpreted according to the character
1992 set and conventions of the machine and operating system on which the
1993 preprocessor is running.  The GNU C preprocessor uses the C data type
1994 @samp{char} for these character constants; therefore, whether some
1995 character codes are negative is determined by the C compiler used to
1996 compile the preprocessor.  If it treats @samp{char} as signed, then
1997 character codes large enough to set the sign bit will be considered
1998 negative; otherwise, no character code is considered negative.
2000 @item
2001 Arithmetic operators for addition, subtraction, multiplication,
2002 division, bitwise operations, shifts, comparisons, and logical
2003 operations (@samp{&&} and @samp{||}).
2005 @item
2006 Identifiers that are not macros, which are all treated as zero(!).
2008 @item
2009 Macro calls.  All macro calls in the expression are expanded before
2010 actual computation of the expression's value begins.
2011 @end itemize
2013 Note that @samp{sizeof} operators and @code{enum}-type values are not allowed.
2014 @code{enum}-type values, like all other identifiers that are not taken
2015 as macro calls and expanded, are treated as zero.
2017 The @var{controlled text} inside of a conditional can include
2018 preprocessing directives.  Then the directives inside the conditional are
2019 obeyed only if that branch of the conditional succeeds.  The text can
2020 also contain other conditional groups.  However, the @samp{#if} and
2021 @samp{#endif} directives must balance.
2023 @node #else Directive
2024 @subsubsection The @samp{#else} Directive
2026 @findex #else
2027 The @samp{#else} directive can be added to a conditional to provide
2028 alternative text to be used if the condition is false.  This is what
2029 it looks like:
2031 @example
2032 #if @var{expression}
2033 @var{text-if-true}
2034 #else /* Not @var{expression} */
2035 @var{text-if-false}
2036 #endif /* Not @var{expression} */
2037 @end example
2039 If @var{expression} is nonzero, and thus the @var{text-if-true} is 
2040 active, then @samp{#else} acts like a failing conditional and the
2041 @var{text-if-false} is ignored.  Contrariwise, if the @samp{#if}
2042 conditional fails, the @var{text-if-false} is considered included.
2044 @node #elif Directive
2045 @subsubsection The @samp{#elif} Directive
2047 @findex #elif
2048 One common case of nested conditionals is used to check for more than two
2049 possible alternatives.  For example, you might have
2051 @example
2052 #if X == 1
2053 @dots{}
2054 #else /* X != 1 */
2055 #if X == 2
2056 @dots{}
2057 #else /* X != 2 */
2058 @dots{}
2059 #endif /* X != 2 */
2060 #endif /* X != 1 */
2061 @end example
2063 Another conditional directive, @samp{#elif}, allows this to be abbreviated
2064 as follows:
2066 @example
2067 #if X == 1
2068 @dots{}
2069 #elif X == 2
2070 @dots{}
2071 #else /* X != 2 and X != 1*/
2072 @dots{}
2073 #endif /* X != 2 and X != 1*/
2074 @end example
2076 @samp{#elif} stands for ``else if''.  Like @samp{#else}, it goes in the
2077 middle of a @samp{#if}-@samp{#endif} pair and subdivides it; it does not
2078 require a matching @samp{#endif} of its own.  Like @samp{#if}, the
2079 @samp{#elif} directive includes an expression to be tested.
2081 The text following the @samp{#elif} is processed only if the original
2082 @samp{#if}-condition failed and the @samp{#elif} condition succeeds.
2083 More than one @samp{#elif} can go in the same @samp{#if}-@samp{#endif}
2084 group.  Then the text after each @samp{#elif} is processed only if the
2085 @samp{#elif} condition succeeds after the original @samp{#if} and any
2086 previous @samp{#elif} directives within it have failed.  @samp{#else} is
2087 equivalent to @samp{#elif 1}, and @samp{#else} is allowed after any
2088 number of @samp{#elif} directives, but @samp{#elif} may not follow
2089 @samp{#else}.
2091 @node Deleted Code
2092 @subsection Keeping Deleted Code for Future Reference
2093 @cindex commenting out code
2095 If you replace or delete a part of the program but want to keep the old
2096 code around as a comment for future reference, the easy way to do this
2097 is to put @samp{#if 0} before it and @samp{#endif} after it.  This is
2098 better than using comment delimiters @samp{/*} and @samp{*/} since those
2099 won't work if the code already contains comments (C comments do not
2100 nest).
2102 This works even if the code being turned off contains conditionals, but
2103 they must be entire conditionals (balanced @samp{#if} and @samp{#endif}).
2105 Conversely, do not use @samp{#if 0} for comments which are not C code.
2106 Use the comment delimiters @samp{/*} and @samp{*/} instead.  The
2107 interior of @samp{#if 0} must consist of complete tokens; in particular,
2108 singlequote characters must balance.  But comments often contain
2109 unbalanced singlequote characters (known in English as apostrophes).
2110 These confuse @samp{#if 0}.  They do not confuse @samp{/*}.
2112 @node Conditionals-Macros
2113 @subsection Conditionals and Macros
2115 Conditionals are useful in connection with macros or assertions, because
2116 those are the only ways that an expression's value can vary from one
2117 compilation to another.  A @samp{#if} directive whose expression uses no
2118 macros or assertions is equivalent to @samp{#if 1} or @samp{#if 0}; you
2119 might as well determine which one, by computing the value of the
2120 expression yourself, and then simplify the program.
2122 For example, here is a conditional that tests the expression
2123 @samp{BUFSIZE == 1020}, where @samp{BUFSIZE} must be a macro.
2125 @example
2126 #if BUFSIZE == 1020
2127   printf ("Large buffers!\n");
2128 #endif /* BUFSIZE is large */
2129 @end example
2131 (Programmers often wish they could test the size of a variable or data
2132 type in @samp{#if}, but this does not work.  The preprocessor does not
2133 understand @code{sizeof}, or typedef names, or even the type keywords
2134 such as @code{int}.)
2136 @findex defined
2137 The special operator @samp{defined} is used in @samp{#if} expressions to
2138 test whether a certain name is defined as a macro.  Either @samp{defined
2139 @var{name}} or @samp{defined (@var{name})} is an expression whose value
2140 is 1 if @var{name} is defined as macro at the current point in the
2141 program, and 0 otherwise.  For the @samp{defined} operator it makes no
2142 difference what the definition of the macro is; all that matters is
2143 whether there is a definition.  Thus, for example,@refill
2145 @example
2146 #if defined (vax) || defined (ns16000)
2147 @end example
2149 @noindent
2150 would succeed if either of the names @samp{vax} and @samp{ns16000} is
2151 defined as a macro.  You can test the same condition using assertions
2152 (@pxref{Assertions}), like this:
2154 @example
2155 #if #cpu (vax) || #cpu (ns16000)
2156 @end example
2158 If a macro is defined and later undefined with @samp{#undef},
2159 subsequent use of the @samp{defined} operator returns 0, because
2160 the name is no longer defined.  If the macro is defined again with
2161 another @samp{#define}, @samp{defined} will recommence returning 1.
2163 @findex #ifdef
2164 @findex #ifndef
2165 Conditionals that test whether just one name is defined are very common,
2166 so there are two special short conditional directives for this case.
2168 @table @code
2169 @item #ifdef @var{name}
2170 is equivalent to @samp{#if defined (@var{name})}.
2172 @item #ifndef @var{name}
2173 is equivalent to @samp{#if ! defined (@var{name})}.
2174 @end table
2176 Macro definitions can vary between compilations for several reasons.
2178 @itemize @bullet
2179 @item
2180 Some macros are predefined on each kind of machine.  For example, on a
2181 Vax, the name @samp{vax} is a predefined macro.  On other machines, it
2182 would not be defined.
2184 @item
2185 Many more macros are defined by system header files.  Different
2186 systems and machines define different macros, or give them different
2187 values.  It is useful to test these macros with conditionals to avoid
2188 using a system feature on a machine where it is not implemented.
2190 @item
2191 Macros are a common way of allowing users to customize a program for
2192 different machines or applications.  For example, the macro
2193 @samp{BUFSIZE} might be defined in a configuration file for your
2194 program that is included as a header file in each source file.  You
2195 would use @samp{BUFSIZE} in a preprocessing conditional in order to
2196 generate different code depending on the chosen configuration.
2198 @item
2199 Macros can be defined or undefined with @samp{-D} and @samp{-U}
2200 command options when you compile the program.  You can arrange to
2201 compile the same source file into two different programs by choosing
2202 a macro name to specify which program you want, writing conditionals
2203 to test whether or how this macro is defined, and then controlling
2204 the state of the macro with compiler command options.
2205 @xref{Invocation}.
2206 @end itemize
2208 @ifinfo
2209 Assertions are usually predefined, but can be defined with preprocessor
2210 directives or command-line options.
2211 @end ifinfo
2213 @node Assertions
2214 @subsection Assertions
2216 @cindex assertions
2217 @dfn{Assertions} are a more systematic alternative to macros in writing
2218 conditionals to test what sort of computer or system the compiled
2219 program will run on.  Assertions are usually predefined, but you can
2220 define them with preprocessing directives or command-line options.
2222 @cindex predicates
2223 The macros traditionally used to describe the type of target are not
2224 classified in any way according to which question they answer; they may
2225 indicate a hardware architecture, a particular hardware model, an
2226 operating system, a particular version of an operating system, or
2227 specific configuration options.  These are jumbled together in a single
2228 namespace.  In contrast, each assertion consists of a named question and
2229 an answer.  The question is usually called the @dfn{predicate}.
2230 An assertion looks like this:
2232 @example
2233 #@var{predicate} (@var{answer})
2234 @end example
2236 @noindent
2237 You must use a properly formed identifier for @var{predicate}.  The
2238 value of @var{answer} can be any sequence of words; all characters are
2239 significant except for leading and trailing whitespace, and differences
2240 in internal whitespace sequences are ignored.  Thus, @samp{x + y} is
2241 different from @samp{x+y} but equivalent to @samp{x + y}.  @samp{)} is
2242 not allowed in an answer.
2244 @cindex testing predicates
2245 Here is a conditional to test whether the answer @var{answer} is asserted
2246 for the predicate @var{predicate}:
2248 @example
2249 #if #@var{predicate} (@var{answer})
2250 @end example
2252 @noindent
2253 There may be more than one answer asserted for a given predicate.  If
2254 you omit the answer, you can test whether @emph{any} answer is asserted
2255 for @var{predicate}:
2257 @example
2258 #if #@var{predicate}
2259 @end example
2261 @findex #system
2262 @findex #machine
2263 @findex #cpu
2264 Most of the time, the assertions you test will be predefined assertions.
2265 GNU C provides three predefined predicates: @code{system}, @code{cpu},
2266 and @code{machine}.  @code{system} is for assertions about the type of
2267 software, @code{cpu} describes the type of computer architecture, and
2268 @code{machine} gives more information about the computer.  For example,
2269 on a GNU system, the following assertions would be true:
2271 @example
2272 #system (gnu)
2273 #system (mach)
2274 #system (mach 3)
2275 #system (mach 3.@var{subversion})
2276 #system (hurd)
2277 #system (hurd @var{version})
2278 @end example
2280 @noindent
2281 and perhaps others.  The alternatives with
2282 more or less version information let you ask more or less detailed
2283 questions about the type of system software.
2285 On a Unix system, you would find @code{#system (unix)} and perhaps one of:
2286 @code{#system (aix)}, @code{#system (bsd)}, @code{#system (hpux)},
2287 @code{#system (lynx)}, @code{#system (mach)}, @code{#system (posix)},
2288 @code{#system (svr3)}, @code{#system (svr4)}, or @code{#system (xpg4)}
2289 with possible version numbers following.
2291 Other values for @code{system} are @code{#system (mvs)}
2292 and @code{#system (vms)}.
2294 @strong{Portability note:} Many Unix C compilers provide only one answer
2295 for the @code{system} assertion: @code{#system (unix)}, if they support
2296 assertions at all.  This is less than useful.
2298 An assertion with a multi-word answer is completely different from several
2299 assertions with individual single-word answers.  For example, the presence
2300 of @code{system (mach 3.0)} does not mean that @code{system (3.0)} is true.
2301 It also does not directly imply @code{system (mach)}, but in GNU C, that
2302 last will normally be asserted as well.
2304 The current list of possible assertion values for @code{cpu} is:
2305 @code{#cpu (a29k)}, @code{#cpu (alpha)}, @code{#cpu (arm)}, @code{#cpu
2306 (clipper)}, @code{#cpu (convex)}, @code{#cpu (elxsi)}, @code{#cpu
2307 (tron)}, @code{#cpu (h8300)}, @code{#cpu (i370)}, @code{#cpu (i386)},
2308 @code{#cpu (i860)}, @code{#cpu (i960)}, @code{#cpu (m68k)}, @code{#cpu
2309 (m88k)}, @code{#cpu (mips)}, @code{#cpu (ns32k)}, @code{#cpu (hppa)},
2310 @code{#cpu (pyr)}, @code{#cpu (ibm032)}, @code{#cpu (rs6000)},
2311 @code{#cpu (sh)}, @code{#cpu (sparc)}, @code{#cpu (spur)}, @code{#cpu
2312 (tahoe)}, @code{#cpu (vax)}, @code{#cpu (we32000)}.
2314 @findex #assert
2315 You can create assertions within a C program using @samp{#assert}, like
2316 this:
2318 @example
2319 #assert @var{predicate} (@var{answer})
2320 @end example
2322 @noindent
2323 (Note the absence of a @samp{#} before @var{predicate}.)
2325 @cindex unassert
2326 @cindex assertions, undoing
2327 @cindex retracting assertions
2328 @findex #unassert
2329 Each time you do this, you assert a new true answer for @var{predicate}.
2330 Asserting one answer does not invalidate previously asserted answers;
2331 they all remain true.  The only way to remove an assertion is with
2332 @samp{#unassert}.  @samp{#unassert} has the same syntax as
2333 @samp{#assert}.  You can also remove all assertions about
2334 @var{predicate} like this:
2336 @example
2337 #unassert @var{predicate}
2338 @end example
2340 You can also add or cancel assertions using command options
2341 when you run @code{gcc} or @code{cpp}.  @xref{Invocation}.
2343 @node #error Directive
2344 @subsection The @samp{#error} and @samp{#warning} Directives
2346 @findex #error
2347 The directive @samp{#error} causes the preprocessor to report a fatal
2348 error.  The rest of the line that follows @samp{#error} is used as the
2349 error message.  The line must consist of complete tokens.
2351 You would use @samp{#error} inside of a conditional that detects a
2352 combination of parameters which you know the program does not properly
2353 support.  For example, if you know that the program will not run
2354 properly on a Vax, you might write
2356 @smallexample
2357 @group
2358 #ifdef __vax__
2359 #error "Won't work on Vaxen.  See comments at get_last_object."
2360 #endif
2361 @end group
2362 @end smallexample
2364 @noindent
2365 @xref{Nonstandard Predefined}, for why this works.
2367 If you have several configuration parameters that must be set up by
2368 the installation in a consistent way, you can use conditionals to detect
2369 an inconsistency and report it with @samp{#error}.  For example,
2371 @smallexample
2372 #if HASH_TABLE_SIZE % 2 == 0 || HASH_TABLE_SIZE % 3 == 0 \
2373     || HASH_TABLE_SIZE % 5 == 0
2374 #error HASH_TABLE_SIZE should not be divisible by a small prime
2375 #endif
2376 @end smallexample
2378 @findex #warning
2379 The directive @samp{#warning} is like the directive @samp{#error}, but causes
2380 the preprocessor to issue a warning and continue preprocessing.  The rest of
2381 the line that follows @samp{#warning} is used as the warning message.
2383 You might use @samp{#warning} in obsolete header files, with a message
2384 directing the user to the header file which should be used instead.
2386 @node Combining Sources, Other Directives, Conditionals, Top
2387 @section Combining Source Files
2389 @cindex line control
2390 One of the jobs of the C preprocessor is to inform the C compiler of where
2391 each line of C code came from: which source file and which line number.
2393 C code can come from multiple source files if you use @samp{#include};
2394 both @samp{#include} and the use of conditionals and macros can cause
2395 the line number of a line in the preprocessor output to be different
2396 from the line's number in the original source file.  You will appreciate
2397 the value of making both the C compiler (in error messages) and symbolic
2398 debuggers such as GDB use the line numbers in your source file.
2400 The C preprocessor builds on this feature by offering a directive by which
2401 you can control the feature explicitly.  This is useful when a file for
2402 input to the C preprocessor is the output from another program such as the
2403 @code{bison} parser generator, which operates on another file that is the
2404 true source file.  Parts of the output from @code{bison} are generated from
2405 scratch, other parts come from a standard parser file.  The rest are copied
2406 nearly verbatim from the source file, but their line numbers in the
2407 @code{bison} output are not the same as their original line numbers.
2408 Naturally you would like compiler error messages and symbolic debuggers to
2409 know the original source file and line number of each line in the
2410 @code{bison} input.
2412 @findex #line
2413 @code{bison} arranges this by writing @samp{#line} directives into the output
2414 file.  @samp{#line} is a directive that specifies the original line number
2415 and source file name for subsequent input in the current preprocessor input
2416 file.  @samp{#line} has three variants:
2418 @table @code
2419 @item #line @var{linenum}
2420 Here @var{linenum} is a decimal integer constant.  This specifies that
2421 the line number of the following line of input, in its original source file,
2422 was @var{linenum}.
2424 @item #line @var{linenum} @var{filename}
2425 Here @var{linenum} is a decimal integer constant and @var{filename}
2426 is a string constant.  This specifies that the following line of input
2427 came originally from source file @var{filename} and its line number there
2428 was @var{linenum}.  Keep in mind that @var{filename} is not just a
2429 file name; it is surrounded by doublequote characters so that it looks
2430 like a string constant.
2432 @item #line @var{anything else}
2433 @var{anything else} is checked for macro calls, which are expanded.
2434 The result should be a decimal integer constant followed optionally
2435 by a string constant, as described above.
2436 @end table
2438 @samp{#line} directives alter the results of the @samp{__FILE__} and
2439 @samp{__LINE__} predefined macros from that point on.  @xref{Standard
2440 Predefined}.
2442 The output of the preprocessor (which is the input for the rest of the
2443 compiler) contains directives that look much like @samp{#line} directives.
2444 They start with just @samp{#} instead of @samp{#line}, but this is
2445 followed by a line number and file name as in @samp{#line}.  @xref{Output}.
2447 @node Other Directives, Output, Combining Sources, Top
2448 @section Miscellaneous Preprocessing Directives
2450 @cindex null directive
2451 This section describes three additional preprocessing directives.  They are
2452 not very useful, but are mentioned for completeness.
2454 The @dfn{null directive} consists of a @samp{#} followed by a Newline, with
2455 only whitespace (including comments) in between.  A null directive is
2456 understood as a preprocessing directive but has no effect on the preprocessor
2457 output.  The primary significance of the existence of the null directive is
2458 that an input line consisting of just a @samp{#} will produce no output,
2459 rather than a line of output containing just a @samp{#}.  Supposedly
2460 some old C programs contain such lines.
2462 @findex #pragma
2463 The ANSI standard specifies that the effect of the @samp{#pragma}
2464 directive is implementation-defined.  In the GNU C preprocessor,
2465 @samp{#pragma} directives are not used, except for @samp{#pragma once}
2466 (@pxref{Once-Only}).  However, they are left in the preprocessor output,
2467 so they are available to the compilation pass.
2469 @findex #ident
2470 The @samp{#ident} directive is supported for compatibility with certain
2471 other systems.  It is followed by a line of text.  On some systems, the
2472 text is copied into a special place in the object file; on most systems,
2473 the text is ignored and this directive has no effect.  Typically
2474 @samp{#ident} is only used in header files supplied with those systems
2475 where it is meaningful.
2477 @node Output, Invocation, Other Directives, Top
2478 @section C Preprocessor Output
2480 @cindex output format
2481 The output from the C preprocessor looks much like the input, except
2482 that all preprocessing directive lines have been replaced with blank lines
2483 and all comments with spaces.  Whitespace within a line is not altered;
2484 however, unless @samp{-traditional} is used, spaces may be inserted into
2485 the expansions of macro calls to prevent tokens from being concatenated.
2487 Source file name and line number information is conveyed by lines of
2488 the form
2490 @example
2491 # @var{linenum} @var{filename} @var{flags}
2492 @end example
2494 @noindent
2495 which are inserted as needed into the middle of the input (but never
2496 within a string or character constant).  Such a line means that the
2497 following line originated in file @var{filename} at line @var{linenum}.
2499 After the file name comes zero or more flags, which are @samp{1},
2500 @samp{2}, @samp{3}, or @samp{4}.  If there are multiple flags, spaces separate
2501 them.  Here is what the flags mean:
2503 @table @samp
2504 @item 1
2505 This indicates the start of a new file.
2506 @item 2
2507 This indicates returning to a file (after having included another file).
2508 @item 3
2509 This indicates that the following text comes from a system header file,
2510 so certain warnings should be suppressed.
2511 @item 4
2512 This indicates that the following text should be treated as C@.
2513 @c maybe cross reference NO_IMPLICIT_EXTERN_C
2514 @end table
2516 @node Invocation, Concept Index, Output, Top
2517 @section Invoking the C Preprocessor
2518 @cindex invocation of the preprocessor
2520 Most often when you use the C preprocessor you will not have to invoke it
2521 explicitly: the C compiler will do so automatically.  However, the
2522 preprocessor is sometimes useful on its own.
2524 The C preprocessor expects two file names as arguments, @var{infile} and
2525 @var{outfile}.  The preprocessor reads @var{infile} together with any other
2526 files it specifies with @samp{#include}.  All the output generated by the
2527 combined input files is written in @var{outfile}.
2529 Either @var{infile} or @var{outfile} may be @samp{-}, which as @var{infile}
2530 means to read from standard input and as @var{outfile} means to write to
2531 standard output.  Also, if @var{outfile} or both file names are omitted,
2532 the standard output and standard input are used for the omitted file names.
2534 @cindex options
2535 Here is a table of command options accepted by the C preprocessor.
2536 These options can also be given when compiling a C program; they are
2537 passed along automatically to the preprocessor when it is invoked by the
2538 compiler.
2540 @table @samp
2541 @item -P
2542 @findex -P
2543 Inhibit generation of @samp{#}-lines with line-number information in
2544 the output from the preprocessor (@pxref{Output}).  This might be
2545 useful when running the preprocessor on something that is not C code
2546 and will be sent to a program which might be confused by the
2547 @samp{#}-lines.
2549 @item -C
2550 @findex -C
2551 Do not discard comments: pass them through to the output file.
2552 Comments appearing in arguments of a macro call will be copied to the
2553 output before the expansion of the macro call.
2555 @item -traditional
2556 @findex -traditional
2557 Try to imitate the behavior of old-fashioned C, as opposed to ANSI C@.
2559 @itemize @bullet
2560 @item
2561 Traditional macro expansion pays no attention to singlequote or
2562 doublequote characters; macro argument symbols are replaced by the
2563 argument values even when they appear within apparent string or
2564 character constants.
2566 @item
2567 Traditionally, it is permissible for a macro expansion to end in the
2568 middle of a string or character constant.  The constant continues into
2569 the text surrounding the macro call.
2571 @item
2572 However, traditionally the end of the line terminates a string or
2573 character constant, with no error.
2575 @item
2576 In traditional C, a comment is equivalent to no text at all.  (In ANSI
2577 C, a comment counts as whitespace.)
2579 @item
2580 Traditional C does not have the concept of a ``preprocessing number''.
2581 It considers @samp{1.0e+4} to be three tokens: @samp{1.0e}, @samp{+},
2582 and @samp{4}.
2584 @item
2585 A macro is not suppressed within its own definition, in traditional C@.
2586 Thus, any macro that is used recursively inevitably causes an error.
2588 @item
2589 The character @samp{#} has no special meaning within a macro definition
2590 in traditional C@.
2592 @item
2593 In traditional C, the text at the end of a macro expansion can run
2594 together with the text after the macro call, to produce a single token.
2595 (This is impossible in ANSI C@.)
2597 @item
2598 Traditionally, @samp{\} inside a macro argument suppresses the syntactic
2599 significance of the following character.
2600 @end itemize
2602 @item -trigraphs
2603 @findex -trigraphs
2604 Process ANSI standard trigraph sequences.  These are three-character
2605 sequences, all starting with @samp{??}, that are defined by ANSI C to
2606 stand for single characters.  For example, @samp{??/} stands for
2607 @samp{\}, so @samp{'??/n'} is a character constant for a newline.
2608 Strictly speaking, the GNU C preprocessor does not support all
2609 programs in ANSI Standard C unless @samp{-trigraphs} is used, but if
2610 you ever notice the difference it will be with relief.
2612 You don't want to know any more about trigraphs.
2614 @item -pedantic
2615 @findex -pedantic
2616 Issue warnings required by the ANSI C standard in certain cases such
2617 as when text other than a comment follows @samp{#else} or @samp{#endif}.
2619 @item -pedantic-errors
2620 @findex -pedantic-errors
2621 Like @samp{-pedantic}, except that errors are produced rather than
2622 warnings.
2624 @item -Wtrigraphs
2625 @findex -Wtrigraphs
2626 Warn if any trigraphs are encountered (assuming they are enabled).
2628 @item -Wcomment
2629 @findex -Wcomment
2630 @ignore
2631 @c "Not worth documenting" both singular and plural forms of this
2632 @c option, per RMS.  But also unclear which is better; hence may need to
2633 @c switch this at some future date.  pesch@cygnus.com, 2jan92.
2634 @itemx -Wcomments
2635 (Both forms have the same effect).
2636 @end ignore
2637 Warn whenever a comment-start sequence @samp{/*} appears in a @samp{/*}
2638 comment, or whenever a Backslash-Newline appears in a @samp{//} comment.
2640 @item -Wall
2641 @findex -Wall
2642 Requests both @samp{-Wtrigraphs} and @samp{-Wcomment} (but not
2643 @samp{-Wtraditional} or @samp{-Wundef}). 
2645 @item -Wtraditional
2646 @findex -Wtraditional
2647 Warn about certain constructs that behave differently in traditional and
2648 ANSI C@.
2650 @item -Wundef
2651 @findex -Wundef
2652 Warn if an undefined identifier is evaluated in an @samp{#if} directive.
2654 @item -I @var{directory}
2655 @findex -I
2656 Add the directory @var{directory} to the head of the list of
2657 directories to be searched for header files (@pxref{Include Syntax}).
2658 This can be used to override a system header file, substituting your
2659 own version, since these directories are searched before the system
2660 header file directories.  If you use more than one @samp{-I} option,
2661 the directories are scanned in left-to-right order; the standard
2662 system directories come after.
2664 @item -I-
2665 Any directories specified with @samp{-I} options before the @samp{-I-}
2666 option are searched only for the case of @samp{#include "@var{file}"};
2667 they are not searched for @samp{#include <@var{file}>}.
2669 If additional directories are specified with @samp{-I} options after
2670 the @samp{-I-}, these directories are searched for all @samp{#include}
2671 directives.
2673 In addition, the @samp{-I-} option inhibits the use of the current
2674 directory as the first search directory for @samp{#include "@var{file}"}.
2675 Therefore, the current directory is searched only if it is requested
2676 explicitly with @samp{-I.}.  Specifying both @samp{-I-} and @samp{-I.}
2677 allows you to control precisely which directories are searched before
2678 the current one and which are searched after.
2680 @item -nostdinc
2681 @findex -nostdinc
2682 Do not search the standard system directories for header files.
2683 Only the directories you have specified with @samp{-I} options
2684 (and the current directory, if appropriate) are searched.
2686 @item -nostdinc++
2687 @findex -nostdinc++
2688 Do not search for header files in the C++-specific standard directories,
2689 but do still search the other standard directories.
2690 (This option is used when building libg++.)
2692 @item -remap
2693 @findex -remap
2694 When searching for a header file in a directory, remap file names if a
2695 file named @file{header.gcc} exists in that directory.  This can be used
2696 to work around limitations of file systems with file name restrictions.
2697 The @file{header.gcc} file should contain a series of lines with two
2698 tokens on each line: the first token is the name to map, and the second
2699 token is the actual name to use.
2701 @item -D @var{name}
2702 @findex -D
2703 Predefine @var{name} as a macro, with definition @samp{1}.
2705 @item -D @var{name}=@var{definition}
2706 Predefine @var{name} as a macro, with definition @var{definition}.
2707 There are no restrictions on the contents of @var{definition}, but if
2708 you are invoking the preprocessor from a shell or shell-like program you
2709 may need to use the shell's quoting syntax to protect characters such as
2710 spaces that have a meaning in the shell syntax.  If you use more than
2711 one @samp{-D} for the same @var{name}, the rightmost definition takes
2712 effect.
2714 @item -U @var{name}
2715 @findex -U
2716 Do not predefine @var{name}.  If both @samp{-U} and @samp{-D} are
2717 specified for one name, the @samp{-U} beats the @samp{-D} and the name
2718 is not predefined.
2720 @item -undef
2721 @findex -undef
2722 Do not predefine any nonstandard macros.
2724 @item -A @var{predicate}(@var{answer})
2725 @findex -A
2726 Make an assertion with the predicate @var{predicate} and answer
2727 @var{answer}.  @xref{Assertions}.
2729 @noindent
2730 You can use @samp{-A-} to disable all predefined assertions; it also
2731 undefines all predefined macros that identify the type of target system.
2733 @item -dM
2734 @findex -dM
2735 Instead of outputting the result of preprocessing, output a list of
2736 @samp{#define} directives for all the macros defined during the
2737 execution of the preprocessor, including predefined macros.  This gives
2738 you a way of finding out what is predefined in your version of the
2739 preprocessor; assuming you have no file @samp{foo.h}, the command
2741 @example
2742 touch foo.h; cpp -dM foo.h
2743 @end example
2745 @noindent 
2746 will show the values of any predefined macros.
2748 @item -dD
2749 @findex -dD
2750 Like @samp{-dM} except in two respects: it does @emph{not} include the
2751 predefined macros, and it outputs @emph{both} the @samp{#define}
2752 directives and the result of preprocessing.  Both kinds of output go to
2753 the standard output file.
2755 @item -dI
2756 @findex -dI
2757 Output @samp{#include} directives in addition to the result of preprocessing.
2759 @item -M [-MG]
2760 @findex -M
2761 Instead of outputting the result of preprocessing, output a rule
2762 suitable for @code{make} describing the dependencies of the main
2763 source file.  The preprocessor outputs one @code{make} rule containing
2764 the object file name for that source file, a colon, and the names of
2765 all the included files.  If there are many included files then the
2766 rule is split into several lines using @samp{\}-newline.
2768 @samp{-MG} says to treat missing header files as generated files and assume
2769 they live in the same directory as the source file.  It must be specified
2770 in addition to @samp{-M}.
2772 This feature is used in automatic updating of makefiles.
2774 @item -MM [-MG]
2775 @findex -MM
2776 Like @samp{-M} but mention only the files included with @samp{#include
2777 "@var{file}"}.  System header files included with @samp{#include
2778 <@var{file}>} are omitted.
2780 @item -MD @var{file}
2781 @findex -MD
2782 Like @samp{-M} but the dependency information is written to @var{file}.
2783 This is in addition to compiling the file as specified---@samp{-MD} does
2784 not inhibit ordinary compilation the way @samp{-M} does.
2786 When invoking gcc, do not specify the @var{file} argument.
2787 Gcc will create file names made by replacing ".c" with ".d" at
2788 the end of the input file names.
2790 In Mach, you can use the utility @code{md} to merge multiple dependency
2791 files into a single dependency file suitable for using with the @samp{make}
2792 command.
2794 @item -MMD @var{file}
2795 @findex -MMD
2796 Like @samp{-MD} except mention only user header files, not system
2797 header files.
2799 @item -H
2800 @findex -H
2801 Print the name of each header file used, in addition to other normal
2802 activities.
2804 @item -imacros @var{file}
2805 @findex -imacros
2806 Process @var{file} as input, discarding the resulting output, before
2807 processing the regular input file.  Because the output generated from
2808 @var{file} is discarded, the only effect of @samp{-imacros @var{file}}
2809 is to make the macros defined in @var{file} available for use in the
2810 main input.
2812 @item -include @var{file}
2813 @findex -include
2814 Process @var{file} as input, and include all the resulting output,
2815 before processing the regular input file.  
2817 @item -idirafter @var{dir}
2818 @findex -idirafter
2819 @cindex second include path
2820 Add the directory @var{dir} to the second include path.  The directories
2821 on the second include path are searched when a header file is not found
2822 in any of the directories in the main include path (the one that
2823 @samp{-I} adds to).
2825 @item -iprefix @var{prefix}
2826 @findex -iprefix
2827 Specify @var{prefix} as the prefix for subsequent @samp{-iwithprefix}
2828 options.
2830 @item -iwithprefix @var{dir}
2831 @findex -iwithprefix
2832 Add a directory to the second include path.  The directory's name is
2833 made by concatenating @var{prefix} and @var{dir}, where @var{prefix}
2834 was specified previously with @samp{-iprefix}.
2836 @item -isystem @var{dir}
2837 @findex -isystem
2838 Add a directory to the beginning of the second include path, marking it
2839 as a system directory, so that it gets the same special treatment as
2840 is applied to the standard system directories.
2842 @item -lang-c
2843 @itemx -lang-c89
2844 @itemx -lang-c++
2845 @itemx -lang-objc
2846 @itemx -lang-objc++
2847 @findex -lang-c
2848 @findex -lang-c89
2849 @findex -lang-c++
2850 @findex -lang-objc
2851 @findex -lang-objc++
2852 Specify the source language.  @samp{-lang-c} is the default; it
2853 allows recognition of C++ comments (comments that begin with
2854 @samp{//} and end at end of line) and hexadecimal floating-point constants,
2855 since these features will most likely appear in the next C standard.
2856 @samp{-lang-c89} disables recognition of C++ comments and
2857 hexadecimal floating-point constants.  @samp{-lang-c++}
2858 handles C++ comment syntax and includes extra default include
2859 directories for C++.  @samp{-lang-objc} enables the Objective C
2860 @samp{#import} directive.  @samp{-lang-objc++} enables both C++ and Objective C
2861 extensions.
2863 These options are generated by the compiler driver @code{gcc}, but not
2864 passed from the @samp{gcc} command line unless you use the driver's
2865 @samp{-Wp} option.
2867 @item -lint
2868 Look for commands to the program checker @code{lint} embedded in
2869 comments, and emit them preceded by @samp{#pragma lint}.  For example,
2870 the comment @samp{/* NOTREACHED */} becomes @samp{#pragma lint
2871 NOTREACHED}.
2873 This option is available only when you call @code{cpp} directly;
2874 @code{gcc} will not pass it from its command line.
2876 @item -$
2877 @findex -$
2878 Forbid the use of @samp{$} in identifiers.  This was formerly required
2879 for strict conformance to the C Standard before the standard was
2880 corrected.
2882 This option is available only when you call @code{cpp} directly;
2883 @code{gcc} will not pass it from its command line.
2885 @end table
2887 @node Concept Index, Index, Invocation, Top
2888 @unnumbered Concept Index
2889 @printindex cp
2891 @node Index,, Concept Index, Top
2892 @unnumbered Index of Directives, Macros and Options
2893 @printindex fn
2895 @contents
2896 @bye