?? lang.texi
字號:
The @code{va_arg} macro returns the value of the next optional argument,and modifies the value of @var{ap} to point to the subsequent argument.Thus, successive uses of @code{va_arg} return successive optional arguments.The type of the value returned by @code{va_arg} is @var{type} asspecified in the call. @var{type} must be a self-promoting type (not@code{char} or @code{short int} or @code{float}) that matches the typeof the actual argument.@end deftypefn@comment stdarg.h@comment ANSI@deftypefn {Macro} void va_end (va_list @var{ap})This ends the use of @var{ap}. After a @code{va_end} call, further@code{va_arg} calls with the same @var{ap} may not work. You should invoke@code{va_end} before returning from the function in which @code{va_start}was invoked with the same @var{ap} argument.In the GNU C library, @code{va_end} does nothing, and you need not everuse it except for reasons of portability.@refill@end deftypefn@node Variadic Example@subsection Example of a Variadic FunctionHere is a complete sample function that accepts a variable number ofarguments. The first argument to the function is the count of remainingarguments, which are added up and the result returned. While trivial,this function is sufficient to illustrate how to use the variablearguments facility.@comment Yes, this example has been tested.@smallexample@include add.c.texi@end smallexample@node Old Varargs@subsubsection Old-Style Variadic Functions@pindex varargs.hBefore ANSI C, programmers used a slightly different facility forwriting variadic functions. The GNU C compiler still supports it;currently, it is more portable than the ANSI C facility, since supportfor ANSI C is still not universal. The header file which defines theold-fashioned variadic facility is called @file{varargs.h}.Using @file{varargs.h} is almost the same as using @file{stdarg.h}.There is no difference in how you call a variadic function;@xref{Calling Variadics}. The only difference is in how you definethem. First of all, you must use old-style non-prototype syntax, likethis:@smallexampletreebuild (va_alist) va_dcl@{@end smallexampleSecondly, you must give @code{va_start} just one argument, like this:@smallexample va_list p; va_start (p);@end smallexampleThese are the special macros used for defining old-style variadicfunctions:@comment varargs.h@comment Unix@deffn Macro va_alistThis macro stands for the argument name list required in a variadicfunction. @end deffn@comment varargs.h@comment Unix@deffn Macro va_dclThis macro declares the implicit argument or arguments for a variadicfunction.@end deffn@comment varargs.h@comment Unix@deftypefn {Macro} void va_start (va_list @var{ap})This macro, as defined in @file{varargs.h}, initializes the argumentpointer variable @var{ap} to point to the first argument of the currentfunction.@end deftypefnThe other argument macros, @code{va_arg} and @code{va_end}, are the samein @file{varargs.h} as in @file{stdarg.h}; see @ref{Argument Macros} fordetails.It does not work to include both @file{varargs.h} and @file{stdarg.h} inthe same compilation; they define @code{va_start} in conflicting ways.@node Null Pointer Constant@section Null Pointer Constant@cindex null pointer constantThe null pointer constant is guaranteed not to point to any real object.You can assign it to any pointer variable since it has type @code{void*}. The preferred way to write a null pointer constant is with@code{NULL}.@comment stddef.h@comment ANSI@deftypevr Macro {void *} NULLThis is a null pointer constant.@end deftypevrYou can also use @code{0} or @code{(void *)0} as a null pointerconstant, but using @code{NULL} is cleaner because it makes the purposeof the constant more evident.If you use the null pointer constant as a function argument, then forcomplete portability you should make sure that the function has aprototype declaration. Otherwise, if the target machine has twodifferent pointer representations, the compiler won't know whichrepresentation to use for that argument. You can avoid the problem byexplicitly casting the constant to the proper pointer type, but werecommend instead adding a prototype for the function you are calling.@node Important Data Types@section Important Data TypesThe result of subtracting two pointers in C is always an integer, but theprecise data type varies from C compiler to C compiler. Likewise, thedata type of the result of @code{sizeof} also varies between compilers.ANSI defines standard aliases for these two types, so you can refer tothem in a portable fashion. They are defined in the header file @file{stddef.h}.@pindex stddef.h@comment stddef.h@comment ANSI@deftp {Data Type} ptrdiff_tThis is the signed integer type of the result of subtracting twopointers. For example, with the declaration @code{char *p1, *p2;}, theexpression @code{p2 - p1} is of type @code{ptrdiff_t}. This willprobably be one of the standard signed integer types (@w{@code{shortint}}, @code{int} or @w{@code{long int}}), but might be a nonstandardtype that exists only for this purpose.@end deftp@comment stddef.h@comment ANSI@deftp {Data Type} size_tThis is an unsigned integer type used to represent the sizes of objects.The result of the @code{sizeof} operator is of this type, and functionssuch as @code{malloc} (@pxref{Unconstrained Allocation}) and@code{memcpy} (@pxref{Copying and Concatenation}) accept arguments ofthis type to specify object sizes.@strong{Usage Note:} @code{size_t} is the preferred way to declare anyarguments or variables that hold the size of an object.@end deftpIn the GNU system @code{size_t} is equivalent to either@w{@code{unsigned int}} or @w{@code{unsigned long int}}. These typeshave identical properties on the GNU system, and for most purposes, youcan use them interchangeably. However, they are distinct as data types,which makes a difference in certain contexts.For example, when you specify the type of a function argument in afunction prototype, it makes a difference which one you use. If thesystem header files declare @code{malloc} with an argument of type@code{size_t} and you declare @code{malloc} with an argument of type@code{unsigned int}, you will get a compilation error if @code{size_t}happens to be @code{unsigned long int} on your system. To avoid anypossibility of error, when a function argument or value is supposed tohave type @code{size_t}, never declare its type in any other way.@strong{Compatibility Note:} Implementations of C before the advent ofANSI C generally used @code{unsigned int} for representing object sizesand @code{int} for pointer subtraction results. They did notnecessarily define either @code{size_t} or @code{ptrdiff_t}. Unixsystems did define @code{size_t}, in @file{sys/types.h}, but thedefinition was usually a signed type.@node Data Type Measurements@section Data Type MeasurementsMost of the time, if you choose the proper C data type for each objectin your program, you need not be concerned with just how it isrepresented or how many bits it uses. When you do need suchinformation, the C language itself does not provide a way to get it.The header files @file{limits.h} and @file{float.h} contain macroswhich give you this information in full detail.@menu* Width of Type:: How many bits does an integer type hold?* Range of Type:: What are the largest and smallest values that an integer type can hold?* Floating Type Macros:: Parameters that measure the floating point types. * Structure Measurement:: Getting measurements on structure types.@end menu@node Width of Type@subsection Computing the Width of an Integer Data Type@cindex integer type width@cindex width of integer type@cindex type measurements, integerThe most common reason that a program needs to know how many bits are inan integer type is for using an array of @code{long int} as a bit vector.You can access the bit at index @var{n} with@smallexamplevector[@var{n} / LONGBITS] & (1 << (@var{n} % LONGBITS))@end smallexample@noindentprovided you define @code{LONGBITS} as the number of bits in a@code{long int}.@pindex limits.hThere is no operator in the C language that can give you the number ofbits in an integer data type. But you can compute it from the macro@code{CHAR_BIT}, defined in the header file @file{limits.h}.@table @code@comment limits.h@comment ANSI@item CHAR_BITThis is the number of bits in a @code{char}---eight, on most systems.The value has type @code{int}.You can compute the number of bits in any data type @var{type} likethis:@smallexamplesizeof (@var{type}) * CHAR_BIT@end smallexample@end table@node Range of Type@subsection Range of an Integer Type@cindex integer type range@cindex range of integer type@cindex limits, integer typesSuppose you need to store an integer value which can range from zero toone million. Which is the smallest type you can use? There is nogeneral rule; it depends on the C compiler and target machine. You canuse the @samp{MIN} and @samp{MAX} macros in @file{limits.h} to determinewhich type will work.Each signed integer type has a pair of macros which give the smallestand largest values that it can hold. Each unsigned integer type has onesuch macro, for the maximum value; the minimum value is, of course,zero.The values of these macros are all integer constant expressions. The@samp{MAX} and @samp{MIN} macros for @code{char} and @w{@code{shortint}} types have values of type @code{int}. The @samp{MAX} and@samp{MIN} macros for the other types have values of the same typedescribed by the macro---thus, @code{ULONG_MAX} has type@w{@code{unsigned long int}}.@comment Extra blank lines make it look better.@table @code@comment limits.h@comment ANSI@item SCHAR_MINThis is the minimum value that can be represented by a @w{@code{signed char}}.@comment limits.h@comment ANSI@item SCHAR_MAX@comment limits.h@comment ANSI@itemx UCHAR_MAXThese are the maximum values that can be represented by a@w{@code{signed char}} and @w{@code{unsigned char}}, respectively.@comment limits.h@comment ANSI@item CHAR_MINThis is the minimum value that can be represented by a @code{char}.It's equal to @code{SCHAR_MIN} if @code{char} is signed, or zerootherwise.@comment limits.h@comment ANSI@item CHAR_MAXThis is the maximum value that can be represented by a @code{char}.It's equal to @code{SCHAR_MAX} if @code{char} is signed, or@code{UCHAR_MAX} otherwise.@comment limits.h@comment ANSI@item SHRT_MINThis is the minimum value that can be represented by a @w{@code{signedshort int}}. On most machines that the GNU C library runs on,@code{short} integers are 16-bit quantities.@comment limits.h@comment ANSI@item SHRT_MAX@comment limits.h@comment ANSI@itemx USHRT_MAXThese are the maximum values that can be represented by a@w{@code{signed short int}} and @w{@code{unsigned short int}},respectively.@comment limits.h@comment ANSI@item INT_MINThis is the minimum value that can be represented by a @w{@code{signedint}}. On most machines that the GNU C system runs on, an @code{int} isa 32-bit quantity.@comment limits.h@comment ANSI@item INT_MAX@comment limits.h@comment ANSI@itemx UINT_MAXThese are the maximum values that can be represented by, respectively,the type @w{@code{signed int}} and the type @w{@code{unsigned int}}.@comment limits.h@comment ANSI@item LONG_MINThis is the minimum value that can be represented by a @w{@code{signedlong int}}. On most machines that the GNU C system runs on, @code{long}integers are 32-bit quantities, the same size as @code{int}.@comment limits.h@comment ANSI@item LONG_MAX@comment limits.h@comment ANSI@itemx ULONG_MAXThese are the maximum values that can be represented by a@w{@code{signed long int}} and @code{unsigned long int}, respectively.@comment limits.h@comment GNU@item LONG_LONG_MINThis is the minimum value that can be represented by a @w{@code{signedlong long int}}. On most machines that the GNU C system runs on,@w{@code{long long}} integers are 64-bit quantities.@comment limits.h@comment GNU@item LONG_LONG_MAX@comment limits.h@comment ANSI@itemx ULONG_LONG_MAXThese are the maximum values that can be represented by a @code{signedlong long int} and @code{unsigned long long int}, respectively.@comment limits.h@comment GNU@item WCHAR_MAXThis is the maximum value that can be represented by a @code{wchar_t}.@xref{Wide Char Intro}.@end tableThe header file @file{limits.h} also defines some additional constantsthat parameterize various operating system and file system limits. Theseconstants are described in @ref{System Configuration}.@node Floating Type Macros@subsection Floating Type Macros@cindex floating type measurements@cindex measurements of floating types@cindex type measurements, floating@cindex limits, floating typesThe specific representation of floating point numbers varies frommachine to machine. Because floating point numbers are representedinternally as approximate quantities, algorithms for manipulatingfloating point data often need to take account of the precise details ofthe machine's floating point representation.Some of the functions in the C library itself need this information; forexample, the algorithms for printing and reading floating point numbers(@pxref{I/O on Streams}) and for calculating trigonometric andirrational functions (@pxref{Mathematics}) use it to avoid round-offerror and loss of accuracy. User programs that implement numericalanalysis techniques also often need this information in order tominimize or compute error bounds.The header file @file{float.h} describes the format used by your
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -