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

📄 extend.texi

📁 早期freebsd实现
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
@exampleeprintf ("%s:%d: ", input_file_name, line_number)@expansion{}fprintf (stderr, "%s:%d: ", input_file_name, line_number)@end example@noindentNote that the comma after the string constant comes from the definitionof @code{eprintf}, whereas the last comma comes from the value of@code{args}.The reason for using @samp{##} is to handle the case when @code{args}matches no arguments at all.  In this case, @code{args} has an emptyvalue.  In this case, the second comma in the definition becomes anembarrassment: if it got through to the expansion of the macro, we wouldget something like this:@examplefprintf (stderr, "success!\n", )@end example@noindentwhich is invalid C syntax.  @samp{##} gets rid of the comma, so we getthe following instead:@examplefprintf (stderr, "success!\n")@end exampleThis is a special feature of the GNU C preprocessor: @samp{##} adjacentto a rest argument discards the token on the other side of the@samp{##}, if the rest argument value is empty.@node Subscripting@section Non-Lvalue Arrays May Have Subscripts@cindex subscripting@cindex arrays, non-lvalue@cindex subscripting and function valuesSubscripting is allowed on arrays that are not lvalues, even though theunary @samp{&} operator is not.  For example, this is valid in GNU C thoughnot valid in other C dialects:@examplestruct foo @{int a[4];@};struct foo f();bar (int index)@{  return f().a[index];@}@end example@node Pointer Arith@section Arithmetic on @code{void}- and Function-Pointers@cindex void pointers, arithmetic@cindex void, size of pointer to@cindex function pointers, arithmetic@cindex function, size of pointer toIn GNU C, addition and subtraction operations are supported on pointers to@code{void} and on pointers to functions.  This is done by treating thesize of a @code{void} or of a function as 1.A consequence of this is that @code{sizeof} is also allowed on @code{void}and on function types, and returns 1.The option @samp{-Wpointer-arith} requests a warning if these extensionsare used.@node Initializers@section Non-Constant Initializers@cindex initializers, non-constant@cindex non-constant initializersThe elements of an aggregate initializer for an automatic variable arenot required to be constant expressions in GNU C.  Here is an example ofan initializer with run-time varying elements:@examplefoo (float f, float g)@{  float beat_freqs[2] = @{ f-g, f+g @};  @dots{}@}@end example@node Constructors@section Constructor Expressions@cindex constructor expressions@cindex initializations in expressions@cindex structures, constructor expression@cindex expressions, constructor GNU C supports constructor expressions.  A constructor looks likea cast containing an initializer.  Its value is an object of thetype specified in the cast, containing the elements specified inthe initializer.Usually, the specified type is a structure.  Assume that@code{struct foo} and @code{structure} are declared as shown:@examplestruct foo @{int a; char b[2];@} structure;@end example@noindentHere is an example of constructing a @code{struct foo} with a constructor:@examplestructure = ((struct foo) @{x + y, 'a', 0@});@end example@noindentThis is equivalent to writing the following:@example@{  struct foo temp = @{x + y, 'a', 0@};  structure = temp;@}@end exampleYou can also construct an array.  If all the elements of the constructorare (made up of) simple constant expressions, suitable for use ininitializers, then the constructor is an lvalue and can be coerced to apointer to its first element, as shown here:@examplechar **foo = (char *[]) @{ "x", "y", "z" @};@end exampleArray constructors whose elements are not simple constants arenot very useful, because the constructor is not an lvalue.  Thereare only two valid ways to use it: to subscript it, or initializean array variable with it.  The former is probably slower than a@code{switch} statement, while the latter does the same thing anordinary C initializer would do.  Here is an example ofsubscripting an array constructor:@exampleoutput = ((int[]) @{ 2, x, 28 @}) [input];@end exampleConstructor expressions for scalar types and union types are isalso allowed, but then the constructor expression is equivalentto a cast.@node Labeled Elements@section Labeled Elements in Initializers@cindex initializers with labeled elements@cindex labeled elements in initializers@cindex case labels in initializersStandard C requires the elements of an initializer to appear in a fixedorder, the same as the order of the elements in the array or structurebeing initialized.In GNU C you can give the elements in any order, specifying the arrayindices or structure field names they apply to.To specify an array index, write @samp{[@var{index}]} before theelement value.  For example,@exampleint a[6] = @{ [4] 29, [2] 15 @};@end example@noindentis equivalent to@exampleint a[6] = @{ 0, 0, 15, 0, 29, 0 @};@end example@noindentThe index values must be constant expressions, even if the array beinginitialized is automatic.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 exampleYou can also use an element label when initializing a union, tospecify which element of 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 like any other cast, except that the typespecified is a union type.  You can specify the type either with@code{union @var{tag}} or with a typedef name.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 @code{volatile} applied to function@cindex @code{const} applied to functionIn GNU C, you declare certain things about functions called in your programwhich help the compiler optimize function calls.A 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{volatile} to tell the compiler this fact.  For example,@exampleextern void volatile fatal ();voidfatal (@dots{})@{  @dots{} /* @r{Print error message.} */ @dots{}  exit (1);@}@end exampleThe @code{volatile} keyword tells the compiler to assume that@code{fatal} cannot return.  This makes slightly better code, but moreimportantly it helps avoid spurious warnings of uninitialized variables.It does not make sense for a @code{volatile} function to have a returntype other than @code{void}.Many 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 declared@code{const}.  For example,@exampleextern int const square ();@end example@noindentsays that the hypothetical function @code{square} is safe to callfewer times than the program says.@cindex pointer argumentsNote that a function that has pointer arguments and examines the data

⌨️ 快捷键说明

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