home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Programming / gcc-2.95.3-3 / info / cpp.info-2 < prev    next >
Encoding:
GNU Info File  |  2001-07-15  |  41.1 KB  |  1,064 lines

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