⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpp.texi

📁 早期freebsd实现
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
@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.)If 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 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} 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 __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} command and decremented at everyend of file.@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 __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 __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}.  The preprocessor usesthis macro to determine whether or not to sign-extend large characterconstants written in octal; see @ref{#if Command,,The @samp{#if} Command}.@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; @pxref{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 29000CPU family.@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 will 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 (when @code{cpp} is itself compiled) by the macro@samp{CPP_PREDEFINES}, which should be a string containing @samp{-D}options, separated by spaces.  For example, on the Sun 3, we use thefollowing definition:@example#define CPP_PREDEFINES "-Dmc68000 -Dsun -Dunix -Dm68k"@end example@noindent This macro is usually specified in @file{tm.h}.@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:@smallexample#define WARN_IF(EXP) \do @{ if (EXP) \        fprintf (stderr, "Warning: " #EXP "\n"); @} \while (0)@end smallexample@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}).

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -