Annotation of researchv10dc/cmd/gcc/cpp.texinfo, revision 1.1.1.1

1.1       root        1: \input texinfo
                      2: @setfilename cpp.info
                      3: @settitle The C Preprocessor
                      4: @ifinfo
                      5: This file documents the GNU C Preprocessor.
                      6: 
                      7: Copyright (C) 1987 Richard M. Stallman.
                      8: 
                      9: Permission is granted to make and distribute verbatim copies of
                     10: this manual provided the copyright notice and this permission notice
                     11: are preserved on all copies.
                     12: 
                     13: @ignore
                     14: Permission is granted to process this file through Tex and print the
                     15: results, provided the printed document carries copying permission
                     16: notice identical to this one except for the removal of this paragraph
                     17: (this paragraph not being relevant to the printed manual).
                     18: 
                     19: @end ignore
                     20: Permission is granted to copy and distribute modified versions of this
                     21: manual under the conditions for verbatim copying, provided also that
                     22: the entire resulting derived work is distributed under the terms of a
                     23: permission notice identical to this one.
                     24: 
                     25: Permission is granted to copy and distribute translations of this manual
                     26: into another language, under the above conditions for modified versions.
                     27: @end ifinfo
                     28: 
                     29: @titlepage
                     30: @sp 6
                     31: @center @titlefont{The C Preprocessor}
                     32: @sp 4
                     33: @center First Edition
                     34: @sp 1
                     35: @center January 1987
                     36: @sp 5
                     37: @center Richard M. Stallman
                     38: @page
                     39: @vskip 0pt plus 1filll
                     40: Copyright @copyright{} 1987 Richard M. Stallman.
                     41: 
                     42: Permission is granted to make and distribute verbatim copies of
                     43: this manual provided the copyright notice and this permission notice
                     44: are preserved on all copies.
                     45: 
                     46: Permission is granted to copy and distribute modified versions of this
                     47: manual under the conditions for verbatim copying, provided also that
                     48: the entire resulting derived work is distributed under the terms of a
                     49: permission notice identical to this one.
                     50: 
                     51: Permission is granted to copy and distribute translations of this manual
                     52: into another language, under the above conditions for modified versions.
                     53: @end titlepage
                     54: @page
                     55: 
                     56: @node Top, Global Actions,, (DIR)
                     57: @chapter The C Preprocessor
                     58: 
                     59: The C preprocessor is a @dfn{macro processor} that is used automatically by
                     60: the C compiler to transform your program before actual compilation.  It is
                     61: called a macro processor because it allows you to define @dfn{macros},
                     62: which are brief abbreviations for longer constructs.
                     63: 
                     64: The C preprocessor provides four separate facilities that you can use as
                     65: you see fit:
                     66: 
                     67: @itemize @bullet
                     68: @item
                     69: Inclusion of header files.  These are files of declarations that can be
                     70: substituted into your program.
                     71: 
                     72: @item
                     73: Macro expansion.  You can define @dfn{macros}, which are abbreviations
                     74: for arbitrary fragments of C code, and then the C preprocessor will
                     75: replace the macros with their definitions throughout the program.
                     76: 
                     77: @item
                     78: Conditional compilation.  Using special preprocessor commands, you
                     79: can include or exclude parts of the program according to various
                     80: conditions.
                     81: 
                     82: @item
                     83: Line control.  If you use a program to combine or rearrange source files into
                     84: an intermediate file which is then compiled, you can use line control
                     85: to inform the compiler of where each source line originally came from.
                     86: @end itemize
                     87: 
                     88: C preprocessors vary in some details.  This manual discusses the GNU C
                     89: preprocessor, the C Compatible Compiler Preprocessor.  The GNU C
                     90: preprocessor provides a superset of the features of ANSI Standard C.
                     91: 
                     92: ANSI Standard C requires the rejection of many harmless constructs commonly
                     93: used by today's C programs.  Such incompatibility would be inconvenient for
                     94: users, so the GNU C preprocessor is configured to accept these constructs
                     95: by default.  Strictly speaking, to get ANSI Standard C, you must use the
                     96: options @samp{-T}, @samp{-undef} and @samp{-pedantic}, but in practice the
                     97: consequences of having strict ANSI Standard C make it undesirable to do
                     98: this.  @xref{Invocation}.
                     99: 
                    100: @menu
                    101: * Global Actions::    Actions made uniformly on all input files.
                    102: * Commands::          General syntax of preprocessor commands.
                    103: * Header Files::      How and why to use header files.
                    104: * Macros::            How and why to use macros.
                    105: * Conditionals::      How and why to use conditionals.
                    106: * Combining Sources:: Use of line control when you combine source files.
                    107: * Other Commands::    Miscellaneous preprocessor commands.
                    108: * Output::            Format of output from the C preprocessor.
                    109: * Invocation::        How to invoke the preprocessor; command options.
                    110: * Concept Index::     Index of concepts and terms.
                    111: * Index::             Index of commands, predefined macros and options.
                    112: @end menu
                    113: 
                    114: @node Global Actions, Commands, Top, Top
                    115: @section Transformations Made Globally
                    116: 
                    117: Most C preprocessor features are inactive unless you give specific commands
                    118: to request their use.  (Preprocessor commands are lines starting with
                    119: @samp{#}; @pxref{Commands}).  But there are three transformations that the
                    120: preprocessor always makes on all the input it receives, even in the absence
                    121: of commands.
                    122: 
                    123: @itemize @bullet
                    124: @item
                    125: All C comments are replaced with single spaces.
                    126: 
                    127: @item
                    128: Backslash-Newline sequences are deleted, no matter where.  This
                    129: feature allows you to break long lines for cosmetic purposes without
                    130: changing their meaning.
                    131: 
                    132: @item
                    133: Predefined macro names are replaced with their expansions
                    134: (@pxref{Predefined}).
                    135: @end itemize
                    136: 
                    137: The first two transformations are done @emph{before} nearly all other parsing
                    138: and before preprocessor commands are recognized.  Thus, for example, you
                    139: can split a line cosmetically with Backslash-Newline anywhere (except
                    140: when trigraphs are in use; see below).
                    141: 
                    142: @example
                    143: /*
                    144: */ # /*
                    145: */ defi\
                    146: ne FO\
                    147: O 10\
                    148: 20
                    149: @end example
                    150: 
                    151: @noindent
                    152: is equivalent into @samp{#define FOO 1020}.  You can split even an escape
                    153: sequence with Backslash-Newline.  For example, you can split @code{"foo\bar"}
                    154: between the @samp{\} and the @samp{b} to get
                    155: 
                    156: @example
                    157: "foo\\
                    158: bar"
                    159: @end example
                    160: 
                    161: @noindent
                    162: This behavior is unclean: in all other contexts, a Backslash can be
                    163: inserted in a string constant as an ordinary character by writing a double
                    164: Backslash, and this creates an exception.  But the ANSI C standard requires
                    165: it.  (Strict ANSI C does not allow Newlines in string constants, so they
                    166: do not consider this a problem.)
                    167: 
                    168: But there are a few exceptions to all three transformations.
                    169: 
                    170: @itemize @bullet
                    171: @item
                    172: C comments and predefined macro names are not recognized inside a
                    173: @samp{#include} command in which the file name is delimited with
                    174: @samp{<} and @samp{>}.
                    175: 
                    176: @item
                    177: C comments and predefined macro names are never recognized within a
                    178: character or string constant.  (Strictly speaking, this is the rule,
                    179: not an exception, but it is worth noting here anyway.)
                    180: 
                    181: @item
                    182: Backslash-Newline may not safely be used within an ANSI ``trigraph''.
                    183: Trigraphs are converted before Backslash-Newline is deleted.  If you
                    184: write what looks like a trigraph with a Backslash-Newline inside, the
                    185: Backslash-Newline is deleted as usual, but it is then too late to
                    186: recognize the trigraph.
                    187: 
                    188: This exception is relevant only if you use the @samp{-T} option to
                    189: enable trigraph processing.  @xref{Invocation}.
                    190: @end itemize
                    191: 
                    192: @node Commands, Header Files, Global Actions, Top
                    193: @section Preprocessor Commands
                    194: 
                    195: @cindex preprocessor commands
                    196: @cindex commands
                    197: Most preprocessor features are active only if you use preprocessor commands
                    198: to request their use.
                    199: 
                    200: Preprocessor commands are lines in your program that start with @samp{#}.
                    201: The @samp{#} is followed by an identifier that is the @dfn{command name}.
                    202: For example, @samp{#define} is the command that defines a macro.
                    203: Whitespace is also allowed before and after the @samp{#}.
                    204: 
                    205: The set of valid command names is fixed.  Programs cannot define new
                    206: preprocessor commands.
                    207: 
                    208: Some command names require arguments; these make up the rest of the command
                    209: line and must be separated from the command name by whitespace.  For example,
                    210: @samp{#define} must be followed by a macro name and the intended expansion
                    211: of the macro.
                    212: 
                    213: A preprocessor command cannot be more than one line in normal circumstances.
                    214: It may be split cosmetically with Backslash-Newline, but that has no effect
                    215: on its meaning.  Comments containing Newlines can also divide the command into
                    216: multiple lines, but the comments are changed to Spaces before the command
                    217: is interpreted.  The only way a significant Newline can occur in a preprocessor
                    218: command is within a string constant or character constant.  Note that
                    219: most C compilers that might be applied to the output from the preprocessor
                    220: do not accept string or character constants containing Newlines.
                    221: 
                    222: The @samp{#} and the command name cannot come from a macro expansion.  For
                    223: example, if @samp{foo} is defined as a macro expanding to @samp{define},
                    224: that does not make @samp{#foo} a valid preprocessor command.
                    225: 
                    226: @node Header Files, Macros, Commands, Top
                    227: @section Header Files
                    228: 
                    229: @cindex header file
                    230: A header file is a file containing C declarations and macro definitions
                    231: (@pxref{Macros}) to be shared between several source files.  You request
                    232: the use of a header file in your program with the C preprocessor command
                    233: @samp{#include}.
                    234: 
                    235: @node Header Uses, Include Syntax, Header Files, Header Files
                    236: @subsection Uses of Header Files
                    237: 
                    238: Header files serve two kinds of purposes.
                    239: 
                    240: @itemize @bullet
                    241: @item
                    242: @findex system header files
                    243: System header files declare the interfaces to parts of the operating
                    244: system.  You include them in your program to supply the definitions
                    245: you need to invoke system calls and libraries.
                    246: 
                    247: @item
                    248: Your own header files contain declarations for interfaces between the
                    249: source files of your program.  Each time you have a group of related
                    250: declarations and macro definitions all or most of which are needed in
                    251: several different source files, it is a good idea to create a header
                    252: file for them.
                    253: @end itemize
                    254: 
                    255: Including a header file produces the same results in C compilation as
                    256: copying the header file into each source file that needs it.  But such
                    257: copying would be time-consuming and error-prone.  With a header file, the
                    258: related declarations appear in only one place.  If they need to be changed,
                    259: they can be changed in one place, and programs that include the header file
                    260: will automatically use the new version when next recompiled.  The header
                    261: file eliminates the labor of finding and changing all the copies as well as
                    262: the risk that a failure to find one copy will result in inconsistencies
                    263: within a program.
                    264: 
                    265: The usual convention is to give header files names that end with @file{.h}.
                    266: 
                    267: @node Include Syntax, Include Operation, Header Uses, Header Files
                    268: @subsection The @samp{#include} Command
                    269: 
                    270: @findex #include
                    271: Both user and system header files are included using the preprocessor
                    272: command @samp{#include}.  It has three variants:
                    273: 
                    274: @table @code
                    275: @item #include <@var{file}>
                    276: This variant is used for system header files.  It searches for a file
                    277: named @var{file} in a list of directories specified by you, then in a
                    278: standard list of system directories.  You specify directories to
                    279: search for header files with the command option @samp{-I}
                    280: (@pxref{Invocation}).  The option @samp{-nostdinc} inhibits searching
                    281: the standard system directories; in this case only the directories
                    282: you specify are searched.
                    283: 
                    284: The parsing of this form of @samp{#include} is slightly special
                    285: because comments are not recognized within the @samp{<@dots{}>}.
                    286: Thus, in @samp{#include <x/*y>} the @samp{/*} does not start a comment
                    287: and the command specifies inclusion of a system header file named
                    288: @file{x/*y}.  Of course, a header file with such a name is unlikely to
                    289: exist on Unix, where shell wildcard features would make it hard to
                    290: manipulate.@refill
                    291: 
                    292: The argument @var{file} may not contain a @samp{>} character.  It may,
                    293: however, contain a @samp{<} character.
                    294: 
                    295: @item #include "@var{file}"
                    296: This variant is used for header files of your own program.  It
                    297: searches for a file named @var{file} first in the current
                    298: directory, then in the same directories used for system header
                    299: files.  The current directory is tried first because it is
                    300: presumed to be the location of the files of the program being
                    301: compiled.  (If the @samp{-I-} option is used, the special treatment
                    302: of the current directory is inhibited.)
                    303: 
                    304: The argument @var{file} may not contain @samp{"} characters.  If
                    305: backslashes occur within @var{file}, they are considered ordinary text
                    306: characters, not escape characters.  None of the character escape
                    307: sequences appropriate to string constants in C are processed.  Thus,
                    308: @samp{#include "x\n\\y"} specifies a filename containing three
                    309: backslashes.  It is not clear why this behavior is ever useful, but
                    310: the ANSI standard specifies it.
                    311: 
                    312: @item #include @var{anything else}
                    313: This variant is called a @dfn{computed #include}.  Any @samp{#include}
                    314: command whose argument does not fit the above two forms is a computed
                    315: include.  The text @var{anything else} is checked for macro calls,
                    316: which are expanded (@pxref{Macros}).  When this is done, the result
                    317: must fit one of the above two variants.
                    318: 
                    319: This feature allows you to define a macro which controls the file name
                    320: to be used at a later point in the program.  One application of this
                    321: is to allow a site-configuration file for your program to specify the
                    322: names of the system include files to be used.  This can help in
                    323: porting the program to various operating systems in which the
                    324: necessary system header files are found in different places.
                    325: @end table
                    326: 
                    327: @node Include Operation,, Include Syntax, Header Files
                    328: @subsection How @samp{#include} Works
                    329: 
                    330: The @samp{#include} command works by directing the C preprocessor to scan
                    331: the specified file as input before continuing with the rest of the current
                    332: file.  The output from the preprocessor contains the output already
                    333: generated, followed by the output resulting from the included file,
                    334: followed by the output that comes from the text after the @samp{#include}
                    335: command.  For example, given two files as follows:
                    336: 
                    337: @example
                    338: /* File program.c */
                    339: int x;
                    340: #include "header.h"
                    341: 
                    342: main ()
                    343: @{
                    344:   printf (test ());
                    345: @}
                    346: 
                    347: 
                    348: /* File header.h */
                    349: char *test ();
                    350: @end example
                    351: 
                    352: @noindent
                    353: the output generated by the C preprocessor for @file{program.c} as input
                    354: would be
                    355: 
                    356: @example
                    357: int x;
                    358: char *test ();
                    359: 
                    360: main ()
                    361: @{
                    362:   printf (test ());
                    363: @}
                    364: @end example
                    365: 
                    366: Included files are not limited to declarations and macro definitions; they
                    367: are merely the typical use.  Any fragment of a C program can be included
                    368: from another file.  The include file could even contain the beginning of a
                    369: statement that is concluded in the containing file, or the end of a
                    370: statement that was started in the including file.  However, a comment or a
                    371: string or character constant may not start in the included file and finish
                    372: in the including file.  An unterminated comment, string constant or
                    373: character constant in an included file is considered to end (with an error
                    374: message) at the end of the file.
                    375: 
                    376: The line following the @samp{#include} command is always treated as a
                    377: separate line by the C preprocessor even if the included file lacks a final
                    378: newline.
                    379: 
                    380: @node Macros, Conditionals, Header Files, Top
                    381: @section Macros
                    382: 
                    383: A macro is a sort of abbreviation which you can define once and then
                    384: use later.  There are many complicated features associated with macros
                    385: in the C preprocessor.
                    386: 
                    387: @menu
                    388: * Simple Macros::    Macros that always expand the same way.
                    389: * Argument Macros::  Macros that accept arguments that are substituted
                    390:                        into the macro expansion.
                    391: * Predefined::       Predefined macros that are always available.
                    392: * Stringification::  Macro arguments converted into string constants.
                    393: * Concatenation::    Building tokens from parts taken from macro arguments.
                    394: * Undefining::       Cancelling a macro's definition.
                    395: * Redefining::       Changing a macro's definition.
                    396: * Macro Pitfalls::   Macros can confuse the unwary.  Here we explain
                    397:                        several common problems and strange features.
                    398: @end menu
                    399: 
                    400: @node Simple Macros, Argument Macros, Macros, Macros
                    401: @subsection Simple Macros
                    402: 
                    403: A @dfn{simple macro} is a kind of abbreviation.  It is a name which stands
                    404: for a fragment of code.
                    405: 
                    406: Before you can use a macro, you must @dfn{define} it explicitly with the
                    407: @samp{#define} command.  @samp{#define} is followed by the name of the
                    408: macro and then the code it should be an abbreviation for.  For example,
                    409: 
                    410: @example
                    411: #define BUFFER_SIZE 1020
                    412: @end example
                    413: 
                    414: @noindent
                    415: defines a macro named @samp{BUFFER_SIZE} as an abbreviation for the text
                    416: @samp{1020}.  Therefore, if somewhere after this @samp{#define} command
                    417: there comes a C statement of the form
                    418: 
                    419: @example
                    420: foo = (char *) xmalloc (BUFFER_SIZE);
                    421: @end example
                    422: 
                    423: @noindent
                    424: then the C preprocessor will recognize and @dfn{expand} the macro
                    425: @samp{BUFFER_SIZE}, resulting in
                    426: 
                    427: @example
                    428: foo = (char *) xmalloc (1020);
                    429: @end example
                    430: 
                    431: @noindent
                    432: the definition must be a single line; however, it may not end in the
                    433: middle of a multi-line string constant or character constant.
                    434: 
                    435: The use of all upper case for macro names is a standard convention.
                    436: Programs are easier to read when it is possible to tell at a glance which
                    437: names are macros.
                    438: 
                    439: Normally, a macro definition must be a single line, like all C preprocessor
                    440: commands.  (You can split a long macro definition cosmetically with
                    441: Backslash-Newline.)  There is one exception: Newlines can be included in
                    442: the macro definition if within a string or character constant.  By the same
                    443: token, it is not possible for a macro definition to contain an unbalanced
                    444: quote character; the definition automatically extends to include the
                    445: matching quote character that ends the string or character constant.
                    446: Comments within a macro definition may contain Newlines, which make no
                    447: difference since the comments are entirely replaced with Spaces regardless
                    448: of their contents.
                    449: 
                    450: Aside from the above, there is no restriction on what can go in a macro
                    451: body.  Parentheses need not balance.  The body need not resemble valid C
                    452: code.  (Of course, you might get error messages from the C compiler when
                    453: you use the macro.)
                    454: 
                    455: The C preprocessor scans your program sequentially, so macro definitions
                    456: take effect at the place you write them.  Therefore, the following input to
                    457: the C preprocessor
                    458: 
                    459: @example
                    460: foo = X;
                    461: #define X 4
                    462: bar = X;
                    463: @end example
                    464: 
                    465: @noindent
                    466: produces as output
                    467: 
                    468: @example
                    469: foo = X;
                    470: 
                    471: bar = 4;
                    472: @end example
                    473: 
                    474: After the preprocessor expands a macro name, the macro's definition body is
                    475: appended to the front of the remaining input, and the check for macro calls
                    476: continues.  Therefore, the macro body can contain calls to other macros.
                    477: For example, after
                    478: 
                    479: @example
                    480: #define BUFSIZE 1020
                    481: #define TABLESIZE BUFSIZE
                    482: @end example
                    483: 
                    484: @noindent
                    485: the name @samp{TABLESIZE} when used in the program would go through two
                    486: stages of expansion, resulting ultimately in @samp{1020}.
                    487: 
                    488: This is not at all the same as defining @samp{TABLESIZE} to be @samp{1020}.
                    489: The @samp{#define} for @samp{TABLESIZE} uses exactly the body you
                    490: specify---in this case, @samp{BUFSIZE}---and does not check to see whether
                    491: it too is the name of a macro.  It's only when you @emph{use} @samp{TABLESIZE}
                    492: that the result of its expansion is checked for more macro names.
                    493: @xref{Cascaded Macros}.
                    494: 
                    495: @node Argument Macros, Predefined, Simple Macros, Macros
                    496: @subsection Macros with Arguments
                    497: 
                    498: A simple macro always stands for exactly the same text, each time it is
                    499: used.  Macros can be more flexible when they accept @dfn{arguments}.
                    500: Arguments are fragments of code that you supply each time the macro is
                    501: used.  These fragments are included in the expansion of the macro according
                    502: to the directions in the macro definition.
                    503: 
                    504: To define a macro that uses arguments, you write a @samp{#define} command
                    505: with a list of @dfn{argument names} in parentheses after the name of the
                    506: macro.  The argument names may be any valid C identifiers, separated by
                    507: commas and optionally whitespace.  The open-parenthesis must follow the
                    508: macro name immediately, with no space in between.
                    509: 
                    510: For example, here is a macro that computes the minimum of two numeric
                    511: values, as it is defined in many C programs:
                    512: 
                    513: @example
                    514: #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
                    515: @end example
                    516: 
                    517: @noindent
                    518: (This is not the best way to define a ``minimum'' macro in GNU C.
                    519: @xref{Side Effects}, for more information.)
                    520: 
                    521: To use a macro that expects arguments, you write the name of the macro
                    522: followed by a list of @dfn{actual arguments} in parentheses. separated by
                    523: commas.  The number of actual arguments you give must match the number of
                    524: arguments the macro expects.   Examples of use of the macro @samp{min}
                    525: include @samp{min (1, 2)} and @samp{min (x + 28, *p)}.
                    526: 
                    527: The expansion text of the macro depends on the arguments you use.
                    528: Each of the argument names of the macro is replaced, throughout the
                    529: macro definition, with the corresponding actual argument.  Using the
                    530: same macro @samp{min} defined above, @samp{min (1, 2)} expands into
                    531: 
                    532: @example
                    533: ((1) < (2) ? (1) : (2))
                    534: @end example
                    535: 
                    536: @noindent
                    537: where @samp{1} has been substituted for @samp{X} and @samp{2} for @samp{Y}.
                    538: 
                    539: Likewise, @samp{min (x + 28, *p)} expands into
                    540: 
                    541: @example
                    542: ((x + 28) < (*p) ? (x + 28) : (*p))
                    543: @end example
                    544: 
                    545: Parentheses in the actual arguments must balance; a comma within
                    546: parentheses does not end an argument.  However, there is no requirement for
                    547: brackets or braces to balance; thus, if you want to supply @samp{array[x =
                    548: y, x + 1]} as an argument, you must write it as @samp{array[(x = y, x +
                    549: 1)]}, which is equivalent C code.
                    550: 
                    551: After the actual arguments are substituted into the macro body, the entire
                    552: result is appended to the front of the remaining input, and the check for
                    553: macro calls continues.  Therefore, the actual arguments can contain calls
                    554: to other macros, either with or without arguments, or even to the same
                    555: macro.  The macro body can also contain calls to other macros.  For
                    556: example, @samp{min (min (a, b), c)} expands into
                    557: 
                    558: @example
                    559: ((((a) < (b) ? (a) : (b))) < (c)
                    560:  ? (((a) < (b) ? (a) : (b)))
                    561:  : (c))
                    562: @end example
                    563: 
                    564: @noindent
                    565: (Line breaks shown here for clarity would not actually be generated.)
                    566: 
                    567: If you use the macro name followed by something other than an
                    568: open-parenthesis (after ignoring any spaces, tabs and comments that
                    569: follow), it is not a call to the macro, and the preprocessor does not
                    570: change what you have written.  Therefore, it is possible for the same name
                    571: to be a variable or function in your program as well as a macro, and you
                    572: can choose in each instance whether to refer to the macro (if an actual
                    573: argument list follows) or the variable or function (if an argument list
                    574: does not follow).
                    575: 
                    576: Such dual use of one name could be confusing and should be avoided
                    577: except when the two meanings are effectively synonymous: that is, when the
                    578: name is both a macro and a function and the two have similar effects.  You
                    579: can think of the name simply as a function; use of the name for purposes
                    580: other than calling it (such as, to take the address) will refer to the
                    581: function, while calls will expand the macro and generate better but
                    582: equivalent code.  For example, you can use a function named @samp{min} in
                    583: the same source file that defines the macro.  If you write @samp{&min} with
                    584: no argument list, you refer to the function.  If you write @samp{min (x,
                    585: bb)}, with an argument list, the macro is expanded.  If you write
                    586: @samp{(min) (a, bb)}, where the name @samp{min} is not followed by an
                    587: open-parenthesis, the macro is not expanded, so you wind up with a call to
                    588: the function @samp{min}.
                    589: 
                    590: It is not allowed to define the same name as both a simple macro and
                    591: a macro with arguments.
                    592: 
                    593: In the definition of a macro with arguments, the list of argument names
                    594: must follow the macro name immediately with no space in between.  If there
                    595: is a space after the macro name, the macro is defined as taking no
                    596: arguments, and all the rest of the name is taken to be the expansion.  The
                    597: reason for this is that it is often useful to define a macro that takes no
                    598: arguments and whose definition begins with an identifier in parentheses.
                    599: This rule about spaces makes it possible for you to do either this:
                    600: 
                    601: @example
                    602: #define FOO(x) - 1 / (x)
                    603: @end example
                    604: 
                    605: @noindent
                    606: (which defines @samp{FOO} to take an argument and expand into minus the
                    607: reciprocal of that argument) or this:
                    608: 
                    609: @example
                    610: #define BAR (x) - 1 / (x)
                    611: @end example
                    612: 
                    613: @noindent
                    614: (which defines @samp{BAR} to take no argument and always expand into
                    615: @samp{(x) - 1 / (x)}).
                    616: 
                    617: Note that the @emph{uses} of a macro with arguments can have spaces before
                    618: the left parenthesis; it's the @emph{definition} where it matters whether
                    619: there is a space.
                    620: 
                    621: @node Predefined, Stringification, Argument Macros, Macros
                    622: @subsection Predefined Macros
                    623: 
                    624: @cindex predefined macros
                    625: Several simple macros are predefined.  You can use them without giving
                    626: definitions for them.  They fall into two classes: standard macros and
                    627: system-specific macros.
                    628: 
                    629: @menu
                    630: * Standard Predefined::     Standard predefined macros.
                    631: * Nonstandard Predefined::  Nonstandard predefined macros.
                    632: @end menu
                    633: 
                    634: @node Standard Predefined, Nonstandard Predefined, Predefined, Predefined
                    635: @subsubsection Standard Predefined Macros
                    636: 
                    637: The standard predefined macros are available with the same meanings
                    638: regardless of the machine or operating system on which you are using GNU C.
                    639: Their names all start and end with double underscores.  Those preceding
                    640: @code{__GNUC__} in this table are standardized by ANSI C; the rest are
                    641: GNU C extensions.
                    642: 
                    643: @table @code
                    644: @item __FILE__
                    645: @findex __FILE__
                    646: This macro expands to the name of the current input file, in the form
                    647: of a C string constant.
                    648: 
                    649: @item __LINE__
                    650: @findex __LINE__
                    651: This macro expands to the current input line number, in the form of a
                    652: decimal integer constant.  While we call it a predefined macro, it's
                    653: a pretty strange macro, since its ``definition'' changes with each
                    654: new line of source code.
                    655: 
                    656: This and @samp{__FILE__} are useful in generating an error message to
                    657: report an inconsistency detected by the program; the message can state
                    658: the source line at which the inconsistency was detected.  For example,
                    659: 
                    660: @example
                    661: fprintf (stderr, "Internal error: negative string length "
                    662:                  "%d at %s, line %d.",
                    663:          length, __FILE__, __LINE__);
                    664: @end example
                    665: 
                    666: A @samp{#include} command changes the expansions of @samp{__FILE__}
                    667: and @samp{__LINE__} to correspond to the included file.  At the end of
                    668: that file, when processing resumes on the input file that contained
                    669: the @samp{#include} command, the expansions of @samp{__FILE__} and
                    670: @samp{__LINE__} revert to the values they had before the
                    671: @samp{#include} (but @samp{__LINE__} is then incremented by one as
                    672: processing moves to the line after the @samp{#include}).
                    673: 
                    674: The expansions of both @samp{__FILE__} and @samp{__LINE__} are altered
                    675: if a @samp{#line} command is used.  @xref{Combining Sources}.
                    676: 
                    677: @item __DATE__
                    678: @findex __DATE__
                    679: This macro expands to a string constant that describes the date on
                    680: which the preprocessor is being run.  The string constant contains
                    681: eleven characters and looks like @samp{"Jan 29 1987"} or @w{@samp{"Apr
                    682: 1 1905"}}.
                    683: 
                    684: @item __TIME__
                    685: @findex __TIME__
                    686: This macro expands to a string constant that describes the time at
                    687: which the preprocessor is being run.  The string constant contains
                    688: eight characters and looks like @samp{"23:59:01"}.
                    689: 
                    690: @item __STDC__
                    691: @findex __STDC__
                    692: This macro expands to the constant 1, to signify that this is ANSI
                    693: Standard C.  (Whether that is actually true depends on what C compiler
                    694: will operate on the output from the preprocessor.)
                    695: 
                    696: @item __GNUC__
                    697: This macro is defined if and only if this is GNU C.  This macro is
                    698: defined only when the entire GNU C compiler is in use; if you invoke
                    699: the preprocessor directly, @samp{__GNUC__} is undefined.
                    700: 
                    701: @item __STRICT_ANSI__
                    702: This macro is defined if and only if the @samp{-ansi} switch was
                    703: specified when GNU C was invoked.  Its definition is the null string.
                    704: This macro exists primarily to direct certain GNU header files not to
                    705: define certain traditional Unix constructs which are incompatible with
                    706: ANSI C.
                    707: 
                    708: @item __VERSION__
                    709: This macro expands to a string which describes the version number of
                    710: GNU C.  The string is normally a sequence of decimal numbers separated
                    711: by periods, such as @samp{"1.18"}.  The only reasonable use of this
                    712: macro is to incorporate it into a string constant.
                    713: 
                    714: @item __OPTIMIZE__
                    715: This macro is defined in optimizing compilations.  It causes certain
                    716: GNU header files to define alternative macro definitions for some
                    717: system library functions.  It is unwise to refer to or test the
                    718: definition of this macro unless you make very sure that programs will
                    719: execute with the same effect regardless.
                    720: 
                    721: @item __CHAR_UNSIGNED__
                    722: This macro is defined if and only if the data type @code{char} is
                    723: unsigned on the target machine.  It exists to cause the standard
                    724: header file @file{limit.h} to work correctly.  It is bad practice
                    725: to refer to this macro yourself; instead, refer to the standard
                    726: macros defined in @file{limit.h}.
                    727: @end table
                    728: 
                    729: @node Nonstandard Predefined,, Standard Predefined, Predefined
                    730: @subsubsection Nonstandard Predefined Macros
                    731: 
                    732: The C preprocessor normally has several predefined macros that vary between
                    733: machines because their purpose is to indicate what type of system and
                    734: machine is in use.  This manual, being for all systems and machines, cannot
                    735: tell you exactly what their names are; instead, we offer a list of some
                    736: typical ones.
                    737: 
                    738: Some nonstandard predefined macros describe the operating system in use,
                    739: with more or less specificity.  For example,
                    740: 
                    741: @table @code
                    742: @item unix
                    743: @findex unix
                    744: @samp{unix} is normally predefined on all Unix systems.
                    745: 
                    746: @item BSD
                    747: @findex BSD
                    748: @samp{BSD} is predefined on recent versions of Berkeley Unix
                    749: (perhaps only in version 4.3).
                    750: @end table
                    751: 
                    752: Other nonstandard predefined macros describe the kind of CPU, with more or
                    753: less specificity.  For example,
                    754: 
                    755: @table @code
                    756: @item vax
                    757: @findex vax
                    758: @samp{vax} is predefined on Vax computers.
                    759: 
                    760: @item mc68000
                    761: @findex mc68000
                    762: @samp{mc68000} is predefined on most computers whose CPU is a Motorola
                    763: 68000, 68010 or 68020.
                    764: 
                    765: @item m68k
                    766: @findex m68k
                    767: @samp{m68k} is also predefined on most computers whose CPU is a 68000,
                    768: 68010 or 68020; however, some makers use @samp{mc68000} and some use
                    769: @samp{m68k}.  Some predefine both names.  What happens in GNU C
                    770: depends on the system you are using it on.
                    771: 
                    772: @item M68020
                    773: @findex M68020
                    774: @samp{M68020} has been observed to be predefined on some systems that
                    775: use 68020 CPUs---in addition to @samp{mc68000} and @samp{m68k} that
                    776: are less specific.
                    777: 
                    778: @item ns32000
                    779: @findex ns32000
                    780: @samp{ns32000} is predefined on computers which use the National
                    781: Semiconductor 32000 series CPU.
                    782: @end table
                    783: 
                    784: Yet other nonstandard predefined macros describe the manufacturer of
                    785: the system.  For example,
                    786: 
                    787: @table @code
                    788: @item sun
                    789: @findex sun
                    790: @samp{sun} is predefined on all models of Sun computers.
                    791: 
                    792: @item pyr
                    793: @findex pyr
                    794: @samp{pyr} is predefined on all models of Pyramid computers.
                    795: 
                    796: @item sequent
                    797: @findex sequent
                    798: @samp{sequent} is predefined on all models of Sequent computers.
                    799: @end table
                    800: 
                    801: These predefined symbols are not only nonstandard, they are contrary to the
                    802: ANSI standard because their names do not start with underscores.  However,
                    803: the GNU C preprocessor would be useless if it did not predefine the same
                    804: names that are normally predefined on the system and machine you are using.
                    805: Even system header files check the predefined names and will generate
                    806: incorrect declarations if they do not find the names that are expected.
                    807: 
                    808: The set of nonstandard predefined names in the GNU C preprocessor is
                    809: controlled by the macro @samp{CPP_PREDEFINES}, which should be a string
                    810: containing @samp{-D} options, separated by spaces.  For example, on the Sun,
                    811: the definition
                    812: 
                    813: @example
                    814: #define CPP_PREDEFINES "-Dmc68000 -Dsun -Dunix -Dm68k"
                    815: @end example
                    816: 
                    817: @noindent
                    818: is used.
                    819: 
                    820: The @samp{-ansi} option which requests complete support for ANSI C
                    821: inhibits the definition of these predefined symbols.
                    822: 
                    823: @node Stringification, Concatenation, Predefined, Macros
                    824: @subsection Stringification
                    825: 
                    826: @cindex stringification
                    827: @dfn{Stringification} means turning a code fragment into a string constant
                    828: whose contents are the text for the code fragment.  For example,
                    829: stringifying @samp{foo (z)} results in @samp{"foo (z)"}.
                    830: 
                    831: In the C preprocessor, stringification is an option available when macro
                    832: arguments are substituted into the macro definition.  In the body of the
                    833: definition, when an argument name appears, the character @samp{#} before
                    834: the name specifies stringification of the corresponding actual argument
                    835: when it is substituted at that point in the definition.  The same argument
                    836: may be substituted in other places in the definition without
                    837: stringification if the argument name appears in those places with no
                    838: @samp{#}.
                    839: 
                    840: Here is an example of a macro definition that uses stringification:
                    841: 
                    842: @example
                    843: #define WARN_IF(EXP) \
                    844: do @{ if (EXP) fprintf (stderr, "Warning: " #EXP "\n"); @} while (0)
                    845: @end example
                    846: 
                    847: @noindent
                    848: Here the actual argument for @samp{EXP} is substituted once as given,
                    849: into the @samp{if} statement, and once as stringified, into the
                    850: argument to @samp{fprintf}.  The @samp{do} and @samp{while (0)} are
                    851: a kludge to make it possible to write @samp{WARN_IF (@var{arg});},
                    852: which the resemblance of @samp{WARN_IF} to a function would make
                    853: C programmers want to do; @pxref{Swallow Semicolon}).
                    854: 
                    855: The stringification feature is limited to transforming one macro argument
                    856: into one string constant: there is no way to combine the argument with
                    857: other text and then stringify it all together.  But the example above shows
                    858: how an equivalent result can be obtained in ANSI Standard C using the
                    859: feature that adjacent string constants are concatenated as one string
                    860: constant.  The preprocessor stringifies @samp{EXP}'s actual argument
                    861: into a separate string constant, resulting in text like
                    862: 
                    863: @example
                    864: do @{ if (x == 0) fprintf (stderr, "Warning: " "x == 0" "\n"); @} while (0)
                    865: @end example
                    866: 
                    867: @noindent
                    868: but the C compiler then sees three consecutive string constants and
                    869: concatenates them into one, producing effectively
                    870: 
                    871: @example
                    872: do @{ if (x == 0) fprintf (stderr, "Warning: x == 0\n"); @} while (0)
                    873: @end example
                    874: 
                    875: Stringification in C involves more than putting doublequote characters
                    876: around the fragment; it is necessary to put backslashes in front of all
                    877: doublequote characters, and all backslashes in string and character
                    878: constants, in order to get a valid C string constant with the proper
                    879: contents.  Thus, stringifying @samp{p = "foo\n";} results in @samp{"p =
                    880: \"foo\\n\";"}.  However, backslashes that are not inside of string or
                    881: character constants are not duplicated: @samp{\n} by itself stringifies to
                    882: @samp{"\n"}.
                    883: 
                    884: Whitespace (including comments) in the text being stringified is handled
                    885: according to precise rules.  All leading and trailing whitespace is ignored.
                    886: Any sequence of whitespace in the middle of the text is converted to
                    887: a single space in the stringified result.
                    888: 
                    889: @node Concatenation, Undefining, Stringification, Macros
                    890: @subsection Concatenation
                    891: 
                    892: @cindex concatenation
                    893: @dfn{Concatenation} means joining two strings into one.  In the context
                    894: of macro expansion, concatenation refers to joining two lexical units
                    895: into one longer one.  Specifically, an actual argument to the macro can be
                    896: concatenated with another actual argument or with fixed text to produce
                    897: a longer name.  The longer name might be the name of a function,
                    898: variable or type, or a C keyword; it might even be the name of another
                    899: macro, in which case it will be expanded.
                    900: 
                    901: When you define a macro, you request concatenation with the special
                    902: operator @samp{##} in the macro body.  When the macro is called,
                    903: after actual arguments are substituted, all @samp{##} operators are
                    904: deleted, and so is any whitespace next to them (including whitespace
                    905: that was part of an actual argument).  The result is to concatenate
                    906: the syntactic tokens on either side of the @samp{##}.
                    907: 
                    908: Consider a C program that interprets named commands.  There probably needs
                    909: to be a table of commands, perhaps an array of structures declared as
                    910: follows:
                    911: 
                    912: @example
                    913: struct command
                    914: @{
                    915:   char *name;
                    916:   void (*function) ();
                    917: @};
                    918: 
                    919: struct command commands[] =
                    920: @{
                    921:   @{ "quit", quit_command@},
                    922:   @{ "help", help_command@},
                    923:   @dots{}
                    924: @};
                    925: @end example
                    926: 
                    927: It would be cleaner not to have to give each command name twice, once in
                    928: the string constant and once in the function name.  A macro which takes the
                    929: name of a command as an argument can make this unnecessary.  The string
                    930: constant can be created with stringification, and the function name by
                    931: concatenating the argument with @samp{_command}.  Here is how it is done:
                    932: 
                    933: @example
                    934: #define COMMAND(NAME)  @{ #NAME, NAME ## _command @}
                    935: 
                    936: struct command commands[] =
                    937: @{
                    938:   COMMAND (quit),
                    939:   COMMAND (help),
                    940:   @dots{}
                    941: @};
                    942: @end example
                    943: 
                    944: The usual case of concatenation is concatenating two names (or a name and a
                    945: number) into a longer name.  But this isn't the only valid case.  It is
                    946: also possible to concatenate two numbers (or a number and a name, such as
                    947: @samp{1.5} and @samp{e3}) into a number.  Also, multi-character operators
                    948: such as @samp{+=} can be formed by concatenation.  In some cases it is even
                    949: possible to piece together a string constant.  However, two pieces of text
                    950: that don't together form a valid lexical unit cannot be concatenated.  For
                    951: example, concatenation with @samp{x} on one side and @samp{+} on the other
                    952: is not meaningful because those two characters can't fit together in any
                    953: lexical unit of C.  The ANSI standard says that such attempts at
                    954: concatenation are undefined, but in the GNU C preprocessor it is well
                    955: defined: it puts the @samp{x} and @samp{+} side by side with no particular
                    956: special results.
                    957: 
                    958: Keep in mind that the C preprocessor converts comments to whitespace before
                    959: macros are even considered.  Therefore, you cannot create a comment by
                    960: concatenating @samp{/} and @samp{*}: the @samp{/*} sequence that starts a
                    961: comment is not a lexical unit, but rather the beginning of a ``long'' space
                    962: character.  Also, you can freely use comments next to a @samp{##} in a
                    963: macro definition, or in actual arguments that will be concatenated, because
                    964: the comments will be converted to spaces at first sight, and concatenation
                    965: will later discard the spaces.
                    966: 
                    967: @node Undefining, Redefining, Concatenation, Macros
                    968: @subsection Undefining Macros
                    969: 
                    970: @cindex undefining macros
                    971: To @dfn{undefine} a macro means to cancel its definition.  This is done
                    972: with the @samp{#undef} command.  @samp{#undef} is followed by the macro
                    973: name to be undefined.
                    974: 
                    975: Like definition, undefinition occurs at a specific point in the source
                    976: file, and it applies starting from that point.  The name ceases to be a
                    977: macro name, and from that point on it is treated by the preprocessor as if
                    978: it had never been a macro name.
                    979: 
                    980: For example,
                    981: 
                    982: @example
                    983: #define FOO 4
                    984: x = FOO;
                    985: #undef FOO
                    986: x = FOO;
                    987: @end example
                    988: 
                    989: @noindent
                    990: expands into
                    991: 
                    992: @example
                    993: x = 4;
                    994: 
                    995: x = FOO;
                    996: @end example
                    997: 
                    998: @noindent
                    999: In this example, @samp{FOO} had better be a variable or function as well
                   1000: as (temporarily) a macro, in order for the result of the expansion to be
                   1001: valid C code.
                   1002: 
                   1003: The same form of @samp{#undef} command will cancel definitions with
                   1004: arguments or definitions that don't expect arguments.  The @samp{#undef}
                   1005: command has no effect when used on a name not currently defined as a macro.
                   1006: 
                   1007: @node Redefining, Macro Pitfalls, Undefining, Macros
                   1008: @subsection Redefining Macros
                   1009: 
                   1010: @cindex redefining macros
                   1011: @dfn{Redefining} a macro means defining (with @samp{#define}) a name that
                   1012: is already defined as a macro.
                   1013: 
                   1014: A redefinition is trivial if the new definition is transparently identical
                   1015: to the old one.  You probably wouldn't deliberately write a trivial
                   1016: redefinition, but they can happen automatically when a header file is
                   1017: included more than once (@pxref{Header Files}), so they are accepted
                   1018: silently and without effect.
                   1019: 
                   1020: Nontrivial redefinition is considered likely to be an error, so
                   1021: it provokes a warning message from the preprocessor.  However, sometimes it
                   1022: is useful to change the definition of a macro in mid-compilation.  You can
                   1023: inhibit the warning by undefining the macro with @samp{#undef} before the
                   1024: second definition.
                   1025: 
                   1026: In order for a reefinition to be trivial, the new definition must exactly match
                   1027: the one already in effect, with two possible exceptions:
                   1028: 
                   1029: @itemize @bullet
                   1030: @item
                   1031: Whitespace may be added or deleted at the beginning or the end.
                   1032: 
                   1033: @item
                   1034: Whitespace may be changed in the middle (but not inside strings).
                   1035: However, it may not be eliminated entirely, and it may not be added
                   1036: where there was no whitespace at all.
                   1037: @end itemize
                   1038: 
                   1039: Recall that a comment counts as whitespace.
                   1040: 
                   1041: @node Macro Pitfalls,, Redefining, Macros
                   1042: @subsection Pitfalls and Subtleties of Macros
                   1043: 
                   1044: In this section we describe some special rules that apply to macros and
                   1045: macro expansion, and point out certain cases in which the rules have 
                   1046: counterintuitive consequences that you must watch out for.
                   1047: 
                   1048: @menu
                   1049: * Misnesting::        Macros can contain unmatched parentheses.
                   1050: * Macro Parentheses:: Why apparently superfluous parentheses
                   1051:                          may be necessary to avoid incorrect grouping.
                   1052: * Swallow Semicolon:: Macros that look like functions
                   1053:                          but expand into compound statements.
                   1054: * Side Effects::      Unsafe macros that cause trouble when
                   1055:                          arguments contain side effects.
                   1056: * Self-Reference::    Macros whose definitions use the macros' own names.
                   1057: * Argument Prescan::  Actual arguments are checked for macro calls
                   1058:                          before they are substituted.
                   1059: * Cascaded Macros::   Macros whose definitions use other macros.
                   1060: @end menu
                   1061: 
                   1062: @node Misnesting, Macro Parentheses, Macro Pitfalls, Macro Pitfalls
                   1063: @subsubsection Improperly Nested Constructs
                   1064: 
                   1065: Recall that when a macro is called with arguments, the arguments are
                   1066: substituted into the macro body and the result is checked, together with
                   1067: the rest of the input file, for more macro calls.
                   1068: 
                   1069: It is possible to piece together a macro call coming partially from the
                   1070: macro body and partially from the actual arguments.  For example,
                   1071: 
                   1072: @example
                   1073: #define double(x) (2*(x))
                   1074: #define call_with_1(x) x(1)
                   1075: @end example
                   1076: 
                   1077: @noindent
                   1078: would expand @samp{call_with_1 (double)} into @samp{(2*(1))}.
                   1079: 
                   1080: Macro definitions do not have to have balanced parentheses.  By writing an
                   1081: unbalanced open parenthesis in a macro body, it is possible to create a
                   1082: macro call that begins inside the macro body but ends outside of it.  For
                   1083: example,
                   1084: 
                   1085: @example
                   1086: #define strange(file) fprintf (file, "%s %d",
                   1087: @dots{}
                   1088: strange(stderr) p, 35)
                   1089: @end example
                   1090: 
                   1091: @noindent
                   1092: This bizarre example expands to @samp{fprintf (stderr, "%s %d", p, 35)}!
                   1093: 
                   1094: @node Macro Parentheses, Swallow Semicolon, Misnesting, Macro Pitfalls
                   1095: @subsubsection Unintended Grouping of Arithmetic
                   1096: 
                   1097: You may have noticed that in most of the macro definition examples shown
                   1098: above, each occurrence of a macro argument name had parentheses around it.
                   1099: In addition, another pair of parentheses usually surround the entire macro
                   1100: definition.  Here is why it is best to write macros that way.
                   1101: 
                   1102: Suppose you define a macro as follows
                   1103: 
                   1104: @example
                   1105: #define ceil_div(x, y) (x + y - 1) / y
                   1106: @end example
                   1107: 
                   1108: @noindent
                   1109: whose purpose is to divide, rounding up.  (One use for this
                   1110: operation is to compute how many @samp{int}'s are needed to hold
                   1111: a certain number of @samp{char}'s.)  Then suppose it is used as follows:
                   1112: 
                   1113: @example
                   1114: a = ceil_div (b & c, sizeof (int));
                   1115: @end example
                   1116: 
                   1117: @noindent
                   1118: This expands into
                   1119: 
                   1120: @example
                   1121: a = (b & c + sizeof (int) - 1) / sizeof (int);
                   1122: @end example
                   1123: 
                   1124: @noindent
                   1125: which does not do what is intended.  The operator-precedence rules of
                   1126: C make it equivalent to this:  
                   1127: 
                   1128: @example
                   1129: a = (b & (c + sizeof (int) - 1)) / sizeof (int);
                   1130: @end example
                   1131: 
                   1132: @noindent
                   1133: But what we want is this:
                   1134: 
                   1135: @example
                   1136: a = ((b & c) + sizeof (int) - 1)) / sizeof (int);
                   1137: @end example
                   1138: 
                   1139: @noindent
                   1140: Defining the macro as
                   1141: 
                   1142: @example
                   1143: #define ceil_div(x, y) ((x) + (y) - 1) / (y)
                   1144: @end example
                   1145: 
                   1146: @noindent
                   1147: provides the desired result.
                   1148: 
                   1149: However, unintended grouping can result in another way.  Consider
                   1150: @samp{sizeof ceil_div(1, 2)}.  That has the appearance of a C expression
                   1151: that would compute the size of the type of @samp{ceil_div (1, 2)}, but in
                   1152: fact it means something very different.  Here is what it expands to:
                   1153: 
                   1154: @example
                   1155: sizeof ((1) + (2) - 1) / (2)
                   1156: @end example
                   1157: 
                   1158: @noindent
                   1159: This would take the size of an integer and divide it by two.  The precedence
                   1160: rules have put the division outside the @samp{sizeof} when it was intended
                   1161: to be inside.
                   1162: 
                   1163: Parentheses around the entire macro definition can prevent such problems.
                   1164: Here, then, is the recommended way to define @samp{ceil_div}:
                   1165: 
                   1166: @example
                   1167: #define ceil_div(x, y) (((x) + (y) - 1) / (y))
                   1168: @end example
                   1169: 
                   1170: @node Swallow Semicolon, Side Effects, Macro Parentheses, Macro Pitfalls
                   1171: @subsubsection Swallowing the Semicolon
                   1172: 
                   1173: @cindex semicolons (after macro calls)
                   1174: Often it is desirable to define a macro that expands into a compound
                   1175: statement.  Consider, for example, the following macro, that advances a
                   1176: pointer (the argument @samp{p} says where to find it) across whitespace
                   1177: characters:
                   1178: 
                   1179: @example
                   1180: #define SKIP_SPACES (p, limit)  \
                   1181: @{ register char *lim = (limit); \
                   1182:   while (p != lim) @{            \
                   1183:     if (*p++ != ' ') @{          \
                   1184:       p--; break; @}@}@}
                   1185: @end example
                   1186: 
                   1187: @noindent
                   1188: Here Backslash-Newline is used to split the macro definition, which must
                   1189: be a single line, so that it resembles the way such C code would be
                   1190: layed out if not part of a macro definition.
                   1191: 
                   1192: A call to this macro might be @samp{SKIP_SPACES (p, lim)}.  Strictly
                   1193: speaking, the call expands to a compound statement, which is a complete
                   1194: statement with no need for a semicolon to end it.  But it looks like a
                   1195: function call.  So it minimizes confusion if you can use it like a function
                   1196: call, writing a semicolon afterward, as in @samp{SKIP_SPACES (p, lim);}
                   1197: 
                   1198: But this can cause trouble before @samp{else} statements, because the
                   1199: semicolon is actually a null statement.  Suppose you write
                   1200: 
                   1201: @example
                   1202: if (*p != 0)
                   1203:   SKIP_SPACES (p, lim);
                   1204: else @dots{}
                   1205: @end example
                   1206: 
                   1207: @noindent
                   1208: The presence of two statements---the compound statement and a null
                   1209: statement---in between the @samp{if} condition and the @samp{else}
                   1210: makes invalid C code.
                   1211: 
                   1212: The definition of the macro @samp{SKIP_SPACES} can be altered to solve
                   1213: this problem, using a @samp{do @dots{} while} statement.  Here is how:
                   1214:   
                   1215: @example
                   1216: #define SKIP_SPACES (p, limit)     \
                   1217: do @{ register char *lim = (limit); \
                   1218:      while (p != lim) @{            \
                   1219:        if (*p++ != ' ') @{          \
                   1220:          p--; break; @}@}@}           \
                   1221: while (0)
                   1222: @end example
                   1223: 
                   1224: Now @samp{SKIP_SPACES (p, lim);} expands into
                   1225: 
                   1226: @example
                   1227: do @{@dots{}@} while (0);
                   1228: @end example
                   1229: 
                   1230: @noindent
                   1231: which is one statement.
                   1232: 
                   1233: @node Side Effects, Self-Reference, Swallow Semicolon, Macro Pitfalls
                   1234: @subsubsection Duplication of Side Effects
                   1235: 
                   1236: @cindex side effects (in macro arguments)
                   1237: @cindex unsafe macros
                   1238: Many C programs define a macro @samp{min}, for ``minimum'', like this:
                   1239: 
                   1240: @example
                   1241: #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
                   1242: @end example
                   1243: 
                   1244: When you use this macro with an argument containing a side effect,
                   1245: as shown here,
                   1246: 
                   1247: @example
                   1248: next = min (x + y, foo (z));
                   1249: @end example
                   1250: 
                   1251: @noindent
                   1252: it expands as follows:
                   1253: 
                   1254: @example
                   1255: next = ((x + y) < (foo (z)) ? (x + y) : (foo (z)));
                   1256: @end example
                   1257: 
                   1258: @noindent
                   1259: where @samp{x + y} has been substituted for @samp{X} and @samp{foo (z)}
                   1260: for @samp{Y}.
                   1261: 
                   1262: The function @samp{foo} is used only once in the statement as it appears
                   1263: in the program, but the expression @samp{foo (z)} has been substituted
                   1264: twice into the macro expansion.  As a result, @samp{foo} might be called
                   1265: two times when the statement is executed.  If it has side effects or
                   1266: if it takes a long time to compute, the results might not be what you
                   1267: intended.  We say that @samp{min} is an @dfn{unsafe} macro.
                   1268: 
                   1269: The best solution to this problem is to define @samp{min} in a way that
                   1270: computes the value of @samp{foo (z)} only once.  The C language offers no
                   1271: way standard way to do this, but it can be done with GNU C extensions as
                   1272: follows:
                   1273: 
                   1274: @example
                   1275: #define min(X, Y)                     \
                   1276: (@{ typeof (X) __x = (X), __y = (Y);   \
                   1277:    (__x < __y) ? __x : __y; @})
                   1278: @end example
                   1279: 
                   1280: If you do not wish to use GNU C extensions, the only solution is to be
                   1281: careful when @emph{using} the macro @samp{min}.  For example, you can
                   1282: calculate the value of @samp{foo (z)}, save it in a variable, and use that
                   1283: variable in @samp{min}:
                   1284: 
                   1285: @example
                   1286: #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
                   1287: @dots{}
                   1288: @{
                   1289:   int tem = foo (z);
                   1290:   next = min (x + y, tem);
                   1291: @}
                   1292: @end example
                   1293: 
                   1294: @noindent
                   1295: (where I assume that @samp{foo} returns type @samp{int}).
                   1296: 
                   1297: @node Self-Reference, Argument Prescan, Side Effects, Macro Pitfalls
                   1298: @subsubsection Self-Referential Macros
                   1299: 
                   1300: @cindex self-reference
                   1301: A @dfn{self-referential} macro is one whose name appears in its definition.
                   1302: A special feature of ANSI Standard C is that the self-reference is not
                   1303: considered a macro call.  It is passed into the preprocessor output
                   1304: unchanged.
                   1305: 
                   1306: Let's consider an example:
                   1307: 
                   1308: @example
                   1309: #define foo (4 + foo)
                   1310: @end example
                   1311: 
                   1312: @noindent
                   1313: where @samp{foo} is also a variable in your program.
                   1314: 
                   1315: Following the ordinary rules, each reference to @samp{foo} will expand into
                   1316: @samp{(4 + foo)}; then this will be rescanned and will expand into @samp{(4
                   1317: + (4 + foo))}; and so on until it causes a fatal error (memory full) in the
                   1318: preprocessor.
                   1319: 
                   1320: However, the special rule about self-reference cuts this process short
                   1321: after one step, at @samp{(4 + foo)}.  Therefore, this macro definition
                   1322: has the possibly useful effect of causing the program to add 4 to
                   1323: the value of @samp{foo} wherever @samp{foo} is referred to.
                   1324: 
                   1325: In most cases, it is a bad idea to take advantage of this feature.
                   1326: A person reading the program who sees that @samp{foo} is a variable will
                   1327: not expect that it is a macro as well.  The reader will come across a
                   1328: the identifier @samp{foo} in the program and think its value should be
                   1329: that of the variable @samp{foo}, whereas in fact the value is four
                   1330: greater.
                   1331: 
                   1332: The special rule for self-reference applies also to @dfn{indirect}
                   1333: self-reference.  This is the case where a macro @var{x} expands to use a
                   1334: macro @samp{y}, and @samp{y}'s expansion refers to the macro @samp{x}.  The
                   1335: resulting reference to @samp{x} comes indirectly from the expansion of
                   1336: @samp{x}, so it is a self-reference and is not further expanded.  Thus,
                   1337: after
                   1338: 
                   1339: @example
                   1340: #define x (4 + y)
                   1341: #define y (2 * x)
                   1342: @end example
                   1343: 
                   1344: @noindent
                   1345: @samp{x} would expand into @samp{(4 + (2 * x))}.  Clear?
                   1346: 
                   1347: But suppose @samp{y} is used elsewhere, not from the definition of @samp{x}.
                   1348: Then the use of @samp{x} in the expansion of @samp{y} is not a self-reference
                   1349: because @samp{x} is not ``in progress''.  So it does expand.  However,
                   1350: the expansion of @samp{x} contains a reference to @samp{y}, and that
                   1351: is an indirect self-reference now because @samp{y} is ``in progress''.
                   1352: The result is that @samp{y} expands to @samp{(2 * (4 + y))}.
                   1353: 
                   1354: It is not clear that this behavior would ever be useful, but it is specified
                   1355: by the ANSI C standard, so you need to understand it.
                   1356: 
                   1357: @node Argument Prescan, Cascaded Macros, Self-Reference, Macro Pitfalls
                   1358: @subsubsection Separate Expansion of Macro Arguments
                   1359: 
                   1360: We have explained that the expansion of a macro, including the substituted
                   1361: actual arguments, is scanned over again for macro calls to be expanded.
                   1362: 
                   1363: What really happens is more subtle: first each actual argument text is scanned
                   1364: separately for macro calls.  Then the results of this are substituted into
                   1365: the macro body to produce the macro expansion, and the macro expansion
                   1366: is scanned again for macros to expand.
                   1367: 
                   1368: The result is that the actual arguments are scanned @emph{twice} to expand
                   1369: macro calls in them.
                   1370: 
                   1371: Most of the time, this has no effect.  If the actual argument contained
                   1372: any macro calls, they are expanded during the first scan.  The result
                   1373: therefore contains no macro calls, so the second scan does not change it.
                   1374: If the actual argument were substituted as given, with no prescan,
                   1375: the single remaining scan would find the same macro calls and produce
                   1376: the same results.
                   1377: 
                   1378: You might expect the double scan to change the results when a
                   1379: self-referential macro is used in an actual argument of another macro
                   1380: (@pxref{Self-Reference}): the self-referential macro would be expanded once
                   1381: in the first scan, and a second time in the second scan.  But this is not
                   1382: what happens.  The self-references that do not expand in the first scan are
                   1383: marked so that they will not expand in the second scan either.
                   1384: 
                   1385: The prescan is not done when an argument is stringified or concatenated.
                   1386: (More precisely, stringification and concatenation use the argument as
                   1387: written, in un-prescanned form.  The same actual argument would be used in
                   1388: prescanned form if it is substituted elsewhere without stringification or
                   1389: concatenation.)  Thus,
                   1390: 
                   1391: @example
                   1392: #define str(s) #s
                   1393: #define foo 4
                   1394: str (foo)
                   1395: @end example
                   1396: 
                   1397: @noindent
                   1398: expands to @samp{"foo"}.  Once more, prescan has been prevented from
                   1399: having any noticeable effect.
                   1400: 
                   1401: You might now ask, ``Why mention the prescan, if it makes no difference?
                   1402: Why not skip it and make the preprocessor faster?''  The answer is that the
                   1403: prescan does make a difference in two special cases:
                   1404: 
                   1405: @itemize @bullet
                   1406: @item
                   1407: Nested calls to a macro.
                   1408: 
                   1409: @item
                   1410: Macros that call other macros that stringify or concatenate.
                   1411: @end itemize
                   1412: 
                   1413: We say that @dfn{nested} calls to a macro occur when a macro's actual
                   1414: argument contains a call to that very macro.  For example, if @samp{f}
                   1415: is a macro that expects one argument, @samp{f (f (1))} is a nested
                   1416: pair of calls to @samp{f}.  The desired expansion is made by
                   1417: expanding @samp{f (1)} and substituting that into the definition of
                   1418: @samp{f}.  The prescan causes the expected result to happen.
                   1419: Without the prescan, @samp{f (1)} itself would be substituted as
                   1420: an actual argument, and the inner use of @samp{f} would appear
                   1421: during the main scan as an indirect self-reference and would not
                   1422: be expanded.  Here, the prescan cancels an undesirable side effect
                   1423: (in the medical, not computational, sense of the term) of the special
                   1424: rule for self-referential macros.
                   1425: 
                   1426: There is also one case where prescan is useful.  It is possible
                   1427: to use prescan to expand an argument and then stringify it---if you use
                   1428: two levels of macros.  Let's add a new macro @samp{xstr} to the
                   1429: example shown above:
                   1430: 
                   1431: @example
                   1432: #define xstr(s) str(s)
                   1433: #define str(s) #s
                   1434: #define foo 4
                   1435: xstr (foo)
                   1436: @end example
                   1437: 
                   1438: This expands into @samp{"4"}, not @samp{"foo"}.  The reason for the
                   1439: difference is that the argument of @samp{xstr} is expanded at prescan
                   1440: (because @samp{xstr} does not specify stringification or concatenation of
                   1441: the argument).  The result of prescan then forms the actual argument for
                   1442: @samp{str}.  @samp{str} uses its argument without prescan because it
                   1443: performs strigification; but it cannot prevent or undo the prescanning
                   1444: already done by @samp{xstr}.
                   1445: 
                   1446: @node Cascaded Macros,, Argument Prescan, Macro Pitfalls
                   1447: @subsubsection Cascaded Use of Macros
                   1448: 
                   1449: @cindex cascaded macros
                   1450: @cindex macro body uses macro
                   1451: A @dfn{cascade} of macros is when one macro's body contains a reference
                   1452: to another macro.  This is very common practice.  For example,
                   1453: 
                   1454: @example
                   1455: #define BUFSIZE 1020
                   1456: #define TABLESIZE BUFSIZE
                   1457: @end example
                   1458: 
                   1459: This is not at all the same as defining @samp{TABLESIZE} to be @samp{1020}.
                   1460: The @samp{#define} for @samp{TABLESIZE} uses exactly the body you
                   1461: specify---in this case, @samp{BUFSIZE}---and does not check to see whether
                   1462: it too is the name of a macro.
                   1463: 
                   1464: It's only when you @emph{use} @samp{TABLESIZE} that the result of its expansion
                   1465: is checked for more macro names.
                   1466: 
                   1467: This makes a difference if you change the definition of @samp{BUFSIZE}
                   1468: at some point in the source file.  @samp{TABLESIZE}, defined as shown,
                   1469: will always expand using the definition of @samp{BUFSIZE} that is
                   1470: currently in effect:
                   1471: 
                   1472: @example
                   1473: #define BUFSIZE 1020
                   1474: #define TABLESIZE BUFSIZE
                   1475: #undef BUFSIZE
                   1476: #define BUFSIZE 37
                   1477: @end example
                   1478: 
                   1479: @noindent
                   1480: Now @samp{TABLESIZE} expands (in two stages) to @samp{37}.
                   1481: 
                   1482: @node Conditionals, Combining Sources, Macros, Top
                   1483: @section Conditionals
                   1484: 
                   1485: @cindex conditionals
                   1486: In a macro processor, a @dfn{conditional} is a command that allows a part
                   1487: of the program to be ignored during compilation, on some conditions.
                   1488: In the C preprocessor, a conditional can test either an arithmetic expression
                   1489: or whether a name is defined as a macro.
                   1490: 
                   1491: A conditional in the C preprocessor resembles in some ways an @samp{if}
                   1492: statement in C, but it is important to understand the difference between
                   1493: them.  The condition in an @samp{if} statement is tested during the execution
                   1494: of your program.  Its purpose is to allow your program to behave differently
                   1495: from run to run, depending on the data it is operating on.  The condition
                   1496: in a preprocessor conditional command is tested when your program is compiled.
                   1497: Its purpose is to allow different code to be included in the program depending
                   1498: on the situation at the time of compilation.
                   1499: 
                   1500: @menu
                   1501: * Uses: Conditional Uses.       What conditionals are for.
                   1502: * Syntax: Conditional Syntax.   How conditionals are written.
                   1503: * Deletion: Deleted Code.       Making code into a comment.
                   1504: * Macros: Conditionals-Macros.  Why conditionals are used with macros.
                   1505: * Errors: #error Command.       Detecting inconsistent compilation parameters.
                   1506: @end menu
                   1507: 
                   1508: @node Conditional Uses, Conditional Syntax, Conditionals, Conditionals
                   1509: Generally there are three kinds of reason to use a conditional.
                   1510: 
                   1511: @itemize @bullet
                   1512: @item
                   1513: A program may need to use different code depending on the machine or
                   1514: operating system it is to run on.  In some cases the code for one
                   1515: operating system may be erroneous on another operating system; for
                   1516: example, it might refer to library routines that do not exist on the
                   1517: other system.  When this happens, it is not enough to avoid executing
                   1518: the invalid code: merely having it in the program makes it impossible
                   1519: to link the program and run it.  With a preprocessor conditional, the
                   1520: offending code can be effectively excised from the program when it is
                   1521: not valid.
                   1522: 
                   1523: @item
                   1524: You may want to be able to compile the same source file into two
                   1525: different programs.  Sometimes the difference between the programs is
                   1526: that one makes frequent time-consuming consistency checks on its
                   1527: intermediate data while the other does not.
                   1528: 
                   1529: @item
                   1530: A conditional whose condition is always false is a good way to exclude
                   1531: code from the program but keep it as a sort of comment for future
                   1532: reference.
                   1533: @end itemize
                   1534: 
                   1535: Most simple programs that are intended to run on only one machine will
                   1536: not need to use preprocessor conditionals.
                   1537: 
                   1538: @node Conditional Syntax, Conditionals-Macros, Conditional Uses, Conditionals
                   1539: @subsection Syntax of Conditionals
                   1540: 
                   1541: @findex #if
                   1542: A conditional in the C preprocessor begins with a @dfn{conditional
                   1543: command}: @samp{#if}, @samp{#ifdef} or @samp{#ifndef}.@xref{Conditionals},
                   1544: for info on @samp{#ifdef} and @samp{#ifndef}; only @samp{#if} is explained
                   1545: here.
                   1546: 
                   1547: @menu
                   1548: * If: #if Command.     Basic conditionals using @samp{#if} and @samp{#endif}.
                   1549: * Else: #else Command. Including some text if the condition fails.
                   1550: * Elif: #elif Command. Testing several alternative possibilities.
                   1551: @end menu
                   1552: 
                   1553: @node #if Command, #else Command, Conditional Syntax, Conditional Syntax
                   1554: @subsubsection The @samp{#if} Command
                   1555: 
                   1556: The @samp{#if} command in its simplest form consists of
                   1557: 
                   1558: @example
                   1559: #if @var{expression}
                   1560: @var{controlled text}
                   1561: #endif /* @var{expression} */
                   1562: @end example
                   1563: 
                   1564: The comment following the @samp{#endif} is not required, but it is a good
                   1565: practice because it helps people match the @samp{#endif} to the
                   1566: corresponding @samp{#if}.  Such comments should always be used, except in
                   1567: short conditionals that are not nested.  In fact, you can put anything at
                   1568: all after the @samp{#endif} and it will be ignored by the GNU C preprocessor,
                   1569: but only comments are acceptable in ANSI Standard C.
                   1570: 
                   1571: @var{expression} is a C expression of integer type, subject to stringent
                   1572: restrictions.  It may contain
                   1573: 
                   1574: @itemize @bullet
                   1575: @item
                   1576: Integer constants, which are all regarded as @code{long} or
                   1577: @code{unsigned long}.
                   1578: 
                   1579: @item
                   1580: Character constants, which are interpreted according to the character
                   1581: set and conventions of the machine and operating system on which the
                   1582: preprocessor is running.  The GNU C preprocessor uses the C data type
                   1583: @samp{char} for these character constants; therefore, whether some
                   1584: character codes are negative is determined by the C compiler used to
                   1585: compile the preprocessor.  If it treats @samp{char} as signed, then
                   1586: character codes large enough to set the sign bit will be considered
                   1587: negative; otherwise, no character code is considered negative.
                   1588: 
                   1589: @item
                   1590: Arithmetic operators for addition, subtraction, multiplication,
                   1591: division, bitwise operations, shifts, comparisons, and @samp{&&} and
                   1592: @samp{||}.
                   1593: 
                   1594: @item
                   1595: Identifiers that are not macros, which are all treated as zero(!).
                   1596: 
                   1597: @item
                   1598: Macro calls.  All macro calls in the expression are expanded before
                   1599: actual computation of the expression's value begins.
                   1600: @end itemize
                   1601: 
                   1602: Note that @samp{sizeof} operators and @code{enum}-type values are not allowed.
                   1603: @code{enum}-type values, like all other identifiers that are not taken
                   1604: as macro calls and expanded, are treated as zero.
                   1605: 
                   1606: The text inside of a conditional can include preprocessor commands.  Then
                   1607: the commands inside the conditional are obeyed only if that branch of the
                   1608: conditional succeeds.  The text can also contain other conditional groups.
                   1609: However, the @samp{#if}'s and @samp{#endif}'s must balance.
                   1610: 
                   1611: @node #else Command, #elif Command, #if Command, Conditional Syntax
                   1612: @subsubsection The @samp{#else} Command
                   1613: 
                   1614: @findex #else
                   1615: The @samp{#else} command can be added a conditional to provide alternative
                   1616: text to be used if the condition is false.  This looks like
                   1617: 
                   1618: @example
                   1619: #if @var{expression}
                   1620: @var{text-if-true}
                   1621: #else /* Not @var{expression} */
                   1622: @var{text-if-false}
                   1623: #endif /* Not @var{expression} */
                   1624: @end example
                   1625: 
                   1626: If @var{expression} is nonzero, and the @var{text-if-true} is considered
                   1627: included, then @samp{#else} acts like a failing conditional and the
                   1628: @var{text-if-false} is ignored.  Contrariwise, if the @samp{#if}
                   1629: conditional fails, the @var{text-if-false} is considered included.
                   1630: 
                   1631: @node #elif Command,, #else Command, Conditional Syntax
                   1632: @subsubsection The @samp{#elif} Command
                   1633: 
                   1634: @findex #elif
                   1635: One common case of nested conditionals is used to check for more than two
                   1636: possible alternatives.  For example, you might have
                   1637: 
                   1638: @example
                   1639: #if X == 1
                   1640: @dots{}
                   1641: #else /* X != 1 */
                   1642: #if X == 2
                   1643: @dots{}
                   1644: #else /* X != 2 */
                   1645: @dots{}
                   1646: #endif /* X != 2 */
                   1647: #endif /* X != 1 */
                   1648: @end example
                   1649: 
                   1650: Another conditional command, @samp{#elif}, allows this to be abbreviated
                   1651: as follows:
                   1652: 
                   1653: @example
                   1654: #if X == 1
                   1655: @dots{}
                   1656: #elif X == 2
                   1657: @dots{}
                   1658: #else /* X != 2 and X != 1*/
                   1659: @dots{}
                   1660: #endif /* X != 2 and X != 1*/
                   1661: @end example
                   1662: 
                   1663: @samp{#elif} stands for ``else if''.  Like @samp{#else}, it goes in the
                   1664: middle of a @samp{#if}-@samp{#endif} pair and subdivides it; it does not
                   1665: require a matching @samp{#endif} of its own.  Like @samp{#if}, the
                   1666: @samp{#elif} command includes an expression to be tested.
                   1667: 
                   1668: The text following the @samp{#elif} is processed only if the original
                   1669: @samp{#if}-condition failed and the @samp{#elif} condition succeeeds.  More
                   1670: than one @samp{#elif} can go in the same @samp{#if}-@samp{#endif} group.
                   1671: Then the text after each @samp{#elif} is processed only if the @samp{#elif}
                   1672: condition succeeds after the original @samp{#if} and any previous
                   1673: @samp{#elif}'s within it have failed.  @samp{#else} is equivalent to
                   1674: @samp{#elif 1}, and @samp{#else} is allowed after any number of
                   1675: @samp{#elif}'s, but @samp{#elif} may not follow a @samp{#else}.
                   1676: 
                   1677: @node Deleted Code, Conditionals-Macros, Conditional Syntax, Conditionals
                   1678: @subsection Keeping Deleted Code for Future Reference
                   1679: 
                   1680: If you replace or delete a part of the program but want to keep the old
                   1681: code around as a comment for future reference, the easy way to do this is
                   1682: to put @samp{#if 0} before it and @samp{#endif} after it.
                   1683: 
                   1684: This works even if the code being turned off contains conditionals, but
                   1685: they must be entire conditionals (balanced @samp{#if} and @samp{#endif}).
                   1686: 
                   1687: @node Conditionals-Macros, #error Command, Deleted Code, Conditionals
                   1688: @subsection Conditionals and Macros
                   1689: 
                   1690: Conditionals are rarely useful except in connection with macros.  A
                   1691: @samp{#if} command whose expression uses no macros is equivalent to
                   1692: @samp{#if 1} or @samp{#if 0}; you might as well determine which one, by
                   1693: computing the value of the expression yourself, and then simplify the
                   1694: program.  But when the expression uses macros, its value can vary from
                   1695: compilation to compilation.
                   1696: 
                   1697: For example, here is a conditional that tests the expression
                   1698: @samp{BUFSIZE == 1020}, where @samp{BUFSIZE} must be a macro.
                   1699: 
                   1700: @example
                   1701: #if BUFSIZE == 1020
                   1702:   printf ("Large buffers!\n");
                   1703: #endif /* BUFSIZE is large */
                   1704: @end example
                   1705: 
                   1706: @findex defined
                   1707: The special operator @samp{defined} may be used in @samp{#if} expressions
                   1708: to test whether a certain name is defined as a macro.  Either @samp{defined
                   1709: @var{name}} or @samp{defined (@var{name})} is an expression whose value is
                   1710: 1 if @var{name} is defined as macro at the current point in the program,
                   1711: and 0 otherwise.  For the @samp{defined} operator it makes no difference
                   1712: what the definition of the macro is; all that matters is whether there is a
                   1713: definition.  Thus, for example,@refill
                   1714: 
                   1715: @example
                   1716: #if defined (vax) || defined (ns16000)
                   1717: @end example
                   1718: 
                   1719: @noindent
                   1720: would include the following code if either of the names @samp{vax} and
                   1721: @samp{ns16000} is defined as a macro.
                   1722: 
                   1723: If a macro is defined and later undefined with @samp{#undef},
                   1724: subsequent use of the @samp{defined} operator will return 0, because
                   1725: the name is no longer defined.  If the macro is defined again with
                   1726: another @samp{#define}, @samp{defined} will recommence returning 1.
                   1727: 
                   1728: @findex #ifdef
                   1729: @findex #ifndef
                   1730: Conditionals that test just the definedness of one name are very common, so
                   1731: there are two special short conditional commands for this case.  They are
                   1732: 
                   1733: @table @code
                   1734: @item #ifdef @var{name}
                   1735: is equivalent to @samp{#if defined (@var{name})}.
                   1736: 
                   1737: @item #ifndef @var{name}
                   1738: is equivalent to @samp{#if ! defined (@var{name})}.
                   1739: @end table
                   1740: 
                   1741: Macro definitions can vary between compilations for several reasons.
                   1742: 
                   1743: @itemize @bullet
                   1744: @item
                   1745: Some macros are predefined on each kind of machine.  For example, on a
                   1746: Vax, the name @samp{vax} is a predefined macro.  On other machines, it
                   1747: would not be defined.
                   1748: 
                   1749: @item
                   1750: Many more macros are defined by system header files.  Different
                   1751: systems and machines define different macros, or give them different
                   1752: values.  It is useful to test these macros with conditionals to avoid
                   1753: using a system feature on a machine where it is not implemented.
                   1754: 
                   1755: @item
                   1756: Macros are a common way of allowing users to customize a program for
                   1757: different machines or applications.  For example, the macro
                   1758: @samp{BUFSIZE} might be defined in a configuration file for your
                   1759: program that is included as a header file in each source file.  You
                   1760: would use @samp{BUFSIZE} in a preprocessor conditional in order to
                   1761: generate different code depending on the chosen configuration.
                   1762: 
                   1763: @item
                   1764: Macros can be defined or undefined with @samp{-D} and @samp{-U}
                   1765: command options when you compile the program.  You can arrange to
                   1766: compile the same source file into two different programs by choosing
                   1767: a macro name to specify which program you want, writing conditionals
                   1768: to test whether or how this macro is defined, and then controlling
                   1769: the state of the macro with compiler command options.
                   1770: @xref{Invocation}.
                   1771: @end itemize
                   1772: 
                   1773: @node #error Command,, Conditionals-Macros, Conditionals
                   1774: @subsection The @samp{#error} Command
                   1775: 
                   1776: @findex #error
                   1777: The command @samp{#error} causes the preprocessor to report a fatal
                   1778: error.  The rest of the line that follows @samp{#error} is used as the
                   1779: error message.
                   1780: 
                   1781: You would use @samp{#error} inside of a conditional that detects a
                   1782: combination of parameters which you know the program does not properly
                   1783: support.  For example, if you know that the program will not run
                   1784: properly on a Vax, you might write
                   1785: 
                   1786: @example
                   1787: #ifdef vax
                   1788: #error Won't work on Vaxen.  See comments at get_last_object.
                   1789: #endif
                   1790: @end example
                   1791: 
                   1792: @noindent
                   1793: @xref{Nonstandard Predefined}, for why this works.
                   1794: 
                   1795: If you have several configuration parameters that must be set up by
                   1796: the installation in a consistent way, you can use conditionals to detect
                   1797: an inconsistency and report it with @samp{#error}.  For example,
                   1798: 
                   1799: @example
                   1800: #if HASH_TABLE_SIZE % 2 == 0 || HASH_TABLE_SIZE % 3 == 0 \
                   1801:     || HASH_TABLE_SIZE % 5 == 0
                   1802: #error HASH_TABLE_SIZE should not be divisible by a small prime
                   1803: #endif
                   1804: @end example
                   1805: 
                   1806: @node Combining Sources, Other Commands, Conditionals, Top
                   1807: @section Combining Source Files
                   1808: 
                   1809: @cindex line control
                   1810: @findex #line
                   1811: One of the jobs of the C preprocessor is to inform the C compiler of where
                   1812: each line of C code came from: which source file and which line number.
                   1813: 
                   1814: C code can come from multiple source files if you use @samp{#include};
                   1815: both @samp{#include} and the use of conditionals and macros can cause
                   1816: the line number of a line in the preprocessor output to be different
                   1817: from the line's number in the original source file.  You will appreciate
                   1818: the value of making both the C compiler (in error messages) and symbolic
                   1819: debuggers such as GDB use the line numbers in your source file.
                   1820: 
                   1821: The C preprocessor builds on this feature by offering a command by which
                   1822: you can control the feature explicitly.  This is useful when a file for
                   1823: input to the C preprocessor is the output from another program such as the
                   1824: @code{bison} parser generator, which operates on another file that is the
                   1825: true source file.  Parts of the output from @code{bison} are generated from
                   1826: scratch, other parts come from a standard parser file.  The rest are copied
                   1827: nearly verbatim from the source file, but their line numbers in the
                   1828: @code{bison} output are not the same as their original line numbers.
                   1829: Naturally you would like compiler error messages and symbolic debuggers to
                   1830: know the original source file and line number of each line in the
                   1831: @code{bison} output.
                   1832: 
                   1833: @code{bison} arranges this by writing @samp{#line} commands into the output
                   1834: file.  @samp{#line} is a command that specifies the original line number
                   1835: and source file name for subsequent input in the current preprocessor input
                   1836: file.  @samp{#line} has three variants:
                   1837: 
                   1838: @table @code
                   1839: @item #line @var{linenum}
                   1840: Here @var{linenum} is a decimal integer constant.  This specifies that
                   1841: the line number of the following line of input, in its original source file,
                   1842: was @var{linenum}.
                   1843: 
                   1844: @item #line @var{linenum} @var{filename}
                   1845: Here @var{linenum} is a decimal integer constant and @var{filename}
                   1846: is a string constant.  This specifies that the following line of input
                   1847: came originally from source file @var{filename} and its line number there
                   1848: was @var{linenum}.  Keep in mind that @var{filename} is not just a
                   1849: file name; it is surrounded by doublequote characters so that it looks
                   1850: like a string constant.
                   1851: 
                   1852: @item #line @var{anything else}
                   1853: @var{anything else} is checked for macro calls, which are expanded.
                   1854: The result should be a decimal integer constant followed optionally
                   1855: by a string constant, as described above.
                   1856: @end table
                   1857: 
                   1858: @samp{#line} commands alter the results of the @samp{__FILE__} and
                   1859: @samp{__LINE__} predefined macros from that point on.  @xref{Standard
                   1860: Predefined}.
                   1861: 
                   1862: @node Other Commands, Output, Combining Sources, Top
                   1863: @section Miscellaneous Preprocessor Commands
                   1864: 
                   1865: @findex #pragma
                   1866: @cindex null command
                   1867: This section describes two additional preprocesor commands.  They are
                   1868: not very useful, but are mentioned for completeness.
                   1869: 
                   1870: The @dfn{null command} consists of a @samp{#} followed by a Newline, with
                   1871: only whitespace (including comments) in between.  A null command is
                   1872: understood as a preprocessor command but has no effect on the preprocessor
                   1873: output.  The primary significance of the existence of the null command is
                   1874: that an input line consisting of just a @samp{#} will produce no output,
                   1875: rather than a line of output containing just a @samp{#}.  Supposedly
                   1876: some old C programs contain such lines.
                   1877: 
                   1878: The @samp{#pragma} command is specified in the ANSI standard to have an
                   1879: arbitrary implementation-defined effect.  In the GNU C preprocessor,
                   1880: @samp{#pragma} first attempts to run the game @code{rogue}; if that fails,
                   1881: it tries to run the game @code{hack}; if that fails, it tries to run
                   1882: GNU Emacs displaying the Tower of Hanoi; if that fails, it reports a
                   1883: fatal error.  In any case, preprocessing does not continue.
                   1884: 
                   1885: @node Output, Invocation, Other Commands, Top
                   1886: @section C Preprocessor Output
                   1887: 
                   1888: @cindex output format
                   1889: The output from the C preprocessor looks much like the input, except
                   1890: that all preprocessor command lines have been replaced with blank lines
                   1891: and all comments with spaces.  Whitespace within a line is not altered;
                   1892: however, a space is inserted after the expansions of most macro calls.
                   1893: 
                   1894: Source file name and line number information is conveyed by lines of
                   1895: the form
                   1896: 
                   1897: @example
                   1898: # @var{linenum} @var{filename}
                   1899: @end example
                   1900: 
                   1901: @noindent
                   1902: which are inserted as needed into the middle of the input (but never within
                   1903: a string or character constant).  Such a line means that the following line
                   1904: originated in file @var{filename} at line @var{linenum}.
                   1905: 
                   1906: @node Invocation,, Output, Top
                   1907: @section Invoking the C Preprocessor
                   1908: 
                   1909: Most often when you use the C preprocessor you will not have to invoke it
                   1910: explicitly: the C compiler will do so automatically.  However, the
                   1911: preprocessor is sometimes useful individually.
                   1912: 
                   1913: The C preprocessor expects two file names as arguments, @var{infile} and
                   1914: @var{outfile}.  The preprocessor reads @var{infile} together with any other
                   1915: files it specifies with @samp{#include}.  All the output generated by the
                   1916: combined input files is written in @var{outfile}.
                   1917: 
                   1918: Either @var{infile} or @var{outfile} may be @samp{-}, which as @var{infile}
                   1919: means to read from standard input and as @var{outfile} means to write to
                   1920: standard output.  Also, if @var{outfile} or both file names are omitted,
                   1921: the standard output and standard input are used for the omitted file names.
                   1922: 
                   1923: @cindex options
                   1924: Here is a table of command options accepted by the C preprocessor.  Most
                   1925: of them can also be given when compiling a C program; they are passed along
                   1926: automatically to the preprocessor when it is invoked by the compiler.
                   1927: 
                   1928: @table @samp
                   1929: @item -P
                   1930: @findex -P
                   1931: Inhibit generation of @samp{#}-lines with line-number information in
                   1932: the output from the preprocessor (@pxref{Output}).  This might be
                   1933: useful when running the preprocessor on something that is not C code
                   1934: and will be sent to a program which might be confused by the
                   1935: @samp{#}-lines
                   1936: 
                   1937: @item -C
                   1938: @findex -C
                   1939: Do not discard comments: pass them through to the output file.
                   1940: Comments appearing in arguments of a macro call will be copied to the
                   1941: output before the expansion of the macro call.
                   1942: 
                   1943: @item -T
                   1944: @findex -T
                   1945: Process ANSI standard trigraph sequences.  These are three-character
                   1946: sequences, all starting with @samp{??}, that are defined by ANSI C to
                   1947: stand for single characters.  For example, @samp{??/} stands for
                   1948: @samp{\}, so @samp{'??/n'} is a character constant for Newline.
                   1949: Strictly speaking, the GNU C preprocessor does not support all
                   1950: programs in ANSI Standard C unless @samp{-T} is used, but if you
                   1951: ever notice the difference it will be with relief.
                   1952: 
                   1953: You don't want to know any more about trigraphs.
                   1954: 
                   1955: @item -pedantic
                   1956: @findex -pedantic
                   1957: Issue warnings required by the ANSI C standard in certain cases such
                   1958: as when text other than a comment follows @samp{#else} or @samp{#endif}.
                   1959: 
                   1960: @item -I @var{directory}
                   1961: @findex -I
                   1962: Add the directory @var{directory} to the end of the list of
                   1963: directories to be searched for header files (@pxref{Include Syntax}).
                   1964: This can be used to override a system header file, substituting your
                   1965: own version, since these directories are searched before the system
                   1966: header file directories.  If you use more than one @samp{-I} option,
                   1967: the directories are scanned in left-to-right order; the standard
                   1968: system directories come after.
                   1969: 
                   1970: @item -I-
                   1971: Any directories specified with @samp{-I} options before the @samp{-I-}
                   1972: option are searched only for the case of @samp{#include "@var{file}"};
                   1973: they are not searched for @samp{#include <@var{file}>}.
                   1974: 
                   1975: If additional directories are specified with @samp{-I} options after
                   1976: the @samp{-I-}, these directories are searched for all @samp{#include}
                   1977: directives.
                   1978: 
                   1979: In addition, the @samp{-I-} option inhibits the use of the current
                   1980: directory as the first search directory for @samp{#include "@var{file}"}.
                   1981: Therefore, the current directory is searched only if it is requested
                   1982: explicitly with @samp{-I.}.  Specifying both @samp{-I-} and @samp{-I.}
                   1983: allows you to control precisely which directories are searched before
                   1984: the current one and which are searched after.
                   1985: 
                   1986: @item -nostdinc
                   1987: Do not search the standard system directories for header files.
                   1988: Only the directories you have specified with @samp{-I} options
                   1989: (and the current directory, if appropriate) are searched.
                   1990: 
                   1991: @item -D @var{name}
                   1992: @findex -D
                   1993: Predefine @var{name} as a macro, with definition @samp{1}.
                   1994: 
                   1995: @item -D @var{name}=@var{definition}
                   1996: Predefine @var{name} as a macro, with definition @var{definition}.
                   1997: There are no restrictions on the contents of @var{definition}, but if
                   1998: you are invoking the preprocessor from a shell or shell-like program
                   1999: you may need to use the shell's quoting syntax to protect characters
                   2000: such as spaces that have a meaning in the shell syntax.
                   2001: 
                   2002: @item -U @var{name}
                   2003: @findex -U
                   2004: Do not predefine @var{name}.  If both @samp{-U} and @samp{-D} are
                   2005: specified for one name, the @samp{-U} beats the @samp{-D} and the name
                   2006: is not predefined.
                   2007: 
                   2008: @item -undef
                   2009: @findex -undef
                   2010: Do not predefine any nonstandard macros.
                   2011: 
                   2012: @item -d
                   2013: @findex -d
                   2014: Instead of outputting the result of preprocessing, output a list of
                   2015: @samp{#define} commands for all the macros defined during the
                   2016: execution of the preprocessor.
                   2017: 
                   2018: @item -M
                   2019: @findex -M
                   2020: Instead of outputting the result of preprocessing, output a rule
                   2021: suitable for @code{make} describing the dependencies of the main
                   2022: source file.  The preprocessor outputs one @code{make} rule containing
                   2023: the object file name for that source file, a colon, and the names of
                   2024: all the included files.  If there are many included files then the
                   2025: rule is split into several lines using @samp{\}-newline.
                   2026: 
                   2027: This feature is used in automatic updating of makefiles.
                   2028: 
                   2029: @item -MM
                   2030: @findex -MM
                   2031: Like @samp{-M} but mention only the files included with @samp{#include
                   2032: "@var{file}"}.  System header files included with @samp{#include
                   2033: <@var{file}>} are omitted.
                   2034: 
                   2035: @item -i @var{file}
                   2036: @findex -i
                   2037: Process @var{file} as input, discarding the resulting output, before
                   2038: processing the regular input file.  Because the output generated from
                   2039: @var{file} is discarded, the only effect of @samp{-i @var{file}} is to
                   2040: make the macros defined in @var{file} available for use in the main
                   2041: input.
                   2042: @end table
                   2043: 
                   2044: @node Concept Index, Index, Invocation, Top
                   2045: @unnumbered Concept Index
                   2046: @printindex cp
                   2047: 
                   2048: @node Index,, Concept Index, Top
                   2049: @unnumbered Index of Commands, Macros and Options
                   2050: @printindex fn
                   2051: 
                   2052: @contents
                   2053: @bye

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.