| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | @c This node must have no pointers. | 
|  | 2 | @node Language Features | 
|  | 3 | @c @node Language Features, Library Summary, , Top | 
|  | 4 | @c %MENU% C language features provided by the library | 
|  | 5 | @appendix C Language Facilities in the Library | 
|  | 6 |  | 
|  | 7 | Some of the facilities implemented by the C library really should be | 
|  | 8 | thought of as parts of the C language itself.  These facilities ought to | 
|  | 9 | be documented in the C Language Manual, not in the library manual; but | 
|  | 10 | since we don't have the language manual yet, and documentation for these | 
|  | 11 | features has been written, we are publishing it here. | 
|  | 12 |  | 
|  | 13 | @menu | 
|  | 14 | * Consistency Checking::        Using @code{assert} to abort if | 
|  | 15 | something ``impossible'' happens. | 
|  | 16 | * Variadic Functions::          Defining functions with varying numbers | 
|  | 17 | of args. | 
|  | 18 | * Null Pointer Constant::       The macro @code{NULL}. | 
|  | 19 | * Important Data Types::        Data types for object sizes. | 
|  | 20 | * Data Type Measurements::      Parameters of data type representations. | 
|  | 21 | @end menu | 
|  | 22 |  | 
|  | 23 | @node Consistency Checking | 
|  | 24 | @section Explicitly Checking Internal Consistency | 
|  | 25 | @cindex consistency checking | 
|  | 26 | @cindex impossible events | 
|  | 27 | @cindex assertions | 
|  | 28 |  | 
|  | 29 | When you're writing a program, it's often a good idea to put in checks | 
|  | 30 | at strategic places for ``impossible'' errors or violations of basic | 
|  | 31 | assumptions.  These kinds of checks are helpful in debugging problems | 
|  | 32 | with the interfaces between different parts of the program, for example. | 
|  | 33 |  | 
|  | 34 | @pindex assert.h | 
|  | 35 | The @code{assert} macro, defined in the header file @file{assert.h}, | 
|  | 36 | provides a convenient way to abort the program while printing a message | 
|  | 37 | about where in the program the error was detected. | 
|  | 38 |  | 
|  | 39 | @vindex NDEBUG | 
|  | 40 | Once you think your program is debugged, you can disable the error | 
|  | 41 | checks performed by the @code{assert} macro by recompiling with the | 
|  | 42 | macro @code{NDEBUG} defined.  This means you don't actually have to | 
|  | 43 | change the program source code to disable these checks. | 
|  | 44 |  | 
|  | 45 | But disabling these consistency checks is undesirable unless they make | 
|  | 46 | the program significantly slower.  All else being equal, more error | 
|  | 47 | checking is good no matter who is running the program.  A wise user | 
|  | 48 | would rather have a program crash, visibly, than have it return nonsense | 
|  | 49 | without indicating anything might be wrong. | 
|  | 50 |  | 
|  | 51 | @comment assert.h | 
|  | 52 | @comment ISO | 
|  | 53 | @deftypefn Macro void assert (int @var{expression}) | 
|  | 54 | @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asucorrupt{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}} | 
|  | 55 | @c assert_fail_base calls asprintf, and fflushes stderr. | 
|  | 56 | Verify the programmer's belief that @var{expression} is nonzero at | 
|  | 57 | this point in the program. | 
|  | 58 |  | 
|  | 59 | If @code{NDEBUG} is not defined, @code{assert} tests the value of | 
|  | 60 | @var{expression}.  If it is false (zero), @code{assert} aborts the | 
|  | 61 | program (@pxref{Aborting a Program}) after printing a message of the | 
|  | 62 | form: | 
|  | 63 |  | 
|  | 64 | @smallexample | 
|  | 65 | @file{@var{file}}:@var{linenum}: @var{function}: Assertion `@var{expression}' failed. | 
|  | 66 | @end smallexample | 
|  | 67 |  | 
|  | 68 | @noindent | 
|  | 69 | on the standard error stream @code{stderr} (@pxref{Standard Streams}). | 
|  | 70 | The filename and line number are taken from the C preprocessor macros | 
|  | 71 | @code{__FILE__} and @code{__LINE__} and specify where the call to | 
|  | 72 | @code{assert} was made.  When using the GNU C compiler, the name of | 
|  | 73 | the function which calls @code{assert} is taken from the built-in | 
|  | 74 | variable @code{__PRETTY_FUNCTION__}; with older compilers, the function | 
|  | 75 | name and following colon are omitted. | 
|  | 76 |  | 
|  | 77 | If the preprocessor macro @code{NDEBUG} is defined before | 
|  | 78 | @file{assert.h} is included, the @code{assert} macro is defined to do | 
|  | 79 | absolutely nothing. | 
|  | 80 |  | 
|  | 81 | @strong{Warning:} Even the argument expression @var{expression} is not | 
|  | 82 | evaluated if @code{NDEBUG} is in effect.  So never use @code{assert} | 
|  | 83 | with arguments that involve side effects.  For example, @code{assert | 
|  | 84 | (++i > 0);} is a bad idea, because @code{i} will not be incremented if | 
|  | 85 | @code{NDEBUG} is defined. | 
|  | 86 | @end deftypefn | 
|  | 87 |  | 
|  | 88 | Sometimes the ``impossible'' condition you want to check for is an error | 
|  | 89 | return from an operating system function.  Then it is useful to display | 
|  | 90 | not only where the program crashes, but also what error was returned. | 
|  | 91 | The @code{assert_perror} macro makes this easy. | 
|  | 92 |  | 
|  | 93 | @comment assert.h | 
|  | 94 | @comment GNU | 
|  | 95 | @deftypefn Macro void assert_perror (int @var{errnum}) | 
|  | 96 | @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asucorrupt{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}} | 
|  | 97 | @c assert_fail_base calls asprintf, and fflushes stderr. | 
|  | 98 | Similar to @code{assert}, but verifies that @var{errnum} is zero. | 
|  | 99 |  | 
|  | 100 | If @code{NDEBUG} is not defined, @code{assert_perror} tests the value of | 
|  | 101 | @var{errnum}.  If it is nonzero, @code{assert_perror} aborts the program | 
|  | 102 | after printing a message of the form: | 
|  | 103 |  | 
|  | 104 | @smallexample | 
|  | 105 | @file{@var{file}}:@var{linenum}: @var{function}: @var{error text} | 
|  | 106 | @end smallexample | 
|  | 107 |  | 
|  | 108 | @noindent | 
|  | 109 | on the standard error stream.  The file name, line number, and function | 
|  | 110 | name are as for @code{assert}.  The error text is the result of | 
|  | 111 | @w{@code{strerror (@var{errnum})}}.  @xref{Error Messages}. | 
|  | 112 |  | 
|  | 113 | Like @code{assert}, if @code{NDEBUG} is defined before @file{assert.h} | 
|  | 114 | is included, the @code{assert_perror} macro does absolutely nothing.  It | 
|  | 115 | does not evaluate the argument, so @var{errnum} should not have any side | 
|  | 116 | effects.  It is best for @var{errnum} to be just a simple variable | 
|  | 117 | reference; often it will be @code{errno}. | 
|  | 118 |  | 
|  | 119 | This macro is a GNU extension. | 
|  | 120 | @end deftypefn | 
|  | 121 |  | 
|  | 122 | @strong{Usage note:} The @code{assert} facility is designed for | 
|  | 123 | detecting @emph{internal inconsistency}; it is not suitable for | 
|  | 124 | reporting invalid input or improper usage by the @emph{user} of the | 
|  | 125 | program. | 
|  | 126 |  | 
|  | 127 | The information in the diagnostic messages printed by the @code{assert} | 
|  | 128 | and @code{assert_perror} macro is intended to help you, the programmer, | 
|  | 129 | track down the cause of a bug, but is not really useful for telling a user | 
|  | 130 | of your program why his or her input was invalid or why a command could not | 
|  | 131 | be carried out.  What's more, your program should not abort when given | 
|  | 132 | invalid input, as @code{assert} would do---it should exit with nonzero | 
|  | 133 | status (@pxref{Exit Status}) after printing its error messages, or perhaps | 
|  | 134 | read another command or move on to the next input file. | 
|  | 135 |  | 
|  | 136 | @xref{Error Messages}, for information on printing error messages for | 
|  | 137 | problems that @emph{do not} represent bugs in the program. | 
|  | 138 |  | 
|  | 139 |  | 
|  | 140 | @node Variadic Functions | 
|  | 141 | @section Variadic Functions | 
|  | 142 | @cindex variable number of arguments | 
|  | 143 | @cindex variadic functions | 
|  | 144 | @cindex optional arguments | 
|  | 145 |  | 
|  | 146 | @w{ISO C} defines a syntax for declaring a function to take a variable | 
|  | 147 | number or type of arguments.  (Such functions are referred to as | 
|  | 148 | @dfn{varargs functions} or @dfn{variadic functions}.)  However, the | 
|  | 149 | language itself provides no mechanism for such functions to access their | 
|  | 150 | non-required arguments; instead, you use the variable arguments macros | 
|  | 151 | defined in @file{stdarg.h}. | 
|  | 152 |  | 
|  | 153 | This section describes how to declare variadic functions, how to write | 
|  | 154 | them, and how to call them properly. | 
|  | 155 |  | 
|  | 156 | @strong{Compatibility Note:} Many older C dialects provide a similar, | 
|  | 157 | but incompatible, mechanism for defining functions with variable numbers | 
|  | 158 | of arguments, using @file{varargs.h}. | 
|  | 159 |  | 
|  | 160 | @menu | 
|  | 161 | * Why Variadic::                Reasons for making functions take | 
|  | 162 | variable arguments. | 
|  | 163 | * How Variadic::                How to define and call variadic functions. | 
|  | 164 | * Variadic Example::            A complete example. | 
|  | 165 | @end menu | 
|  | 166 |  | 
|  | 167 | @node Why Variadic | 
|  | 168 | @subsection Why Variadic Functions are Used | 
|  | 169 |  | 
|  | 170 | Ordinary C functions take a fixed number of arguments.  When you define | 
|  | 171 | a function, you specify the data type for each argument.  Every call to | 
|  | 172 | the function should supply the expected number of arguments, with types | 
|  | 173 | that can be converted to the specified ones.  Thus, if the function | 
|  | 174 | @samp{foo} is declared with @code{int foo (int, char *);} then you must | 
|  | 175 | call it with two arguments, a number (any kind will do) and a string | 
|  | 176 | pointer. | 
|  | 177 |  | 
|  | 178 | But some functions perform operations that can meaningfully accept an | 
|  | 179 | unlimited number of arguments. | 
|  | 180 |  | 
|  | 181 | In some cases a function can handle any number of values by operating on | 
|  | 182 | all of them as a block.  For example, consider a function that allocates | 
|  | 183 | a one-dimensional array with @code{malloc} to hold a specified set of | 
|  | 184 | values.  This operation makes sense for any number of values, as long as | 
|  | 185 | the length of the array corresponds to that number.  Without facilities | 
|  | 186 | for variable arguments, you would have to define a separate function for | 
|  | 187 | each possible array size. | 
|  | 188 |  | 
|  | 189 | The library function @code{printf} (@pxref{Formatted Output}) is an | 
|  | 190 | example of another class of function where variable arguments are | 
|  | 191 | useful.  This function prints its arguments (which can vary in type as | 
|  | 192 | well as number) under the control of a format template string. | 
|  | 193 |  | 
|  | 194 | These are good reasons to define a @dfn{variadic} function which can | 
|  | 195 | handle as many arguments as the caller chooses to pass. | 
|  | 196 |  | 
|  | 197 | Some functions such as @code{open} take a fixed set of arguments, but | 
|  | 198 | occasionally ignore the last few.  Strict adherence to @w{ISO C} requires | 
|  | 199 | these functions to be defined as variadic; in practice, however, the GNU | 
|  | 200 | C compiler and most other C compilers let you define such a function to | 
|  | 201 | take a fixed set of arguments---the most it can ever use---and then only | 
|  | 202 | @emph{declare} the function as variadic (or not declare its arguments | 
|  | 203 | at all!). | 
|  | 204 |  | 
|  | 205 | @node How Variadic | 
|  | 206 | @subsection How Variadic Functions are Defined and Used | 
|  | 207 |  | 
|  | 208 | Defining and using a variadic function involves three steps: | 
|  | 209 |  | 
|  | 210 | @itemize @bullet | 
|  | 211 | @item | 
|  | 212 | @emph{Define} the function as variadic, using an ellipsis | 
|  | 213 | (@samp{@dots{}}) in the argument list, and using special macros to | 
|  | 214 | access the variable arguments.  @xref{Receiving Arguments}. | 
|  | 215 |  | 
|  | 216 | @item | 
|  | 217 | @emph{Declare} the function as variadic, using a prototype with an | 
|  | 218 | ellipsis (@samp{@dots{}}), in all the files which call it. | 
|  | 219 | @xref{Variadic Prototypes}. | 
|  | 220 |  | 
|  | 221 | @item | 
|  | 222 | @emph{Call} the function by writing the fixed arguments followed by the | 
|  | 223 | additional variable arguments.  @xref{Calling Variadics}. | 
|  | 224 | @end itemize | 
|  | 225 |  | 
|  | 226 | @menu | 
|  | 227 | * Variadic Prototypes::  How to make a prototype for a function | 
|  | 228 | with variable arguments. | 
|  | 229 | * Receiving Arguments::  Steps you must follow to access the | 
|  | 230 | optional argument values. | 
|  | 231 | * How Many Arguments::   How to decide whether there are more arguments. | 
|  | 232 | * Calling Variadics::    Things you need to know about calling | 
|  | 233 | variable arguments functions. | 
|  | 234 | * Argument Macros::      Detailed specification of the macros | 
|  | 235 | for accessing variable arguments. | 
|  | 236 | @end menu | 
|  | 237 |  | 
|  | 238 | @node Variadic Prototypes | 
|  | 239 | @subsubsection Syntax for Variable Arguments | 
|  | 240 | @cindex function prototypes (variadic) | 
|  | 241 | @cindex prototypes for variadic functions | 
|  | 242 | @cindex variadic function prototypes | 
|  | 243 |  | 
|  | 244 | A function that accepts a variable number of arguments must be declared | 
|  | 245 | with a prototype that says so.   You write the fixed arguments as usual, | 
|  | 246 | and then tack on @samp{@dots{}} to indicate the possibility of | 
|  | 247 | additional arguments.  The syntax of @w{ISO C} requires at least one fixed | 
|  | 248 | argument before the @samp{@dots{}}.  For example, | 
|  | 249 |  | 
|  | 250 | @smallexample | 
|  | 251 | int | 
|  | 252 | func (const char *a, int b, @dots{}) | 
|  | 253 | @{ | 
|  | 254 | @dots{} | 
|  | 255 | @} | 
|  | 256 | @end smallexample | 
|  | 257 |  | 
|  | 258 | @noindent | 
|  | 259 | defines a function @code{func} which returns an @code{int} and takes two | 
|  | 260 | required arguments, a @code{const char *} and an @code{int}.  These are | 
|  | 261 | followed by any number of anonymous arguments. | 
|  | 262 |  | 
|  | 263 | @strong{Portability note:} For some C compilers, the last required | 
|  | 264 | argument must not be declared @code{register} in the function | 
|  | 265 | definition.  Furthermore, this argument's type must be | 
|  | 266 | @dfn{self-promoting}: that is, the default promotions must not change | 
|  | 267 | its type.  This rules out array and function types, as well as | 
|  | 268 | @code{float}, @code{char} (whether signed or not) and @w{@code{short int}} | 
|  | 269 | (whether signed or not).  This is actually an @w{ISO C} requirement. | 
|  | 270 |  | 
|  | 271 | @node Receiving Arguments | 
|  | 272 | @subsubsection Receiving the Argument Values | 
|  | 273 | @cindex variadic function argument access | 
|  | 274 | @cindex arguments (variadic functions) | 
|  | 275 |  | 
|  | 276 | Ordinary fixed arguments have individual names, and you can use these | 
|  | 277 | names to access their values.  But optional arguments have no | 
|  | 278 | names---nothing but @samp{@dots{}}.  How can you access them? | 
|  | 279 |  | 
|  | 280 | @pindex stdarg.h | 
|  | 281 | The only way to access them is sequentially, in the order they were | 
|  | 282 | written, and you must use special macros from @file{stdarg.h} in the | 
|  | 283 | following three step process: | 
|  | 284 |  | 
|  | 285 | @enumerate | 
|  | 286 | @item | 
|  | 287 | You initialize an argument pointer variable of type @code{va_list} using | 
|  | 288 | @code{va_start}.  The argument pointer when initialized points to the | 
|  | 289 | first optional argument. | 
|  | 290 |  | 
|  | 291 | @item | 
|  | 292 | You access the optional arguments by successive calls to @code{va_arg}. | 
|  | 293 | The first call to @code{va_arg} gives you the first optional argument, | 
|  | 294 | the next call gives you the second, and so on. | 
|  | 295 |  | 
|  | 296 | You can stop at any time if you wish to ignore any remaining optional | 
|  | 297 | arguments.  It is perfectly all right for a function to access fewer | 
|  | 298 | arguments than were supplied in the call, but you will get garbage | 
|  | 299 | values if you try to access too many arguments. | 
|  | 300 |  | 
|  | 301 | @item | 
|  | 302 | You indicate that you are finished with the argument pointer variable by | 
|  | 303 | calling @code{va_end}. | 
|  | 304 |  | 
|  | 305 | (In practice, with most C compilers, calling @code{va_end} does nothing. | 
|  | 306 | This is always true in the GNU C compiler.  But you might as well call | 
|  | 307 | @code{va_end} just in case your program is someday compiled with a peculiar | 
|  | 308 | compiler.) | 
|  | 309 | @end enumerate | 
|  | 310 |  | 
|  | 311 | @xref{Argument Macros}, for the full definitions of @code{va_start}, | 
|  | 312 | @code{va_arg} and @code{va_end}. | 
|  | 313 |  | 
|  | 314 | Steps 1 and 3 must be performed in the function that accepts the | 
|  | 315 | optional arguments.  However, you can pass the @code{va_list} variable | 
|  | 316 | as an argument to another function and perform all or part of step 2 | 
|  | 317 | there. | 
|  | 318 |  | 
|  | 319 | You can perform the entire sequence of three steps multiple times | 
|  | 320 | within a single function invocation.  If you want to ignore the optional | 
|  | 321 | arguments, you can do these steps zero times. | 
|  | 322 |  | 
|  | 323 | You can have more than one argument pointer variable if you like.  You | 
|  | 324 | can initialize each variable with @code{va_start} when you wish, and | 
|  | 325 | then you can fetch arguments with each argument pointer as you wish. | 
|  | 326 | Each argument pointer variable will sequence through the same set of | 
|  | 327 | argument values, but at its own pace. | 
|  | 328 |  | 
|  | 329 | @strong{Portability note:} With some compilers, once you pass an | 
|  | 330 | argument pointer value to a subroutine, you must not keep using the same | 
|  | 331 | argument pointer value after that subroutine returns.  For full | 
|  | 332 | portability, you should just pass it to @code{va_end}.  This is actually | 
|  | 333 | an @w{ISO C} requirement, but most ANSI C compilers work happily | 
|  | 334 | regardless. | 
|  | 335 |  | 
|  | 336 | @node How Many Arguments | 
|  | 337 | @subsubsection How Many Arguments Were Supplied | 
|  | 338 | @cindex number of arguments passed | 
|  | 339 | @cindex how many arguments | 
|  | 340 | @cindex arguments, how many | 
|  | 341 |  | 
|  | 342 | There is no general way for a function to determine the number and type | 
|  | 343 | of the optional arguments it was called with.  So whoever designs the | 
|  | 344 | function typically designs a convention for the caller to specify the number | 
|  | 345 | and type of arguments.  It is up to you to define an appropriate calling | 
|  | 346 | convention for each variadic function, and write all calls accordingly. | 
|  | 347 |  | 
|  | 348 | One kind of calling convention is to pass the number of optional | 
|  | 349 | arguments as one of the fixed arguments.  This convention works provided | 
|  | 350 | all of the optional arguments are of the same type. | 
|  | 351 |  | 
|  | 352 | A similar alternative is to have one of the required arguments be a bit | 
|  | 353 | mask, with a bit for each possible purpose for which an optional | 
|  | 354 | argument might be supplied.  You would test the bits in a predefined | 
|  | 355 | sequence; if the bit is set, fetch the value of the next argument, | 
|  | 356 | otherwise use a default value. | 
|  | 357 |  | 
|  | 358 | A required argument can be used as a pattern to specify both the number | 
|  | 359 | and types of the optional arguments.  The format string argument to | 
|  | 360 | @code{printf} is one example of this (@pxref{Formatted Output Functions}). | 
|  | 361 |  | 
|  | 362 | Another possibility is to pass an ``end marker'' value as the last | 
|  | 363 | optional argument.  For example, for a function that manipulates an | 
|  | 364 | arbitrary number of pointer arguments, a null pointer might indicate the | 
|  | 365 | end of the argument list.  (This assumes that a null pointer isn't | 
|  | 366 | otherwise meaningful to the function.)  The @code{execl} function works | 
|  | 367 | in just this way; see @ref{Executing a File}. | 
|  | 368 |  | 
|  | 369 |  | 
|  | 370 | @node Calling Variadics | 
|  | 371 | @subsubsection Calling Variadic Functions | 
|  | 372 | @cindex variadic functions, calling | 
|  | 373 | @cindex calling variadic functions | 
|  | 374 | @cindex declaring variadic functions | 
|  | 375 |  | 
|  | 376 | You don't have to do anything special to call a variadic function. | 
|  | 377 | Just put the arguments (required arguments, followed by optional ones) | 
|  | 378 | inside parentheses, separated by commas, as usual.  But you must declare | 
|  | 379 | the function with a prototype and know how the argument values are converted. | 
|  | 380 |  | 
|  | 381 | In principle, functions that are @emph{defined} to be variadic must also | 
|  | 382 | be @emph{declared} to be variadic using a function prototype whenever | 
|  | 383 | you call them.  (@xref{Variadic Prototypes}, for how.)  This is because | 
|  | 384 | some C compilers use a different calling convention to pass the same set | 
|  | 385 | of argument values to a function depending on whether that function | 
|  | 386 | takes variable arguments or fixed arguments. | 
|  | 387 |  | 
|  | 388 | In practice, the GNU C compiler always passes a given set of argument | 
|  | 389 | types in the same way regardless of whether they are optional or | 
|  | 390 | required.  So, as long as the argument types are self-promoting, you can | 
|  | 391 | safely omit declaring them.  Usually it is a good idea to declare the | 
|  | 392 | argument types for variadic functions, and indeed for all functions. | 
|  | 393 | But there are a few functions which it is extremely convenient not to | 
|  | 394 | have to declare as variadic---for example, @code{open} and | 
|  | 395 | @code{printf}. | 
|  | 396 |  | 
|  | 397 | @cindex default argument promotions | 
|  | 398 | @cindex argument promotion | 
|  | 399 | Since the prototype doesn't specify types for optional arguments, in a | 
|  | 400 | call to a variadic function the @dfn{default argument promotions} are | 
|  | 401 | performed on the optional argument values.  This means the objects of | 
|  | 402 | type @code{char} or @w{@code{short int}} (whether signed or not) are | 
|  | 403 | promoted to either @code{int} or @w{@code{unsigned int}}, as | 
|  | 404 | appropriate; and that objects of type @code{float} are promoted to type | 
|  | 405 | @code{double}.  So, if the caller passes a @code{char} as an optional | 
|  | 406 | argument, it is promoted to an @code{int}, and the function can access | 
|  | 407 | it with @code{va_arg (@var{ap}, int)}. | 
|  | 408 |  | 
|  | 409 | Conversion of the required arguments is controlled by the function | 
|  | 410 | prototype in the usual way: the argument expression is converted to the | 
|  | 411 | declared argument type as if it were being assigned to a variable of | 
|  | 412 | that type. | 
|  | 413 |  | 
|  | 414 | @node Argument Macros | 
|  | 415 | @subsubsection Argument Access Macros | 
|  | 416 |  | 
|  | 417 | Here are descriptions of the macros used to retrieve variable arguments. | 
|  | 418 | These macros are defined in the header file @file{stdarg.h}. | 
|  | 419 | @pindex stdarg.h | 
|  | 420 |  | 
|  | 421 | @comment stdarg.h | 
|  | 422 | @comment ISO | 
|  | 423 | @deftp {Data Type} va_list | 
|  | 424 | The type @code{va_list} is used for argument pointer variables. | 
|  | 425 | @end deftp | 
|  | 426 |  | 
|  | 427 | @comment stdarg.h | 
|  | 428 | @comment ISO | 
|  | 429 | @deftypefn {Macro} void va_start (va_list @var{ap}, @var{last-required}) | 
|  | 430 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | 
|  | 431 | @c This is no longer provided by glibc, but rather by the compiler. | 
|  | 432 | This macro initializes the argument pointer variable @var{ap} to point | 
|  | 433 | to the first of the optional arguments of the current function; | 
|  | 434 | @var{last-required} must be the last required argument to the function. | 
|  | 435 | @end deftypefn | 
|  | 436 |  | 
|  | 437 | @comment stdarg.h | 
|  | 438 | @comment ISO | 
|  | 439 | @deftypefn {Macro} @var{type} va_arg (va_list @var{ap}, @var{type}) | 
|  | 440 | @safety{@prelim{}@mtsafe{@mtsrace{:ap}}@assafe{}@acunsafe{@acucorrupt{}}} | 
|  | 441 | @c This is no longer provided by glibc, but rather by the compiler. | 
|  | 442 | @c Unlike the other va_ macros, that either start/end the lifetime of | 
|  | 443 | @c the va_list object or don't modify it, this one modifies ap, and it | 
|  | 444 | @c may leave it in a partially updated state. | 
|  | 445 | The @code{va_arg} macro returns the value of the next optional argument, | 
|  | 446 | and modifies the value of @var{ap} to point to the subsequent argument. | 
|  | 447 | Thus, successive uses of @code{va_arg} return successive optional | 
|  | 448 | arguments. | 
|  | 449 |  | 
|  | 450 | The type of the value returned by @code{va_arg} is @var{type} as | 
|  | 451 | specified in the call.  @var{type} must be a self-promoting type (not | 
|  | 452 | @code{char} or @code{short int} or @code{float}) that matches the type | 
|  | 453 | of the actual argument. | 
|  | 454 | @end deftypefn | 
|  | 455 |  | 
|  | 456 | @comment stdarg.h | 
|  | 457 | @comment ISO | 
|  | 458 | @deftypefn {Macro} void va_end (va_list @var{ap}) | 
|  | 459 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | 
|  | 460 | @c This is no longer provided by glibc, but rather by the compiler. | 
|  | 461 | This ends the use of @var{ap}.  After a @code{va_end} call, further | 
|  | 462 | @code{va_arg} calls with the same @var{ap} may not work.  You should invoke | 
|  | 463 | @code{va_end} before returning from the function in which @code{va_start} | 
|  | 464 | was invoked with the same @var{ap} argument. | 
|  | 465 |  | 
|  | 466 | In @theglibc{}, @code{va_end} does nothing, and you need not ever | 
|  | 467 | use it except for reasons of portability. | 
|  | 468 | @refill | 
|  | 469 | @end deftypefn | 
|  | 470 |  | 
|  | 471 | Sometimes it is necessary to parse the list of parameters more than once | 
|  | 472 | or one wants to remember a certain position in the parameter list.  To | 
|  | 473 | do this, one will have to make a copy of the current value of the | 
|  | 474 | argument.  But @code{va_list} is an opaque type and one cannot necessarily | 
|  | 475 | assign the value of one variable of type @code{va_list} to another variable | 
|  | 476 | of the same type. | 
|  | 477 |  | 
|  | 478 | @comment stdarg.h | 
|  | 479 | @comment ISO | 
|  | 480 | @deftypefn {Macro} void va_copy (va_list @var{dest}, va_list @var{src}) | 
|  | 481 | @deftypefnx {Macro} void __va_copy (va_list @var{dest}, va_list @var{src}) | 
|  | 482 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | 
|  | 483 | @c This is no longer provided by glibc, but rather by the compiler. | 
|  | 484 | The @code{va_copy} macro allows copying of objects of type | 
|  | 485 | @code{va_list} even if this is not an integral type.  The argument pointer | 
|  | 486 | in @var{dest} is initialized to point to the same argument as the | 
|  | 487 | pointer in @var{src}. | 
|  | 488 |  | 
|  | 489 | This macro was added in ISO C99.  When building for strict conformance | 
|  | 490 | to ISO C90 (@samp{gcc -ansi}), it is not available.  The macro | 
|  | 491 | @code{__va_copy} is available as a GNU extension in any standards | 
|  | 492 | mode; before GCC 3.0, it was the only macro for this functionality. | 
|  | 493 | @end deftypefn | 
|  | 494 |  | 
|  | 495 | If you want to use @code{va_copy} and be portable to pre-C99 systems, | 
|  | 496 | you should always be prepared for the | 
|  | 497 | possibility that this macro will not be available.  On architectures where a | 
|  | 498 | simple assignment is invalid, hopefully @code{va_copy} @emph{will} be available, | 
|  | 499 | so one should always write something like this if concerned about | 
|  | 500 | pre-C99 portability: | 
|  | 501 |  | 
|  | 502 | @smallexample | 
|  | 503 | @{ | 
|  | 504 | va_list ap, save; | 
|  | 505 | @dots{} | 
|  | 506 | #ifdef va_copy | 
|  | 507 | va_copy (save, ap); | 
|  | 508 | #else | 
|  | 509 | save = ap; | 
|  | 510 | #endif | 
|  | 511 | @dots{} | 
|  | 512 | @} | 
|  | 513 | @end smallexample | 
|  | 514 |  | 
|  | 515 |  | 
|  | 516 | @node Variadic Example | 
|  | 517 | @subsection Example of a Variadic Function | 
|  | 518 |  | 
|  | 519 | Here is a complete sample function that accepts a variable number of | 
|  | 520 | arguments.  The first argument to the function is the count of remaining | 
|  | 521 | arguments, which are added up and the result returned.  While trivial, | 
|  | 522 | this function is sufficient to illustrate how to use the variable | 
|  | 523 | arguments facility. | 
|  | 524 |  | 
|  | 525 | @comment Yes, this example has been tested. | 
|  | 526 | @smallexample | 
|  | 527 | @include add.c.texi | 
|  | 528 | @end smallexample | 
|  | 529 |  | 
|  | 530 | @node Null Pointer Constant | 
|  | 531 | @section Null Pointer Constant | 
|  | 532 | @cindex null pointer constant | 
|  | 533 |  | 
|  | 534 | The null pointer constant is guaranteed not to point to any real object. | 
|  | 535 | You can assign it to any pointer variable since it has type @code{void | 
|  | 536 | *}.  The preferred way to write a null pointer constant is with | 
|  | 537 | @code{NULL}. | 
|  | 538 |  | 
|  | 539 | @comment stddef.h | 
|  | 540 | @comment ISO | 
|  | 541 | @deftypevr Macro {void *} NULL | 
|  | 542 | This is a null pointer constant. | 
|  | 543 | @end deftypevr | 
|  | 544 |  | 
|  | 545 | You can also use @code{0} or @code{(void *)0} as a null pointer | 
|  | 546 | constant, but using @code{NULL} is cleaner because it makes the purpose | 
|  | 547 | of the constant more evident. | 
|  | 548 |  | 
|  | 549 | If you use the null pointer constant as a function argument, then for | 
|  | 550 | complete portability you should make sure that the function has a | 
|  | 551 | prototype declaration.  Otherwise, if the target machine has two | 
|  | 552 | different pointer representations, the compiler won't know which | 
|  | 553 | representation to use for that argument.  You can avoid the problem by | 
|  | 554 | explicitly casting the constant to the proper pointer type, but we | 
|  | 555 | recommend instead adding a prototype for the function you are calling. | 
|  | 556 |  | 
|  | 557 | @node Important Data Types | 
|  | 558 | @section Important Data Types | 
|  | 559 |  | 
|  | 560 | The result of subtracting two pointers in C is always an integer, but the | 
|  | 561 | precise data type varies from C compiler to C compiler.  Likewise, the | 
|  | 562 | data type of the result of @code{sizeof} also varies between compilers. | 
|  | 563 | ISO defines standard aliases for these two types, so you can refer to | 
|  | 564 | them in a portable fashion.  They are defined in the header file | 
|  | 565 | @file{stddef.h}. | 
|  | 566 | @pindex stddef.h | 
|  | 567 |  | 
|  | 568 | @comment stddef.h | 
|  | 569 | @comment ISO | 
|  | 570 | @deftp {Data Type} ptrdiff_t | 
|  | 571 | This is the signed integer type of the result of subtracting two | 
|  | 572 | pointers.  For example, with the declaration @code{char *p1, *p2;}, the | 
|  | 573 | expression @code{p2 - p1} is of type @code{ptrdiff_t}.  This will | 
|  | 574 | probably be one of the standard signed integer types (@w{@code{short | 
|  | 575 | int}}, @code{int} or @w{@code{long int}}), but might be a nonstandard | 
|  | 576 | type that exists only for this purpose. | 
|  | 577 | @end deftp | 
|  | 578 |  | 
|  | 579 | @comment stddef.h | 
|  | 580 | @comment ISO | 
|  | 581 | @deftp {Data Type} size_t | 
|  | 582 | This is an unsigned integer type used to represent the sizes of objects. | 
|  | 583 | The result of the @code{sizeof} operator is of this type, and functions | 
|  | 584 | such as @code{malloc} (@pxref{Unconstrained Allocation}) and | 
|  | 585 | @code{memcpy} (@pxref{Copying Strings and Arrays}) accept arguments of | 
|  | 586 | this type to specify object sizes.  On systems using @theglibc{}, this | 
|  | 587 | will be @w{@code{unsigned int}} or @w{@code{unsigned long int}}. | 
|  | 588 |  | 
|  | 589 | @strong{Usage Note:} @code{size_t} is the preferred way to declare any | 
|  | 590 | arguments or variables that hold the size of an object. | 
|  | 591 | @end deftp | 
|  | 592 |  | 
|  | 593 | @strong{Compatibility Note:} Implementations of C before the advent of | 
|  | 594 | @w{ISO C} generally used @code{unsigned int} for representing object sizes | 
|  | 595 | and @code{int} for pointer subtraction results.  They did not | 
|  | 596 | necessarily define either @code{size_t} or @code{ptrdiff_t}.  Unix | 
|  | 597 | systems did define @code{size_t}, in @file{sys/types.h}, but the | 
|  | 598 | definition was usually a signed type. | 
|  | 599 |  | 
|  | 600 | @node Data Type Measurements | 
|  | 601 | @section Data Type Measurements | 
|  | 602 |  | 
|  | 603 | Most of the time, if you choose the proper C data type for each object | 
|  | 604 | in your program, you need not be concerned with just how it is | 
|  | 605 | represented or how many bits it uses.  When you do need such | 
|  | 606 | information, the C language itself does not provide a way to get it. | 
|  | 607 | The header files @file{limits.h} and @file{float.h} contain macros | 
|  | 608 | which give you this information in full detail. | 
|  | 609 |  | 
|  | 610 | @menu | 
|  | 611 | * Width of Type::           How many bits does an integer type hold? | 
|  | 612 | * Range of Type::           What are the largest and smallest values | 
|  | 613 | that an integer type can hold? | 
|  | 614 | * Floating Type Macros::    Parameters that measure the floating point types. | 
|  | 615 | * Structure Measurement::   Getting measurements on structure types. | 
|  | 616 | @end menu | 
|  | 617 |  | 
|  | 618 | @node Width of Type | 
|  | 619 | @subsection Computing the Width of an Integer Data Type | 
|  | 620 | @cindex integer type width | 
|  | 621 | @cindex width of integer type | 
|  | 622 | @cindex type measurements, integer | 
|  | 623 |  | 
|  | 624 | The most common reason that a program needs to know how many bits are in | 
|  | 625 | an integer type is for using an array of @code{long int} as a bit vector. | 
|  | 626 | You can access the bit at index @var{n} with | 
|  | 627 |  | 
|  | 628 | @smallexample | 
|  | 629 | vector[@var{n} / LONGBITS] & (1 << (@var{n} % LONGBITS)) | 
|  | 630 | @end smallexample | 
|  | 631 |  | 
|  | 632 | @noindent | 
|  | 633 | provided you define @code{LONGBITS} as the number of bits in a | 
|  | 634 | @code{long int}. | 
|  | 635 |  | 
|  | 636 | @pindex limits.h | 
|  | 637 | There is no operator in the C language that can give you the number of | 
|  | 638 | bits in an integer data type.  But you can compute it from the macro | 
|  | 639 | @code{CHAR_BIT}, defined in the header file @file{limits.h}. | 
|  | 640 |  | 
|  | 641 | @table @code | 
|  | 642 | @comment limits.h | 
|  | 643 | @comment ISO | 
|  | 644 | @item CHAR_BIT | 
|  | 645 | This is the number of bits in a @code{char}---eight, on most systems. | 
|  | 646 | The value has type @code{int}. | 
|  | 647 |  | 
|  | 648 | You can compute the number of bits in any data type @var{type} like | 
|  | 649 | this: | 
|  | 650 |  | 
|  | 651 | @smallexample | 
|  | 652 | sizeof (@var{type}) * CHAR_BIT | 
|  | 653 | @end smallexample | 
|  | 654 | @end table | 
|  | 655 |  | 
|  | 656 | @node Range of Type | 
|  | 657 | @subsection Range of an Integer Type | 
|  | 658 | @cindex integer type range | 
|  | 659 | @cindex range of integer type | 
|  | 660 | @cindex limits, integer types | 
|  | 661 |  | 
|  | 662 | Suppose you need to store an integer value which can range from zero to | 
|  | 663 | one million.  Which is the smallest type you can use?  There is no | 
|  | 664 | general rule; it depends on the C compiler and target machine.  You can | 
|  | 665 | use the @samp{MIN} and @samp{MAX} macros in @file{limits.h} to determine | 
|  | 666 | which type will work. | 
|  | 667 |  | 
|  | 668 | Each signed integer type has a pair of macros which give the smallest | 
|  | 669 | and largest values that it can hold.  Each unsigned integer type has one | 
|  | 670 | such macro, for the maximum value; the minimum value is, of course, | 
|  | 671 | zero. | 
|  | 672 |  | 
|  | 673 | The values of these macros are all integer constant expressions.  The | 
|  | 674 | @samp{MAX} and @samp{MIN} macros for @code{char} and @w{@code{short | 
|  | 675 | int}} types have values of type @code{int}.  The @samp{MAX} and | 
|  | 676 | @samp{MIN} macros for the other types have values of the same type | 
|  | 677 | described by the macro---thus, @code{ULONG_MAX} has type | 
|  | 678 | @w{@code{unsigned long int}}. | 
|  | 679 |  | 
|  | 680 | @comment Extra blank lines make it look better. | 
|  | 681 | @vtable @code | 
|  | 682 | @comment limits.h | 
|  | 683 | @comment ISO | 
|  | 684 | @item SCHAR_MIN | 
|  | 685 |  | 
|  | 686 | This is the minimum value that can be represented by a @w{@code{signed char}}. | 
|  | 687 |  | 
|  | 688 | @comment limits.h | 
|  | 689 | @comment ISO | 
|  | 690 | @item SCHAR_MAX | 
|  | 691 | @comment limits.h | 
|  | 692 | @comment ISO | 
|  | 693 | @itemx UCHAR_MAX | 
|  | 694 |  | 
|  | 695 | These are the maximum values that can be represented by a | 
|  | 696 | @w{@code{signed char}} and @w{@code{unsigned char}}, respectively. | 
|  | 697 |  | 
|  | 698 | @comment limits.h | 
|  | 699 | @comment ISO | 
|  | 700 | @item CHAR_MIN | 
|  | 701 |  | 
|  | 702 | This is the minimum value that can be represented by a @code{char}. | 
|  | 703 | It's equal to @code{SCHAR_MIN} if @code{char} is signed, or zero | 
|  | 704 | otherwise. | 
|  | 705 |  | 
|  | 706 | @comment limits.h | 
|  | 707 | @comment ISO | 
|  | 708 | @item CHAR_MAX | 
|  | 709 |  | 
|  | 710 | This is the maximum value that can be represented by a @code{char}. | 
|  | 711 | It's equal to @code{SCHAR_MAX} if @code{char} is signed, or | 
|  | 712 | @code{UCHAR_MAX} otherwise. | 
|  | 713 |  | 
|  | 714 | @comment limits.h | 
|  | 715 | @comment ISO | 
|  | 716 | @item SHRT_MIN | 
|  | 717 |  | 
|  | 718 | This is the minimum value that can be represented by a @w{@code{signed | 
|  | 719 | short int}}.  On most machines that @theglibc{} runs on, | 
|  | 720 | @code{short} integers are 16-bit quantities. | 
|  | 721 |  | 
|  | 722 | @comment limits.h | 
|  | 723 | @comment ISO | 
|  | 724 | @item SHRT_MAX | 
|  | 725 | @comment limits.h | 
|  | 726 | @comment ISO | 
|  | 727 | @itemx USHRT_MAX | 
|  | 728 |  | 
|  | 729 | These are the maximum values that can be represented by a | 
|  | 730 | @w{@code{signed short int}} and @w{@code{unsigned short int}}, | 
|  | 731 | respectively. | 
|  | 732 |  | 
|  | 733 | @comment limits.h | 
|  | 734 | @comment ISO | 
|  | 735 | @item INT_MIN | 
|  | 736 |  | 
|  | 737 | This is the minimum value that can be represented by a @w{@code{signed | 
|  | 738 | int}}.  On most machines that @theglibc{} runs on, an @code{int} is | 
|  | 739 | a 32-bit quantity. | 
|  | 740 |  | 
|  | 741 | @comment limits.h | 
|  | 742 | @comment ISO | 
|  | 743 | @item INT_MAX | 
|  | 744 | @comment limits.h | 
|  | 745 | @comment ISO | 
|  | 746 | @itemx UINT_MAX | 
|  | 747 |  | 
|  | 748 | These are the maximum values that can be represented by, respectively, | 
|  | 749 | the type @w{@code{signed int}} and the type @w{@code{unsigned int}}. | 
|  | 750 |  | 
|  | 751 | @comment limits.h | 
|  | 752 | @comment ISO | 
|  | 753 | @item LONG_MIN | 
|  | 754 |  | 
|  | 755 | This is the minimum value that can be represented by a @w{@code{signed | 
|  | 756 | long int}}.  On most machines that @theglibc{} runs on, @code{long} | 
|  | 757 | integers are 32-bit quantities, the same size as @code{int}. | 
|  | 758 |  | 
|  | 759 | @comment limits.h | 
|  | 760 | @comment ISO | 
|  | 761 | @item LONG_MAX | 
|  | 762 | @comment limits.h | 
|  | 763 | @comment ISO | 
|  | 764 | @itemx ULONG_MAX | 
|  | 765 |  | 
|  | 766 | These are the maximum values that can be represented by a | 
|  | 767 | @w{@code{signed long int}} and @code{unsigned long int}, respectively. | 
|  | 768 |  | 
|  | 769 | @comment limits.h | 
|  | 770 | @comment ISO | 
|  | 771 | @item LLONG_MIN | 
|  | 772 |  | 
|  | 773 | This is the minimum value that can be represented by a @w{@code{signed | 
|  | 774 | long long int}}.  On most machines that @theglibc{} runs on, | 
|  | 775 | @w{@code{long long}} integers are 64-bit quantities. | 
|  | 776 |  | 
|  | 777 | @comment limits.h | 
|  | 778 | @comment ISO | 
|  | 779 | @item LLONG_MAX | 
|  | 780 | @comment limits.h | 
|  | 781 | @comment ISO | 
|  | 782 | @itemx ULLONG_MAX | 
|  | 783 |  | 
|  | 784 | These are the maximum values that can be represented by a @code{signed | 
|  | 785 | long long int} and @code{unsigned long long int}, respectively. | 
|  | 786 |  | 
|  | 787 | @comment limits.h | 
|  | 788 | @comment GNU | 
|  | 789 | @item LONG_LONG_MIN | 
|  | 790 | @comment limits.h | 
|  | 791 | @comment GNU | 
|  | 792 | @itemx LONG_LONG_MAX | 
|  | 793 | @comment limits.h | 
|  | 794 | @comment GNU | 
|  | 795 | @itemx ULONG_LONG_MAX | 
|  | 796 | These are obsolete names for @code{LLONG_MIN}, @code{LLONG_MAX}, and | 
|  | 797 | @code{ULLONG_MAX}.  They are only available if @code{_GNU_SOURCE} is | 
|  | 798 | defined (@pxref{Feature Test Macros}).  In GCC versions prior to 3.0, | 
|  | 799 | these were the only names available. | 
|  | 800 |  | 
|  | 801 | @comment limits.h | 
|  | 802 | @comment GNU | 
|  | 803 | @item WCHAR_MAX | 
|  | 804 |  | 
|  | 805 | This is the maximum value that can be represented by a @code{wchar_t}. | 
|  | 806 | @xref{Extended Char Intro}. | 
|  | 807 | @end vtable | 
|  | 808 |  | 
|  | 809 | The header file @file{limits.h} also defines some additional constants | 
|  | 810 | that parameterize various operating system and file system limits.  These | 
|  | 811 | constants are described in @ref{System Configuration}. | 
|  | 812 |  | 
|  | 813 | @node Floating Type Macros | 
|  | 814 | @subsection Floating Type Macros | 
|  | 815 | @cindex floating type measurements | 
|  | 816 | @cindex measurements of floating types | 
|  | 817 | @cindex type measurements, floating | 
|  | 818 | @cindex limits, floating types | 
|  | 819 |  | 
|  | 820 | The specific representation of floating point numbers varies from | 
|  | 821 | machine to machine.  Because floating point numbers are represented | 
|  | 822 | internally as approximate quantities, algorithms for manipulating | 
|  | 823 | floating point data often need to take account of the precise details of | 
|  | 824 | the machine's floating point representation. | 
|  | 825 |  | 
|  | 826 | Some of the functions in the C library itself need this information; for | 
|  | 827 | example, the algorithms for printing and reading floating point numbers | 
|  | 828 | (@pxref{I/O on Streams}) and for calculating trigonometric and | 
|  | 829 | irrational functions (@pxref{Mathematics}) use it to avoid round-off | 
|  | 830 | error and loss of accuracy.  User programs that implement numerical | 
|  | 831 | analysis techniques also often need this information in order to | 
|  | 832 | minimize or compute error bounds. | 
|  | 833 |  | 
|  | 834 | The header file @file{float.h} describes the format used by your | 
|  | 835 | machine. | 
|  | 836 |  | 
|  | 837 | @menu | 
|  | 838 | * Floating Point Concepts::     Definitions of terminology. | 
|  | 839 | * Floating Point Parameters::   Details of specific macros. | 
|  | 840 | * IEEE Floating Point::         The measurements for one common | 
|  | 841 | representation. | 
|  | 842 | @end menu | 
|  | 843 |  | 
|  | 844 | @node Floating Point Concepts | 
|  | 845 | @subsubsection Floating Point Representation Concepts | 
|  | 846 |  | 
|  | 847 | This section introduces the terminology for describing floating point | 
|  | 848 | representations. | 
|  | 849 |  | 
|  | 850 | You are probably already familiar with most of these concepts in terms | 
|  | 851 | of scientific or exponential notation for floating point numbers.  For | 
|  | 852 | example, the number @code{123456.0} could be expressed in exponential | 
|  | 853 | notation as @code{1.23456e+05}, a shorthand notation indicating that the | 
|  | 854 | mantissa @code{1.23456} is multiplied by the base @code{10} raised to | 
|  | 855 | power @code{5}. | 
|  | 856 |  | 
|  | 857 | More formally, the internal representation of a floating point number | 
|  | 858 | can be characterized in terms of the following parameters: | 
|  | 859 |  | 
|  | 860 | @itemize @bullet | 
|  | 861 | @item | 
|  | 862 | @cindex sign (of floating point number) | 
|  | 863 | The @dfn{sign} is either @code{-1} or @code{1}. | 
|  | 864 |  | 
|  | 865 | @item | 
|  | 866 | @cindex base (of floating point number) | 
|  | 867 | @cindex radix (of floating point number) | 
|  | 868 | The @dfn{base} or @dfn{radix} for exponentiation, an integer greater | 
|  | 869 | than @code{1}.  This is a constant for a particular representation. | 
|  | 870 |  | 
|  | 871 | @item | 
|  | 872 | @cindex exponent (of floating point number) | 
|  | 873 | The @dfn{exponent} to which the base is raised.  The upper and lower | 
|  | 874 | bounds of the exponent value are constants for a particular | 
|  | 875 | representation. | 
|  | 876 |  | 
|  | 877 | @cindex bias (of floating point number exponent) | 
|  | 878 | Sometimes, in the actual bits representing the floating point number, | 
|  | 879 | the exponent is @dfn{biased} by adding a constant to it, to make it | 
|  | 880 | always be represented as an unsigned quantity.  This is only important | 
|  | 881 | if you have some reason to pick apart the bit fields making up the | 
|  | 882 | floating point number by hand, which is something for which @theglibc{} | 
|  | 883 | provides no support.  So this is ignored in the discussion that | 
|  | 884 | follows. | 
|  | 885 |  | 
|  | 886 | @item | 
|  | 887 | @cindex mantissa (of floating point number) | 
|  | 888 | @cindex significand (of floating point number) | 
|  | 889 | The @dfn{mantissa} or @dfn{significand} is an unsigned integer which is a | 
|  | 890 | part of each floating point number. | 
|  | 891 |  | 
|  | 892 | @item | 
|  | 893 | @cindex precision (of floating point number) | 
|  | 894 | The @dfn{precision} of the mantissa.  If the base of the representation | 
|  | 895 | is @var{b}, then the precision is the number of base-@var{b} digits in | 
|  | 896 | the mantissa.  This is a constant for a particular representation. | 
|  | 897 |  | 
|  | 898 | @cindex hidden bit (of floating point number mantissa) | 
|  | 899 | Many floating point representations have an implicit @dfn{hidden bit} in | 
|  | 900 | the mantissa.  This is a bit which is present virtually in the mantissa, | 
|  | 901 | but not stored in memory because its value is always 1 in a normalized | 
|  | 902 | number.  The precision figure (see above) includes any hidden bits. | 
|  | 903 |  | 
|  | 904 | Again, @theglibc{} provides no facilities for dealing with such | 
|  | 905 | low-level aspects of the representation. | 
|  | 906 | @end itemize | 
|  | 907 |  | 
|  | 908 | The mantissa of a floating point number represents an implicit fraction | 
|  | 909 | whose denominator is the base raised to the power of the precision.  Since | 
|  | 910 | the largest representable mantissa is one less than this denominator, the | 
|  | 911 | value of the fraction is always strictly less than @code{1}.  The | 
|  | 912 | mathematical value of a floating point number is then the product of this | 
|  | 913 | fraction, the sign, and the base raised to the exponent. | 
|  | 914 |  | 
|  | 915 | @cindex normalized floating point number | 
|  | 916 | We say that the floating point number is @dfn{normalized} if the | 
|  | 917 | fraction is at least @code{1/@var{b}}, where @var{b} is the base.  In | 
|  | 918 | other words, the mantissa would be too large to fit if it were | 
|  | 919 | multiplied by the base.  Non-normalized numbers are sometimes called | 
|  | 920 | @dfn{denormal}; they contain less precision than the representation | 
|  | 921 | normally can hold. | 
|  | 922 |  | 
|  | 923 | If the number is not normalized, then you can subtract @code{1} from the | 
|  | 924 | exponent while multiplying the mantissa by the base, and get another | 
|  | 925 | floating point number with the same value.  @dfn{Normalization} consists | 
|  | 926 | of doing this repeatedly until the number is normalized.  Two distinct | 
|  | 927 | normalized floating point numbers cannot be equal in value. | 
|  | 928 |  | 
|  | 929 | (There is an exception to this rule: if the mantissa is zero, it is | 
|  | 930 | considered normalized.  Another exception happens on certain machines | 
|  | 931 | where the exponent is as small as the representation can hold.  Then | 
|  | 932 | it is impossible to subtract @code{1} from the exponent, so a number | 
|  | 933 | may be normalized even if its fraction is less than @code{1/@var{b}}.) | 
|  | 934 |  | 
|  | 935 | @node Floating Point Parameters | 
|  | 936 | @subsubsection Floating Point Parameters | 
|  | 937 |  | 
|  | 938 | @pindex float.h | 
|  | 939 | These macro definitions can be accessed by including the header file | 
|  | 940 | @file{float.h} in your program. | 
|  | 941 |  | 
|  | 942 | Macro names starting with @samp{FLT_} refer to the @code{float} type, | 
|  | 943 | while names beginning with @samp{DBL_} refer to the @code{double} type | 
|  | 944 | and names beginning with @samp{LDBL_} refer to the @code{long double} | 
|  | 945 | type.  (If GCC does not support @code{long double} as a distinct data | 
|  | 946 | type on a target machine then the values for the @samp{LDBL_} constants | 
|  | 947 | are equal to the corresponding constants for the @code{double} type.) | 
|  | 948 |  | 
|  | 949 | Of these macros, only @code{FLT_RADIX} is guaranteed to be a constant | 
|  | 950 | expression.  The other macros listed here cannot be reliably used in | 
|  | 951 | places that require constant expressions, such as @samp{#if} | 
|  | 952 | preprocessing directives or in the dimensions of static arrays. | 
|  | 953 |  | 
|  | 954 | Although the @w{ISO C} standard specifies minimum and maximum values for | 
|  | 955 | most of these parameters, the GNU C implementation uses whatever values | 
|  | 956 | describe the floating point representation of the target machine.  So in | 
|  | 957 | principle GNU C actually satisfies the @w{ISO C} requirements only if the | 
|  | 958 | target machine is suitable.  In practice, all the machines currently | 
|  | 959 | supported are suitable. | 
|  | 960 |  | 
|  | 961 | @vtable @code | 
|  | 962 | @comment float.h | 
|  | 963 | @comment ISO | 
|  | 964 | @item FLT_ROUNDS | 
|  | 965 | This value characterizes the rounding mode for floating point addition. | 
|  | 966 | The following values indicate standard rounding modes: | 
|  | 967 |  | 
|  | 968 | @need 750 | 
|  | 969 |  | 
|  | 970 | @table @code | 
|  | 971 | @item -1 | 
|  | 972 | The mode is indeterminable. | 
|  | 973 | @item 0 | 
|  | 974 | Rounding is towards zero. | 
|  | 975 | @item 1 | 
|  | 976 | Rounding is to the nearest number. | 
|  | 977 | @item 2 | 
|  | 978 | Rounding is towards positive infinity. | 
|  | 979 | @item 3 | 
|  | 980 | Rounding is towards negative infinity. | 
|  | 981 | @end table | 
|  | 982 |  | 
|  | 983 | @noindent | 
|  | 984 | Any other value represents a machine-dependent nonstandard rounding | 
|  | 985 | mode. | 
|  | 986 |  | 
|  | 987 | On most machines, the value is @code{1}, in accordance with the IEEE | 
|  | 988 | standard for floating point. | 
|  | 989 |  | 
|  | 990 | Here is a table showing how certain values round for each possible value | 
|  | 991 | of @code{FLT_ROUNDS}, if the other aspects of the representation match | 
|  | 992 | the IEEE single-precision standard. | 
|  | 993 |  | 
|  | 994 | @smallexample | 
|  | 995 | 0      1             2             3 | 
|  | 996 | 1.00000003    1.0    1.0           1.00000012    1.0 | 
|  | 997 | 1.00000007    1.0    1.00000012    1.00000012    1.0 | 
|  | 998 | -1.00000003   -1.0   -1.0          -1.0          -1.00000012 | 
|  | 999 | -1.00000007   -1.0   -1.00000012   -1.0          -1.00000012 | 
|  | 1000 | @end smallexample | 
|  | 1001 |  | 
|  | 1002 | @comment float.h | 
|  | 1003 | @comment ISO | 
|  | 1004 | @item FLT_RADIX | 
|  | 1005 | This is the value of the base, or radix, of the exponent representation. | 
|  | 1006 | This is guaranteed to be a constant expression, unlike the other macros | 
|  | 1007 | described in this section.  The value is 2 on all machines we know of | 
|  | 1008 | except the IBM 360 and derivatives. | 
|  | 1009 |  | 
|  | 1010 | @comment float.h | 
|  | 1011 | @comment ISO | 
|  | 1012 | @item FLT_MANT_DIG | 
|  | 1013 | This is the number of base-@code{FLT_RADIX} digits in the floating point | 
|  | 1014 | mantissa for the @code{float} data type.  The following expression | 
|  | 1015 | yields @code{1.0} (even though mathematically it should not) due to the | 
|  | 1016 | limited number of mantissa digits: | 
|  | 1017 |  | 
|  | 1018 | @smallexample | 
|  | 1019 | float radix = FLT_RADIX; | 
|  | 1020 |  | 
|  | 1021 | 1.0f + 1.0f / radix / radix / @dots{} / radix | 
|  | 1022 | @end smallexample | 
|  | 1023 |  | 
|  | 1024 | @noindent | 
|  | 1025 | where @code{radix} appears @code{FLT_MANT_DIG} times. | 
|  | 1026 |  | 
|  | 1027 | @comment float.h | 
|  | 1028 | @comment ISO | 
|  | 1029 | @item DBL_MANT_DIG | 
|  | 1030 | @itemx LDBL_MANT_DIG | 
|  | 1031 | This is the number of base-@code{FLT_RADIX} digits in the floating point | 
|  | 1032 | mantissa for the data types @code{double} and @code{long double}, | 
|  | 1033 | respectively. | 
|  | 1034 |  | 
|  | 1035 | @comment Extra blank lines make it look better. | 
|  | 1036 | @comment float.h | 
|  | 1037 | @comment ISO | 
|  | 1038 | @item FLT_DIG | 
|  | 1039 |  | 
|  | 1040 | This is the number of decimal digits of precision for the @code{float} | 
|  | 1041 | data type.  Technically, if @var{p} and @var{b} are the precision and | 
|  | 1042 | base (respectively) for the representation, then the decimal precision | 
|  | 1043 | @var{q} is the maximum number of decimal digits such that any floating | 
|  | 1044 | point number with @var{q} base 10 digits can be rounded to a floating | 
|  | 1045 | point number with @var{p} base @var{b} digits and back again, without | 
|  | 1046 | change to the @var{q} decimal digits. | 
|  | 1047 |  | 
|  | 1048 | The value of this macro is supposed to be at least @code{6}, to satisfy | 
|  | 1049 | @w{ISO C}. | 
|  | 1050 |  | 
|  | 1051 | @comment float.h | 
|  | 1052 | @comment ISO | 
|  | 1053 | @item DBL_DIG | 
|  | 1054 | @itemx LDBL_DIG | 
|  | 1055 |  | 
|  | 1056 | These are similar to @code{FLT_DIG}, but for the data types | 
|  | 1057 | @code{double} and @code{long double}, respectively.  The values of these | 
|  | 1058 | macros are supposed to be at least @code{10}. | 
|  | 1059 |  | 
|  | 1060 | @comment float.h | 
|  | 1061 | @comment ISO | 
|  | 1062 | @item FLT_MIN_EXP | 
|  | 1063 | This is the smallest possible exponent value for type @code{float}. | 
|  | 1064 | More precisely, is the minimum negative integer such that the value | 
|  | 1065 | @code{FLT_RADIX} raised to this power minus 1 can be represented as a | 
|  | 1066 | normalized floating point number of type @code{float}. | 
|  | 1067 |  | 
|  | 1068 | @comment float.h | 
|  | 1069 | @comment ISO | 
|  | 1070 | @item DBL_MIN_EXP | 
|  | 1071 | @itemx LDBL_MIN_EXP | 
|  | 1072 |  | 
|  | 1073 | These are similar to @code{FLT_MIN_EXP}, but for the data types | 
|  | 1074 | @code{double} and @code{long double}, respectively. | 
|  | 1075 |  | 
|  | 1076 | @comment float.h | 
|  | 1077 | @comment ISO | 
|  | 1078 | @item FLT_MIN_10_EXP | 
|  | 1079 | This is the minimum negative integer such that @code{10} raised to this | 
|  | 1080 | power minus 1 can be represented as a normalized floating point number | 
|  | 1081 | of type @code{float}.  This is supposed to be @code{-37} or even less. | 
|  | 1082 |  | 
|  | 1083 | @comment float.h | 
|  | 1084 | @comment ISO | 
|  | 1085 | @item DBL_MIN_10_EXP | 
|  | 1086 | @itemx LDBL_MIN_10_EXP | 
|  | 1087 | These are similar to @code{FLT_MIN_10_EXP}, but for the data types | 
|  | 1088 | @code{double} and @code{long double}, respectively. | 
|  | 1089 |  | 
|  | 1090 | @comment float.h | 
|  | 1091 | @comment ISO | 
|  | 1092 | @item FLT_MAX_EXP | 
|  | 1093 | This is the largest possible exponent value for type @code{float}.  More | 
|  | 1094 | precisely, this is the maximum positive integer such that value | 
|  | 1095 | @code{FLT_RADIX} raised to this power minus 1 can be represented as a | 
|  | 1096 | floating point number of type @code{float}. | 
|  | 1097 |  | 
|  | 1098 | @comment float.h | 
|  | 1099 | @comment ISO | 
|  | 1100 | @item DBL_MAX_EXP | 
|  | 1101 | @itemx LDBL_MAX_EXP | 
|  | 1102 | These are similar to @code{FLT_MAX_EXP}, but for the data types | 
|  | 1103 | @code{double} and @code{long double}, respectively. | 
|  | 1104 |  | 
|  | 1105 | @comment float.h | 
|  | 1106 | @comment ISO | 
|  | 1107 | @item FLT_MAX_10_EXP | 
|  | 1108 | This is the maximum positive integer such that @code{10} raised to this | 
|  | 1109 | power minus 1 can be represented as a normalized floating point number | 
|  | 1110 | of type @code{float}.  This is supposed to be at least @code{37}. | 
|  | 1111 |  | 
|  | 1112 | @comment float.h | 
|  | 1113 | @comment ISO | 
|  | 1114 | @item DBL_MAX_10_EXP | 
|  | 1115 | @itemx LDBL_MAX_10_EXP | 
|  | 1116 | These are similar to @code{FLT_MAX_10_EXP}, but for the data types | 
|  | 1117 | @code{double} and @code{long double}, respectively. | 
|  | 1118 |  | 
|  | 1119 | @comment float.h | 
|  | 1120 | @comment ISO | 
|  | 1121 | @item FLT_MAX | 
|  | 1122 |  | 
|  | 1123 | The value of this macro is the maximum number representable in type | 
|  | 1124 | @code{float}.  It is supposed to be at least @code{1E+37}.  The value | 
|  | 1125 | has type @code{float}. | 
|  | 1126 |  | 
|  | 1127 | The smallest representable number is @code{- FLT_MAX}. | 
|  | 1128 |  | 
|  | 1129 | @comment float.h | 
|  | 1130 | @comment ISO | 
|  | 1131 | @item DBL_MAX | 
|  | 1132 | @itemx LDBL_MAX | 
|  | 1133 |  | 
|  | 1134 | These are similar to @code{FLT_MAX}, but for the data types | 
|  | 1135 | @code{double} and @code{long double}, respectively.  The type of the | 
|  | 1136 | macro's value is the same as the type it describes. | 
|  | 1137 |  | 
|  | 1138 | @comment float.h | 
|  | 1139 | @comment ISO | 
|  | 1140 | @item FLT_MIN | 
|  | 1141 |  | 
|  | 1142 | The value of this macro is the minimum normalized positive floating | 
|  | 1143 | point number that is representable in type @code{float}.  It is supposed | 
|  | 1144 | to be no more than @code{1E-37}. | 
|  | 1145 |  | 
|  | 1146 | @comment float.h | 
|  | 1147 | @comment ISO | 
|  | 1148 | @item DBL_MIN | 
|  | 1149 | @itemx LDBL_MIN | 
|  | 1150 |  | 
|  | 1151 | These are similar to @code{FLT_MIN}, but for the data types | 
|  | 1152 | @code{double} and @code{long double}, respectively.  The type of the | 
|  | 1153 | macro's value is the same as the type it describes. | 
|  | 1154 |  | 
|  | 1155 | @comment float.h | 
|  | 1156 | @comment ISO | 
|  | 1157 | @item FLT_EPSILON | 
|  | 1158 |  | 
|  | 1159 | This is the difference between 1 and the smallest floating point | 
|  | 1160 | number of type @code{float} that is greater than 1.  It's supposed to | 
|  | 1161 | be no greater than @code{1E-5}. | 
|  | 1162 |  | 
|  | 1163 | @comment float.h | 
|  | 1164 | @comment ISO | 
|  | 1165 | @item DBL_EPSILON | 
|  | 1166 | @itemx LDBL_EPSILON | 
|  | 1167 |  | 
|  | 1168 | These are similar to @code{FLT_EPSILON}, but for the data types | 
|  | 1169 | @code{double} and @code{long double}, respectively.  The type of the | 
|  | 1170 | macro's value is the same as the type it describes.  The values are not | 
|  | 1171 | supposed to be greater than @code{1E-9}. | 
|  | 1172 | @end vtable | 
|  | 1173 |  | 
|  | 1174 | @node IEEE Floating Point | 
|  | 1175 | @subsubsection IEEE Floating Point | 
|  | 1176 | @cindex IEEE floating point representation | 
|  | 1177 | @cindex floating point, IEEE | 
|  | 1178 |  | 
|  | 1179 | Here is an example showing how the floating type measurements come out | 
|  | 1180 | for the most common floating point representation, specified by the | 
|  | 1181 | @cite{IEEE Standard for Binary Floating Point Arithmetic (ANSI/IEEE Std | 
|  | 1182 | 754-1985)}.  Nearly all computers designed since the 1980s use this | 
|  | 1183 | format. | 
|  | 1184 |  | 
|  | 1185 | The IEEE single-precision float representation uses a base of 2.  There | 
|  | 1186 | is a sign bit, a mantissa with 23 bits plus one hidden bit (so the total | 
|  | 1187 | precision is 24 base-2 digits), and an 8-bit exponent that can represent | 
|  | 1188 | values in the range -125 to 128, inclusive. | 
|  | 1189 |  | 
|  | 1190 | So, for an implementation that uses this representation for the | 
|  | 1191 | @code{float} data type, appropriate values for the corresponding | 
|  | 1192 | parameters are: | 
|  | 1193 |  | 
|  | 1194 | @smallexample | 
|  | 1195 | FLT_RADIX                             2 | 
|  | 1196 | FLT_MANT_DIG                         24 | 
|  | 1197 | FLT_DIG                               6 | 
|  | 1198 | FLT_MIN_EXP                        -125 | 
|  | 1199 | FLT_MIN_10_EXP                      -37 | 
|  | 1200 | FLT_MAX_EXP                         128 | 
|  | 1201 | FLT_MAX_10_EXP                      +38 | 
|  | 1202 | FLT_MIN                 1.17549435E-38F | 
|  | 1203 | FLT_MAX                 3.40282347E+38F | 
|  | 1204 | FLT_EPSILON             1.19209290E-07F | 
|  | 1205 | @end smallexample | 
|  | 1206 |  | 
|  | 1207 | Here are the values for the @code{double} data type: | 
|  | 1208 |  | 
|  | 1209 | @smallexample | 
|  | 1210 | DBL_MANT_DIG                         53 | 
|  | 1211 | DBL_DIG                              15 | 
|  | 1212 | DBL_MIN_EXP                       -1021 | 
|  | 1213 | DBL_MIN_10_EXP                     -307 | 
|  | 1214 | DBL_MAX_EXP                        1024 | 
|  | 1215 | DBL_MAX_10_EXP                      308 | 
|  | 1216 | DBL_MAX         1.7976931348623157E+308 | 
|  | 1217 | DBL_MIN         2.2250738585072014E-308 | 
|  | 1218 | DBL_EPSILON     2.2204460492503131E-016 | 
|  | 1219 | @end smallexample | 
|  | 1220 |  | 
|  | 1221 | @node Structure Measurement | 
|  | 1222 | @subsection Structure Field Offset Measurement | 
|  | 1223 |  | 
|  | 1224 | You can use @code{offsetof} to measure the location within a structure | 
|  | 1225 | type of a particular structure member. | 
|  | 1226 |  | 
|  | 1227 | @comment stddef.h | 
|  | 1228 | @comment ISO | 
|  | 1229 | @deftypefn {Macro} size_t offsetof (@var{type}, @var{member}) | 
|  | 1230 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | 
|  | 1231 | @c This is no longer provided by glibc, but rather by the compiler. | 
|  | 1232 | This expands to an integer constant expression that is the offset of the | 
|  | 1233 | structure member named @var{member} in the structure type @var{type}. | 
|  | 1234 | For example, @code{offsetof (struct s, elem)} is the offset, in bytes, | 
|  | 1235 | of the member @code{elem} in a @code{struct s}. | 
|  | 1236 |  | 
|  | 1237 | This macro won't work if @var{member} is a bit field; you get an error | 
|  | 1238 | from the C compiler in that case. | 
|  | 1239 | @end deftypefn |