📄 cpp.texinfo
字号:
@item __BASE_FILE__@findex __BASE_FILE__This macro expands to the name of the main input file, in the formof a C string constant. This is the source file that was specifiedas an argument when the C compiler was invoked.@item __LINE__@findex __LINE__This macro expands to the current input line number, in the form of adecimal integer constant. While we call it a predefined macro, it'sa pretty strange macro, since its ``definition'' changes with eachnew line of source code.This and @samp{__FILE__} are useful in generating an error message toreport an inconsistency detected by the program; the message can statethe source line at which the inconsistency was detected. For example,@examplefprintf (stderr, "Internal error: negative string length " "%d at %s, line %d.", length, __FILE__, __LINE__);@end exampleA @samp{#include} command changes the expansions of @samp{__FILE__}and @samp{__LINE__} to correspond to the included file. At the end ofthat file, when processing resumes on the input file that containedthe @samp{#include} command, the expansions of @samp{__FILE__} and@samp{__LINE__} revert to the values they had before the@samp{#include} (but @samp{__LINE__} is then incremented by one asprocessing moves to the line after the @samp{#include}).The expansions of both @samp{__FILE__} and @samp{__LINE__} are alteredif a @samp{#line} command is used. @xref{Combining Sources}.@item __DATE__@findex __DATE__This macro expands to a string constant that describes the date onwhich the preprocessor is being run. The string constant containseleven characters and looks like @samp{"Jan 29 1987"} or @w{@samp{"Apr1 1905"}}.@item __TIME__@findex __TIME__This macro expands to a string constant that describes the time atwhich the preprocessor is being run. The string constant containseight characters and looks like @samp{"23:59:01"}.@item __STDC__@findex __STDC__This macro expands to the constant 1, to signify that this is ANSIStandard C. (Whether that is actually true depends on what C compilerwill operate on the output from the preprocessor.)@item __GNUC__This macro is defined if and only if this is GNU C. This macro isdefined only when the entire GNU C compiler is in use; if you invokethe preprocessor directly, @samp{__GNUC__} is undefined.@item __STRICT_ANSI__This macro is defined if and only if the @samp{-ansi} switch wasspecified when GNU C was invoked. Its definition is the null string.This macro exists primarily to direct certain GNU header files not todefine certain traditional Unix constructs which are incompatible withANSI C.@item __VERSION__This macro expands to a string which describes the version number ofGNU C. The string is normally a sequence of decimal numbers separatedby periods, such as @samp{"1.18"}. The only reasonable use of thismacro is to incorporate it into a string constant.@item __OPTIMIZE__This macro is defined in optimizing compilations. It causes certainGNU header files to define alternative macro definitions for somesystem library functions. It is unwise to refer to or test thedefinition of this macro unless you make very sure that programs willexecute with the same effect regardless.@item __CHAR_UNSIGNED__This macro is defined if and only if the data type @code{char} isunsigned on the target machine. It exists to cause the standardheader file @file{limit.h} to work correctly. It is bad practiceto refer to this macro yourself; instead, refer to the standardmacros defined in @file{limit.h}.@end table@node Nonstandard Predefined,, Standard Predefined, Predefined@subsubsection Nonstandard Predefined MacrosThe C preprocessor normally has several predefined macros that vary betweenmachines because their purpose is to indicate what type of system andmachine is in use. This manual, being for all systems and machines, cannottell you exactly what their names are; instead, we offer a list of sometypical ones.Some nonstandard predefined macros describe the operating system in use,with more or less specificity. For example,@table @code@item unix@findex unix@samp{unix} is normally predefined on all Unix systems.@item BSD@findex BSD@samp{BSD} is predefined on recent versions of Berkeley Unix(perhaps only in version 4.3).@end tableOther nonstandard predefined macros describe the kind of CPU, with more orless specificity. For example,@table @code@item vax@findex vax@samp{vax} is predefined on Vax computers.@item mc68000@findex mc68000@samp{mc68000} is predefined on most computers whose CPU is a Motorola68000, 68010 or 68020.@item m68k@findex m68k@samp{m68k} is also predefined on most computers whose CPU is a 68000,68010 or 68020; however, some makers use @samp{mc68000} and some use@samp{m68k}. Some predefine both names. What happens in GNU Cdepends on the system you are using it on.@item M68020@findex M68020@samp{M68020} has been observed to be predefined on some systems thatuse 68020 CPUs---in addition to @samp{mc68000} and @samp{m68k} thatare less specific.@item ns32000@findex ns32000@samp{ns32000} is predefined on computers which use the NationalSemiconductor 32000 series CPU.@end tableYet other nonstandard predefined macros describe the manufacturer ofthe system. For example,@table @code@item sun@findex sun@samp{sun} is predefined on all models of Sun computers.@item pyr@findex pyr@samp{pyr} is predefined on all models of Pyramid computers.@item sequent@findex sequent@samp{sequent} is predefined on all models of Sequent computers.@end tableThese predefined symbols are not only nonstandard, they are contrary to theANSI standard because their names do not start with underscores.Therefore, the option @samp{-ansi} inhibits the definition of thesesymbols.This tends to make @samp{-ansi} useless, since many programs depend on thecustomary nonstandard predefined symbols. Even system header files checkthem and will generate incorrect declarations if they do not find the namesthat are expected. You might think that the header files supplied for theUglix computer would not need to test what machine they are running on,because they can simply assume it is the Uglix; but often they do, and theydo so using the customary names. As a result, very few C programs willcompile with @samp{-ansi}. We intend to avoid such problems on the GNUsystem.What, then, should you do in an ANSI C program to test the type of machineit is to run on?GNU C offers a parallel series of symbols for this purpose, whose namesare made from the customary ones by adding @samp{__} at the beginningand end. Thus, the symbol @code{__vax__} would be available on a vax,and so on.The set of nonstandard predefined names in the GNU C preprocessor iscontrolled by the macro @samp{CPP_PREDEFINES}, which should be a stringcontaining @samp{-D} options, separated by spaces. For example, on theSun 3, we use the following definition:@example#define CPP_PREDEFINES "-Dmc68000 -Dsun -Dunix -Dm68k"@end example@node Stringification, Concatenation, Predefined, Macros@subsection Stringification@cindex stringification@dfn{Stringification} means turning a code fragment into a string constantwhose contents are the text for the code fragment. For example,stringifying @samp{foo (z)} results in @samp{"foo (z)"}.In the C preprocessor, stringification is an option available when macroarguments are substituted into the macro definition. In the body of thedefinition, when an argument name appears, the character @samp{#} beforethe name specifies stringification of the corresponding actual argumentwhen it is substituted at that point in the definition. The same argumentmay be substituted in other places in the definition withoutstringification if the argument name appears in those places with no@samp{#}.Here is an example of a macro definition that uses stringification:@example#define WARN_IF(EXP) \do @{ if (EXP) fprintf (stderr, "Warning: " #EXP "\n"); @} while (0)@end example@noindentHere the actual argument for @samp{EXP} is substituted once as given,into the @samp{if} statement, and once as stringified, into theargument to @samp{fprintf}. The @samp{do} and @samp{while (0)} area kludge to make it possible to write @samp{WARN_IF (@var{arg});},which the resemblance of @samp{WARN_IF} to a function would makeC programmers want to do; @pxref{Swallow Semicolon}).The stringification feature is limited to transforming one macro argumentinto one string constant: there is no way to combine the argument withother text and then stringify it all together. But the example above showshow an equivalent result can be obtained in ANSI Standard C using thefeature that adjacent string constants are concatenated as one stringconstant. The preprocessor stringifies @samp{EXP}'s actual argumentinto a separate string constant, resulting in text like@exampledo @{ if (x == 0) fprintf (stderr, "Warning: " "x == 0" "\n"); @} while (0)@end example@noindentbut the C compiler then sees three consecutive string constants andconcatenates them into one, producing effectively@exampledo @{ if (x == 0) fprintf (stderr, "Warning: x == 0\n"); @} while (0)@end exampleStringification in C involves more than putting doublequote charactersaround the fragment; it is necessary to put backslashes in front of alldoublequote characters, and all backslashes in string and characterconstants, in order to get a valid C string constant with the propercontents. Thus, stringifying @samp{p = "foo\n";} results in @samp{"p =\"foo\\n\";"}. However, backslashes that are not inside of string orcharacter constants are not duplicated: @samp{\n} by itself stringifies to@samp{"\n"}.Whitespace (including comments) in the text being stringified is handledaccording to precise rules. All leading and trailing whitespace is ignored.Any sequence of whitespace in the middle of the text is converted toa single space in the stringified result.@node Concatenation, Undefining, Stringification, Macros@subsection Concatenation@cindex concatenation@dfn{Concatenation} means joining two strings into one. In the contextof macro expansion, concatenation refers to joining two lexical unitsinto one longer one. Specifically, an actual argument to the macro can beconcatenated with another actual argument or with fixed text to producea longer name. The longer name might be the name of a function,variable or type, or a C keyword; it might even be the name of anothermacro, in which case it will be expanded.When you define a macro, you request concatenation with the specialoperator @samp{##} in the macro body. When the macro is called,after actual arguments are substituted, all @samp{##} operators aredeleted, and so is any whitespace next to them (including whitespacethat was part of an actual argument). The result is to concatenatethe syntactic tokens on either side of the @samp{##}.Consider a C program that interprets named commands. There probably needsto be a table of commands, perhaps an array of structures declared asfollows:@examplestruct command@{ char *name; void (*function) ();@};struct command commands[] =@{ @{ "quit", quit_command@}, @{ "help", help_command@}, @dots{}@};@end exampleIt would be cleaner not to have to give each command name twice, once inthe string constant and once in the function name. A macro which takes thename of a command as an argument can make this unnecessary. The stringconstant can be created with stringification, and the function name byconcatenating the argument with @samp{_command}. Here is how it is done:@example#define COMMAND(NAME) @{ #NAME, NAME ## _command @}struct command commands[] =@{ COMMAND (quit), COMMAND (help), @dots{}@};@end exampleThe usual case of concatenation is concatenating two names (or a name and anumber) into a longer name. But this isn't the only valid case. It isalso possible to concatenate two numbers (or a number and a name, such as@samp{1.5} and @samp{e3}) into a number. Also, multi-character operatorssuch as @samp{+=} can be formed by concatenation. In some cases it is evenpossible to piece together a string constant. However, two pieces of textthat don't together form a valid lexical unit cannot be concatenated. Forexample, concatenation with @samp{x} on one side and @samp{+} on the otheris not meaningful because those two characters can't fit together in anylexical unit of C. The ANSI standard says that such attempts atconcatenation are undefined, but in the GNU C preprocessor it is welldefined: it puts the @samp{x} and @samp{+} side by side with no particularspecial results.Keep in mind that the C preprocessor converts comments to whitespace beforemacros are even considered. Therefore, you cannot create a comment byconcatenating @samp{/} and @samp{*}: the @samp{/*} sequence that starts acomment is not a lexical unit, but rather the beginning of a ``long'' spacecharacter. Also, you can freely use comments next to a @samp{##} in amacro definition, or in actual arguments that will be concatenated, becausethe comments will be converted to spaces at first sight, and concatenationwill later discard the spaces.@node Undefining, Redefining, Concatenation, Macros@subsection Undefining Macros@cindex undefining macrosTo @dfn{undefine} a macro means to cancel its definition. This is donewith the @samp{#undef} command. @samp{#undef} is followed by the macroname to be undefined.Like definition, undefinition occurs at a specific point in the sourcefile, and it applies starting from that point. The name ceases to be amacro name, and from that point on it is treated by the preprocessor as ifit had never been a macro name.For example,@example#define FOO 4x = FOO;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -