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

📄 extend.texi

📁 gcc库的原代码,对编程有很大帮助.
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
To initialize a range of elements to the same value, write@samp{[@var{first} ... @var{last}] = @var{value}}.  For example,@exampleint widths[] = @{ [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 @};@end example@noindentNote that the length of the array is the highest value specifiedplus one.In a structure initializer, specify the name of a field to initializewith @samp{@var{fieldname}:} before the element value.  For example,given the following structure, @examplestruct point @{ int x, y; @};@end example@noindentthe following initialization@examplestruct point p = @{ y: yvalue, x: xvalue @};@end example@noindentis equivalent to@examplestruct point p = @{ xvalue, yvalue @};@end exampleAnother syntax which has the same meaning is @samp{.@var{fieldname} =}.,as shown here:@examplestruct point p = @{ .y = yvalue, .x = xvalue @};@end exampleYou can also use an element label (with either the colon syntax or theperiod-equal syntax) when initializing a union, to specify which elementof the union should be used.  For example,@exampleunion foo @{ int i; double d; @};union foo f = @{ d: 4 @};@end example@noindentwill convert 4 to a @code{double} to store it in the union usingthe second element.  By contrast, casting 4 to type @code{union foo}would store it into the union as the integer @code{i}, since it isan integer.  (@xref{Cast to Union}.)You can combine this technique of naming elements with ordinary Cinitialization of successive elements.  Each initializer element thatdoes not have a label applies to the next consecutive element of thearray or structure.  For example,@exampleint a[6] = @{ [1] = v1, v2, [4] = v4 @};@end example@noindentis equivalent to@exampleint a[6] = @{ 0, v1, v2, 0, v4, 0 @};@end exampleLabeling the elements of an array initializer is especially usefulwhen the indices are characters or belong to an @code{enum} type.For example:@exampleint whitespace[256]  = @{ [' '] = 1, ['\t'] = 1, ['\h'] = 1,      ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 @};@end example@node Case Ranges@section Case Ranges@cindex case ranges@cindex ranges in case statementsYou can specify a range of consecutive values in a single @code{case} label,like this:@examplecase @var{low} ... @var{high}:@end example@noindentThis has the same effect as the proper number of individual @code{case}labels, one for each integer value from @var{low} to @var{high}, inclusive.This feature is especially useful for ranges of ASCII character codes:@examplecase 'A' ... 'Z':@end example@strong{Be careful:} Write spaces around the @code{...}, for otherwiseit may be parsed wrong when you use it with integer values.  For example,write this:@examplecase 1 ... 5:@end example@noindent rather than this:@examplecase 1...5:@end example@node Cast to Union@section Cast to a Union Type@cindex cast to a union@cindex union, casting to a A cast to union type is similar to other casts, except that the typespecified is a union type.  You can specify the type either with@code{union @var{tag}} or with a typedef name.  A cast to union is actuallya constructor though, not a cast, and hence does not yield an lvalue likenormal casts.  (@xref{Constructors}.)The types that may be cast to the union type are those of the membersof the union.  Thus, given the following union and variables:@exampleunion foo @{ int i; double d; @};int x;double y;@end example@noindentboth @code{x} and @code{y} can be cast to type @code{union} foo.Using the cast as the right-hand side of an assignment to a variable ofunion type is equivalent to storing in a member of the union:@exampleunion foo u;@dots{}u = (union foo) x  @equiv{}  u.i = xu = (union foo) y  @equiv{}  u.d = y@end exampleYou can also use the union cast as a function argument:@examplevoid hack (union foo);@dots{}hack ((union foo) x);@end example@node Function Attributes@section Declaring Attributes of Functions@cindex function attributes@cindex declaring attributes of functions@cindex functions that never return@cindex functions that have no side effects@cindex functions in arbitrary sections@cindex @code{volatile} applied to function@cindex @code{const} applied to function@cindex functions with @code{printf} or @code{scanf} style arguments@cindex functions that are passed arguments in registers on the 386@cindex functions that pop the argument stack on the 386@cindex functions that do not pop the argument stack on the 386In GNU C, you declare certain things about functions called in your programwhich help the compiler optimize function calls and check your code morecarefully.The keyword @code{__attribute__} allows you to specify specialattributes when making a declaration.  This keyword is followed by anattribute specification inside double parentheses.  Eight attributes,@code{noreturn}, @code{const}, @code{format}, @code{section},@code{constructor}, @code{destructor}, @code{unused} and @code{weak} arecurrently defined for functions.  Other attributes, including@code{section} are supported for variables declarations (@pxref{VariableAttributes}) and for types (@pxref{Type Attributes}).You may also specify attributes with @samp{__} preceding and followingeach keyword.  This allows you to use them in header files withoutbeing concerned about a possible macro of the same name.  For example,you may use @code{__noreturn__} instead of @code{noreturn}.@table @code@cindex @code{noreturn} function attribute@item noreturnA few standard library functions, such as @code{abort} and @code{exit},cannot return.  GNU CC knows this automatically.  Some programs definetheir own functions that never return.  You can declare them@code{noreturn} to tell the compiler this fact.  For example,@smallexamplevoid fatal () __attribute__ ((noreturn));voidfatal (@dots{})@{  @dots{} /* @r{Print error message.} */ @dots{}  exit (1);@}@end smallexampleThe @code{noreturn} keyword tells the compiler to assume that@code{fatal} cannot return.  It can then optimize without regard to whatwould happen if @code{fatal} ever did return.  This makes slightlybetter code.  More importantly, it helps avoid spurious warnings ofuninitialized variables.Do not assume that registers saved by the calling function arerestored before calling the @code{noreturn} function.It does not make sense for a @code{noreturn} function to have a returntype other than @code{void}.The attribute @code{noreturn} is not implemented in GNU C versionsearlier than 2.5.  An alternative way to declare that a function doesnot return, which works in the current version and in some olderversions, is as follows:@smallexample  typedef void voidfn ();volatile voidfn fatal;@end smallexample@cindex @code{const} function attribute@item constMany functions do not examine any values except their arguments, andhave no effects except the return value.  Such a function can be subjectto common subexpression elimination and loop optimization just as anarithmetic operator would be.  These functions should be declaredwith the attribute @code{const}.  For example,@smallexampleint square (int) __attribute__ ((const));@end smallexample@noindentsays that the hypothetical function @code{square} is safe to callfewer times than the program says.The attribute @code{const} is not implemented in GNU C versions earlierthan 2.5.  An alternative way to declare that a function has no sideeffects, which works in the current version and in some older versions,is as follows:@smallexampletypedef int intfn ();extern const intfn square;@end smallexampleThis approach does not work in GNU C++ from 2.6.0 on, since the languagespecifies that the @samp{const} must be attached to the return value.@cindex pointer argumentsNote that a function that has pointer arguments and examines the datapointed to must @emph{not} be declared @code{const}.  Likewise, afunction that calls a non-@code{const} function usually must not be@code{const}.  It does not make sense for a @code{const} function toreturn @code{void}.@item format (@var{archetype}, @var{string-index}, @var{first-to-check})@cindex @code{format} function attributeThe @code{format} attribute specifies that a function takes @code{printf}or @code{scanf} style arguments which should be type-checked against aformat string.  For example, the declaration:@smallexampleextern intmy_printf (void *my_object, const char *my_format, ...)      __attribute__ ((format (printf, 2, 3)));@end smallexample@noindentcauses the compiler to check the arguments in calls to @code{my_printf}for consistency with the @code{printf} style format string argument@code{my_format}.The parameter @var{archetype} determines how the format string isinterpreted, and should be either @code{printf} or @code{scanf}.  Theparameter @var{string-index} specifies which argument is the formatstring argument (starting from 1), while @var{first-to-check} is thenumber of the first argument to check against the format string.  Forfunctions where the arguments are not available to be checked (such as@code{vprintf}), specify the third parameter as zero.  In this case thecompiler only checks the format string for consistency.In the example above, the format string (@code{my_format}) is the secondargument of the function @code{my_print}, and the arguments to checkstart with the third argument, so the correct parameters for the formatattribute are 2 and 3.The @code{format} attribute allows you to identify your own functionswhich take format strings as arguments, so that GNU CC can check thecalls to these functions for errors.  The compiler always checks formatsfor the ANSI library functions @code{printf}, @code{fprintf},@code{sprintf}, @code{scanf}, @code{fscanf}, @code{sscanf},@code{vprintf}, @code{vfprintf} and @code{vsprintf} whenever suchwarnings are requested (using @samp{-Wformat}), so there is no need tomodify the header file @file{stdio.h}.@item section ("section-name")@cindex @code{section} function attributeNormally, the compiler places the code it generates in the @code{text} section.Sometimes, however, you need additional sections, or you need certainparticular functions to appear in special sections.  The @code{section}attribute specifies that a function lives in a particular section.For example, the declaration:@smallexampleextern void foobar (void) __attribute__ ((section ("bar")));@end smallexample@noindentputs the function @code{foobar} in the @code{bar} section.Some file formats do not support arbitrary sections so the @code{section}attribute is not available on all platforms.If you need to map the entire contents of a module to a particularsection, consider using the facilities of the linker instead.@item constructor@itemx destructor@cindex @code{constructor} function attribute@cindex @code{destructor} function attributeThe @code{constructor} attribute causes the function to be calledautomatically before execution enters @code{main ()}.  Similarly, the@code{destructor} attribute causes the function to be calledautomatically after @code{main ()} has completed or @code{exit ()} hasbeen called.  Functions with these attributes are useful forinitializing data that will be used implicitly during the execution ofthe program.These attributes are not currently implemented for Objective C.@item unusedThis attribute, attached to a function, means that the function is meantto be possibly unused.  GNU CC will not produce a warning for thisfunction.@item weak@cindex @code{weak} attributeThe @code{weak} attribute causes the declaration to be emitted as a weaksymbol rather than a global.  This is primarily useful in defininglibrary functions which can be overridden in user code, though it canalso be used with non-function declarations.  Weak symbols are supportedfor ELF targets, and also for a.out targets when using the GNU assemblerand linker.@item alias ("target")@cindex @code{alias} attributeThe @code{alias} attribute causes the declaration to be emitted as analias for another symbol, which must be specified.  For instance,@smallexamplevoid __f () @{ /* do something */; @}void f () __attribute__ ((weak, alias ("__f")));@end smallexample

⌨️ 快捷键说明

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