📄 extend.texi
字号:
if the machine supports fullword-to-doubleword a widening multiplyinstruction. Division and shifts are open-coded only on machines thatprovide special support. The operations that are not open-coded usespecial library routines that come with GNU CC.There may be pitfalls when you use @code{long long} types for functionarguments, unless you declare function prototypes. If a functionexpects type @code{int} for its argument, and you pass a value of type@code{long long int}, confusion will result because the caller and thesubroutine will disagree about the number of bytes for the argument.Likewise, if the function expects @code{long long int} and you pass@code{int}. The best way to avoid such problems is to use prototypes.@node Complex@section Complex Numbers@cindex complex numbersGNU C supports complex data types. You can declare both complex integertypes and complex floating types, using the keyword @code{__complex__}.For example, @samp{__complex__ double x;} declares @code{x} as avariable whose real part and imaginary part are both of type@code{double}. @samp{__complex__ short int y;} declares @code{y} tohave real and imaginary parts of type @code{short int}; this is notlikely to be useful, but it shows that the set of complex types iscomplete.To write a constant with a complex data type, use the suffix @samp{i} or@samp{j} (either one; they are equivalent). For example, @code{2.5fi}has type @code{__complex__ float} and @code{3i} has type@code{__complex__ int}. Such a constant always has a pure imaginaryvalue, but you can form any complex value you like by adding one to areal constant.To extract the real part of a complex-valued expression @var{exp}, write@code{__real__ @var{exp}}. Likewise, use @code{__imag__} toextract the imaginary part.The operator @samp{~} performs complex conjugation when used on a valuewith a complex type.GNU CC can allocate complex automatic variables in a noncontiguousfashion; it's even possible for the real part to be in a register whilethe imaginary part is on the stack (or vice-versa). None of thesupported debugging info formats has a way to represent noncontiguousallocation like this, so GNU CC describes a noncontiguous complexvariable as if it were two separate variables of noncomplex type.If the variable's actual name is @code{foo}, the two fictitious variables are named @code{foo$real} and @code{foo$imag}. You canexamine and set these two fictitious variables with your debugger.A future version of GDB will know how to recognize such pairs and treatthem as a single variable with a complex type.@node Zero Length@section Arrays of Length Zero@cindex arrays of length zero@cindex zero-length arrays@cindex length-zero arraysZero-length arrays are allowed in GNU C. They are very useful as the lastelement of a structure which is really a header for a variable-lengthobject:@examplestruct line @{ int length; char contents[0];@};@{ struct line *thisline = (struct line *) malloc (sizeof (struct line) + this_length); thisline->length = this_length;@}@end exampleIn standard C, you would have to give @code{contents} a length of 1, whichmeans either you waste space or complicate the argument to @code{malloc}.@node Variable Length@section Arrays of Variable Length@cindex variable-length arrays@cindex arrays of variable lengthVariable-length automatic arrays are allowed in GNU C. These arrays aredeclared like any other automatic arrays, but with a length that is nota constant expression. The storage is allocated at the point ofdeclaration and deallocated when the brace-level is exited. Forexample:@exampleFILE *concat_fopen (char *s1, char *s2, char *mode)@{ char str[strlen (s1) + strlen (s2) + 1]; strcpy (str, s1); strcat (str, s2); return fopen (str, mode);@}@end example@cindex scope of a variable length array@cindex variable-length array scope@cindex deallocating variable length arraysJumping or breaking out of the scope of the array name deallocates thestorage. Jumping into the scope is not allowed; you get an errormessage for it.@cindex @code{alloca} vs variable-length arraysYou can use the function @code{alloca} to get an effect much likevariable-length arrays. The function @code{alloca} is available inmany other C implementations (but not in all). On the other hand,variable-length arrays are more elegant.There are other differences between these two methods. Space allocatedwith @code{alloca} exists until the containing @emph{function} returns.The space for a variable-length array is deallocated as soon as the arrayname's scope ends. (If you use both variable-length arrays and@code{alloca} in the same function, deallocation of a variable-length arraywill also deallocate anything more recently allocated with @code{alloca}.)You can also use variable-length arrays as arguments to functions:@examplestruct entrytester (int len, char data[len][len])@{ @dots{}@}@end exampleThe length of an array is computed once when the storage is allocatedand is remembered for the scope of the array in case you access it with@code{sizeof}.If you want to pass the array first and the length afterward, you canuse a forward declaration in the parameter list---another GNU extension.@examplestruct entrytester (int len; char data[len][len], int len)@{ @dots{}@}@end example@cindex parameter forward declarationThe @samp{int len} before the semicolon is a @dfn{parameter forwarddeclaration}, and it serves the purpose of making the name @code{len}known when the declaration of @code{data} is parsed.You can write any number of such parameter forward declarations in theparameter list. They can be separated by commas or semicolons, but thelast one must end with a semicolon, which is followed by the ``real''parameter declarations. Each forward declaration must match a ``real''declaration in parameter name and data type.@node Macro Varargs@section Macros with Variable Numbers of Arguments@cindex variable number of arguments@cindex macro with variable arguments@cindex rest argument (in macro)In GNU C, a macro can accept a variable number of arguments, much as afunction can. The syntax for defining the macro looks much like thatused for a function. Here is an example:@example#define eprintf(format, args...) \ fprintf (stderr, format , ## args)@end exampleHere @code{args} is a @dfn{rest argument}: it takes in zero or morearguments, as many as the call contains. All of them plus the commasbetween them form the value of @code{args}, which is substituted intothe macro body where @code{args} is used. Thus, we have this expansion:@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{##} before arest argument that is empty discards the preceding sequence ofnon-whitespace characters from the macro definition. (If another macroargument precedes, none of it is discarded.)It might be better to discard the last preprocessor token instead of thelast preceding sequence of non-whitespace characters; in fact, we maysomeday change this feature to do so. We advise you to write the macrodefinition so that the preceding sequence of non-whitespace charactersis just a single token, so that the meaning will not change if we changethe definition of this feature.@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:@example@groupstruct foo @{int a[4];@};struct foo f();bar (int index)@{ return f().a[index];@}@end group@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 initializersAs in standard C++, the elements of an aggregate initializer for anautomatic variable are not required to be constant expressions in GNU C.Here is an example of an 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. This extension is notimplemented in GNU C++.To specify an array index, write @samp{[@var{index}]} or@samp{[@var{index}] =} before the element 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -