-
Notifications
You must be signed in to change notification settings - Fork 16
/
cpp.html
3213 lines (3104 loc) · 161 KB
/
cpp.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>The GNU C Preprocessor</TITLE>
<STYLE TYPE="TEXT/CSS">
<!--
.IE3-DUMMY { CONT-SIZE: 100%; }
BODY { FONT-FAMILY: Verdana,Arial,Helvetica,Sans-Serif; BACKGROUND-COLOR: #E0E0E0; }
P { FONT-FAMILY: Verdana,Arial,Helvetica,Sans-Serif; }
H1 { FONT-FAMILY: Verdana,Arial,Helvetica,Sans-Serif; }
H2 { FONT-FAMILY: Verdana,Arial,Helvetica,Sans-Serif; }
H3 { FONT-FAMILY: Verdana,Arial,Helvetica,Sans-Serif; }
H4 { FONT-FAMILY: Verdana,Arial,Helvetica,Sans-Serif; }
H5 { FONT-FAMILY: Verdana,Arial,Helvetica,Sans-Serif; }
H6 { FONT-FAMILY: Verdana,Arial,Helvetica,Sans-Serif; }
UL { FONT-FAMILY: Verdana,Arial,Helvetica,Sans-Serif; }
TD { FONT-FAMILY: Verdana,Arial,Helvetica,Sans-Serif; BACKGROUND-COLOR: #FFFFFF; }
.NOBORDER { BACKGROUND-COLOR: #E0E0E0; PADDING: 0pt; }
.NOBORDER TD { FONT-FAMILY: Verdana,Arial,Helvetica,Sans-Serif; BACKGROUND-COLOR: #E0E0E0; PADDING: 0pt; }
.CODE { FONT-FAMILY: Courier New; }
-->
</STYLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#E0E0E0">
<FONT SIZE="5"><B>The GNU C Preprocessor</B></FONT>
<HR>
<P>This part of the documentation is a modified version of the <A HREF="http://gcc.gnu.org/onlinedocs/cpp/">GNU CPP Manual</A>.
Therefore it is licensed under the <A HREF="gnugpl.html#gnufdl">GNU Free Documentation License</A>.
<BR><BR>
The C preprocessor is a <U>macro processor</U> that is used automatically by
the C compiler to transform your program before actual compilation. It is
called a macro processor because it allows you to define <U>macros</U>,
which are brief abbreviations for longer constructs.</P>
<UL>
<LI><B><A HREF="#SEC2">Preprocessor Overview</A></B>
<LI><B><A HREF="#SEC4">Header Files</A></B>
<LI><B><A HREF="#SEC10">Macros</A></B>
<LI><B><A HREF="#SEC31">Conditionals</A></B>
<LI><B><A HREF="#SEC46">Pragmas</A></B>
<LI><B><A HREF="#SEC42">Other Directives</A></B>
<LI><B><A HREF="#SEC40">User-defined Diagnostics</A></B>
<LI><B><A HREF="#SEC41">Line Control</A></B>
<LI><B><A HREF="#SEC43">Preprocessor Output</A></B>
<LI><B><A HREF="#SEC44">C Preprocessor Command-Line Options</A></B>
<LI><B><A HREF="#SEC70">Traditional Mode</A></B>
<LI><B><A HREF="#SEC80">Implementation Details</A></B>
<LI><B><A HREF="#history">History</A></B>
<LI><B><A HREF="gnugpl.html">GNU General Public License</A></B>
<LI><B><A HREF="gnugpl.html#gnufdl">GNU Free Documentation License</A></B>
<LI><B><A HREF="gnugpl.html#funding">Funding Free Software</A></B>
</UL>
<P>Original author: Free Software Foundation, Inc.
<BR>
Authors of the modifications: Zeljko Juric, Sebastian Reichelt, and Kevin Kofler
<BR>
Published by the TIGCC Team, and now the GCC4TI project.
<BR>
See the <A HREF="#history">History</A> section for details and copyright information.
<BR><BR>
Permission is granted to copy, distribute and/or modify this document
under the terms of the <A HREF="gnugpl.html#gnufdl">GNU Free Documentation License</A>, Version 1.1 or any
later version published by the Free Software Foundation. A copy of the license is included in the section entitled
"<A HREF="gnugpl.html#gnufdl">GNU Free Documentation License</A>".
<BR><BR>
This manual contains no Invariant Sections. The Front-Cover Texts are
(a) (see below), and the Back-Cover Texts are (b) (see below).
<BR><BR>
(a) The FSF's Front-Cover Text is:
<BR><BR>
A GNU Manual
<BR><BR>
(b) The FSF's Back-Cover Text is:
<BR><BR>
You have freedom to copy and modify this GNU Manual, like GNU
software. Copies published by the Free Software Foundation raise
funds for GNU development.</P>
<HR>
<H2><A NAME="SEC2"><U>Preprocessor Overview</U></A></H2>
<P>The C preprocessor, often known as <U>cpp</U>, is a <U>macro processor</U>
that is used automatically by the C compiler to transform your program
before compilation. It is called a macro processor because it allows
you to define <U>macros</U>, which are brief abbreviations for longer
constructs.
<BR><BR>
The C preprocessor is intended to be used only with C, C++, and
Objective-C source code. In the past, it has been abused as a general
text processor. It will choke on input which does not obey C's lexical
rules. For example, apostrophes will be interpreted as the beginning of
character constants, and cause errors. Also, you cannot rely on it
preserving characteristics of the input which are not significant to
C-family languages. If a Makefile is preprocessed, all the hard tabs
will be removed, and the Makefile will not work.
<BR><BR>
Having said that, you can often get away with using cpp on things which
are not C. Other Algol-ish programming languages are often safe
(Pascal, Ada, etc.) So is assembly, with caution. <B>'-traditional-cpp'</B>
mode preserves more white space, and is otherwise more permissive. Many
of the problems can be avoided by writing C or C++ style comments
instead of native language comments, and keeping macros simple.
<BR><BR>
Wherever possible, you should use a preprocessor geared to the language
you are writing in. Modern versions of the GNU assembler have macro
facilities. Most high level programming languages have their own
conditional compilation and inclusion mechanism. If all else fails,
try a true general text processor, such as GNU M4.
<BR><BR>
C preprocessors vary in some details. This manual discusses the GNU C
preprocessor, which provides a small superset of the features of ISO
Standard C. In its default mode, the GNU C preprocessor does not do a
few things required by the standard. These are features which are
rarely, if ever, used, and may cause surprising changes to the meaning
of a program which does not expect them. To get strict ISO Standard C,
you should use the <B>'-std=c89'</B> or <B>'-std=c99'</B> options, depending
on which version of the standard you want. To get all the mandatory
diagnostics, you must also use <B>'-pedantic'</B>. See <A HREF="#SEC44">Invocation</A>.
<BR><BR>
This manual describes the behavior of the ISO preprocessor. To
minimize gratuitous differences, where the ISO preprocessor's
behavior does not conflict with traditional semantics, the
traditional preprocessor should behave the same way. The various
differences that do exist are detailed in the section <A HREF="#SEC70">Traditional
Mode</A>.
<BR><BR>
For clarity, unless noted otherwise, references to <CODE>CPP</CODE> in this
manual refer to GNU CPP.</P>
<UL>
<LI><B><A HREF="#SEC3">Initial processing</A></B>
<LI><B><A HREF="#SEC3a">Tokenization</A></B>
<LI><B><A HREF="#SEC3b">The preprocessing language</A></B>
</UL>
<H3><A NAME="SEC3"><U>Initial processing</U></A></H3>
<P>The preprocessor performs a series of textual transformations on its
input. These happen before all other processing. Conceptually, they
happen in a rigid order, and the entire file is run through each
transformation before the next one begins. CPP actually does them
all at once, for performance reasons. These transformations correspond
roughly to the first three "phases of translation" described in the C
standard.</P>
<OL>
<LI><P>The input file is read into memory and broken into lines.
<BR><BR>
CPP expects its input to be a text file, that is, an unstructured
stream of ASCII characters, with some characters indicating the end of a
line of text. Extended ASCII character sets, such as ISO Latin-1 or
Unicode encoded in UTF-8, are also acceptable. Character sets that are
not strict supersets of seven-bit ASCII will not work. We plan to add
complete support for international character sets in a future release.
<BR><BR>
Different systems use different conventions to indicate the end of a
line. GCC accepts the ASCII control sequences <CODE>LF</CODE>, <CODE>CR
LF</CODE>, <CODE>CR</CODE>, and <CODE>LF CR</CODE> as end-of-line markers. The first
three are the canonical sequences used by Unix, DOS and VMS, and the
classic Mac OS (before OSX) respectively. You may therefore safely copy
source code written on any of those systems to a different one and use
it without conversion. (GCC may lose track of the current line number
if a file doesn't consistently use one convention, as sometimes happens
when it is edited on computers with different conventions that share a
network file system.) <CODE>LF CR</CODE> is included because it has been
reported as an end-of-line marker under exotic conditions.
<BR><BR>
If the last line of any input file lacks an end-of-line marker, the end
of the file is considered to implicitly supply one. The C standard says
that this condition provokes undefined behavior, so GCC will emit a
warning message.
</P></LI>
<LI><P>If trigraphs are enabled, they are replaced by their
corresponding single characters. By default GCC ignores trigraphs,
but if you request a strictly conforming mode with the <B>'-std'</B>
option, or you specify the <B>'-trigraphs'</B> option, then it
converts them.
<BR><BR>
These are nine three-character sequences, all starting with <CODE>??</CODE>,
that are defined by ISO C to stand for single characters. They permit
obsolete systems that lack some of C's punctuation to use C. For
example, <CODE>??/</CODE> stands for <CODE>\</CODE>, so <CODE>'??/n'</CODE> is a character
constant for a newline.
<BR><BR>
Trigraphs are not popular and many compilers implement them incorrectly.
Portable code should not rely on trigraphs being either converted or
ignored. If you use the <B>'-Wall'</B> or <B>'-Wtrigraphs'</B> options,
GCC will warn you when a trigraph would change the meaning of your
program if it were converted.
<BR><BR>
In a string constant, you can prevent a sequence of question marks from
being confused with a trigraph by inserting a backslash between the
question marks. <CODE>"(??\?)"</CODE> is the string <CODE>(???)</CODE>, not
<CODE>(?]</CODE>. Traditional C compilers do not recognize this idiom.
<BR><BR>
The nine trigraphs and their replacements are</P>
<PRE>Trigraph: ??( ??) ??< ??> ??= ??/ ??' ??! ??-
Replacement: [ ] { } # \ ^ | ~
</PRE>
</LI>
<LI><P>Continued lines are merged into one long line.
<BR><BR>
A continued line is a line which ends with a backslash, <CODE>\</CODE>. The
backslash is removed and the following line is joined with the current
one. No space is inserted, so you may split a line anywhere, even in
the middle of a word. (It is generally more readable to split lines
only at white space.)
<BR><BR>
The trailing backslash on a continued line is commonly referred to as a
<U>backslash-newline</U>.
<BR><BR>
If there is white space between a backslash and the end of a line, that
is still a continued line. However, as this is usually the result of an
editing mistake, and many compilers will not accept it as a continued
line, GCC will warn you about it.
</P></LI>
<LI><P>All comments are replaced with single spaces.
<BR><BR>
There are two kinds of comments. <U>Block comments</U> begin with
<CODE>/*</CODE> and continue until the next <CODE>*/</CODE>. Block comments do not
nest:</P>
<PRE>/* this is /* one comment */ text outside comment
</PRE>
<P><U>Line comments</U> begin with <CODE>//</CODE> and continue to the end of the
current line. Line comments do not nest either, but it does not matter,
because they would end in the same place anyway.</P>
<PRE>// this is // one comment
text outside comment
</PRE>
</LI>
</OL>
<P>It is safe to put line comments inside block comments, or vice versa.</P>
<PRE>/* block comment
// contains line comment
yet more comment
*/ outside comment
// line comment /* contains block comment */
</PRE>
<P>But beware of commenting out one end of a block comment with a line
comment.</P>
<PRE> // l.c. /* block comment begins
oops! this isn't a comment anymore */
</PRE>
<P>Comments are not recognized within string literals. <CODE>"/* blah
*/"</CODE> is the string constant <CODE>/* blah */</CODE>, not an empty string.
<BR><BR>
Line comments are not in the 1989 edition of the C standard, but they
are recognized by GCC as an extension. In C++ and in the 1999 edition
of the C standard, they are an official part of the language.
<BR><BR>
Since these transformations happen before all other processing, you can
split a line mechanically with backslash-newline anywhere. You can
comment out the end of a line. You can continue a line comment onto the
next line with backslash-newline. You can even split <CODE>/*</CODE>,
<CODE>*/</CODE>, and <CODE>//</CODE> onto multiple lines with backslash-newline.
For example:</P>
<PRE>/\
*
*/ # /*
*/ defi\
ne FO\
O 10\
20
</PRE>
<P>is equivalent to <CODE>#define FOO 1020</CODE>. All these tricks are
extremely confusing and should not be used in code intended to be
readable.
<BR><BR>
There is no way to prevent a backslash at the end of a line from being
interpreted as a backslash-newline. This cannot affect any correct
program, however.</P>
<H3><A NAME="SEC3a"><U>Tokenization</U></A></H3>
<P>After the textual transformations are finished, the input file is
converted into a sequence of <U>preprocessing tokens</U>. These mostly
correspond to the syntactic tokens used by the C compiler, but there are
a few differences. White space separates tokens; it is not itself a
token of any kind. Tokens do not have to be separated by white space,
but it is often necessary to avoid ambiguities.
<BR><BR>
When faced with a sequence of characters that has more than one possible
tokenization, the preprocessor is greedy. It always makes each token,
starting from the left, as big as possible before moving on to the next
token. For instance, <CODE>a+++++b</CODE> is interpreted as
<CODE>a ++ ++ + b</CODE>, not as <CODE>a ++ + ++ b</CODE>, even though the
latter tokenization could be part of a valid C program and the former
could not.
<BR><BR>
Once the input file is broken into tokens, the token boundaries never
change, except when the <CODE>##</CODE> preprocessing operator is used to paste
tokens together. See <A HREF="#SEC18">Concatenation</A>. For example,</P>
<PRE>#define foo() bar
foo()baz
</PRE>
<P>expands to <CODE>bar baz</CODE>, <I>not</I> <CODE>barbaz</CODE>.
<BR><BR>
The compiler does not re-tokenize the preprocessor's output. Each
preprocessing token becomes one compiler token.
<BR><BR>
Preprocessing tokens fall into five broad classes: identifiers,
preprocessing numbers, string literals, punctuators, and other. An
<U>identifier</U> is the same as an identifier in C: any sequence of
letters, digits, or underscores, which begins with a letter or
underscore. Keywords of C have no significance to the preprocessor;
they are ordinary identifiers. You can define a macro whose name is a
keyword, for instance. The only identifier which can be considered a
preprocessing keyword is <CODE><A HREF="#SEC38a">defined</A></CODE>.
<BR><BR>
In the 1999 C standard, identifiers may contain letters which are not
part of the "basic source character set," at the implementation's
discretion (such as accented Latin letters, Greek letters, or Chinese
ideograms). This may be done with an extended character set, or the
<CODE>\u</CODE> and <CODE>\U</CODE> escape sequences. GCC does not presently
implement either feature in the preprocessor or the compiler.
<BR><BR>
As an extension, GCC treats <CODE>$</CODE> as a letter. This is for
compatibility with some systems, such as VMS, where <CODE>$</CODE> is commonly
used in system-defined function and object names. <CODE>$</CODE> is not a
letter in strictly conforming mode, or if you specify the <B>'-$'</B>
option. See <A HREF="#SEC44">Invocation</A>.
<BR><BR>
A <U>preprocessing number</U> has a rather bizarre definition. The
category includes all the normal integer and floating point constants
one expects of C, but also a number of other things one might not
initially recognize as a number. Formally, preprocessing numbers begin
with an optional period, a required decimal digit, and then continue
with any sequence of letters, digits, underscores, periods, and
exponents. Exponents are the two-character sequences <CODE>e+</CODE>,
<CODE>e-</CODE>, <CODE>E+</CODE>, <CODE>E-</CODE>, <CODE>p+</CODE>, <CODE>p-</CODE>, <CODE>P+</CODE>, and
<CODE>P-</CODE>. (The exponents that begin with <CODE>p</CODE> or <CODE>P</CODE> are new
to C99. They are used for hexadecimal floating-point constants.)
<BR><BR>
The purpose of this unusual definition is to isolate the preprocessor
from the full complexity of numeric constants. It does not have to
distinguish between lexically valid and invalid floating-point numbers,
which is complicated. The definition also permits you to split an
identifier at any position and get exactly two tokens, which can then be
pasted back together with the <CODE>##</CODE> operator.
<BR><BR>
It's possible for preprocessing numbers to cause programs to be
misinterpreted. For example, <CODE>0xE+12</CODE> is a preprocessing number
which does not translate to any valid numeric constant, therefore a
syntax error. It does not mean <CODE>0xE + 12</CODE>, which is what you
might have intended.
<BR><BR>
<U>String literals</U> are string constants, character constants, and
header file names (the argument of <CODE>#include</CODE>). The C
standard uses the term <U>string literal</U> to refer only to what we are
calling <U>string constants</U>. String constants and character
constants are straightforward: <CODE>"..."</CODE> or <CODE>'...'</CODE>. In
either case embedded quotes should be escaped with a backslash:
<CODE>'\''</CODE> is the character constant for <CODE>'</CODE>. There is no limit on
the length of a character constant, but the value of a character
constant that contains more than one character is
implementation-defined. See <A HREF="#SEC80">Implementation Details</A>.
<BR><BR>
Header file names either look like string constants, <CODE>"..."</CODE>, or are
written with angle brackets instead, <CODE><...></CODE>. In either case,
backslash is an ordinary character. There is no way to escape the
closing quote or angle bracket. The preprocessor looks for the header
file in different places depending on which form you use. See <A HREF="#SEC7">Include
Operation</A>.
<BR><BR>
In standard C, no string literal may extend past the end of a line. GNU
CPP accepts multi-line string constants, but not multi-line character
constants or header file names. To write standards-compliant code,
you may use continued lines instead, or string
constant concatenation. See <A HREF="#SEC86">Differences from previous versions</A>.
<BR><BR>
<U>Punctuators</U> are all the usual bits of punctuation which are
meaningful to C and C++. All but three of the punctuation characters in
ASCII are C punctuators. The exceptions are <CODE>@</CODE>, <CODE>$</CODE>, and
<CODE>'</CODE>. In addition, all the two- and three-character operators are
punctuators. There are also six <U>digraphs</U>, which the C++ standard
calls <U>alternative tokens</U>, which are merely alternate ways to spell
other punctuators. This is a second attempt to work around missing
punctuation in obsolete systems. It has no negative side effects,
unlike trigraphs, but does not cover as much ground. The digraphs and
their corresponding normal punctuators are:</P>
<PRE>Digraph: <% %> <: :> %: %:%:
Punctuator: { } [ ] # ##
</PRE>
<P>Any other single character is considered "other." It is passed on to
the preprocessor's output unmolested. The C compiler will almost
certainly reject source code containing "other" tokens. In ASCII, the
only other characters are <CODE>@</CODE>, <CODE>$</CODE>, <CODE>'</CODE>, and control
characters other than NUL (all bits zero). (Note that <CODE>$</CODE> is
normally considered a letter.) All characters with the high bit set
(numeric range 0x7F--0xFF) are also "other" in the present
implementation. This will change when proper support for international
character sets is added to GCC.
<BR><BR>
NUL is a special case because of the high probability that its
appearance is accidental, and because it may be invisible to the user
(many terminals do not display NUL at all). Within comments, NULs are
silently ignored, just as any other character would be. In running
text, NUL is considered white space. For example, these two directives
have the same meaning.</P>
<PRE>#define X^@1
#define X 1
</PRE>
<P>(where <CODE>^@</CODE> is ASCII NUL). Within string or character constants,
NULs are preserved. In the latter two cases the preprocessor emits a
warning message.</P>
<H3><A NAME="SEC3b"><U>The preprocessing language</U></A></H3>
<P>After tokenization, the stream of tokens may simply be passed straight
to the compiler's parser. However, if it contains any operations in the
<U>preprocessing language</U>, it will be transformed first. This stage
corresponds roughly to the standard's "translation phase 4" and is
what most people think of as the preprocessor's job.
<BR><BR>
The preprocessing language consists of <U>directives</U> to be executed
and <U>macros</U> to be expanded. Its primary capabilities are:</P>
<UL>
<LI><P>Inclusion of header files. These are files of declarations that can be
substituted into your program.
</P></LI>
<LI><P>Macro expansion. You can define <U>macros</U>, which are abbreviations
for arbitrary fragments of C code. The preprocessor will replace the
macros with their definitions throughout the program. Some macros are
automatically defined for you.
</P></LI>
<LI><P>Conditional compilation. You can include or exclude parts of the
program according to various conditions.
</P></LI>
<LI><P>Line control. If you use a program to combine or rearrange source files
into an intermediate file which is then compiled, you can use line
control to inform the compiler where each source line originally came
from.
</P></LI>
<LI><P>Diagnostics. You can detect problems at compile time and issue errors
or warnings.</P></LI>
</UL>
<P>There are a few more, less useful, features.
<BR><BR>
Except for expansion of predefined macros, all these operations are
triggered with <U>preprocessing directives</U>. Preprocessing directives
are lines in your program that start with <CODE>#</CODE>. Whitespace is
allowed before and after the <CODE>#</CODE>. The <CODE>#</CODE> is followed by an
identifier, the <U>directive name</U>. It specifies the operation to
perform. Directives are commonly referred to as <CODE>#<I>name</I></CODE>
where <I>name</I> is the directive name. For example, <CODE>#define</CODE> is
the directive that defines a macro.
<BR><BR>
The <CODE>#</CODE> which begins a directive cannot come from a macro
expansion. Also, the directive name is not macro expanded. Thus, if
<CODE>foo</CODE> is defined as a macro expanding to <CODE>define</CODE>, that does
not make <CODE>#foo</CODE> a valid preprocessing directive.
<BR><BR>
The set of valid directive names is fixed. Programs cannot define new
preprocessing directives.
<BR><BR>
Some directives require arguments; these make up the rest of the
directive line and must be separated from the directive name by
whitespace. For example, <CODE>#define</CODE> must be followed by a macro
name and the intended expansion of the macro.
<BR><BR>
A preprocessing directive cannot cover more than one line. The line
may, however, be continued with backslash-newline, or by a block comment
which extends past the end of the line. In either case, when the
directive is processed, the continuations have already been merged with
the first line to make one long line.</P>
<HR>
<H2><A NAME="SEC4"><U>Header Files</U></A></H2>
<P>A header file is a file containing C declarations and macro definitions
(see <A HREF="#SEC10">Macros</A>) to be shared between several source files. You request
the use of a header file in your program by <U>including</U> it, with the
C preprocessing directive <CODE>#include</CODE>.
<BR><BR>
Header files serve two purposes.</P>
<UL>
<LI><P>System header files declare the interfaces to parts of the operating
system. You include them in your program to supply the definitions and
declarations you need to invoke system calls and libraries.
</P></LI>
<LI><P>Your own header files contain declarations for interfaces between the
source files of your program. Each time you have a group of related
declarations and macro definitions all or most of which are needed in
several different source files, it is a good idea to create a header
file for them.</P></LI>
</UL>
<P>Including a header file produces the same results as copying the header
file into each source file that needs it. Such copying would be
time-consuming and error-prone. With a header file, the related
declarations appear in only one place. If they need to be changed, they
can be changed in one place, and programs that include the header file
will automatically use the new version when next recompiled. The header
file eliminates the labor of finding and changing all the copies as well
as the risk that a failure to find one copy will result in
inconsistencies within a program.
<BR><BR>
In C, the usual convention is to give header files names that end with
<CODE>.h</CODE>. It is most portable to use only letters, digits, dashes, and
underscores in header file names, and at most one dot.</P>
<UL>
<LI><B><A HREF="#SEC6">Include Syntax</A></B>
<LI><B><A HREF="#SEC7">Include Operation</A></B>
<LI><B><A HREF="#SEC8">Once-Only Headers</A></B>
<LI><B><A HREF="#SEC8a">Computed Includes</A></B>
<LI><B><A HREF="#SEC9">Wrapper Headers</A></B>
<LI><B><A HREF="#SEC9a">System Headers</A></B>
</UL>
<H3><A NAME="SEC6"><U>Include Syntax</U></A></H3>
<P>Both user and system header files are included using the preprocessing
directive <CODE>#include</CODE>. It has two variants:</P>
<DL>
<DT><P><B>#include <<I>file</I>></B></P><DD><P>This variant is used for system header files. It searches for a file
named <I>file</I> in a standard list of system directories. You can prepend
directories to this list with the <B>'-I'</B> option (see <A HREF="#SEC44">Invocation</A>).
</P><DT><P><B>#include "<I>file</I>"</B></P><DD><P>This variant is used for header files of your own program. It searches
for a file named <I>file</I> first in the directory containing the
current file, then in the same directories used for <CODE><<I>file</I>></CODE>.</P>
</DL>
<P>The argument of <CODE>#include</CODE>, whether delimited with quote marks or
angle brackets, behaves like a string constant in that comments are not
recognized, and macro names are not expanded. Thus, <CODE>#include
<x/*y></CODE> specifies inclusion of a system header file named <CODE>x/*y</CODE>.
<BR><BR>
However, if backslashes occur within <I>file</I>, they are considered
ordinary text characters, not escape characters. None of the character
escape sequences appropriate to string constants in C are processed.
Thus, <CODE>#include "x\n\\y"</CODE> specifies a filename containing three
backslashes. (Some systems interpret <CODE>\</CODE> as a pathname separator.
All of these also interpret <CODE>/</CODE> the same way. It is most portable
to use only <CODE>/</CODE>.)
<BR><BR>
It is an error if there is anything (other than comments) on the line
after the file name.</P>
<H3><A NAME="SEC7"><U>Include Operation</U></A></H3>
<P>The <CODE>#include</CODE> directive works by directing the C preprocessor to
scan the specified file as input before continuing with the rest of the
current file. The output from the preprocessor contains the output
already generated, followed by the output resulting from the included
file, followed by the output that comes from the text after the
<CODE>#include</CODE> directive. For example, if you have a header file
<CODE>header.h</CODE> as follows,</P>
<PRE>char *test (void);
</PRE>
<P>and a main program called <CODE>program.c</CODE> that uses the header file,
like this,</P>
<PRE>int x;
#include "header.h"
int
main (void)
{
puts (test ());
}
</PRE>
<P>the compiler will see the same token stream as it would if
<CODE>program.c</CODE> read</P>
<PRE>int x;
char *test (void);
int
main (void)
{
puts (test ());
}
</PRE>
<P>Included files are not limited to declarations and macro definitions;
those are merely the typical uses. Any fragment of a C program can be
included from another file. The include file could even contain the
beginning of a statement that is concluded in the containing file, or
the end of a statement that was started in the including file. However,
an included file must consist of complete tokens. Comments and string
literals which have not been closed by the end of an included file are
invalid. For error recovery, they are considered to end at the end of
the file.
<BR><BR>
To avoid confusion, it is best if header files contain only complete
syntactic units - function declarations or definitions, type
declarations, etc.
<BR><BR>
The line following the <CODE>#include</CODE> directive is always treated as a
separate line by the C preprocessor, even if the included file lacks a
final newline.</P>
<H3><A NAME="SEC8"><U>Once-Only Headers</U></A></H3>
<P>If a header file happens to be included twice, the compiler will process
its contents twice. This is very likely to cause an error, e.g. when the
compiler sees the same structure definition twice. Even if it does not,
it will certainly waste time.
<BR><BR>
The standard way to prevent this is to enclose the entire real contents
of the file in a conditional, like this:</P>
<PRE>/* File foo. */
#ifndef FILE_FOO_SEEN
#define FILE_FOO_SEEN
<I>the entire file</I>
#endif /* !FILE_FOO_SEEN */
</PRE>
<P>This construct is commonly known as a <U>wrapper #ifndef</U>.
When the header is included again, the conditional will be false,
because <CODE>FILE_FOO_SEEN</CODE> is defined. The preprocessor will skip
over the entire contents of the file, and the compiler will not see it
twice.
<BR><BR>
CPP optimizes even further. It remembers when a header file has a
wrapper <CODE>#ifndef</CODE>. If a subsequent <CODE>#include</CODE> specifies that
header, and the macro in the <CODE>#ifndef</CODE> is still defined, it does
not bother to rescan the file at all.
<BR><BR>
You can put comments outside the wrapper. They will not interfere with
this optimization.
<BR><BR>
The macro <CODE>FILE_FOO_SEEN</CODE> is called the <U>controlling macro</U> or
<U>guard macro</U>. In a user header file, the macro name should not
begin with <CODE>_</CODE>. In a system header file, it should begin with
<CODE>__</CODE> to avoid conflicts with user programs. In any kind of header
file, the macro name should contain the name of the file and some
additional text, to avoid conflicts with other header files.</P>
<H3><A NAME="SEC8a"><U>Computed Includes</U></A></H3>
<P>Sometimes it is necessary to select one of several different header
files to be included into your program. They might specify
configuration parameters to be used on different sorts of operating
systems, for instance. You could do this with a series of conditionals,</P>
<PRE>#if SYSTEM_1
# include "system_1.h"
#elif SYSTEM_2
# include "system_2.h"
#elif SYSTEM_3
...
#endif
</PRE>
<P>That rapidly becomes tedious. Instead, the preprocessor offers the
ability to use a macro for the header name. This is called a
<U>computed include</U>. Instead of writing a header name as the direct
argument of <CODE>#include</CODE>, you simply put a macro name there instead:</P>
<PRE>#define SYSTEM_H "system_1.h"
...
#include SYSTEM_H
</PRE>
<P><CODE>SYSTEM_H</CODE> will be expanded, and the preprocessor will look for
<CODE>system_1.h</CODE> as if the <CODE>#include</CODE> had been written that way
originally. <CODE>SYSTEM_H</CODE> could be defined by your Makefile with a
<B>'-D'</B> option.
<BR><BR>
You must be careful when you define the macro. <CODE>#define</CODE> saves
tokens, not text. The preprocessor has no way of knowing that the macro
will be used as the argument of <CODE>#include</CODE>, so it generates
ordinary tokens, not a header name. This is unlikely to cause problems
if you use double-quote includes, which are close enough to string
constants. If you use angle brackets, however, you may have trouble.
<BR><BR>
The syntax of a computed include is actually a bit more general than the
above. If the first non-whitespace character after <CODE>#include</CODE> is
not <CODE>"</CODE> or <CODE><</CODE>, then the entire line is macro-expanded
like running text would be.
<BR><BR>
If the line expands to a single string constant, the contents of that
string constant are the file to be included. CPP does not re-examine the
string for embedded quotes, but neither does it process backslash
escapes in the string. Therefore</P>
<PRE>#define HEADER "a\"b"
#include HEADER
</PRE>
<P>looks for a file named <CODE>a\"b</CODE>. CPP searches for the file according
to the rules for double-quoted includes.
<BR><BR>
If the line expands to a token stream beginning with a <CODE><</CODE> token
and including a <CODE>></CODE> token, then the tokens between the <CODE><</CODE> and
the first <CODE>></CODE> are combined to form the filename to be included.
Any whitespace between tokens is reduced to a single space; then any
space after the initial <CODE><</CODE> is retained, but a trailing space
before the closing <CODE>></CODE> is ignored. CPP searches for the file
according to the rules for angle-bracket includes.
<BR><BR>
In either case, if there are any tokens on the line after the file name,
an error occurs and the directive is not processed. It is also an error
if the result of expansion does not match either of the two expected
forms.
<BR><BR>
These rules are implementation-defined behavior according to the C
standard. To minimize the risk of different compilers interpreting your
computed includes differently, we recommend you use only a single
object-like macro which expands to a string constant. This will also
minimize confusion for people reading your program.</P>
<H3><A NAME="SEC9"><U>Wrapper Headers</U></A></H3>
<P>Sometimes it is necessary to adjust the contents of a system-provided
header file without editing it directly (although it is not very likely that
this feature will ever be used in GCC4TI). GCC's <CODE>fixincludes</CODE>
operation does this, for example. One way to do that would be to create
a new header file with the same name and insert it in the search path
before the original header. That works fine as long as you're willing
to replace the old header entirely. But what if you want to refer to
the old header from the new one?
<BR><BR>
You cannot simply include the old header with <CODE>#include</CODE>. That
will start from the beginning, and find your new header again. If your
header is not protected from multiple inclusion (see <A HREF="#SEC8">Once-Only
Headers</A>), it will recurse infinitely and cause a fatal error.
<BR><BR>
You could include the old header with an absolute pathname:</P>
<PRE>#include "/usr/include/old-header.h"
</PRE>
<P>This works, but is not clean; should the system headers ever move, you
would have to edit the new headers to match.
<BR><BR>
There is no way to solve this problem within the C standard, but you can
use the GNU extension <CODE>#include_next</CODE>. It means, "Include the
<I>next</I> file with this name." This directive works like
<CODE>#include</CODE> except in searching for the specified file: it starts
searching the list of header file directories <I>after</I> the directory
in which the current file was found.
<BR><BR>
Suppose you specify <B>'-I /usr/local/include'</B>, and the list of
directories to search also includes <CODE>/usr/include</CODE>; and suppose
both directories contain <CODE>signal.h</CODE>. Ordinary <CODE>#include
<signal.h></CODE> finds the file under <CODE>/usr/local/include</CODE>. If that
file contains <CODE>#include_next <signal.h></CODE>, it starts searching
after that directory, and finds the file in <CODE>/usr/include</CODE>.
<BR><BR>
<CODE>#include_next</CODE> does not distinguish between <CODE><<I>file</I>></CODE>
and <CODE>"<I>file</I>"</CODE> inclusion, nor does it check that the file you
specify has the same name as the current file. It simply looks for the
file named, starting with the directory in the search path after the one
where the current file was found.
<BR><BR>
The use of <CODE>#include_next</CODE> can lead to great confusion. We
recommend it be used only when there is no other alternative. In
particular, it should not be used in the headers belonging to a specific
program; it should be used only to make global corrections along the
lines of <CODE>fixincludes</CODE>.</P>
<H3><A NAME="SEC9a"><U>System Headers</U></A></H3>
<P>The header files declaring interfaces to the operating system and
runtime libraries often cannot be written in strictly conforming C.
Therefore, GCC gives code found in <U>system headers</U> special
treatment. All warnings, other than those generated by <CODE>#warning</CODE>
(see <A HREF="#SEC40">Diagnostics</A>), are suppressed while GCC is processing a system
header. Macros defined in a system header are immune to a few warnings
wherever they are expanded. This immunity is granted on an ad-hoc
basis, when we find that a warning generates lots of false positives
because of code in macros defined in system headers.
<BR><BR>
Normally, only the headers found in specific directories are considered
system headers. These directories are determined when GCC is compiled.
There are, however, two ways to make normal headers into system headers.
<BR><BR>
The <B>'-isystem'</B> command line option adds its argument to the list of
directories to search for headers, just like <B>'-I'</B>. Any headers
found in that directory will be considered system headers.
<BR><BR>
All directories named by <B>'-isystem'</B> are searched <I>after</I> all
directories named by <B>'-I'</B>, no matter what their order was on the
command line. If the same directory is named by both <B>'-I'</B> and
<B>'-isystem'</B>, the <B>'-I'</B> option is ignored. GCC provides an
informative message when this occurs if <B>'-v'</B> is used.
<BR><BR>
There is also a directive, <CODE>#pragma GCC system_header</CODE>, which
tells GCC to consider the rest of the current include file a system
header, no matter where it was found. Code that comes before the
<CODE>#pragma</CODE> in the file will not be affected. <CODE>#pragma GCC
system_header</CODE> has no effect in the primary source file.
<BR><BR>
On very old systems, some of the pre-defined system header directories
get even more special treatment. GNU C++ considers code in headers
found in those directories to be surrounded by an <CODE>extern "C"</CODE>
block. There is no way to request this behavior with a <CODE>#pragma</CODE>,
or from the command line.</P>
<HR>
<H2><A NAME="SEC10"><U>Macros</U></A></H2>
<P>A <U>macro</U> is a fragment of code which has been given a name.
Whenever the name is used, it is replaced by the contents of the macro.
There are two kinds of macros. They differ mostly in what they look
like when they are used. <U>Object-like</U> macros resemble data objects
when used, <U>function-like</U> macros resemble function calls.
<BR><BR>
You may define any valid identifier as a macro, even if it is a C
keyword. The preprocessor does not know anything about keywords. This
can be useful if you wish to hide a keyword such as <CODE><A HREF="keywords.html#const">const</A></CODE> from an
older compiler that does not understand it. However, the preprocessor
operator <CODE><A HREF="#SEC38a">defined</A></CODE> can never be defined as a
macro.</P>
<UL>
<LI><B><A HREF="#SEC11">Object-like Macros</A></B>
<LI><B><A HREF="#SEC12">Function-like Macros</A></B>
<LI><B><A HREF="#SEC17">Stringification</A></B>
<LI><B><A HREF="#SEC18">Concatenation</A></B>
<LI><B><A HREF="#SEC19">Undefining and Redefining Macros</A></B>
<LI><B><A HREF="#SEC14">Predefined Macros</A></B>
<LI><B><A HREF="#SEC20">Directives Within Macro Arguments</A></B>
<LI><B><A HREF="#SEC22">Macro Pitfalls</A></B>
</UL>
<H3><A NAME="SEC11"><U>Object-like Macros</U></A></H3>
<P>An <U>object-like macro</U> is a simple identifier which will be replaced
by a code fragment. It is called object-like because it looks like a
data object in code that uses it. They are most commonly used to give
symbolic names to numeric constants.
<BR><BR>
You create macros with the <CODE>#define</CODE> directive. <CODE>#define</CODE> is
followed by the name of the macro and then the token sequence it should
be an abbreviation for, which is variously referred to as the macro's
<U>body</U>, <U>expansion</U> or <U>replacement list</U>. For example,</P>
<PRE>#define BUFFER_SIZE 1024
</PRE>
<P>defines a macro named <CODE>BUFFER_SIZE</CODE> as an abbreviation for the
token <CODE>1024</CODE>. If somewhere after this <CODE>#define</CODE> directive
there comes a C statement of the form</P>
<PRE>foo = (char *) malloc (BUFFER_SIZE);
</PRE>
<P>then the C preprocessor will recognize and <U>expand</U> the macro
<CODE>BUFFER_SIZE</CODE>. The C compiler will see the same tokens as it would
if you had written</P>
<PRE>foo = (char *) malloc (1024);
</PRE>
<P>By convention, macro names are written in upper case. Programs are
easier to read when it is possible to tell at a glance which names are
macros.
<BR><BR>
The macro's body ends at the end of the <CODE>#define</CODE> line. You may
continue the definition onto multiple lines, if necessary, using
backslash-newline. When the macro is expanded, however, it will all
come out on one line. For example,</P>
<PRE>#define NUMBERS 1, \
2, \
3
int x[] = { NUMBERS };
expands to int x[] = { 1, 2, 3 };
</PRE>
<P>The most common visible consequence of this is surprising line numbers
in error messages.
<BR><BR>
There is no restriction on what can go in a macro body provided it
decomposes into valid preprocessing tokens. Parentheses need not
balance, and the body need not resemble valid C code. (If it does not,
you may get error messages from the C compiler when you use the macro.)
<BR><BR>
The C preprocessor scans your program sequentially. Macro definitions
take effect at the place you write them. Therefore, the following input
to the C preprocessor</P>
<PRE>foo = X;
#define X 4
bar = X;
</PRE>
<P>produces</P>
<PRE>foo = X;
bar = 4;
</PRE>
<P>When the preprocessor expands a macro name, the macro's expansion
replaces the macro invocation, then the expansion is examined for more
macros to expand. For example,</P>
<PRE>#define TABLESIZE BUFSIZE
#define BUFSIZE 1024
TABLESIZE
expands to BUFSIZE
expands to 1024
</PRE>
<P><CODE>TABLESIZE</CODE> is expanded first to produce <CODE>BUFSIZE</CODE>, then that
macro is expanded to produce the final result, <CODE>1024</CODE>.
<BR><BR>
Notice that <CODE>BUFSIZE</CODE> was not defined when <CODE>TABLESIZE</CODE> was
defined. The <CODE>#define</CODE> for <CODE>TABLESIZE</CODE> uses exactly the
expansion you specify - in this case, <CODE>BUFSIZE</CODE> - and does not
check to see whether it too contains macro names. Only when you
<I>use</I> <CODE>TABLESIZE</CODE> is the result of its expansion scanned for
more macro names.
<BR><BR>
This makes a difference if you change the definition of <CODE>BUFSIZE</CODE>
at some point in the source file. <CODE>TABLESIZE</CODE>, defined as shown,
will always expand using the definition of <CODE>BUFSIZE</CODE> that is
currently in effect:</P>
<PRE>#define BUFSIZE 1020
#define TABLESIZE BUFSIZE
#undef BUFSIZE
#define BUFSIZE 37
</PRE>
<P>Now <CODE>TABLESIZE</CODE> expands (in two stages) to <CODE>37</CODE>.
<BR><BR>
If the expansion of a macro contains its own name, either directly or
via intermediate macros, it is not expanded again when the expansion is
examined for more macros. This prevents infinite recursion.
See <A HREF="#SEC27">Self-Referential Macros</A> for the precise details.</P>
<H3><A NAME="SEC12"><U>Function-like Macros</U></A></H3>
<P>You can also define macros whose use looks like a function call. These
are called <U>function-like macros</U>. To define a function-like macro,
you use the same <CODE>#define</CODE> directive, but you put a pair of
parentheses immediately after the macro name. For example,</P>
<PRE>#define lang_init() c_init()
lang_init()
expands to c_init()
</PRE>
<P>A function-like macro is only expanded if its name appears with a pair
of parentheses after it. If you write just the name, it is left alone.
This can be useful when you have a function and a macro of the same
name, and you wish to use the function sometimes.</P>
<PRE>extern void foo(void);
#define foo() /* optimized inline version */
...
foo();
funcptr = foo;
</PRE>
<P>Here the call to <CODE>foo()</CODE> will use the macro, but the function
pointer will get the address of the real function. If the macro were to
be expanded, it would cause a syntax error.
<BR><BR>
If you put spaces between the macro name and the parentheses in the
macro definition, that does not define a function-like macro, it defines
an object-like macro whose expansion happens to begin with a pair of
parentheses.</P>
<PRE>#define lang_init () c_init()
lang_init()
expands to () c_init()()
</PRE>
<P>The first two pairs of parentheses in this expansion come from the
macro. The third is the pair that was originally after the macro
invocation. Since <CODE>lang_init</CODE> is an object-like macro, it does not
consume those parentheses.</P>
<UL>
<LI><B><A HREF="#SEC12a">Macro Arguments</A></B>
<LI><B><A HREF="#SEC13">Variadic Macros</A></B>
</UL>
<H4><A NAME="SEC12a"><U>Macro Arguments</U></A></H4>
<P>Function-like macros can take <U>arguments</U>, just like true functions.
To define a macro that uses arguments, you insert <U>parameters</U>
between the pair of parentheses in the macro definition that make the
macro function-like. The parameters must be valid C identifiers,
separated by commas and optionally whitespace.
<BR><BR>
To invoke a macro that takes arguments, you write the name of the macro
followed by a list of <U>actual arguments</U> in parentheses, separated
by commas. The invocation of the macro need not be restricted to a
single logical line - it can cross as many lines in the source file as
you wish. The number of arguments you give must match the number of
parameters in the macro definition. When the macro is expanded, each
use of a parameter in its body is replaced by the tokens of the
corresponding argument. (You need not use all of the parameters in the
macro body.)
<BR><BR>
As an example, here is a macro that computes the minimum of two numeric
values, as it is defined in many C programs, and some uses.</P>
<PRE>#define min(X, Y) ((X) < (Y) ? (X) : (Y))
x = min(a, b); expands to x = ((a) < (b) ? (a) : (b));
y = min(1, 2); expands to y = ((1) < (2) ? (1) : (2));
z = min(a + 28, *p); expands to z = ((a + 28) < (*p) ? (a + 28) : (*p));
</PRE>
<P>(In this small example you can already see several of the dangers of
macro arguments. See <A HREF="#SEC22">Macro Pitfalls</A> for detailed explanations.)
<BR><BR>
Leading and trailing whitespace in each argument is dropped, and all
whitespace between the tokens of an argument is reduced to a single
space. Parentheses within each argument must balance; a comma within
such parentheses does not end the argument. However, there is no
requirement for square brackets or braces to balance, and they do not
prevent a comma from separating arguments. Thus,</P>
<PRE>macro (array[x = y, x + 1])
</PRE>
<P>passes two arguments to <CODE>macro</CODE>: <CODE>array[x = y</CODE> and <CODE>x +
1]</CODE>. If you want to supply <CODE>array[x = y, x + 1]</CODE> as an argument,
you can write it as <CODE>array[(x = y, x + 1)]</CODE>, which is equivalent C
code.
<BR><BR>
All arguments to a macro are completely macro-expanded before they are
substituted into the macro body. After substitution, the complete text
is scanned again for macros to expand, including the arguments. This rule
may seem strange, but it is carefully designed so you need not worry
about whether any function call is actually a macro invocation. You can
run into trouble if you try to be too clever, though. See <A HREF="#SEC28">Argument
Prescan</A> for detailed discussion.
<BR><BR>
For example, <CODE>min (min (a, b), c)</CODE> is first expanded to</P>
<PRE> min (((a) < (b) ? (a) : (b)), (c))
</PRE>
<P>and then to</P>
<PRE>((((a) < (b) ? (a) : (b))) < (c)
? (((a) < (b) ? (a) : (b)))
: (c))
</PRE>
<P>(Line breaks shown here for clarity would not actually be generated.)
<BR><BR>
You can leave macro arguments empty; this is not an error to the
preprocessor (but many macros will then expand to invalid code).
You cannot leave out arguments entirely; if a macro takes two arguments,
there must be exactly one comma at the top level of its argument list.
Here are some silly examples using <CODE>min</CODE>:</P>
<PRE>min(, b) expands to (( ) < (b) ? ( ) : (b))
min(a, ) expands to ((a ) < ( ) ? (a ) : ( ))
min(,) expands to (( ) < ( ) ? ( ) : ( ))
min((,),) expands to (((,)) < ( ) ? ((,)) : ( ))
min() Error: macro "min" requires 2 arguments, but only 1 given
min(,,) Error: macro "min" passed 3 arguments, but takes just 2
</PRE>
<P>Whitespace is not a preprocessing token, so if a macro <CODE>foo</CODE> takes
one argument, <CODE>foo ()</CODE> and <CODE>foo ( )</CODE> both supply it an
empty argument. Previous GNU preprocessor implementations and
documentation were incorrect on this point, insisting that a