home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / gcc / cpp.info-1 (.txt) < prev    next >
GNU Info File  |  1995-06-15  |  50KB  |  919 lines

  1. This is Info file cpp.info, produced by Makeinfo-1.55 from the input
  2. file cpp.texi.
  3.    This file documents the GNU C Preprocessor.
  4.    Copyright 1987, 1989, 1991, 1992, 1993, 1994, 1995 Free Software
  5. Foundation, Inc.
  6.    Permission is granted to make and distribute verbatim copies of this
  7. manual provided the copyright notice and this permission notice are
  8. preserved on all copies.
  9.    Permission is granted to copy and distribute modified versions of
  10. this manual under the conditions for verbatim copying, provided also
  11. that the entire resulting derived work is distributed under the terms
  12. of a permission notice identical to this one.
  13.    Permission is granted to copy and distribute translations of this
  14. manual into another language, under the above conditions for modified
  15. versions.
  16. File: cpp.info,  Node: Top,  Next: Global Actions,  Up: (DIR)
  17. The C Preprocessor
  18. ******************
  19.    The C preprocessor is a "macro processor" that is used automatically
  20. by the C compiler to transform your program before actual compilation.
  21. It is called a macro processor because it allows you to define "macros",
  22. which are brief abbreviations for longer constructs.
  23.    The C preprocessor provides four separate facilities that you can
  24. use as you see fit:
  25.    * Inclusion of header files.  These are files of declarations that
  26.      can be substituted into your program.
  27.    * Macro expansion.  You can define "macros", which are abbreviations
  28.      for arbitrary fragments of C code, and then the C preprocessor will
  29.      replace the macros with their definitions throughout the program.
  30.    * Conditional compilation.  Using special preprocessing directives,
  31.      you can include or exclude parts of the program according to
  32.      various conditions.
  33.    * Line control.  If you use a program to combine or rearrange source
  34.      files into an intermediate file which is then compiled, you can
  35.      use line control to inform the compiler of where each source line
  36.      originally came from.
  37.    C preprocessors vary in some details.  This manual discusses the GNU
  38. C preprocessor, the C Compatible Compiler Preprocessor.  The GNU C
  39. preprocessor provides a superset of the features of ANSI Standard C.
  40.    ANSI Standard C requires the rejection of many harmless constructs
  41. commonly used by today's C programs.  Such incompatibility would be
  42. inconvenient for users, so the GNU C preprocessor is configured to
  43. accept these constructs by default.  Strictly speaking, to get ANSI
  44. Standard C, you must use the options `-trigraphs', `-undef' and
  45. `-pedantic', but in practice the consequences of having strict ANSI
  46. Standard C make it undesirable to do this.  *Note Invocation::.
  47. * Menu:
  48. * Global Actions::    Actions made uniformly on all input files.
  49. * Directives::        General syntax of preprocessing directives.
  50. * Header Files::      How and why to use header files.
  51. * Macros::            How and why to use macros.
  52. * Conditionals::      How and why to use conditionals.
  53. * Combining Sources:: Use of line control when you combine source files.
  54. * Other Directives::  Miscellaneous preprocessing directives.
  55. * Output::            Format of output from the C preprocessor.
  56. * Invocation::        How to invoke the preprocessor; command options.
  57. * Concept Index::     Index of concepts and terms.
  58. * Index::             Index of directives, predefined macros and options.
  59. File: cpp.info,  Node: Global Actions,  Next: Directives,  Prev: Top,  Up: Top
  60. Transformations Made Globally
  61. =============================
  62.    Most C preprocessor features are inactive unless you give specific
  63. directives to request their use.  (Preprocessing directives are lines
  64. starting with `#'; *note Directives::.).  But there are three
  65. transformations that the preprocessor always makes on all the input it
  66. receives, even in the absence of directives.
  67.    * All C comments are replaced with single spaces.
  68.    * Backslash-Newline sequences are deleted, no matter where.  This
  69.      feature allows you to break long lines for cosmetic purposes
  70.      without changing their meaning.
  71.    * Predefined macro names are replaced with their expansions (*note
  72.      Predefined::.).
  73.    The first two transformations are done *before* nearly all other
  74. parsing and before preprocessing directives are recognized.  Thus, for
  75. example, you can split a line cosmetically with Backslash-Newline
  76. anywhere (except when trigraphs are in use; see below).
  77.      /*
  78.      */ # /*
  79.      */ defi\
  80.      ne FO\
  81.      O 10\
  82.      20
  83. is equivalent into `#define FOO 1020'.  You can split even an escape
  84. sequence with Backslash-Newline.  For example, you can split `"foo\bar"'
  85. between the `\' and the `b' to get
  86.      "foo\\
  87.      bar"
  88. This behavior is unclean: in all other contexts, a Backslash can be
  89. inserted in a string constant as an ordinary character by writing a
  90. double Backslash, and this creates an exception.  But the ANSI C
  91. standard requires it.  (Strict ANSI C does not allow Newlines in string
  92. constants, so they do not consider this a problem.)
  93.    But there are a few exceptions to all three transformations.
  94.    * C comments and predefined macro names are not recognized inside a
  95.      `#include' directive in which the file name is delimited with `<'
  96.      and `>'.
  97.    * C comments and predefined macro names are never recognized within a
  98.      character or string constant.  (Strictly speaking, this is the
  99.      rule, not an exception, but it is worth noting here anyway.)
  100.    * Backslash-Newline may not safely be used within an ANSI "trigraph".
  101.      Trigraphs are converted before Backslash-Newline is deleted.  If
  102.      you write what looks like a trigraph with a Backslash-Newline
  103.      inside, the Backslash-Newline is deleted as usual, but it is then
  104.      too late to recognize the trigraph.
  105.      This exception is relevant only if you use the `-trigraphs' option
  106.      to enable trigraph processing.  *Note Invocation::.
  107. File: cpp.info,  Node: Directives,  Next: Header Files,  Prev: Global Actions,  Up: Top
  108. Preprocessing Directives
  109. ========================
  110.    Most preprocessor features are active only if you use preprocessing
  111. directives to request their use.
  112.    Preprocessing directives are lines in your program that start with
  113. `#'.  The `#' is followed by an identifier that is the "directive name".
  114. For example, `#define' is the directive that defines a macro.
  115. Whitespace is also allowed before and after the `#'.
  116.    The set of valid directive names is fixed.  Programs cannot define
  117. new preprocessing directives.
  118.    Some directive names require arguments; these make up the rest of
  119. the directive line and must be separated from the directive name by
  120. whitespace.  For example, `#define' must be followed by a macro name
  121. and the intended expansion of the macro.  *Note Simple Macros::.
  122.    A preprocessing directive cannot be more than one line in normal
  123. circumstances.  It may be split cosmetically with Backslash-Newline,
  124. but that has no effect on its meaning.  Comments containing Newlines
  125. can also divide the directive into multiple lines, but the comments are
  126. changed to Spaces before the directive is interpreted.  The only way a
  127. significant Newline can occur in a preprocessing directive is within a
  128. string constant or character constant.  Note that most C compilers that
  129. might be applied to the output from the preprocessor do not accept
  130. string or character constants containing Newlines.
  131.    The `#' and the directive name cannot come from a macro expansion.
  132. For example, if `foo' is defined as a macro expanding to `define', that
  133. does not make `#foo' a valid preprocessing directive.
  134. File: cpp.info,  Node: Header Files,  Next: Macros,  Prev: Directives,  Up: Top
  135. Header Files
  136. ============
  137.    A header file is a file containing C declarations and macro
  138. definitions (*note Macros::.) to be shared between several source
  139. files.  You request the use of a header file in your program with the C
  140. preprocessing directive `#include'.
  141. * Menu:
  142. * Header Uses::         What header files are used for.
  143. * Include Syntax::      How to write `#include' directives.
  144. * Include Operation::   What `#include' does.
  145. * Once-Only::        Preventing multiple inclusion of one header file.
  146. * Inheritance::         Including one header file in another header file.
  147. File: cpp.info,  Node: Header Uses,  Next: Include Syntax,  Prev: Header Files,  Up: Header Files
  148. Uses of Header Files
  149. --------------------
  150.    Header files serve two kinds of purposes.
  151.    * System header files declare the interfaces to parts of the
  152.      operating system.  You include them in your program to supply the
  153.      definitions and declarations you need to invoke system calls and
  154.      libraries.
  155.    * Your own header files contain declarations for interfaces between
  156.      the source files of your program.  Each time you have a group of
  157.      related declarations and macro definitions all or most of which
  158.      are needed in several different source files, it is a good idea to
  159.      create a header file for them.
  160.    Including a header file produces the same results in C compilation as
  161. copying the header file into each source file that needs it.  But such
  162. copying would be time-consuming and error-prone.  With a header file,
  163. the related declarations appear in only one place.  If they need to be
  164. changed, they can be changed in one place, and programs that include
  165. the header file will automatically use the new version when next
  166. recompiled.  The header file eliminates the labor of finding and
  167. changing all the copies as well as the risk that a failure to find one
  168. copy will result in inconsistencies within a program.
  169.    The usual convention is to give header files names that end with
  170. `.h'.  Avoid unusual characters in header file names, as they reduce
  171. portability.
  172. File: cpp.info,  Node: Include Syntax,  Next: Include Operation,  Prev: Header Uses,  Up: Header Files
  173. The `#include' Directive
  174. ------------------------
  175.    Both user and system header files are included using the
  176. preprocessing directive `#include'.  It has three variants:
  177. `#include <FILE>'
  178.      This variant is used for system header files.  It searches for a
  179.      file named FILE in a list of directories specified by you, then in
  180.      a standard list of system directories.  You specify directories to
  181.      search for header files with the command option `-I' (*note
  182.      Invocation::.).  The option `-nostdinc' inhibits searching the
  183.      standard system directories; in this case only the directories you
  184.      specify are searched.
  185.      The parsing of this form of `#include' is slightly special because
  186.      comments are not recognized within the `<...>'.  Thus, in
  187.      `#include <x/*y>' the `/*' does not start a comment and the
  188.      directive specifies inclusion of a system header file named
  189.      `x/*y'.  Of course, a header file with such a name is unlikely to
  190.      exist on Unix, where shell wildcard features would make it hard to
  191.      manipulate.
  192.      The argument FILE may not contain a `>' character.  It may,
  193.      however, contain a `<' character.
  194. `#include "FILE"'
  195.      This variant is used for header files of your own program.  It
  196.      searches for a file named FILE first in the current directory,
  197.      then in the same directories used for system header files.  The
  198.      current directory is the directory of the current input file.  It
  199.      is tried first because it is presumed to be the location of the
  200.      files that the current input file refers to.  (If the `-I-' option
  201.      is used, the special treatment of the current directory is
  202.      inhibited.)
  203.      The argument FILE may not contain `"' characters.  If backslashes
  204.      occur within FILE, they are considered ordinary text characters,
  205.      not escape characters.  None of the character escape sequences
  206.      appropriate to string constants in C are processed.  Thus,
  207.      `#include "x\n\\y"' specifies a filename containing three
  208.      backslashes.  It is not clear why this behavior is ever useful, but
  209.      the ANSI standard specifies it.
  210. `#include ANYTHING ELSE'
  211.      This variant is called a "computed #include".  Any `#include'
  212.      directive whose argument does not fit the above two forms is a
  213.      computed include.  The text ANYTHING ELSE is checked for macro
  214.      calls, which are expanded (*note Macros::.).  When this is done,
  215.      the result must fit one of the above two variants--in particular,
  216.      the expanded text must in the end be surrounded by either quotes
  217.      or angle braces.
  218.      This feature allows you to define a macro which controls the file
  219.      name to be used at a later point in the program.  One application
  220.      of this is to allow a site-specific configuration file for your
  221.      program to specify the names of the system include files to be
  222.      used.  This can help in porting the program to various operating
  223.      systems in which the necessary system header files are found in
  224.      different places.
  225. File: cpp.info,  Node: Include Operation,  Next: Once-Only,  Prev: Include Syntax,  Up: Header Files
  226. How `#include' Works
  227. --------------------
  228.    The `#include' directive works by directing the C preprocessor to
  229. scan the specified file as input before continuing with the rest of the
  230. current file.  The output from the preprocessor contains the output
  231. already generated, followed by the output resulting from the included
  232. file, followed by the output that comes from the text after the
  233. `#include' directive.  For example, given a header file `header.h' as
  234. follows,
  235.      char *test ();
  236. and a main program called `program.c' that uses the header file, like
  237. this,
  238.      int x;
  239.      #include "header.h"
  240.      
  241.      main ()
  242.      {
  243.        printf (test ());
  244.      }
  245. the output generated by the C preprocessor for `program.c' as input
  246. would be
  247.      int x;
  248.      char *test ();
  249.      
  250.      main ()
  251.      {
  252.        printf (test ());
  253.      }
  254.    Included files are not limited to declarations and macro
  255. definitions; those are merely the typical uses.  Any fragment of a C
  256. program can be included from another file.  The include file could even
  257. contain the beginning of a statement that is concluded in the
  258. containing file, or the end of a statement that was started in the
  259. including file.  However, a comment or a string or character constant
  260. may not start in the included file and finish in the including file.
  261. An unterminated comment, string constant or character constant in an
  262. included file is considered to end (with an error message) at the end
  263. of the file.
  264.    It is possible for a header file to begin or end a syntactic unit
  265. such as a function definition, but that would be very confusing, so
  266. don't do it.
  267.    The line following the `#include' directive is always treated as a
  268. separate line by the C preprocessor even if the included file lacks a
  269. final newline.
  270. File: cpp.info,  Node: Once-Only,  Next: Inheritance,  Prev: Include Operation,  Up: Header Files
  271. Once-Only Include Files
  272. -----------------------
  273.    Very often, one header file includes another.  It can easily result
  274. that a certain header file is included more than once.  This may lead
  275. to errors, if the header file defines structure types or typedefs, and
  276. is certainly wasteful.  Therefore, we often wish to prevent multiple
  277. inclusion of a header file.
  278.    The standard way to do this is to enclose the entire real contents
  279. of the file in a conditional, like this:
  280.      #ifndef FILE_FOO_SEEN
  281.      #define FILE_FOO_SEEN
  282.      
  283.      THE ENTIRE FILE
  284.      
  285.      #endif /* FILE_FOO_SEEN */
  286.    The macro `FILE_FOO_SEEN' indicates that the file has been included
  287. once already.  In a user header file, the macro name should not begin
  288. with `_'.  In a system header file, this name should begin with `__' to
  289. avoid conflicts with user programs.  In any kind of header file, the
  290. macro name should contain the name of the file and some additional
  291. text, to avoid conflicts with other header files.
  292.    The GNU C preprocessor is programmed to notice when a header file
  293. uses this particular construct and handle it efficiently.  If a header
  294. file is contained entirely in a `#ifndef' conditional, then it records
  295. that fact.  If a subsequent `#include' specifies the same file, and the
  296. macro in the `#ifndef' is already defined, then the file is entirely
  297. skipped, without even reading it.
  298.    There is also an explicit directive to tell the preprocessor that it
  299. need not include a file more than once.  This is called `#pragma once',
  300. and was used *in addition to* the `#ifndef' conditional around the
  301. contents of the header file.  `#pragma once' is now obsolete and should
  302. not be used at all.
  303.    In the Objective C language, there is a variant of `#include' called
  304. `#import' which includes a file, but does so at most once.  If you use
  305. `#import' *instead of* `#include', then you don't need the conditionals
  306. inside the header file to prevent multiple execution of the contents.
  307.    `#import' is obsolete because it is not a well designed feature.  It
  308. requires the users of a header file--the applications programmers--to
  309. know that a certain header file should only be included once.  It is
  310. much better for the header file's implementor to write the file so that
  311. users don't need to know this.  Using `#ifndef' accomplishes this goal.
  312. File: cpp.info,  Node: Inheritance,  Prev: Once-Only,  Up: Header Files
  313. Inheritance and Header Files
  314. ----------------------------
  315.    "Inheritance" is what happens when one object or file derives some
  316. of its contents by virtual copying from another object or file.  In the
  317. case of C header files, inheritance means that one header file includes
  318. another header file and then replaces or adds something.
  319.    If the inheriting header file and the base header file have different
  320. names, then inheritance is straightforward: simply write `#include
  321. "BASE"' in the inheriting file.
  322.    Sometimes it is necessary to give the inheriting file the same name
  323. as the base file.  This is less straightforward.
  324.    For example, suppose an application program uses the system header
  325. file `sys/signal.h', but the version of `/usr/include/sys/signal.h' on
  326. a particular system doesn't do what the application program expects.
  327. It might be convenient to define a "local" version, perhaps under the
  328. name `/usr/local/include/sys/signal.h', to override or add to the one
  329. supplied by the system.
  330.    You can do this by using the option `-I.' for compilation, and
  331. writing a file `sys/signal.h' that does what the application program
  332. expects.  But making this file include the standard `sys/signal.h' is
  333. not so easy--writing `#include <sys/signal.h>' in that file doesn't
  334. work, because it includes your own version of the file, not the
  335. standard system version.  Used in that file itself, this leads to an
  336. infinite recursion and a fatal error in compilation.
  337.    `#include </usr/include/sys/signal.h>' would find the proper file,
  338. but that is not clean, since it makes an assumption about where the
  339. system header file is found.  This is bad for maintenance, since it
  340. means that any change in where the system's header files are kept
  341. requires a change somewhere else.
  342.    The clean way to solve this problem is to use `#include_next', which
  343. means, "Include the *next* file with this name."  This directive works
  344. like `#include' except in searching for the specified file: it starts
  345. searching the list of header file directories *after* the directory in
  346. which the current file was found.
  347.    Suppose you specify `-I /usr/local/include', and the list of
  348. directories to search also includes `/usr/include'; and suppose that
  349. both directories contain a file named `sys/signal.h'.  Ordinary
  350. `#include <sys/signal.h>' finds the file under `/usr/local/include'.
  351. If that file contains `#include_next <sys/signal.h>', it starts
  352. searching after that directory, and finds the file in `/usr/include'.
  353. File: cpp.info,  Node: Macros,  Next: Conditionals,  Prev: Header Files,  Up: Top
  354. Macros
  355. ======
  356.    A macro is a sort of abbreviation which you can define once and then
  357. use later.  There are many complicated features associated with macros
  358. in the C preprocessor.
  359. * Menu:
  360. * Simple Macros::    Macros that always expand the same way.
  361. * Argument Macros::  Macros that accept arguments that are substituted
  362.                        into the macro expansion.
  363. * Predefined::       Predefined macros that are always available.
  364. * Stringification::  Macro arguments converted into string constants.
  365. * Concatenation::    Building tokens from parts taken from macro arguments.
  366. * Undefining::       Cancelling a macro's definition.
  367. * Redefining::       Changing a macro's definition.
  368. * Macro Pitfalls::   Macros can confuse the unwary.  Here we explain
  369.                        several common problems and strange features.
  370. File: cpp.info,  Node: Simple Macros,  Next: Argument Macros,  Prev: Macros,  Up: Macros
  371. Simple Macros
  372. -------------
  373.    A "simple macro" is a kind of abbreviation.  It is a name which
  374. stands for a fragment of code.  Some people refer to these as "manifest
  375. constants".
  376.    Before you can use a macro, you must "define" it explicitly with the
  377. `#define' directive.  `#define' is followed by the name of the macro
  378. and then the code it should be an abbreviation for.  For example,
  379.      #define BUFFER_SIZE 1020
  380. defines a macro named `BUFFER_SIZE' as an abbreviation for the text
  381. `1020'.  If somewhere after this `#define' directive there comes a C
  382. statement of the form
  383.      foo = (char *) xmalloc (BUFFER_SIZE);
  384. then the C preprocessor will recognize and "expand" the macro
  385. `BUFFER_SIZE', resulting in
  386.      foo = (char *) xmalloc (1020);
  387.    The use of all upper case for macro names is a standard convention.
  388. Programs are easier to read when it is possible to tell at a glance
  389. which names are macros.
  390.    Normally, a macro definition must be a single line, like all C
  391. preprocessing directives.  (You can split a long macro definition
  392. cosmetically with Backslash-Newline.)  There is one exception: Newlines
  393. can be included in the macro definition if within a string or character
  394. constant.  This is because it is not possible for a macro definition to
  395. contain an unbalanced quote character; the definition automatically
  396. extends to include the matching quote character that ends the string or
  397. character constant.  Comments within a macro definition may contain
  398. Newlines, which make no difference since the comments are entirely
  399. replaced with Spaces regardless of their contents.
  400.    Aside from the above, there is no restriction on what can go in a
  401. macro body.  Parentheses need not balance.  The body need not resemble
  402. valid C code.  (But if it does not, you may get error messages from the
  403. C compiler when you use the macro.)
  404.    The C preprocessor scans your program sequentially, so macro
  405. definitions take effect at the place you write them.  Therefore, the
  406. following input to the C preprocessor
  407.      foo = X;
  408.      #define X 4
  409.      bar = X;
  410. produces as output
  411.      foo = X;
  412.      
  413.      bar = 4;
  414.    After the preprocessor expands a macro name, the macro's definition
  415. body is appended to the front of the remaining input, and the check for
  416. macro calls continues.  Therefore, the macro body can contain calls to
  417. other macros.  For example, after
  418.      #define BUFSIZE 1020
  419.      #define TABLESIZE BUFSIZE
  420. the name `TABLESIZE' when used in the program would go through two
  421. stages of expansion, resulting ultimately in `1020'.
  422.    This is not at all the same as defining `TABLESIZE' to be `1020'.
  423. The `#define' for `TABLESIZE' uses exactly the body you specify--in
  424. this case, `BUFSIZE'--and does not check to see whether it too is the
  425. name of a macro.  It's only when you *use* `TABLESIZE' that the result
  426. of its expansion is checked for more macro names.  *Note Cascaded
  427. Macros::.
  428. File: cpp.info,  Node: Argument Macros,  Next: Predefined,  Prev: Simple Macros,  Up: Macros
  429. Macros with Arguments
  430. ---------------------
  431.    A simple macro always stands for exactly the same text, each time it
  432. is used.  Macros can be more flexible when they accept "arguments".
  433. Arguments are fragments of code that you supply each time the macro is
  434. used.  These fragments are included in the expansion of the macro
  435. according to the directions in the macro definition.  A macro that
  436. accepts arguments is called a "function-like macro" because the syntax
  437. for using it looks like a function call.
  438.    To define a macro that uses arguments, you write a `#define'
  439. directive with a list of "argument names" in parentheses after the name
  440. of the macro.  The argument names may be any valid C identifiers,
  441. separated by commas and optionally whitespace.  The open-parenthesis
  442. must follow the macro name immediately, with no space in between.
  443.    For example, here is a macro that computes the minimum of two numeric
  444. values, as it is defined in many C programs:
  445.      #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
  446. (This is not the best way to define a "minimum" macro in GNU C.  *Note
  447. Side Effects::, for more information.)
  448.    To use a macro that expects arguments, you write the name of the
  449. macro followed by a list of "actual arguments" in parentheses,
  450. separated by commas.  The number of actual arguments you give must
  451. match the number of arguments the macro expects.   Examples of use of
  452. the macro `min' include `min (1, 2)' and `min (x + 28, *p)'.
  453.    The expansion text of the macro depends on the arguments you use.
  454. Each of the argument names of the macro is replaced, throughout the
  455. macro definition, with the corresponding actual argument.  Using the
  456. same macro `min' defined above, `min (1, 2)' expands into
  457.      ((1) < (2) ? (1) : (2))
  458. where `1' has been substituted for `X' and `2' for `Y'.
  459.    Likewise, `min (x + 28, *p)' expands into
  460.      ((x + 28) < (*p) ? (x + 28) : (*p))
  461.    Parentheses in the actual arguments must balance; a comma within
  462. parentheses does not end an argument.  However, there is no requirement
  463. for brackets or braces to balance, and they do not prevent a comma from
  464. separating arguments.  Thus,
  465.      macro (array[x = y, x + 1])
  466. passes two arguments to `macro': `array[x = y' and `x + 1]'.  If you
  467. want to supply `array[x = y, x + 1]' as an argument, you must write it
  468. as `array[(x = y, x + 1)]', which is equivalent C code.
  469.    After the actual arguments are substituted into the macro body, the
  470. entire result is appended to the front of the remaining input, and the
  471. check for macro calls continues.  Therefore, the actual arguments can
  472. contain calls to other macros, either with or without arguments, or
  473. even to the same macro.  The macro body can also contain calls to other
  474. macros.  For example, `min (min (a, b), c)' expands into this text:
  475.      ((((a) < (b) ? (a) : (b))) < (c)
  476.       ? (((a) < (b) ? (a) : (b)))
  477.       : (c))
  478. (Line breaks shown here for clarity would not actually be generated.)
  479.    If a macro `foo' takes one argument, and you want to supply an empty
  480. argument, you must write at least some whitespace between the
  481. parentheses, like this: `foo ( )'.  Just `foo ()' is providing no
  482. arguments, which is an error if `foo' expects an argument.  But `foo0
  483. ()' is the correct way to call a macro defined to take zero arguments,
  484. like this:
  485.      #define foo0() ...
  486.    If you use the macro name followed by something other than an
  487. open-parenthesis (after ignoring any spaces, tabs and comments that
  488. follow), it is not a call to the macro, and the preprocessor does not
  489. change what you have written.  Therefore, it is possible for the same
  490. name to be a variable or function in your program as well as a macro,
  491. and you can choose in each instance whether to refer to the macro (if
  492. an actual argument list follows) or the variable or function (if an
  493. argument list does not follow).
  494.    Such dual use of one name could be confusing and should be avoided
  495. except when the two meanings are effectively synonymous: that is, when
  496. the name is both a macro and a function and the two have similar
  497. effects.  You can think of the name simply as a function; use of the
  498. name for purposes other than calling it (such as, to take the address)
  499. will refer to the function, while calls will expand the macro and
  500. generate better but equivalent code.  For example, you can use a
  501. function named `min' in the same source file that defines the macro.
  502. If you write `&min' with no argument list, you refer to the function.
  503. If you write `min (x, bb)', with an argument list, the macro is
  504. expanded.  If you write `(min) (a, bb)', where the name `min' is not
  505. followed by an open-parenthesis, the macro is not expanded, so you wind
  506. up with a call to the function `min'.
  507.    You may not define the same name as both a simple macro and a macro
  508. with arguments.
  509.    In the definition of a macro with arguments, the list of argument
  510. names must follow the macro name immediately with no space in between.
  511. If there is a space after the macro name, the macro is defined as
  512. taking no arguments, and all the rest of the line is taken to be the
  513. expansion.  The reason for this is that it is often useful to define a
  514. macro that takes no arguments and whose definition begins with an
  515. identifier in parentheses.  This rule about spaces makes it possible
  516. for you to do either this:
  517.      #define FOO(x) - 1 / (x)
  518. (which defines `FOO' to take an argument and expand into minus the
  519. reciprocal of that argument) or this:
  520.      #define BAR (x) - 1 / (x)
  521. (which defines `BAR' to take no argument and always expand into `(x) -
  522. 1 / (x)').
  523.    Note that the *uses* of a macro with arguments can have spaces before
  524. the left parenthesis; it's the *definition* where it matters whether
  525. there is a space.
  526. File: cpp.info,  Node: Predefined,  Next: Stringification,  Prev: Argument Macros,  Up: Macros
  527. Predefined Macros
  528. -----------------
  529.    Several simple macros are predefined.  You can use them without
  530. giving definitions for them.  They fall into two classes: standard
  531. macros and system-specific macros.
  532. * Menu:
  533. * Standard Predefined::     Standard predefined macros.
  534. * Nonstandard Predefined::  Nonstandard predefined macros.
  535. File: cpp.info,  Node: Standard Predefined,  Next: Nonstandard Predefined,  Prev: Predefined,  Up: Predefined
  536. Standard Predefined Macros
  537. ..........................
  538.    The standard predefined macros are available with the same meanings
  539. regardless of the machine or operating system on which you are using
  540. GNU C.  Their names all start and end with double underscores.  Those
  541. preceding `__GNUC__' in this table are standardized by ANSI C; the rest
  542. are GNU C extensions.
  543. `__FILE__'
  544.      This macro expands to the name of the current input file, in the
  545.      form of a C string constant.  The precise name returned is the one
  546.      that was specified in `#include' or as the input file name
  547.      argument.
  548. `__LINE__'
  549.      This macro expands to the current input line number, in the form
  550.      of a decimal integer constant.  While we call it a predefined
  551.      macro, it's a pretty strange macro, since its "definition" changes
  552.      with each new line of source code.
  553.      This and `__FILE__' are useful in generating an error message to
  554.      report an inconsistency detected by the program; the message can
  555.      state the source line at which the inconsistency was detected.
  556.      For example,
  557.           fprintf (stderr, "Internal error: "
  558.                            "negative string length "
  559.                            "%d at %s, line %d.",
  560.                    length, __FILE__, __LINE__);
  561.      A `#include' directive changes the expansions of `__FILE__' and
  562.      `__LINE__' to correspond to the included file.  At the end of that
  563.      file, when processing resumes on the input file that contained the
  564.      `#include' directive, the expansions of `__FILE__' and `__LINE__'
  565.      revert to the values they had before the `#include' (but
  566.      `__LINE__' is then incremented by one as processing moves to the
  567.      line after the `#include').
  568.      The expansions of both `__FILE__' and `__LINE__' are altered if a
  569.      `#line' directive is used.  *Note Combining Sources::.
  570. `__DATE__'
  571.      This macro expands to a string constant that describes the date on
  572.      which the preprocessor is being run.  The string constant contains
  573.      eleven characters and looks like `"Jan 29 1987"' or `"Apr 1 1905"'.
  574. `__TIME__'
  575.      This macro expands to a string constant that describes the time at
  576.      which the preprocessor is being run.  The string constant contains
  577.      eight characters and looks like `"23:59:01"'.
  578. `__STDC__'
  579.      This macro expands to the constant 1, to signify that this is ANSI
  580.      Standard C.  (Whether that is actually true depends on what C
  581.      compiler will operate on the output from the preprocessor.)
  582. `__STDC_VERSION__'
  583.      This macro expands to the C Standard's version number, a long
  584.      integer constant of the form `YYYYMML' where YYYY and MM are the
  585.      year and month of the Standard version.  This signifies which
  586.      version of the C Standard the preprocessor conforms to.  Like
  587.      `__STDC__', whether this version number is accurate for the entire
  588.      implementation depends on what C compiler will operate on the
  589.      output from the preprocessor.
  590. `__GNUC__'
  591.      This macro is defined if and only if this is GNU C.  This macro is
  592.      defined only when the entire GNU C compiler is in use; if you
  593.      invoke the preprocessor directly, `__GNUC__' is undefined.  The
  594.      value identifies the major version number of GNU CC (`1' for GNU CC
  595.      version 1, which is now obsolete, and `2' for version 2).
  596. `__GNUC_MINOR__'
  597.      The macro contains the minor version number of the compiler.  This
  598.      can be used to work around differences between different releases
  599.      of the compiler (for example, if gcc 2.6.3 is known to support a
  600.      feature, you can test for `__GNUC__ > 2 || (__GNUC__ == 2 &&
  601.      __GNUC_MINOR__ >= 6)').  The last number, `3' in the example
  602.      above, denotes the bugfix level of the compiler; no macro contains
  603.      this value.
  604. `__GNUG__'
  605.      The GNU C compiler defines this when the compilation language is
  606.      C++; use `__GNUG__' to distinguish between GNU C and GNU C++.
  607. `__cplusplus'
  608.      The draft ANSI standard for C++ used to require predefining this
  609.      variable.  Though it is no longer required, GNU C++ continues to
  610.      define it, as do other popular C++ compilers.  You can use
  611.      `__cplusplus' to test whether a header is compiled by a C compiler
  612.      or a C++ compiler.
  613. `__STRICT_ANSI__'
  614.      This macro is defined if and only if the `-ansi' switch was
  615.      specified when GNU C was invoked.  Its definition is the null
  616.      string.  This macro exists primarily to direct certain GNU header
  617.      files not to define certain traditional Unix constructs which are
  618.      incompatible with ANSI C.
  619. `__BASE_FILE__'
  620.      This macro expands to the name of the main input file, in the form
  621.      of a C string constant.  This is the source file that was specified
  622.      as an argument when the C compiler was invoked.
  623. `__INCLUDE_LEVEL__'
  624.      This macro expands to a decimal integer constant that represents
  625.      the depth of nesting in include files.  The value of this macro is
  626.      incremented on every `#include' directive and decremented at every
  627.      end of file.  For input files specified by command line arguments,
  628.      the nesting level is zero.
  629. `__VERSION__'
  630.      This macro expands to a string which describes the version number
  631.      of GNU C.  The string is normally a sequence of decimal numbers
  632.      separated by periods, such as `"2.6.0"'.  The only reasonable use
  633.      of this macro is to incorporate it into a string constant.
  634. `__OPTIMIZE__'
  635.      This macro is defined in optimizing compilations.  It causes
  636.      certain GNU header files to define alternative macro definitions
  637.      for some system library functions.  It is unwise to refer to or
  638.      test the definition of this macro unless you make very sure that
  639.      programs will execute with the same effect regardless.
  640. `__CHAR_UNSIGNED__'
  641.      This macro is defined if and only if the data type `char' is
  642.      unsigned on the target machine.  It exists to cause the standard
  643.      header file `limit.h' to work correctly.  It is bad practice to
  644.      refer to this macro yourself; instead, refer to the standard
  645.      macros defined in `limit.h'.  The preprocessor uses this macro to
  646.      determine whether or not to sign-extend large character constants
  647.      written in octal; see *Note The `#if' Directive: #if Directive.
  648. `__REGISTER_PREFIX__'
  649.      This macro expands to a string describing the prefix applied to cpu
  650.      registers in assembler code.  It can be used to write assembler
  651.      code that is usable in multiple environments.  For example, in the
  652.      `m68k-aout' environment it expands to the string `""', but in the
  653.      `m68k-coff' environment it expands to the string `"%"'.
  654. `__USER_LABEL_PREFIX__'
  655.      This macro expands to a string describing the prefix applied to
  656.      user generated labels in assembler code.  It can be used to write
  657.      assembler code that is usable in multiple environments.  For
  658.      example, in the `m68k-aout' environment it expands to the string
  659.      `"_"', but in the `m68k-coff' environment it expands to the string
  660.      `""'.
  661. File: cpp.info,  Node: Nonstandard Predefined,  Prev: Standard Predefined,  Up: Predefined
  662. Nonstandard Predefined Macros
  663. .............................
  664.    The C preprocessor normally has several predefined macros that vary
  665. between machines because their purpose is to indicate what type of
  666. system and machine is in use.  This manual, being for all systems and
  667. machines, cannot tell you exactly what their names are; instead, we
  668. offer a list of some typical ones.  You can use `cpp -dM' to see the
  669. values of predefined macros; see *Note Invocation::.
  670.    Some nonstandard predefined macros describe the operating system in
  671. use, with more or less specificity.  For example,
  672. `unix'
  673.      `unix' is normally predefined on all Unix systems.
  674. `BSD'
  675.      `BSD' is predefined on recent versions of Berkeley Unix (perhaps
  676.      only in version 4.3).
  677.    Other nonstandard predefined macros describe the kind of CPU, with
  678. more or less specificity.  For example,
  679. `vax'
  680.      `vax' is predefined on Vax computers.
  681. `mc68000'
  682.      `mc68000' is predefined on most computers whose CPU is a Motorola
  683.      68000, 68010 or 68020.
  684. `m68k'
  685.      `m68k' is also predefined on most computers whose CPU is a 68000,
  686.      68010 or 68020; however, some makers use `mc68000' and some use
  687.      `m68k'.  Some predefine both names.  What happens in GNU C depends
  688.      on the system you are using it on.
  689. `M68020'
  690.      `M68020' has been observed to be predefined on some systems that
  691.      use 68020 CPUs--in addition to `mc68000' and `m68k', which are
  692.      less specific.
  693. `_AM29K'
  694. `_AM29000'
  695.      Both `_AM29K' and `_AM29000' are predefined for the AMD 29000 CPU
  696.      family.
  697. `ns32000'
  698.      `ns32000' is predefined on computers which use the National
  699.      Semiconductor 32000 series CPU.
  700.    Yet other nonstandard predefined macros describe the manufacturer of
  701. the system.  For example,
  702. `sun'
  703.      `sun' is predefined on all models of Sun computers.
  704. `pyr'
  705.      `pyr' is predefined on all models of Pyramid computers.
  706. `sequent'
  707.      `sequent' is predefined on all models of Sequent computers.
  708.    These predefined symbols are not only nonstandard, they are contrary
  709. to the ANSI standard because their names do not start with underscores.
  710. Therefore, the option `-ansi' inhibits the definition of these symbols.
  711.    This tends to make `-ansi' useless, since many programs depend on the
  712. customary nonstandard predefined symbols.  Even system header files
  713. check them and will generate incorrect declarations if they do not find
  714. the names that are expected.  You might think that the header files
  715. supplied for the Uglix computer would not need to test what machine
  716. they are running on, because they can simply assume it is the Uglix;
  717. but often they do, and they do so using the customary names.  As a
  718. result, very few C programs will compile with `-ansi'.  We intend to
  719. avoid such problems on the GNU system.
  720.    What, then, should you do in an ANSI C program to test the type of
  721. machine it will run on?
  722.    GNU C offers a parallel series of symbols for this purpose, whose
  723. names are made from the customary ones by adding `__' at the beginning
  724. and end.  Thus, the symbol `__vax__' would be available on a Vax, and
  725. so on.
  726.    The set of nonstandard predefined names in the GNU C preprocessor is
  727. controlled (when `cpp' is itself compiled) by the macro
  728. `CPP_PREDEFINES', which should be a string containing `-D' options,
  729. separated by spaces.  For example, on the Sun 3, we use the following
  730. definition:
  731.      #define CPP_PREDEFINES "-Dmc68000 -Dsun -Dunix -Dm68k"
  732. This macro is usually specified in `tm.h'.
  733. File: cpp.info,  Node: Stringification,  Next: Concatenation,  Prev: Predefined,  Up: Macros
  734. Stringification
  735. ---------------
  736.    "Stringification" means turning a code fragment into a string
  737. constant whose contents are the text for the code fragment.  For
  738. example, stringifying `foo (z)' results in `"foo (z)"'.
  739.    In the C preprocessor, stringification is an option available when
  740. macro arguments are substituted into the macro definition.  In the body
  741. of the definition, when an argument name appears, the character `#'
  742. before the name specifies stringification of the corresponding actual
  743. argument when it is substituted at that point in the definition.  The
  744. same argument may be substituted in other places in the definition
  745. without stringification if the argument name appears in those places
  746. with no `#'.
  747.    Here is an example of a macro definition that uses stringification:
  748.      #define WARN_IF(EXP) \
  749.      do { if (EXP) \
  750.              fprintf (stderr, "Warning: " #EXP "\n"); } \
  751.      while (0)
  752. Here the actual argument for `EXP' is substituted once as given, into
  753. the `if' statement, and once as stringified, into the argument to
  754. `fprintf'.  The `do' and `while (0)' are a kludge to make it possible
  755. to write `WARN_IF (ARG);', which the resemblance of `WARN_IF' to a
  756. function would make C programmers want to do; see *Note Swallow
  757. Semicolon::.
  758.    The stringification feature is limited to transforming one macro
  759. argument into one string constant: there is no way to combine the
  760. argument with other text and then stringify it all together.  But the
  761. example above shows how an equivalent result can be obtained in ANSI
  762. Standard C using the feature that adjacent string constants are
  763. concatenated as one string constant.  The preprocessor stringifies the
  764. actual value of `EXP' into a separate string constant, resulting in
  765. text like
  766.      do { if (x == 0) \
  767.              fprintf (stderr, "Warning: " "x == 0" "\n"); } \
  768.      while (0)
  769. but the C compiler then sees three consecutive string constants and
  770. concatenates them into one, producing effectively
  771.      do { if (x == 0) \
  772.              fprintf (stderr, "Warning: x == 0\n"); } \
  773.      while (0)
  774.    Stringification in C involves more than putting doublequote
  775. characters around the fragment; it is necessary to put backslashes in
  776. front of all doublequote characters, and all backslashes in string and
  777. character constants, in order to get a valid C string constant with the
  778. proper contents.  Thus, stringifying `p = "foo\n";' results in `"p =
  779. \"foo\\n\";"'.  However, backslashes that are not inside of string or
  780. character constants are not duplicated: `\n' by itself stringifies to
  781. `"\n"'.
  782.    Whitespace (including comments) in the text being stringified is
  783. handled according to precise rules.  All leading and trailing
  784. whitespace is ignored.  Any sequence of whitespace in the middle of the
  785. text is converted to a single space in the stringified result.
  786. File: cpp.info,  Node: Concatenation,  Next: Undefining,  Prev: Stringification,  Up: Macros
  787. Concatenation
  788. -------------
  789.    "Concatenation" means joining two strings into one.  In the context
  790. of macro expansion, concatenation refers to joining two lexical units
  791. into one longer one.  Specifically, an actual argument to the macro can
  792. be concatenated with another actual argument or with fixed text to
  793. produce a longer name.  The longer name might be the name of a function,
  794. variable or type, or a C keyword; it might even be the name of another
  795. macro, in which case it will be expanded.
  796.    When you define a macro, you request concatenation with the special
  797. operator `##' in the macro body.  When the macro is called, after
  798. actual arguments are substituted, all `##' operators are deleted, and
  799. so is any whitespace next to them (including whitespace that was part
  800. of an actual argument).  The result is to concatenate the syntactic
  801. tokens on either side of the `##'.
  802.    Consider a C program that interprets named commands.  There probably
  803. needs to be a table of commands, perhaps an array of structures
  804. declared as follows:
  805.      struct command
  806.      {
  807.        char *name;
  808.        void (*function) ();
  809.      };
  810.      
  811.      struct command commands[] =
  812.      {
  813.        { "quit", quit_command},
  814.        { "help", help_command},
  815.        ...
  816.      };
  817.    It would be cleaner not to have to give each command name twice,
  818. once in the string constant and once in the function name.  A macro
  819. which takes the name of a command as an argument can make this
  820. unnecessary.  The string constant can be created with stringification,
  821. and the function name by concatenating the argument with `_command'.
  822. Here is how it is done:
  823.      #define COMMAND(NAME)  { #NAME, NAME ## _command }
  824.      
  825.      struct command commands[] =
  826.      {
  827.        COMMAND (quit),
  828.        COMMAND (help),
  829.        ...
  830.      };
  831.    The usual case of concatenation is concatenating two names (or a
  832. name and a number) into a longer name.  But this isn't the only valid
  833. case.  It is also possible to concatenate two numbers (or a number and
  834. a name, such as `1.5' and `e3') into a number.  Also, multi-character
  835. operators such as `+=' can be formed by concatenation.  In some cases
  836. it is even possible to piece together a string constant.  However, two
  837. pieces of text that don't together form a valid lexical unit cannot be
  838. concatenated.  For example, concatenation with `x' on one side and `+'
  839. on the other is not meaningful because those two characters can't fit
  840. together in any lexical unit of C.  The ANSI standard says that such
  841. attempts at concatenation are undefined, but in the GNU C preprocessor
  842. it is well defined: it puts the `x' and `+' side by side with no
  843. particular special results.
  844.    Keep in mind that the C preprocessor converts comments to whitespace
  845. before macros are even considered.  Therefore, you cannot create a
  846. comment by concatenating `/' and `*': the `/*' sequence that starts a
  847. comment is not a lexical unit, but rather the beginning of a "long"
  848. space character.  Also, you can freely use comments next to a `##' in a
  849. macro definition, or in actual arguments that will be concatenated,
  850. because the comments will be converted to spaces at first sight, and
  851. concatenation will later discard the spaces.
  852. File: cpp.info,  Node: Undefining,  Next: Redefining,  Prev: Concatenation,  Up: Macros
  853. Undefining Macros
  854. -----------------
  855.    To "undefine" a macro means to cancel its definition.  This is done
  856. with the `#undef' directive.  `#undef' is followed by the macro name to
  857. be undefined.
  858.    Like definition, undefinition occurs at a specific point in the
  859. source file, and it applies starting from that point.  The name ceases
  860. to be a macro name, and from that point on it is treated by the
  861. preprocessor as if it had never been a macro name.
  862.    For example,
  863.      #define FOO 4
  864.      x = FOO;
  865.      #undef FOO
  866.      x = FOO;
  867. expands into
  868.      x = 4;
  869.      
  870.      x = FOO;
  871. In this example, `FOO' had better be a variable or function as well as
  872. (temporarily) a macro, in order for the result of the expansion to be
  873. valid C code.
  874.    The same form of `#undef' directive will cancel definitions with
  875. arguments or definitions that don't expect arguments.  The `#undef'
  876. directive has no effect when used on a name not currently defined as a
  877. macro.
  878. File: cpp.info,  Node: Redefining,  Next: Macro Pitfalls,  Prev: Undefining,  Up: Macros
  879. Redefining Macros
  880. -----------------
  881.    "Redefining" a macro means defining (with `#define') a name that is
  882. already defined as a macro.
  883.    A redefinition is trivial if the new definition is transparently
  884. identical to the old one.  You probably wouldn't deliberately write a
  885. trivial redefinition, but they can happen automatically when a header
  886. file is included more than once (*note Header Files::.), so they are
  887. accepted silently and without effect.
  888.    Nontrivial redefinition is considered likely to be an error, so it
  889. provokes a warning message from the preprocessor.  However, sometimes it
  890. is useful to change the definition of a macro in mid-compilation.  You
  891. can inhibit the warning by undefining the macro with `#undef' before the
  892. second definition.
  893.    In order for a redefinition to be trivial, the new definition must
  894. exactly match the one already in effect, with two possible exceptions:
  895.    * Whitespace may be added or deleted at the beginning or the end.
  896.    * Whitespace may be changed in the middle (but not inside strings).
  897.      However, it may not be eliminated entirely, and it may not be added
  898.      where there was no whitespace at all.
  899.    Recall that a comment counts as whitespace.
  900. File: cpp.info,  Node: Macro Pitfalls,  Prev: Redefining,  Up: Macros
  901. Pitfalls and Subtleties of Macros
  902. ---------------------------------
  903.    In this section we describe some special rules that apply to macros
  904. and macro expansion, and point out certain cases in which the rules have
  905. counterintuitive consequences that you must watch out for.
  906. * Menu:
  907. * Misnesting::        Macros can contain unmatched parentheses.
  908. * Macro Parentheses:: Why apparently superfluous parentheses
  909.                          may be necessary to avoid incorrect grouping.
  910. * Swallow Semicolon:: Macros that look like functions
  911.                          but expand into compound statements.
  912. * Side Effects::      Unsafe macros that cause trouble when
  913.                          arguments contain side effects.
  914. * Self-Reference::    Macros whose definitions use the macros' own names.
  915. * Argument Prescan::  Actual arguments are checked for macro calls
  916.                          before they are substituted.
  917. * Cascaded Macros::   Macros whose definitions use other macros.
  918. * Newlines in Args::  Sometimes line numbers get confused.
  919.