home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 6 / FreshFish_September1994.bin / bbs / gnu / gcc-2.6.0-src.lha / GNU / src / amiga / gcc-2.6.0 / cpp.info-2 < prev    next >
Encoding:
GNU Info File  |  1994-07-14  |  38.6 KB  |  998 lines

  1. This is Info file cpp.info, produced by Makeinfo-1.54 from the input
  2. file cpp.texi.
  3.  
  4.    This file documents the GNU C Preprocessor.
  5.  
  6.    Copyright 1987, 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
  7.  
  8.    Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.  
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided also
  14. that the entire resulting derived work is distributed under the terms
  15. of a permission notice identical to this one.
  16.  
  17.    Permission is granted to copy and distribute translations of this
  18. manual into another language, under the above conditions for modified
  19. versions.
  20.  
  21. 
  22. File: cpp.info,  Node: Macro Parentheses,  Next: Swallow Semicolon,  Prev: Misnesting,  Up: Macro Pitfalls
  23.  
  24. Unintended Grouping of Arithmetic
  25. .................................
  26.  
  27.    You may have noticed that in most of the macro definition examples
  28. shown above, each occurrence of a macro argument name had parentheses
  29. around it.  In addition, another pair of parentheses usually surround
  30. the entire macro definition.  Here is why it is best to write macros
  31. that way.
  32.  
  33.    Suppose you define a macro as follows,
  34.  
  35.      #define ceil_div(x, y) (x + y - 1) / y
  36.  
  37. whose purpose is to divide, rounding up.  (One use for this operation is
  38. to compute how many `int' objects are needed to hold a certain number
  39. of `char' objects.)  Then suppose it is used as follows:
  40.  
  41.      a = ceil_div (b & c, sizeof (int));
  42.  
  43. This expands into
  44.  
  45.      a = (b & c + sizeof (int) - 1) / sizeof (int);
  46.  
  47. which does not do what is intended.  The operator-precedence rules of C
  48. make it equivalent to this:
  49.  
  50.      a = (b & (c + sizeof (int) - 1)) / sizeof (int);
  51.  
  52. But what we want is this:
  53.  
  54.      a = ((b & c) + sizeof (int) - 1)) / sizeof (int);
  55.  
  56. Defining the macro as
  57.  
  58.      #define ceil_div(x, y) ((x) + (y) - 1) / (y)
  59.  
  60. provides the desired result.
  61.  
  62.    However, unintended grouping can result in another way.  Consider
  63. `sizeof ceil_div(1, 2)'.  That has the appearance of a C expression
  64. that would compute the size of the type of `ceil_div (1, 2)', but in
  65. fact it means something very different.  Here is what it expands to:
  66.  
  67.      sizeof ((1) + (2) - 1) / (2)
  68.  
  69. This would take the size of an integer and divide it by two.  The
  70. precedence rules have put the division outside the `sizeof' when it was
  71. intended to be inside.
  72.  
  73.    Parentheses around the entire macro definition can prevent such
  74. problems.  Here, then, is the recommended way to define `ceil_div':
  75.  
  76.      #define ceil_div(x, y) (((x) + (y) - 1) / (y))
  77.  
  78. 
  79. File: cpp.info,  Node: Swallow Semicolon,  Next: Side Effects,  Prev: Macro Parentheses,  Up: Macro Pitfalls
  80.  
  81. Swallowing the Semicolon
  82. ........................
  83.  
  84.    Often it is desirable to define a macro that expands into a compound
  85. statement.  Consider, for example, the following macro, that advances a
  86. pointer (the argument `p' says where to find it) across whitespace
  87. characters:
  88.  
  89.      #define SKIP_SPACES (p, limit)  \
  90.      { register char *lim = (limit); \
  91.        while (p != lim) {            \
  92.          if (*p++ != ' ') {          \
  93.            p--; break; }}}
  94.  
  95. Here Backslash-Newline is used to split the macro definition, which must
  96. be a single line, so that it resembles the way such C code would be
  97. laid out if not part of a macro definition.
  98.  
  99.    A call to this macro might be `SKIP_SPACES (p, lim)'.  Strictly
  100. speaking, the call expands to a compound statement, which is a complete
  101. statement with no need for a semicolon to end it.  But it looks like a
  102. function call.  So it minimizes confusion if you can use it like a
  103. function call, writing a semicolon afterward, as in `SKIP_SPACES (p,
  104. lim);'
  105.  
  106.    But this can cause trouble before `else' statements, because the
  107. semicolon is actually a null statement.  Suppose you write
  108.  
  109.      if (*p != 0)
  110.        SKIP_SPACES (p, lim);
  111.      else ...
  112.  
  113. The presence of two statements--the compound statement and a null
  114. statement--in between the `if' condition and the `else' makes invalid C
  115. code.
  116.  
  117.    The definition of the macro `SKIP_SPACES' can be altered to solve
  118. this problem, using a `do ... while' statement.  Here is how:
  119.  
  120.      #define SKIP_SPACES (p, limit)     \
  121.      do { register char *lim = (limit); \
  122.           while (p != lim) {            \
  123.             if (*p++ != ' ') {          \
  124.               p--; break; }}}           \
  125.      while (0)
  126.  
  127.    Now `SKIP_SPACES (p, lim);' expands into
  128.  
  129.      do {...} while (0);
  130.  
  131. which is one statement.
  132.  
  133. 
  134. File: cpp.info,  Node: Side Effects,  Next: Self-Reference,  Prev: Swallow Semicolon,  Up: Macro Pitfalls
  135.  
  136. Duplication of Side Effects
  137. ...........................
  138.  
  139.    Many C programs define a macro `min', for "minimum", like this:
  140.  
  141.      #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
  142.  
  143.    When you use this macro with an argument containing a side effect,
  144. as shown here,
  145.  
  146.      next = min (x + y, foo (z));
  147.  
  148. it expands as follows:
  149.  
  150.      next = ((x + y) < (foo (z)) ? (x + y) : (foo (z)));
  151.  
  152. where `x + y' has been substituted for `X' and `foo (z)' for `Y'.
  153.  
  154.    The function `foo' is used only once in the statement as it appears
  155. in the program, but the expression `foo (z)' has been substituted twice
  156. into the macro expansion.  As a result, `foo' might be called two times
  157. when the statement is executed.  If it has side effects or if it takes
  158. a long time to compute, the results might not be what you intended.  We
  159. say that `min' is an "unsafe" macro.
  160.  
  161.    The best solution to this problem is to define `min' in a way that
  162. computes the value of `foo (z)' only once.  The C language offers no
  163. standard way to do this, but it can be done with GNU C extensions as
  164. follows:
  165.  
  166.      #define min(X, Y)                     \
  167.      ({ typeof (X) __x = (X), __y = (Y);   \
  168.         (__x < __y) ? __x : __y; })
  169.  
  170.    If you do not wish to use GNU C extensions, the only solution is to
  171. be careful when *using* the macro `min'.  For example, you can
  172. calculate the value of `foo (z)', save it in a variable, and use that
  173. variable in `min':
  174.  
  175.      #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
  176.      ...
  177.      {
  178.        int tem = foo (z);
  179.        next = min (x + y, tem);
  180.      }
  181.  
  182. (where we assume that `foo' returns type `int').
  183.  
  184. 
  185. File: cpp.info,  Node: Self-Reference,  Next: Argument Prescan,  Prev: Side Effects,  Up: Macro Pitfalls
  186.  
  187. Self-Referential Macros
  188. .......................
  189.  
  190.    A "self-referential" macro is one whose name appears in its
  191. definition.  A special feature of ANSI Standard C is that the
  192. self-reference is not considered a macro call.  It is passed into the
  193. preprocessor output unchanged.
  194.  
  195.    Let's consider an example:
  196.  
  197.      #define foo (4 + foo)
  198.  
  199. where `foo' is also a variable in your program.
  200.  
  201.    Following the ordinary rules, each reference to `foo' will expand
  202. into `(4 + foo)'; then this will be rescanned and will expand into `(4
  203. + (4 + foo))'; and so on until it causes a fatal error (memory full) in
  204. the preprocessor.
  205.  
  206.    However, the special rule about self-reference cuts this process
  207. short after one step, at `(4 + foo)'.  Therefore, this macro definition
  208. has the possibly useful effect of causing the program to add 4 to the
  209. value of `foo' wherever `foo' is referred to.
  210.  
  211.    In most cases, it is a bad idea to take advantage of this feature.  A
  212. person reading the program who sees that `foo' is a variable will not
  213. expect that it is a macro as well.  The reader will come across the
  214. identifier `foo' in the program and think its value should be that of
  215. the variable `foo', whereas in fact the value is four greater.
  216.  
  217.    The special rule for self-reference applies also to "indirect"
  218. self-reference.  This is the case where a macro X expands to use a
  219. macro `y', and the expansion of `y' refers to the macro `x'.  The
  220. resulting reference to `x' comes indirectly from the expansion of `x',
  221. so it is a self-reference and is not further expanded.  Thus, after
  222.  
  223.      #define x (4 + y)
  224.      #define y (2 * x)
  225.  
  226. `x' would expand into `(4 + (2 * x))'.  Clear?
  227.  
  228.    But suppose `y' is used elsewhere, not from the definition of `x'.
  229. Then the use of `x' in the expansion of `y' is not a self-reference
  230. because `x' is not "in progress".  So it does expand.  However, the
  231. expansion of `x' contains a reference to `y', and that is an indirect
  232. self-reference now because `y' is "in progress".  The result is that
  233. `y' expands to `(2 * (4 + y))'.
  234.  
  235.    It is not clear that this behavior would ever be useful, but it is
  236. specified by the ANSI C standard, so you may need to understand it.
  237.  
  238. 
  239. File: cpp.info,  Node: Argument Prescan,  Next: Cascaded Macros,  Prev: Self-Reference,  Up: Macro Pitfalls
  240.  
  241. Separate Expansion of Macro Arguments
  242. .....................................
  243.  
  244.    We have explained that the expansion of a macro, including the
  245. substituted actual arguments, is scanned over again for macro calls to
  246. be expanded.
  247.  
  248.    What really happens is more subtle: first each actual argument text
  249. is scanned separately for macro calls.  Then the results of this are
  250. substituted into the macro body to produce the macro expansion, and the
  251. macro expansion is scanned again for macros to expand.
  252.  
  253.    The result is that the actual arguments are scanned *twice* to expand
  254. macro calls in them.
  255.  
  256.    Most of the time, this has no effect.  If the actual argument
  257. contained any macro calls, they are expanded during the first scan.
  258. The result therefore contains no macro calls, so the second scan does
  259. not change it.  If the actual argument were substituted as given, with
  260. no prescan, the single remaining scan would find the same macro calls
  261. and produce the same results.
  262.  
  263.    You might expect the double scan to change the results when a
  264. self-referential macro is used in an actual argument of another macro
  265. (*note Self-Reference::.): the self-referential macro would be expanded
  266. once in the first scan, and a second time in the second scan.  But this
  267. is not what happens.  The self-references that do not expand in the
  268. first scan are marked so that they will not expand in the second scan
  269. either.
  270.  
  271.    The prescan is not done when an argument is stringified or
  272. concatenated.  Thus,
  273.  
  274.      #define str(s) #s
  275.      #define foo 4
  276.      str (foo)
  277.  
  278. expands to `"foo"'.  Once more, prescan has been prevented from having
  279. any noticeable effect.
  280.  
  281.    More precisely, stringification and concatenation use the argument as
  282. written, in un-prescanned form.  The same actual argument would be used
  283. in prescanned form if it is substituted elsewhere without
  284. stringification or concatenation.
  285.  
  286.      #define str(s) #s lose(s)
  287.      #define foo 4
  288.      str (foo)
  289.  
  290.    expands to `"foo" lose(4)'.
  291.  
  292.    You might now ask, "Why mention the prescan, if it makes no
  293. difference?  And why not skip it and make the preprocessor faster?"
  294. The answer is that the prescan does make a difference in three special
  295. cases:
  296.  
  297.    * Nested calls to a macro.
  298.  
  299.    * Macros that call other macros that stringify or concatenate.
  300.  
  301.    * Macros whose expansions contain unshielded commas.
  302.  
  303.    We say that "nested" calls to a macro occur when a macro's actual
  304. argument contains a call to that very macro.  For example, if `f' is a
  305. macro that expects one argument, `f (f (1))' is a nested pair of calls
  306. to `f'.  The desired expansion is made by expanding `f (1)' and
  307. substituting that into the definition of `f'.  The prescan causes the
  308. expected result to happen.  Without the prescan, `f (1)' itself would
  309. be substituted as an actual argument, and the inner use of `f' would
  310. appear during the main scan as an indirect self-reference and would not
  311. be expanded.  Here, the prescan cancels an undesirable side effect (in
  312. the medical, not computational, sense of the term) of the special rule
  313. for self-referential macros.
  314.  
  315.    But prescan causes trouble in certain other cases of nested macro
  316. calls.  Here is an example:
  317.  
  318.      #define foo  a,b
  319.      #define bar(x) lose(x)
  320.      #define lose(x) (1 + (x))
  321.      
  322.      bar(foo)
  323.  
  324. We would like `bar(foo)' to turn into `(1 + (foo))', which would then
  325. turn into `(1 + (a,b))'.  But instead, `bar(foo)' expands into
  326. `lose(a,b)', and you get an error because `lose' requires a single
  327. argument.  In this case, the problem is easily solved by the same
  328. parentheses that ought to be used to prevent misnesting of arithmetic
  329. operations:
  330.  
  331.      #define foo (a,b)
  332.      #define bar(x) lose((x))
  333.  
  334.    The problem is more serious when the operands of the macro are not
  335. expressions; for example, when they are statements.  Then parentheses
  336. are unacceptable because they would make for invalid C code:
  337.  
  338.      #define foo { int a, b; ... }
  339.  
  340. In GNU C you can shield the commas using the `({...})' construct which
  341. turns a compound statement into an expression:
  342.  
  343.      #define foo ({ int a, b; ... })
  344.  
  345.    Or you can rewrite the macro definition to avoid such commas:
  346.  
  347.      #define foo { int a; int b; ... }
  348.  
  349.    There is also one case where prescan is useful.  It is possible to
  350. use prescan to expand an argument and then stringify it--if you use two
  351. levels of macros.  Let's add a new macro `xstr' to the example shown
  352. above:
  353.  
  354.      #define xstr(s) str(s)
  355.      #define str(s) #s
  356.      #define foo 4
  357.      xstr (foo)
  358.  
  359.    This expands into `"4"', not `"foo"'.  The reason for the difference
  360. is that the argument of `xstr' is expanded at prescan (because `xstr'
  361. does not specify stringification or concatenation of the argument).
  362. The result of prescan then forms the actual argument for `str'.  `str'
  363. uses its argument without prescan because it performs stringification;
  364. but it cannot prevent or undo the prescanning already done by `xstr'.
  365.  
  366. 
  367. File: cpp.info,  Node: Cascaded Macros,  Next: Newlines in Args,  Prev: Argument Prescan,  Up: Macro Pitfalls
  368.  
  369. Cascaded Use of Macros
  370. ......................
  371.  
  372.    A "cascade" of macros is when one macro's body contains a reference
  373. to another macro.  This is very common practice.  For example,
  374.  
  375.      #define BUFSIZE 1020
  376.      #define TABLESIZE BUFSIZE
  377.  
  378.    This is not at all the same as defining `TABLESIZE' to be `1020'.
  379. The `#define' for `TABLESIZE' uses exactly the body you specify--in
  380. this case, `BUFSIZE'--and does not check to see whether it too is the
  381. name of a macro.
  382.  
  383.    It's only when you *use* `TABLESIZE' that the result of its expansion
  384. is checked for more macro names.
  385.  
  386.    This makes a difference if you change the definition of `BUFSIZE' at
  387. some point in the source file.  `TABLESIZE', defined as shown, will
  388. always expand using the definition of `BUFSIZE' that is currently in
  389. effect:
  390.  
  391.      #define BUFSIZE 1020
  392.      #define TABLESIZE BUFSIZE
  393.      #undef BUFSIZE
  394.      #define BUFSIZE 37
  395.  
  396. Now `TABLESIZE' expands (in two stages) to `37'.  (The `#undef' is to
  397. prevent any warning about the nontrivial redefinition of `BUFSIZE'.)
  398.  
  399. 
  400. File: cpp.info,  Node: Newlines in Args,  Prev: Cascaded Macros,  Up: Macro Pitfalls
  401.  
  402. Newlines in Macro Arguments
  403. ---------------------------
  404.  
  405.    Traditional macro processing carries forward all newlines in macro
  406. arguments into the expansion of the macro.  This means that, if some of
  407. the arguments are substituted more than once, or not at all, or out of
  408. order, newlines can be duplicated, lost, or moved around within the
  409. expansion.  If the expansion consists of multiple statements, then the
  410. effect is to distort the line numbers of some of these statements.  The
  411. result can be incorrect line numbers, in error messages or displayed in
  412. a debugger.
  413.  
  414.    The GNU C preprocessor operating in ANSI C mode adjusts appropriately
  415. for multiple use of an argument--the first use expands all the
  416. newlines, and subsequent uses of the same argument produce no newlines.
  417. But even in this mode, it can produce incorrect line numbering if
  418. arguments are used out of order, or not used at all.
  419.  
  420.    Here is an example illustrating this problem:
  421.  
  422.      #define ignore_second_arg(a,b,c) a; c
  423.      
  424.      ignore_second_arg (foo (),
  425.                         ignored (),
  426.                         syntax error);
  427.  
  428. The syntax error triggered by the tokens `syntax error' results in an
  429. error message citing line four, even though the statement text comes
  430. from line five.
  431.  
  432. 
  433. File: cpp.info,  Node: Conditionals,  Next: Combining Sources,  Prev: Macros,  Up: Top
  434.  
  435. Conditionals
  436. ============
  437.  
  438.    In a macro processor, a "conditional" is a command that allows a part
  439. of the program to be ignored during compilation, on some conditions.
  440. In the C preprocessor, a conditional can test either an arithmetic
  441. expression or whether a name is defined as a macro.
  442.  
  443.    A conditional in the C preprocessor resembles in some ways an `if'
  444. statement in C, but it is important to understand the difference between
  445. them.  The condition in an `if' statement is tested during the execution
  446. of your program.  Its purpose is to allow your program to behave
  447. differently from run to run, depending on the data it is operating on.
  448. The condition in a preprocessor conditional command is tested when your
  449. program is compiled.  Its purpose is to allow different code to be
  450. included in the program depending on the situation at the time of
  451. compilation.
  452.  
  453. * Menu:
  454.  
  455. * Uses: Conditional Uses.       What conditionals are for.
  456. * Syntax: Conditional Syntax.   How conditionals are written.
  457. * Deletion: Deleted Code.       Making code into a comment.
  458. * Macros: Conditionals-Macros.  Why conditionals are used with macros.
  459. * Assertions::                How and why to use assertions.
  460. * Errors: #error Command.       Detecting inconsistent compilation parameters.
  461.  
  462. 
  463. File: cpp.info,  Node: Conditional Uses,  Next: Conditional Syntax,  Up: Conditionals
  464.  
  465. Why Conditionals are Used
  466. -------------------------
  467.  
  468.    Generally there are three kinds of reason to use a conditional.
  469.  
  470.    * A program may need to use different code depending on the machine
  471.      or operating system it is to run on.  In some cases the code for
  472.      one operating system may be erroneous on another operating system;
  473.      for example, it might refer to library routines that do not exist
  474.      on the other system.  When this happens, it is not enough to avoid
  475.      executing the invalid code: merely having it in the program makes
  476.      it impossible to link the program and run it.  With a preprocessor
  477.      conditional, the offending code can be effectively excised from
  478.      the program when it is not valid.
  479.  
  480.    * You may want to be able to compile the same source file into two
  481.      different programs.  Sometimes the difference between the programs
  482.      is that one makes frequent time-consuming consistency checks on its
  483.      intermediate data, or prints the values of those data for
  484.      debugging, while the other does not.
  485.  
  486.    * A conditional whose condition is always false is a good way to
  487.      exclude code from the program but keep it as a sort of comment for
  488.      future reference.
  489.  
  490.    Most simple programs that are intended to run on only one machine
  491. will not need to use preprocessor conditionals.
  492.  
  493. 
  494. File: cpp.info,  Node: Conditional Syntax,  Next: Deleted Code,  Prev: Conditional Uses,  Up: Conditionals
  495.  
  496. Syntax of Conditionals
  497. ----------------------
  498.  
  499.    A conditional in the C preprocessor begins with a "conditional
  500. command": `#if', `#ifdef' or `#ifndef'.  *Note Conditionals-Macros::,
  501. for information on `#ifdef' and `#ifndef'; only `#if' is explained here.
  502.  
  503. * Menu:
  504.  
  505. * If: #if Command.     Basic conditionals using `#if' and `#endif'.
  506. * Else: #else Command. Including some text if the condition fails.
  507. * Elif: #elif Command. Testing several alternative possibilities.
  508.  
  509. 
  510. File: cpp.info,  Node: #if Command,  Next: #else Command,  Up: Conditional Syntax
  511.  
  512. The `#if' Command
  513. .................
  514.  
  515.    The `#if' command in its simplest form consists of
  516.  
  517.      #if EXPRESSION
  518.      CONTROLLED TEXT
  519.      #endif /* EXPRESSION */
  520.  
  521.    The comment following the `#endif' is not required, but it is a good
  522. practice because it helps people match the `#endif' to the
  523. corresponding `#if'.  Such comments should always be used, except in
  524. short conditionals that are not nested.  In fact, you can put anything
  525. at all after the `#endif' and it will be ignored by the GNU C
  526. preprocessor, but only comments are acceptable in ANSI Standard C.
  527.  
  528.    EXPRESSION is a C expression of integer type, subject to stringent
  529. restrictions.  It may contain
  530.  
  531.    * Integer constants, which are all regarded as `long' or `unsigned
  532.      long'.
  533.  
  534.    * Character constants, which are interpreted according to the
  535.      character set and conventions of the machine and operating system
  536.      on which the preprocessor is running.  The GNU C preprocessor uses
  537.      the C data type `char' for these character constants; therefore,
  538.      whether some character codes are negative is determined by the C
  539.      compiler used to compile the preprocessor.  If it treats `char' as
  540.      signed, then character codes large enough to set the sign bit will
  541.      be considered negative; otherwise, no character code is considered
  542.      negative.
  543.  
  544.    * Arithmetic operators for addition, subtraction, multiplication,
  545.      division, bitwise operations, shifts, comparisons, and logical
  546.      operations (`&&' and `||').
  547.  
  548.    * Identifiers that are not macros, which are all treated as zero(!).
  549.  
  550.    * Macro calls.  All macro calls in the expression are expanded before
  551.      actual computation of the expression's value begins.
  552.  
  553.    Note that `sizeof' operators and `enum'-type values are not allowed.
  554. `enum'-type values, like all other identifiers that are not taken as
  555. macro calls and expanded, are treated as zero.
  556.  
  557.    The CONTROLLED TEXT inside of a conditional can include preprocessor
  558. commands.  Then the commands inside the conditional are obeyed only if
  559. that branch of the conditional succeeds.  The text can also contain
  560. other conditional groups.  However, the `#if' and `#endif' commands
  561. must balance.
  562.  
  563. 
  564. File: cpp.info,  Node: #else Command,  Next: #elif Command,  Prev: #if Command,  Up: Conditional Syntax
  565.  
  566. The `#else' Command
  567. ...................
  568.  
  569.    The `#else' command can be added to a conditional to provide
  570. alternative text to be used if the condition is false.  This is what it
  571. looks like:
  572.  
  573.      #if EXPRESSION
  574.      TEXT-IF-TRUE
  575.      #else /* Not EXPRESSION */
  576.      TEXT-IF-FALSE
  577.      #endif /* Not EXPRESSION */
  578.  
  579.    If EXPRESSION is nonzero, and thus the TEXT-IF-TRUE is active, then
  580. `#else' acts like a failing conditional and the TEXT-IF-FALSE is
  581. ignored.  Contrariwise, if the `#if' conditional fails, the
  582. TEXT-IF-FALSE is considered included.
  583.  
  584. 
  585. File: cpp.info,  Node: #elif Command,  Prev: #else Command,  Up: Conditional Syntax
  586.  
  587. The `#elif' Command
  588. ...................
  589.  
  590.    One common case of nested conditionals is used to check for more
  591. than two possible alternatives.  For example, you might have
  592.  
  593.      #if X == 1
  594.      ...
  595.      #else /* X != 1 */
  596.      #if X == 2
  597.      ...
  598.      #else /* X != 2 */
  599.      ...
  600.      #endif /* X != 2 */
  601.      #endif /* X != 1 */
  602.  
  603.    Another conditional command, `#elif', allows this to be abbreviated
  604. as follows:
  605.  
  606.      #if X == 1
  607.      ...
  608.      #elif X == 2
  609.      ...
  610.      #else /* X != 2 and X != 1*/
  611.      ...
  612.      #endif /* X != 2 and X != 1*/
  613.  
  614.    `#elif' stands for "else if".  Like `#else', it goes in the middle
  615. of a `#if'-`#endif' pair and subdivides it; it does not require a
  616. matching `#endif' of its own.  Like `#if', the `#elif' command includes
  617. an expression to be tested.
  618.  
  619.    The text following the `#elif' is processed only if the original
  620. `#if'-condition failed and the `#elif' condition succeeds.  More than
  621. one `#elif' can go in the same `#if'-`#endif' group.  Then the text
  622. after each `#elif' is processed only if the `#elif' condition succeeds
  623. after the original `#if' and any previous `#elif' commands within it
  624. have failed.  `#else' is equivalent to `#elif 1', and `#else' is
  625. allowed after any number of `#elif' commands, but `#elif' may not follow
  626. `#else'.
  627.  
  628. 
  629. File: cpp.info,  Node: Deleted Code,  Next: Conditionals-Macros,  Prev: Conditional Syntax,  Up: Conditionals
  630.  
  631. Keeping Deleted Code for Future Reference
  632. -----------------------------------------
  633.  
  634.    If you replace or delete a part of the program but want to keep the
  635. old code around as a comment for future reference, the easy way to do
  636. this is to put `#if 0' before it and `#endif' after it.  This is better
  637. than using comment delimiters `/*' and `*/' since those won't work if
  638. the code already contains comments (C comments do not nest).
  639.  
  640.    This works even if the code being turned off contains conditionals,
  641. but they must be entire conditionals (balanced `#if' and `#endif').
  642.  
  643.    Conversely, do not use `#if 0' for comments which are not C code.
  644. Use the comment delimiters `/*' and `*/' instead.  The interior of `#if
  645. 0' must consist of complete tokens; in particular, singlequote
  646. characters must balance.  But comments often contain unbalanced
  647. singlequote characters (known in English as apostrophes).  These
  648. confuse `#if 0'.  They do not confuse `/*'.
  649.  
  650. 
  651. File: cpp.info,  Node: Conditionals-Macros,  Next: Assertions,  Prev: Deleted Code,  Up: Conditionals
  652.  
  653. Conditionals and Macros
  654. -----------------------
  655.  
  656.    Conditionals are useful in connection with macros or assertions,
  657. because those are the only ways that an expression's value can vary
  658. from one compilation to another.  A `#if' command whose expression uses
  659. no macros or assertions is equivalent to `#if 1' or `#if 0'; you might
  660. as well determine which one, by computing the value of the expression
  661. yourself, and then simplify the program.
  662.  
  663.    For example, here is a conditional that tests the expression
  664. `BUFSIZE == 1020', where `BUFSIZE' must be a macro.
  665.  
  666.      #if BUFSIZE == 1020
  667.        printf ("Large buffers!\n");
  668.      #endif /* BUFSIZE is large */
  669.  
  670.    (Programmers often wish they could test the size of a variable or
  671. data type in `#if', but this does not work.  The preprocessor does not
  672. understand `sizeof', or typedef names, or even the type keywords such
  673. as `int'.)
  674.  
  675.    The special operator `defined' is used in `#if' expressions to test
  676. whether a certain name is defined as a macro.  Either `defined NAME' or
  677. `defined (NAME)' is an expression whose value is 1 if NAME is defined
  678. as macro at the current point in the program, and 0 otherwise.  For the
  679. `defined' operator it makes no difference what the definition of the
  680. macro is; all that matters is whether there is a definition.  Thus, for
  681. example,
  682.  
  683.      #if defined (vax) || defined (ns16000)
  684.  
  685. would succeed if either of the names `vax' and `ns16000' is defined as
  686. a macro.  You can test the same condition using assertions (*note
  687. Assertions::.), like this:
  688.  
  689.      #if #cpu (vax) || #cpu (ns16000)
  690.  
  691.    If a macro is defined and later undefined with `#undef', subsequent
  692. use of the `defined' operator returns 0, because the name is no longer
  693. defined.  If the macro is defined again with another `#define',
  694. `defined' will recommence returning 1.
  695.  
  696.    Conditionals that test whether just one name is defined are very
  697. common, so there are two special short conditional commands for this
  698. case.
  699.  
  700. `#ifdef NAME'
  701.      is equivalent to `#if defined (NAME)'.
  702.  
  703. `#ifndef NAME'
  704.      is equivalent to `#if ! defined (NAME)'.
  705.  
  706.    Macro definitions can vary between compilations for several reasons.
  707.  
  708.    * Some macros are predefined on each kind of machine.  For example,
  709.      on a Vax, the name `vax' is a predefined macro.  On other
  710.      machines, it would not be defined.
  711.  
  712.    * Many more macros are defined by system header files.  Different
  713.      systems and machines define different macros, or give them
  714.      different values.  It is useful to test these macros with
  715.      conditionals to avoid using a system feature on a machine where it
  716.      is not implemented.
  717.  
  718.    * Macros are a common way of allowing users to customize a program
  719.      for different machines or applications.  For example, the macro
  720.      `BUFSIZE' might be defined in a configuration file for your
  721.      program that is included as a header file in each source file.  You
  722.      would use `BUFSIZE' in a preprocessor conditional in order to
  723.      generate different code depending on the chosen configuration.
  724.  
  725.    * Macros can be defined or undefined with `-D' and `-U' command
  726.      options when you compile the program.  You can arrange to compile
  727.      the same source file into two different programs by choosing a
  728.      macro name to specify which program you want, writing conditionals
  729.      to test whether or how this macro is defined, and then controlling
  730.      the state of the macro with compiler command options.  *Note
  731.      Invocation::.
  732.  
  733.    Assertions are usually predefined, but can be defined with
  734. preprocessor commands or command-line options.
  735.  
  736. 
  737. File: cpp.info,  Node: Assertions,  Next: #error Command,  Prev: Conditionals-Macros,  Up: Conditionals
  738.  
  739. Assertions
  740. ----------
  741.  
  742.    "Assertions" are a more systematic alternative to macros in writing
  743. conditionals to test what sort of computer or system the compiled
  744. program will run on.  Assertions are usually predefined, but you can
  745. define them with preprocessor commands or command-line options.
  746.  
  747.    The macros traditionally used to describe the type of target are not
  748. classified in any way according to which question they answer; they may
  749. indicate a hardware architecture, a particular hardware model, an
  750. operating system, a particular version of an operating system, or
  751. specific configuration options.  These are jumbled together in a single
  752. namespace.  In contrast, each assertion consists of a named question and
  753. an answer.  The question is usually called the "predicate".  An
  754. assertion looks like this:
  755.  
  756.      #PREDICATE (ANSWER)
  757.  
  758. You must use a properly formed identifier for PREDICATE.  The value of
  759. ANSWER can be any sequence of words; all characters are significant
  760. except for leading and trailing whitespace, and differences in internal
  761. whitespace sequences are ignored.  Thus, `x + y' is different from
  762. `x+y' but equivalent to `x + y'.  `)' is not allowed in an answer.
  763.  
  764.    Here is a conditional to test whether the answer ANSWER is asserted
  765. for the predicate PREDICATE:
  766.  
  767.      #if #PREDICATE (ANSWER)
  768.  
  769. There may be more than one answer asserted for a given predicate.  If
  770. you omit the answer, you can test whether *any* answer is asserted for
  771. PREDICATE:
  772.  
  773.      #if #PREDICATE
  774.  
  775.    Most of the time, the assertions you test will be predefined
  776. assertions.  GNU C provides three predefined predicates: `system',
  777. `cpu', and `machine'.  `system' is for assertions about the type of
  778. software, `cpu' describes the type of computer architecture, and
  779. `machine' gives more information about the computer.  For example, on a
  780. GNU system, the following assertions would be true:
  781.  
  782.      #system (gnu)
  783.      #system (mach)
  784.      #system (mach 3)
  785.      #system (mach 3.SUBVERSION)
  786.      #system (hurd)
  787.      #system (hurd VERSION)
  788.  
  789. and perhaps others.  The alternatives with more or less version
  790. information let you ask more or less detailed questions about the type
  791. of system software.
  792.  
  793.    On a Unix system, you would find `#system (unix)' and perhaps one of:
  794. `#system (aix)', `#system (bsd)', `#system (hpux)', `#system (lynx)',
  795. `#system (mach)', `#system (posix)', `#system (svr3)', `#system
  796. (svr4)', or `#system (xpg4)' with possible version numbers following.
  797.  
  798.    Other values for `system' are `#system (mvs)' and `#system (vms)'.
  799.  
  800.    *Portability note:* Many Unix C compilers provide only one answer
  801. for the `system' assertion: `#system (unix)', if they support
  802. assertions at all.  This is less than useful.
  803.  
  804.    An assertion with a multi-word answer is completely different from
  805. several assertions with individual single-word answers.  For example,
  806. the presence of `system (mach 3.0)' does not mean that `system (3.0)'
  807. is true.  It also does not directly imply `system (mach)', but in GNU
  808. C, that last will normally be asserted as well.
  809.  
  810.    The current list of possible assertion values for `cpu' is: `#cpu
  811. (a29k)', `#cpu (alpha)', `#cpu (arm)', `#cpu (clipper)', `#cpu
  812. (convex)', `#cpu (elxsi)', `#cpu (tron)', `#cpu (h8300)', `#cpu
  813. (i370)', `#cpu (i386)', `#cpu (i860)', `#cpu (i960)', `#cpu (m68k)',
  814. `#cpu (m88k)', `#cpu (mips)', `#cpu (ns32k)', `#cpu (hppa)', `#cpu
  815. (pyr)', `#cpu (ibm032)', `#cpu (rs6000)', `#cpu (sh)', `#cpu (sparc)',
  816. `#cpu (spur)', `#cpu (tahoe)', `#cpu (vax)', `#cpu (we32000)'.
  817.  
  818.    You can create assertions within a C program using `#assert', like
  819. this:
  820.  
  821.      #assert PREDICATE (ANSWER)
  822.  
  823. (Note the absence of a `#' before PREDICATE.)
  824.  
  825.    Each time you do this, you assert a new true answer for PREDICATE.
  826. Asserting one answer does not invalidate previously asserted answers;
  827. they all remain true.  The only way to remove an assertion is with
  828. `#unassert'.  `#unassert' has the same syntax as `#assert'.  You can
  829. also remove all assertions about PREDICATE like this:
  830.  
  831.      #unassert PREDICATE
  832.  
  833.    You can also add or cancel assertions using command options when you
  834. run `gcc' or `cpp'.  *Note Invocation::.
  835.  
  836. 
  837. File: cpp.info,  Node: #error Command,  Prev: Assertions,  Up: Conditionals
  838.  
  839. The `#error' and `#warning' Commands
  840. ------------------------------------
  841.  
  842.    The command `#error' causes the preprocessor to report a fatal
  843. error.  The rest of the line that follows `#error' is used as the error
  844. message.
  845.  
  846.    You would use `#error' inside of a conditional that detects a
  847. combination of parameters which you know the program does not properly
  848. support.  For example, if you know that the program will not run
  849. properly on a Vax, you might write
  850.  
  851.      #ifdef __vax__
  852.      #error Won't work on Vaxen.  See comments at get_last_object.
  853.      #endif
  854.  
  855. *Note Nonstandard Predefined::, for why this works.
  856.  
  857.    If you have several configuration parameters that must be set up by
  858. the installation in a consistent way, you can use conditionals to detect
  859. an inconsistency and report it with `#error'.  For example,
  860.  
  861.      #if HASH_TABLE_SIZE % 2 == 0 || HASH_TABLE_SIZE % 3 == 0 \
  862.          || HASH_TABLE_SIZE % 5 == 0
  863.      #error HASH_TABLE_SIZE should not be divisible by a small prime
  864.      #endif
  865.  
  866.    The command `#warning' is like the command `#error', but causes the
  867. preprocessor to issue a warning and continue preprocessing.  The rest of
  868. the line that follows `#warning' is used as the warning message.
  869.  
  870.    You might use `#warning' in obsolete header files, with a message
  871. directing the user to the header file which should be used instead.
  872.  
  873. 
  874. File: cpp.info,  Node: Combining Sources,  Next: Other Commands,  Prev: Conditionals,  Up: Top
  875.  
  876. Combining Source Files
  877. ======================
  878.  
  879.    One of the jobs of the C preprocessor is to inform the C compiler of
  880. where each line of C code came from: which source file and which line
  881. number.
  882.  
  883.    C code can come from multiple source files if you use `#include';
  884. both `#include' and the use of conditionals and macros can cause the
  885. line number of a line in the preprocessor output to be different from
  886. the line's number in the original source file.  You will appreciate the
  887. value of making both the C compiler (in error messages) and symbolic
  888. debuggers such as GDB use the line numbers in your source file.
  889.  
  890.    The C preprocessor builds on this feature by offering a command by
  891. which you can control the feature explicitly.  This is useful when a
  892. file for input to the C preprocessor is the output from another program
  893. such as the `bison' parser generator, which operates on another file
  894. that is the true source file.  Parts of the output from `bison' are
  895. generated from scratch, other parts come from a standard parser file.
  896. The rest are copied nearly verbatim from the source file, but their
  897. line numbers in the `bison' output are not the same as their original
  898. line numbers.  Naturally you would like compiler error messages and
  899. symbolic debuggers to know the original source file and line number of
  900. each line in the `bison' input.
  901.  
  902.    `bison' arranges this by writing `#line' commands into the output
  903. file.  `#line' is a command that specifies the original line number and
  904. source file name for subsequent input in the current preprocessor input
  905. file.  `#line' has three variants:
  906.  
  907. `#line LINENUM'
  908.      Here LINENUM is a decimal integer constant.  This specifies that
  909.      the line number of the following line of input, in its original
  910.      source file, was LINENUM.
  911.  
  912. `#line LINENUM FILENAME'
  913.      Here LINENUM is a decimal integer constant and FILENAME is a
  914.      string constant.  This specifies that the following line of input
  915.      came originally from source file FILENAME and its line number there
  916.      was LINENUM.  Keep in mind that FILENAME is not just a file name;
  917.      it is surrounded by doublequote characters so that it looks like a
  918.      string constant.
  919.  
  920. `#line ANYTHING ELSE'
  921.      ANYTHING ELSE is checked for macro calls, which are expanded.  The
  922.      result should be a decimal integer constant followed optionally by
  923.      a string constant, as described above.
  924.  
  925.    `#line' commands alter the results of the `__FILE__' and `__LINE__'
  926. predefined macros from that point on.  *Note Standard Predefined::.
  927.  
  928.    The output of the preprocessor (which is the input for the rest of
  929. the compiler) contains commands that look much like `#line' commands.
  930. They start with just `#' instead of `#line', but this is followed by a
  931. line number and file name as in `#line'.  *Note Output::.
  932.  
  933. 
  934. File: cpp.info,  Node: Other Commands,  Next: Output,  Prev: Combining Sources,  Up: Top
  935.  
  936. Miscellaneous Preprocessor Commands
  937. ===================================
  938.  
  939.    This section describes three additional preprocessor commands.  They
  940. are not very useful, but are mentioned for completeness.
  941.  
  942.    The "null command" consists of a `#' followed by a Newline, with
  943. only whitespace (including comments) in between.  A null command is
  944. understood as a preprocessor command but has no effect on the
  945. preprocessor output.  The primary significance of the existence of the
  946. null command is that an input line consisting of just a `#' will
  947. produce no output, rather than a line of output containing just a `#'.
  948. Supposedly some old C programs contain such lines.
  949.  
  950.    The ANSI standard specifies that the `#pragma' command has an
  951. arbitrary, implementation-defined effect.  In the GNU C preprocessor,
  952. `#pragma' commands are not used, except for `#pragma once' (*note
  953. Once-Only::.).  However, they are left in the preprocessor output, so
  954. they are available to the compilation pass.
  955.  
  956.    The `#ident' command is supported for compatibility with certain
  957. other systems.  It is followed by a line of text.  On some systems, the
  958. text is copied into a special place in the object file; on most systems,
  959. the text is ignored and this command has no effect.  Typically `#ident'
  960. is only used in header files supplied with those systems where it is
  961. meaningful.
  962.  
  963. 
  964. File: cpp.info,  Node: Output,  Next: Invocation,  Prev: Other Commands,  Up: Top
  965.  
  966. C Preprocessor Output
  967. =====================
  968.  
  969.    The output from the C preprocessor looks much like the input, except
  970. that all preprocessor command lines have been replaced with blank lines
  971. and all comments with spaces.  Whitespace within a line is not altered;
  972. however, a space is inserted after the expansions of most macro calls.
  973.  
  974.    Source file name and line number information is conveyed by lines of
  975. the form
  976.  
  977.      # LINENUM FILENAME FLAGS
  978.  
  979. which are inserted as needed into the middle of the input (but never
  980. within a string or character constant).  Such a line means that the
  981. following line originated in file FILENAME at line LINENUM.
  982.  
  983.    After the file name comes zero or more flags, which are `1', `2' or
  984. `3'.  If there are multiple flags, spaces separate them.  Here is what
  985. the flags mean:
  986.  
  987. `1'
  988.      This indicates the start of a new file.
  989.  
  990. `2'
  991.      This indicates returning to a file (after having included another
  992.      file).
  993.  
  994. `3'
  995.      This indicates that the following text comes from a system header
  996.      file, so certain warnings should be suppressed.
  997.  
  998.