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

📄 cpp.texinfo

📁 这是完整的gcc源代码
💻 TEXINFO
📖 第 1 页 / 共 5 页
字号:
/* File header.h */char *test ();@end example@noindentthe output generated by the C preprocessor for @file{program.c} as inputwould be@exampleint x;char *test ();main ()@{  printf (test ());@}@end exampleIncluded files are not limited to declarations and macro definitions; theyare merely the typical use.  Any fragment of a C program can be includedfrom another file.  The include file could even contain the beginning of astatement that is concluded in the containing file, or the end of astatement that was started in the including file.  However, a comment or astring or character constant may not start in the included file and finishin the including file.  An unterminated comment, string constant orcharacter constant in an included file is considered to end (with an errormessage) at the end of the file.The line following the @samp{#include} command is always treated as aseparate line by the C preprocessor even if the included file lacks a finalnewline.@node Once-Only,, Include Operation, Header Files@subsection Once-Only Include Files@cindex repeated inclusionVery often, one header file includes another.  It can easily result that acertain header file is included more than once.  This may lead to errors,if the header file defines structure types or typedefs, and is certainlywasteful.  Therefore, we often wish to prevent multiple inclusion of aheader file.The standard way to do this is to enclose the entire real contents of thefile in a conditional, like this:@example#ifndef __FILE_FOO_SEEN__#define __FILE_FOO_SEEN__@var{the entire file}#endif /* __FILE_FOO_SEEN__ */@end exampleThe macro @code{__FILE_FOO_SEEN__} indicates that the file has beenincluded once already; its name should begin with @samp{__}, and shouldcontain the name of the file to avoid accidental conflicts.One drawback of this method is that the preprocessor must scan the inputfile completely in order to determine that all of it is to be ignored.This makes compilation slower.  You can avoid the delay by inserting thefollowing command near the beginning of file @emph{in addition to theconditionals described above}:@example#pragma once@end exampleThis command tells the GNU C preprocessor to ignore any future commandsto include the same file (whichever file the @samp{#pragma} appears in).You should not @emph{rely} on @samp{#pragma once} to prevent multipleinclusion of the file.  It is just a hint, and a nonstandard one atthat.  Most C compilers will ignore it entirely.  For this reason, youstill need the conditionals if you want to make certain that the file'scontents are not included twice.Note that @samp{#pragma once} works by file name; if a file has morethan one name, it can be included once under each name, even in GNU CC,despite @samp{#pragma once}.@node Macros, Conditionals, Header Files, Top@section MacrosA macro is a sort of abbreviation which you can define once and thenuse later.  There are many complicated features associated with macrosin the C preprocessor.@menu* Simple Macros::    Macros that always expand the same way.* Argument Macros::  Macros that accept arguments that are substituted                       into the macro expansion.* Predefined::       Predefined macros that are always available.* Stringification::  Macro arguments converted into string constants.* Concatenation::    Building tokens from parts taken from macro arguments.* Undefining::       Cancelling a macro's definition.* Redefining::       Changing a macro's definition.* Macro Pitfalls::   Macros can confuse the unwary.  Here we explain                       several common problems and strange features.@end menu@node Simple Macros, Argument Macros, Macros, Macros@subsection Simple MacrosA @dfn{simple macro} is a kind of abbreviation.  It is a name which standsfor a fragment of code.Before you can use a macro, you must @dfn{define} it explicitly with the@samp{#define} command.  @samp{#define} is followed by the name of themacro and then the code it should be an abbreviation for.  For example,@example#define BUFFER_SIZE 1020@end example@noindentdefines a macro named @samp{BUFFER_SIZE} as an abbreviation for the text@samp{1020}.  Therefore, if somewhere after this @samp{#define} commandthere comes a C statement of the form@examplefoo = (char *) xmalloc (BUFFER_SIZE);@end example@noindentthen the C preprocessor will recognize and @dfn{expand} the macro@samp{BUFFER_SIZE}, resulting in@examplefoo = (char *) xmalloc (1020);@end example@noindentthe definition must be a single line; however, it may not end in themiddle of a multi-line string constant or character constant.The use of all upper case for macro names is a standard convention.Programs are easier to read when it is possible to tell at a glance whichnames are macros.Normally, a macro definition must be a single line, like all C preprocessorcommands.  (You can split a long macro definition cosmetically withBackslash-Newline.)  There is one exception: Newlines can be included inthe macro definition if within a string or character constant.  By the sametoken, it is not possible for a macro definition to contain an unbalancedquote character; the definition automatically extends to include thematching quote character that ends the string or character constant.Comments within a macro definition may contain Newlines, which make nodifference since the comments are entirely replaced with Spaces regardlessof their contents.Aside from the above, there is no restriction on what can go in a macrobody.  Parentheses need not balance.  The body need not resemble valid Ccode.  (Of course, you might get error messages from the C compiler whenyou use the macro.)The C preprocessor scans your program sequentially, so macro definitionstake effect at the place you write them.  Therefore, the following input tothe C preprocessor@examplefoo = X;#define X 4bar = X;@end example@noindentproduces as output@examplefoo = X;bar = 4;@end exampleAfter the preprocessor expands a macro name, the macro's definition body isappended to the front of the remaining input, and the check for macro callscontinues.  Therefore, the macro body can contain calls to other macros.For example, after@example#define BUFSIZE 1020#define TABLESIZE BUFSIZE@end example@noindentthe name @samp{TABLESIZE} when used in the program would go through twostages of expansion, resulting ultimately in @samp{1020}.This is not at all the same as defining @samp{TABLESIZE} to be @samp{1020}.The @samp{#define} for @samp{TABLESIZE} uses exactly the body youspecify---in this case, @samp{BUFSIZE}---and does not check to see whetherit too is the name of a macro.  It's only when you @emph{use} @samp{TABLESIZE}that the result of its expansion is checked for more macro names.@xref{Cascaded Macros}.@node Argument Macros, Predefined, Simple Macros, Macros@subsection Macros with ArgumentsA simple macro always stands for exactly the same text, each time it isused.  Macros can be more flexible when they accept @dfn{arguments}.Arguments are fragments of code that you supply each time the macro isused.  These fragments are included in the expansion of the macro accordingto the directions in the macro definition.To define a macro that uses arguments, you write a @samp{#define} commandwith a list of @dfn{argument names} in parentheses after the name of themacro.  The argument names may be any valid C identifiers, separated bycommas and optionally whitespace.  The open-parenthesis must follow themacro name immediately, with no space in between.For example, here is a macro that computes the minimum of two numericvalues, as it is defined in many C programs:@example#define min(X, Y)  ((X) < (Y) ? (X) : (Y))@end example@noindent(This is not the best way to define a ``minimum'' macro in GNU C.@xref{Side Effects}, for more information.)To use a macro that expects arguments, you write the name of the macrofollowed by a list of @dfn{actual arguments} in parentheses. separated bycommas.  The number of actual arguments you give must match the number ofarguments the macro expects.   Examples of use of the macro @samp{min}include @samp{min (1, 2)} and @samp{min (x + 28, *p)}.The expansion text of the macro depends on the arguments you use.Each of the argument names of the macro is replaced, throughout themacro definition, with the corresponding actual argument.  Using thesame macro @samp{min} defined above, @samp{min (1, 2)} expands into@example((1) < (2) ? (1) : (2))@end example@noindentwhere @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 requirement forbrackets or braces to balance; thus, 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 C code.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@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 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 leaves thename unaltered.  Therefore, it is possible for the same name to be avariable or function in your program as well as a macro, and you canchoose 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}.It is not allowed to define the same name as both a simple macro anda macro with arguments.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 name 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 formof a C string constant.

⌨️ 快捷键说明

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