source: trunk/third/gcc/cpp.texi @ 11288

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