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