📄 cpp.texi
字号:
where @samp{1} has been substituted for @samp{X} and @samp{2} for @samp{Y}.Likewise, @samp{min (x + 28, *p)} expands into@example((x + 28) < (*p) ? (x + 28) : (*p))@end exampleParentheses in the actual arguments must balance; a comma withinparentheses does not end an argument. However, there is no requirementfor brackets or braces to balance, and they do not prevent a comma fromseparating arguments. Thus,@examplemacro (array[x = y, x + 1])@end example@noindentpasses two arguments to @code{macro}: @samp{array[x = y} and @samp{x +1]}. If you want to supply @samp{array[x = y, x + 1]} as an argument,you must write it as @samp{array[(x = y, x + 1)]}, which is equivalent Ccode.After the actual arguments are substituted into the macro body, the entireresult is appended to the front of the remaining input, and the check formacro calls continues. Therefore, the actual arguments can contain callsto other macros, either with or without arguments, or even to the samemacro. The macro body can also contain calls to other macros. Forexample, @samp{min (min (a, b), c)} expands into this text:@example((((a) < (b) ? (a) : (b))) < (c) ? (((a) < (b) ? (a) : (b))) : (c))@end example@noindent(Line breaks shown here for clarity would not actually be generated.)@cindex blank macro arguments@cindex space as macro argumentIf a macro @code{foo} takes one argument, and you want to supply anempty argument, you must write at least some whitespace between theparentheses, like this: @samp{foo ( )}. Just @samp{foo ()} is providingno arguments, which is an error if @code{foo} expects an argument. But@samp{foo0 ()} is the correct way to call a macro defined to take zeroarguments, like this:@example#define foo0() @dots{}@end exampleIf you use the macro name followed by something other than anopen-parenthesis (after ignoring any spaces, tabs and comments thatfollow), it is not a call to the macro, and the preprocessor does notchange what you have written. Therefore, it is possible for the same nameto be a variable or function in your program as well as a macro, and youcan choose in each instance whether to refer to the macro (if an actualargument list follows) or the variable or function (if an argument listdoes not follow).Such dual use of one name could be confusing and should be avoidedexcept when the two meanings are effectively synonymous: that is, when thename is both a macro and a function and the two have similar effects. Youcan think of the name simply as a function; use of the name for purposesother than calling it (such as, to take the address) will refer to thefunction, while calls will expand the macro and generate better butequivalent code. For example, you can use a function named @samp{min} inthe same source file that defines the macro. If you write @samp{&min} withno argument list, you refer to the function. If you write @samp{min (x,bb)}, with an argument list, the macro is expanded. If you write@samp{(min) (a, bb)}, where the name @samp{min} is not followed by anopen-parenthesis, the macro is not expanded, so you wind up with a call tothe function @samp{min}.You may not define the same name as both a simple macro and a macro witharguments.In the definition of a macro with arguments, the list of argument namesmust follow the macro name immediately with no space in between. If thereis a space after the macro name, the macro is defined as taking noarguments, and all the rest of the line is taken to be the expansion. Thereason for this is that it is often useful to define a macro that takes noarguments and whose definition begins with an identifier in parentheses.This rule about spaces makes it possible for you to do either this:@example#define FOO(x) - 1 / (x)@end example@noindent(which defines @samp{FOO} to take an argument and expand into minus thereciprocal of that argument) or this:@example#define BAR (x) - 1 / (x)@end example@noindent(which defines @samp{BAR} to take no argument and always expand into@samp{(x) - 1 / (x)}).Note that the @emph{uses} of a macro with arguments can have spaces beforethe left parenthesis; it's the @emph{definition} where it matters whetherthere is a space.@node Predefined, Stringification, Argument Macros, Macros@subsection Predefined Macros@cindex predefined macrosSeveral simple macros are predefined. You can use them without givingdefinitions for them. They fall into two classes: standard macros andsystem-specific macros.@menu* Standard Predefined:: Standard predefined macros.* Nonstandard Predefined:: Nonstandard predefined macros.@end menu@node Standard Predefined, Nonstandard Predefined, Predefined, Predefined@subsubsection Standard Predefined Macros@cindex standard predefined macrosThe standard predefined macros are available with the same meaningsregardless of the machine or operating system on which you are using GNU C.Their names all start and end with double underscores. Those preceding@code{__GNUC__} in this table are standardized by ANSI C; the rest areGNU C extensions.@table @code@item __FILE__@findex __FILE__This macro expands to the name of the current input file, in the form ofa C string constant. The precise name returned is the one that wasspecified in @samp{#include} or as the input file name argument.@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,@smallexamplefprintf (stderr, "Internal error: " "negative string length " "%d at %s, line %d.", length, __FILE__, __LINE__);@end smallexampleA @samp{#include} directive 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} directive, 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} directive 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 __STDC_VERSION__@findex __STDC_VERSION__This macro expands to the C Standard's version number,a long integer constant of the form @samp{@var{yyyy}@var{mm}L}where @var{yyyy} and @var{mm} are the year and month of the Standard version.This signifies which version of the C Standard the preprocessor conforms to.Like @samp{__STDC__}, whether this version number is accuratefor the entire implementation depends on what C compilerwill operate on the output from the preprocessor.@item __GNUC__@findex __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 invoke thepreprocessor directly, @samp{__GNUC__} is undefined. The valueidentifies the major version number of GNU CC (@samp{1} for GNU CCversion 1, which is now obsolete, and @samp{2} for version 2).@item __GNUC_MINOR__@findex __GNUC_MINOR__The macro contains the minor version number of the compiler. This canbe used to work around differences between different releases of thecompiler (for example, if gcc 2.6.3 is known to support a feature, youcan test for @code{__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 6)}).The last number, @samp{3} in theexample above, denotes the bugfix level of the compiler; no macrocontains this value.@item __GNUG__@findex __GNUG__The GNU C compiler defines this when the compilation language isC++; use @samp{__GNUG__} to distinguish between GNU C and GNUC++.@item __cplusplus @findex __cplusplus The draft ANSI standard for C++ used to require predefining thisvariable. Though it is no longer required, GNU C++ continues to defineit, as do other popular C++ compilers. You can use @samp{__cplusplus}to test whether a header is compiled by a C compiler or a C++ compiler.@item __STRICT_ANSI__@findex __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 __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 __INCLUDE_LEVEL__@findex __INCLUDE_LEVEL_This macro expands to a decimal integer constant that represents thedepth of nesting in include files. The value of this macro isincremented on every @samp{#include} directive and decremented at everyend of file. For input files specified by command line arguments,the nesting level is zero.@item __VERSION__@findex __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{"2.6.0"}. The only reasonable use of thismacro is to incorporate it into a string constant.@item __OPTIMIZE__@findex __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__@findex __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}. The preprocessor usesthis macro to determine whether or not to sign-extend large characterconstants written in octal; see @ref{#if Directive,,The @samp{#if} Directive}.@item __REGISTER_PREFIX__@findex __REGISTER_PREFIX__This macro expands to a string describing the prefix applied to cpuregisters in assembler code. It can be used to write assembler codethat is usable in multiple environments. For example, in the@samp{m68k-aout} environment it expands to the string @samp{""},but in the @samp{m68k-coff} environment it expands to the string@samp{"%"}.@item __USER_LABEL_PREFIX__@findex __USER_LABEL_PREFIX__This macro expands to a string describing the prefix applied touser generated labels in assembler code. It can be used to writeassembler code that is usable in multiple environments.For example, in the @samp{m68k-aout} environment it expands to thestring @samp{"_"}, but in the @samp{m68k-coff} environment it expandsto the string @samp{""}.@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. You can use @samp{cpp -dM} to see the values ofpredefined macros; see @ref{Invocation}.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}, whichare less specific.@item _AM29K@findex _AM29K@itemx _AM29000@findex _AM29000Both @samp{_AM29K} and @samp{_AM29000} are predefined for the AMD 29000
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -