Blame manual/lang.texi

Packit 6c4009
@c This node must have no pointers.
Packit 6c4009
@node Language Features
Packit 6c4009
@c @node Language Features, Library Summary, , Top
Packit 6c4009
@c %MENU% C language features provided by the library
Packit 6c4009
@appendix C Language Facilities in the Library
Packit 6c4009
Packit 6c4009
Some of the facilities implemented by the C library really should be
Packit 6c4009
thought of as parts of the C language itself.  These facilities ought to
Packit 6c4009
be documented in the C Language Manual, not in the library manual; but
Packit 6c4009
since we don't have the language manual yet, and documentation for these
Packit 6c4009
features has been written, we are publishing it here.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Consistency Checking::        Using @code{assert} to abort if
Packit 6c4009
				 something ``impossible'' happens.
Packit 6c4009
* Variadic Functions::          Defining functions with varying numbers
Packit 6c4009
                                 of args.
Packit 6c4009
* Null Pointer Constant::       The macro @code{NULL}.
Packit 6c4009
* Important Data Types::        Data types for object sizes.
Packit 6c4009
* Data Type Measurements::      Parameters of data type representations.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Consistency Checking
Packit 6c4009
@section Explicitly Checking Internal Consistency
Packit 6c4009
@cindex consistency checking
Packit 6c4009
@cindex impossible events
Packit 6c4009
@cindex assertions
Packit 6c4009
Packit 6c4009
When you're writing a program, it's often a good idea to put in checks
Packit 6c4009
at strategic places for ``impossible'' errors or violations of basic
Packit 6c4009
assumptions.  These kinds of checks are helpful in debugging problems
Packit 6c4009
with the interfaces between different parts of the program, for example.
Packit 6c4009
Packit 6c4009
@pindex assert.h
Packit 6c4009
The @code{assert} macro, defined in the header file @file{assert.h},
Packit 6c4009
provides a convenient way to abort the program while printing a message
Packit 6c4009
about where in the program the error was detected.
Packit 6c4009
Packit 6c4009
@vindex NDEBUG
Packit 6c4009
Once you think your program is debugged, you can disable the error
Packit 6c4009
checks performed by the @code{assert} macro by recompiling with the
Packit 6c4009
macro @code{NDEBUG} defined.  This means you don't actually have to
Packit 6c4009
change the program source code to disable these checks.
Packit 6c4009
Packit 6c4009
But disabling these consistency checks is undesirable unless they make
Packit 6c4009
the program significantly slower.  All else being equal, more error
Packit 6c4009
checking is good no matter who is running the program.  A wise user
Packit 6c4009
would rather have a program crash, visibly, than have it return nonsense
Packit 6c4009
without indicating anything might be wrong.
Packit 6c4009
Packit 6c4009
@deftypefn Macro void assert (int @var{expression})
Packit 6c4009
@standards{ISO, assert.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asucorrupt{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
Packit 6c4009
@c assert_fail_base calls asprintf, and fflushes stderr.
Packit 6c4009
Verify the programmer's belief that @var{expression} is nonzero at
Packit 6c4009
this point in the program.
Packit 6c4009
Packit 6c4009
If @code{NDEBUG} is not defined, @code{assert} tests the value of
Packit 6c4009
@var{expression}.  If it is false (zero), @code{assert} aborts the
Packit 6c4009
program (@pxref{Aborting a Program}) after printing a message of the
Packit 6c4009
form:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@file{@var{file}}:@var{linenum}: @var{function}: Assertion `@var{expression}' failed.
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
on the standard error stream @code{stderr} (@pxref{Standard Streams}).
Packit 6c4009
The filename and line number are taken from the C preprocessor macros
Packit 6c4009
@code{__FILE__} and @code{__LINE__} and specify where the call to
Packit 6c4009
@code{assert} was made.  When using the GNU C compiler, the name of
Packit 6c4009
the function which calls @code{assert} is taken from the built-in
Packit 6c4009
variable @code{__PRETTY_FUNCTION__}; with older compilers, the function
Packit 6c4009
name and following colon are omitted.
Packit 6c4009
Packit 6c4009
If the preprocessor macro @code{NDEBUG} is defined before
Packit 6c4009
@file{assert.h} is included, the @code{assert} macro is defined to do
Packit 6c4009
absolutely nothing.
Packit 6c4009
Packit 6c4009
@strong{Warning:} Even the argument expression @var{expression} is not
Packit 6c4009
evaluated if @code{NDEBUG} is in effect.  So never use @code{assert}
Packit 6c4009
with arguments that involve side effects.  For example, @code{assert
Packit 6c4009
(++i > 0);} is a bad idea, because @code{i} will not be incremented if
Packit 6c4009
@code{NDEBUG} is defined.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
Sometimes the ``impossible'' condition you want to check for is an error
Packit 6c4009
return from an operating system function.  Then it is useful to display
Packit 6c4009
not only where the program crashes, but also what error was returned.
Packit 6c4009
The @code{assert_perror} macro makes this easy.
Packit 6c4009
Packit 6c4009
@deftypefn Macro void assert_perror (int @var{errnum})
Packit 6c4009
@standards{GNU, assert.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asucorrupt{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
Packit 6c4009
@c assert_fail_base calls asprintf, and fflushes stderr.
Packit 6c4009
Similar to @code{assert}, but verifies that @var{errnum} is zero.
Packit 6c4009
Packit 6c4009
If @code{NDEBUG} is not defined, @code{assert_perror} tests the value of
Packit 6c4009
@var{errnum}.  If it is nonzero, @code{assert_perror} aborts the program
Packit 6c4009
after printing a message of the form:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@file{@var{file}}:@var{linenum}: @var{function}: @var{error text}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
on the standard error stream.  The file name, line number, and function
Packit 6c4009
name are as for @code{assert}.  The error text is the result of
Packit 6c4009
@w{@code{strerror (@var{errnum})}}.  @xref{Error Messages}.
Packit 6c4009
Packit 6c4009
Like @code{assert}, if @code{NDEBUG} is defined before @file{assert.h}
Packit 6c4009
is included, the @code{assert_perror} macro does absolutely nothing.  It
Packit 6c4009
does not evaluate the argument, so @var{errnum} should not have any side
Packit 6c4009
effects.  It is best for @var{errnum} to be just a simple variable
Packit 6c4009
reference; often it will be @code{errno}.
Packit 6c4009
Packit 6c4009
This macro is a GNU extension.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
@strong{Usage note:} The @code{assert} facility is designed for
Packit 6c4009
detecting @emph{internal inconsistency}; it is not suitable for
Packit 6c4009
reporting invalid input or improper usage by the @emph{user} of the
Packit 6c4009
program.
Packit 6c4009
Packit 6c4009
The information in the diagnostic messages printed by the @code{assert}
Packit 6c4009
and @code{assert_perror} macro is intended to help you, the programmer,
Packit 6c4009
track down the cause of a bug, but is not really useful for telling a user
Packit 6c4009
of your program why his or her input was invalid or why a command could not
Packit 6c4009
be carried out.  What's more, your program should not abort when given
Packit 6c4009
invalid input, as @code{assert} would do---it should exit with nonzero
Packit 6c4009
status (@pxref{Exit Status}) after printing its error messages, or perhaps
Packit 6c4009
read another command or move on to the next input file.
Packit 6c4009
Packit 6c4009
@xref{Error Messages}, for information on printing error messages for
Packit 6c4009
problems that @emph{do not} represent bugs in the program.
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Variadic Functions
Packit 6c4009
@section Variadic Functions
Packit 6c4009
@cindex variable number of arguments
Packit 6c4009
@cindex variadic functions
Packit 6c4009
@cindex optional arguments
Packit 6c4009
Packit 6c4009
@w{ISO C} defines a syntax for declaring a function to take a variable
Packit 6c4009
number or type of arguments.  (Such functions are referred to as
Packit 6c4009
@dfn{varargs functions} or @dfn{variadic functions}.)  However, the
Packit 6c4009
language itself provides no mechanism for such functions to access their
Packit 6c4009
non-required arguments; instead, you use the variable arguments macros
Packit 6c4009
defined in @file{stdarg.h}.
Packit 6c4009
Packit 6c4009
This section describes how to declare variadic functions, how to write
Packit 6c4009
them, and how to call them properly.
Packit 6c4009
Packit 6c4009
@strong{Compatibility Note:} Many older C dialects provide a similar,
Packit 6c4009
but incompatible, mechanism for defining functions with variable numbers
Packit 6c4009
of arguments, using @file{varargs.h}.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Why Variadic::                Reasons for making functions take
Packit 6c4009
                                 variable arguments.
Packit 6c4009
* How Variadic::                How to define and call variadic functions.
Packit 6c4009
* Variadic Example::            A complete example.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Why Variadic
Packit 6c4009
@subsection Why Variadic Functions are Used
Packit 6c4009
Packit 6c4009
Ordinary C functions take a fixed number of arguments.  When you define
Packit 6c4009
a function, you specify the data type for each argument.  Every call to
Packit 6c4009
the function should supply the expected number of arguments, with types
Packit 6c4009
that can be converted to the specified ones.  Thus, if the function
Packit 6c4009
@samp{foo} is declared with @code{int foo (int, char *);} then you must
Packit 6c4009
call it with two arguments, a number (any kind will do) and a string
Packit 6c4009
pointer.
Packit 6c4009
Packit 6c4009
But some functions perform operations that can meaningfully accept an
Packit 6c4009
unlimited number of arguments.
Packit 6c4009
Packit 6c4009
In some cases a function can handle any number of values by operating on
Packit 6c4009
all of them as a block.  For example, consider a function that allocates
Packit 6c4009
a one-dimensional array with @code{malloc} to hold a specified set of
Packit 6c4009
values.  This operation makes sense for any number of values, as long as
Packit 6c4009
the length of the array corresponds to that number.  Without facilities
Packit 6c4009
for variable arguments, you would have to define a separate function for
Packit 6c4009
each possible array size.
Packit 6c4009
Packit 6c4009
The library function @code{printf} (@pxref{Formatted Output}) is an
Packit 6c4009
example of another class of function where variable arguments are
Packit 6c4009
useful.  This function prints its arguments (which can vary in type as
Packit 6c4009
well as number) under the control of a format template string.
Packit 6c4009
Packit 6c4009
These are good reasons to define a @dfn{variadic} function which can
Packit 6c4009
handle as many arguments as the caller chooses to pass.
Packit 6c4009
Packit 6c4009
Some functions such as @code{open} take a fixed set of arguments, but
Packit 6c4009
occasionally ignore the last few.  Strict adherence to @w{ISO C} requires
Packit 6c4009
these functions to be defined as variadic; in practice, however, the GNU
Packit 6c4009
C compiler and most other C compilers let you define such a function to
Packit 6c4009
take a fixed set of arguments---the most it can ever use---and then only
Packit 6c4009
@emph{declare} the function as variadic (or not declare its arguments
Packit 6c4009
at all!).
Packit 6c4009
Packit 6c4009
@node How Variadic
Packit 6c4009
@subsection How Variadic Functions are Defined and Used
Packit 6c4009
Packit 6c4009
Defining and using a variadic function involves three steps:
Packit 6c4009
Packit 6c4009
@itemize @bullet
Packit 6c4009
@item
Packit 6c4009
@emph{Define} the function as variadic, using an ellipsis
Packit 6c4009
(@samp{@dots{}}) in the argument list, and using special macros to
Packit 6c4009
access the variable arguments.  @xref{Receiving Arguments}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
@emph{Declare} the function as variadic, using a prototype with an
Packit 6c4009
ellipsis (@samp{@dots{}}), in all the files which call it.
Packit 6c4009
@xref{Variadic Prototypes}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
@emph{Call} the function by writing the fixed arguments followed by the
Packit 6c4009
additional variable arguments.  @xref{Calling Variadics}.
Packit 6c4009
@end itemize
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Variadic Prototypes::  How to make a prototype for a function
Packit 6c4009
			  with variable arguments.
Packit 6c4009
* Receiving Arguments::  Steps you must follow to access the
Packit 6c4009
			  optional argument values.
Packit 6c4009
* How Many Arguments::   How to decide whether there are more arguments.
Packit 6c4009
* Calling Variadics::    Things you need to know about calling
Packit 6c4009
			  variable arguments functions.
Packit 6c4009
* Argument Macros::      Detailed specification of the macros
Packit 6c4009
        		  for accessing variable arguments.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Variadic Prototypes
Packit 6c4009
@subsubsection Syntax for Variable Arguments
Packit 6c4009
@cindex function prototypes (variadic)
Packit 6c4009
@cindex prototypes for variadic functions
Packit 6c4009
@cindex variadic function prototypes
Packit 6c4009
Packit 6c4009
A function that accepts a variable number of arguments must be declared
Packit 6c4009
with a prototype that says so.   You write the fixed arguments as usual,
Packit 6c4009
and then tack on @samp{@dots{}} to indicate the possibility of
Packit 6c4009
additional arguments.  The syntax of @w{ISO C} requires at least one fixed
Packit 6c4009
argument before the @samp{@dots{}}.  For example,
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
int
Packit 6c4009
func (const char *a, int b, @dots{})
Packit 6c4009
@{
Packit 6c4009
  @dots{}
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
defines a function @code{func} which returns an @code{int} and takes two
Packit 6c4009
required arguments, a @code{const char *} and an @code{int}.  These are
Packit 6c4009
followed by any number of anonymous arguments.
Packit 6c4009
Packit 6c4009
@strong{Portability note:} For some C compilers, the last required
Packit 6c4009
argument must not be declared @code{register} in the function
Packit 6c4009
definition.  Furthermore, this argument's type must be
Packit 6c4009
@dfn{self-promoting}: that is, the default promotions must not change
Packit 6c4009
its type.  This rules out array and function types, as well as
Packit 6c4009
@code{float}, @code{char} (whether signed or not) and @w{@code{short int}}
Packit 6c4009
(whether signed or not).  This is actually an @w{ISO C} requirement.
Packit 6c4009
Packit 6c4009
@node Receiving Arguments
Packit 6c4009
@subsubsection Receiving the Argument Values
Packit 6c4009
@cindex variadic function argument access
Packit 6c4009
@cindex arguments (variadic functions)
Packit 6c4009
Packit 6c4009
Ordinary fixed arguments have individual names, and you can use these
Packit 6c4009
names to access their values.  But optional arguments have no
Packit 6c4009
names---nothing but @samp{@dots{}}.  How can you access them?
Packit 6c4009
Packit 6c4009
@pindex stdarg.h
Packit 6c4009
The only way to access them is sequentially, in the order they were
Packit 6c4009
written, and you must use special macros from @file{stdarg.h} in the
Packit 6c4009
following three step process:
Packit 6c4009
Packit 6c4009
@enumerate
Packit 6c4009
@item
Packit 6c4009
You initialize an argument pointer variable of type @code{va_list} using
Packit 6c4009
@code{va_start}.  The argument pointer when initialized points to the
Packit 6c4009
first optional argument.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
You access the optional arguments by successive calls to @code{va_arg}.
Packit 6c4009
The first call to @code{va_arg} gives you the first optional argument,
Packit 6c4009
the next call gives you the second, and so on.
Packit 6c4009
Packit 6c4009
You can stop at any time if you wish to ignore any remaining optional
Packit 6c4009
arguments.  It is perfectly all right for a function to access fewer
Packit 6c4009
arguments than were supplied in the call, but you will get garbage
Packit 6c4009
values if you try to access too many arguments.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
You indicate that you are finished with the argument pointer variable by
Packit 6c4009
calling @code{va_end}.
Packit 6c4009
Packit 6c4009
(In practice, with most C compilers, calling @code{va_end} does nothing.
Packit 6c4009
This is always true in the GNU C compiler.  But you might as well call
Packit 6c4009
@code{va_end} just in case your program is someday compiled with a peculiar
Packit 6c4009
compiler.)
Packit 6c4009
@end enumerate
Packit 6c4009
Packit 6c4009
@xref{Argument Macros}, for the full definitions of @code{va_start},
Packit 6c4009
@code{va_arg} and @code{va_end}.
Packit 6c4009
Packit 6c4009
Steps 1 and 3 must be performed in the function that accepts the
Packit 6c4009
optional arguments.  However, you can pass the @code{va_list} variable
Packit 6c4009
as an argument to another function and perform all or part of step 2
Packit 6c4009
there.
Packit 6c4009
Packit 6c4009
You can perform the entire sequence of three steps multiple times
Packit 6c4009
within a single function invocation.  If you want to ignore the optional
Packit 6c4009
arguments, you can do these steps zero times.
Packit 6c4009
Packit 6c4009
You can have more than one argument pointer variable if you like.  You
Packit 6c4009
can initialize each variable with @code{va_start} when you wish, and
Packit 6c4009
then you can fetch arguments with each argument pointer as you wish.
Packit 6c4009
Each argument pointer variable will sequence through the same set of
Packit 6c4009
argument values, but at its own pace.
Packit 6c4009
Packit 6c4009
@strong{Portability note:} With some compilers, once you pass an
Packit 6c4009
argument pointer value to a subroutine, you must not keep using the same
Packit 6c4009
argument pointer value after that subroutine returns.  For full
Packit 6c4009
portability, you should just pass it to @code{va_end}.  This is actually
Packit 6c4009
an @w{ISO C} requirement, but most ANSI C compilers work happily
Packit 6c4009
regardless.
Packit 6c4009
Packit 6c4009
@node How Many Arguments
Packit 6c4009
@subsubsection How Many Arguments Were Supplied
Packit 6c4009
@cindex number of arguments passed
Packit 6c4009
@cindex how many arguments
Packit 6c4009
@cindex arguments, how many
Packit 6c4009
Packit 6c4009
There is no general way for a function to determine the number and type
Packit 6c4009
of the optional arguments it was called with.  So whoever designs the
Packit 6c4009
function typically designs a convention for the caller to specify the number
Packit 6c4009
and type of arguments.  It is up to you to define an appropriate calling
Packit 6c4009
convention for each variadic function, and write all calls accordingly.
Packit 6c4009
Packit 6c4009
One kind of calling convention is to pass the number of optional
Packit 6c4009
arguments as one of the fixed arguments.  This convention works provided
Packit 6c4009
all of the optional arguments are of the same type.
Packit 6c4009
Packit 6c4009
A similar alternative is to have one of the required arguments be a bit
Packit 6c4009
mask, with a bit for each possible purpose for which an optional
Packit 6c4009
argument might be supplied.  You would test the bits in a predefined
Packit 6c4009
sequence; if the bit is set, fetch the value of the next argument,
Packit 6c4009
otherwise use a default value.
Packit 6c4009
Packit 6c4009
A required argument can be used as a pattern to specify both the number
Packit 6c4009
and types of the optional arguments.  The format string argument to
Packit 6c4009
@code{printf} is one example of this (@pxref{Formatted Output Functions}).
Packit 6c4009
Packit 6c4009
Another possibility is to pass an ``end marker'' value as the last
Packit 6c4009
optional argument.  For example, for a function that manipulates an
Packit 6c4009
arbitrary number of pointer arguments, a null pointer might indicate the
Packit 6c4009
end of the argument list.  (This assumes that a null pointer isn't
Packit 6c4009
otherwise meaningful to the function.)  The @code{execl} function works
Packit 6c4009
in just this way; see @ref{Executing a File}.
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Calling Variadics
Packit 6c4009
@subsubsection Calling Variadic Functions
Packit 6c4009
@cindex variadic functions, calling
Packit 6c4009
@cindex calling variadic functions
Packit 6c4009
@cindex declaring variadic functions
Packit 6c4009
Packit 6c4009
You don't have to do anything special to call a variadic function.
Packit 6c4009
Just put the arguments (required arguments, followed by optional ones)
Packit 6c4009
inside parentheses, separated by commas, as usual.  But you must declare
Packit 6c4009
the function with a prototype and know how the argument values are converted.
Packit 6c4009
Packit 6c4009
In principle, functions that are @emph{defined} to be variadic must also
Packit 6c4009
be @emph{declared} to be variadic using a function prototype whenever
Packit 6c4009
you call them.  (@xref{Variadic Prototypes}, for how.)  This is because
Packit 6c4009
some C compilers use a different calling convention to pass the same set
Packit 6c4009
of argument values to a function depending on whether that function
Packit 6c4009
takes variable arguments or fixed arguments.
Packit 6c4009
Packit 6c4009
In practice, the GNU C compiler always passes a given set of argument
Packit 6c4009
types in the same way regardless of whether they are optional or
Packit 6c4009
required.  So, as long as the argument types are self-promoting, you can
Packit 6c4009
safely omit declaring them.  Usually it is a good idea to declare the
Packit 6c4009
argument types for variadic functions, and indeed for all functions.
Packit 6c4009
But there are a few functions which it is extremely convenient not to
Packit 6c4009
have to declare as variadic---for example, @code{open} and
Packit 6c4009
@code{printf}.
Packit 6c4009
Packit 6c4009
@cindex default argument promotions
Packit 6c4009
@cindex argument promotion
Packit 6c4009
Since the prototype doesn't specify types for optional arguments, in a
Packit 6c4009
call to a variadic function the @dfn{default argument promotions} are
Packit 6c4009
performed on the optional argument values.  This means the objects of
Packit 6c4009
type @code{char} or @w{@code{short int}} (whether signed or not) are
Packit 6c4009
promoted to either @code{int} or @w{@code{unsigned int}}, as
Packit 6c4009
appropriate; and that objects of type @code{float} are promoted to type
Packit 6c4009
@code{double}.  So, if the caller passes a @code{char} as an optional
Packit 6c4009
argument, it is promoted to an @code{int}, and the function can access
Packit 6c4009
it with @code{va_arg (@var{ap}, int)}.
Packit 6c4009
Packit 6c4009
Conversion of the required arguments is controlled by the function
Packit 6c4009
prototype in the usual way: the argument expression is converted to the
Packit 6c4009
declared argument type as if it were being assigned to a variable of
Packit 6c4009
that type.
Packit 6c4009
Packit 6c4009
@node Argument Macros
Packit 6c4009
@subsubsection Argument Access Macros
Packit 6c4009
Packit 6c4009
Here are descriptions of the macros used to retrieve variable arguments.
Packit 6c4009
These macros are defined in the header file @file{stdarg.h}.
Packit 6c4009
@pindex stdarg.h
Packit 6c4009
Packit 6c4009
@deftp {Data Type} va_list
Packit 6c4009
@standards{ISO, stdarg.h}
Packit 6c4009
The type @code{va_list} is used for argument pointer variables.
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
@deftypefn {Macro} void va_start (va_list @var{ap}, @var{last-required})
Packit 6c4009
@standards{ISO, stdarg.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c This is no longer provided by glibc, but rather by the compiler.
Packit 6c4009
This macro initializes the argument pointer variable @var{ap} to point
Packit 6c4009
to the first of the optional arguments of the current function;
Packit 6c4009
@var{last-required} must be the last required argument to the function.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
@deftypefn {Macro} @var{type} va_arg (va_list @var{ap}, @var{type})
Packit 6c4009
@standards{ISO, stdarg.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsrace{:ap}}@assafe{}@acunsafe{@acucorrupt{}}}
Packit 6c4009
@c This is no longer provided by glibc, but rather by the compiler.
Packit 6c4009
@c Unlike the other va_ macros, that either start/end the lifetime of
Packit 6c4009
@c the va_list object or don't modify it, this one modifies ap, and it
Packit 6c4009
@c may leave it in a partially updated state.
Packit 6c4009
The @code{va_arg} macro returns the value of the next optional argument,
Packit 6c4009
and modifies the value of @var{ap} to point to the subsequent argument.
Packit 6c4009
Thus, successive uses of @code{va_arg} return successive optional
Packit 6c4009
arguments.
Packit 6c4009
Packit 6c4009
The type of the value returned by @code{va_arg} is @var{type} as
Packit 6c4009
specified in the call.  @var{type} must be a self-promoting type (not
Packit 6c4009
@code{char} or @code{short int} or @code{float}) that matches the type
Packit 6c4009
of the actual argument.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
@deftypefn {Macro} void va_end (va_list @var{ap})
Packit 6c4009
@standards{ISO, stdarg.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c This is no longer provided by glibc, but rather by the compiler.
Packit 6c4009
This ends the use of @var{ap}.  After a @code{va_end} call, further
Packit 6c4009
@code{va_arg} calls with the same @var{ap} may not work.  You should invoke
Packit 6c4009
@code{va_end} before returning from the function in which @code{va_start}
Packit 6c4009
was invoked with the same @var{ap} argument.
Packit 6c4009
Packit 6c4009
In @theglibc{}, @code{va_end} does nothing, and you need not ever
Packit 6c4009
use it except for reasons of portability.
Packit 6c4009
@refill
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
Sometimes it is necessary to parse the list of parameters more than once
Packit 6c4009
or one wants to remember a certain position in the parameter list.  To
Packit 6c4009
do this, one will have to make a copy of the current value of the
Packit 6c4009
argument.  But @code{va_list} is an opaque type and one cannot necessarily
Packit 6c4009
assign the value of one variable of type @code{va_list} to another variable
Packit 6c4009
of the same type.
Packit 6c4009
Packit 6c4009
@deftypefn {Macro} void va_copy (va_list @var{dest}, va_list @var{src})
Packit 6c4009
@deftypefnx {Macro} void __va_copy (va_list @var{dest}, va_list @var{src})
Packit 6c4009
@standardsx{va_copy, C99, stdarg.h}
Packit 6c4009
@standardsx{__va_copy, GNU, stdarg.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
The @code{va_copy} macro allows copying of objects of type
Packit 6c4009
@code{va_list} even if this is not an integral type.  The argument pointer
Packit 6c4009
in @var{dest} is initialized to point to the same argument as the
Packit 6c4009
pointer in @var{src}.
Packit 6c4009
Packit 6c4009
@code{va_copy} was added in ISO C99.  When building for strict
Packit 6c4009
conformance to ISO C90 (@samp{gcc -std=c90}), it is not available.
Packit 6c4009
GCC provides @code{__va_copy}, as an extension, in any standards mode;
Packit 6c4009
before GCC 3.0, it was the only macro for this functionality.
Packit 6c4009
Packit 6c4009
These macros are no longer provided by @theglibc{}, but rather by the
Packit 6c4009
compiler.
Packit 6c4009
@end deftypefn
Packit 6c4009
Packit 6c4009
If you want to use @code{va_copy} and be portable to pre-C99 systems,
Packit 6c4009
you should always be prepared for the
Packit 6c4009
possibility that this macro will not be available.  On architectures where a
Packit 6c4009
simple assignment is invalid, hopefully @code{va_copy} @emph{will} be available,
Packit 6c4009
so one should always write something like this if concerned about
Packit 6c4009
pre-C99 portability:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@{
Packit 6c4009
  va_list ap, save;
Packit 6c4009
  @dots{}
Packit 6c4009
#ifdef va_copy
Packit 6c4009
  va_copy (save, ap);
Packit 6c4009
#else
Packit 6c4009
  save = ap;
Packit 6c4009
#endif
Packit 6c4009
  @dots{}
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Variadic Example
Packit 6c4009
@subsection Example of a Variadic Function
Packit 6c4009
Packit 6c4009
Here is a complete sample function that accepts a variable number of
Packit 6c4009
arguments.  The first argument to the function is the count of remaining
Packit 6c4009
arguments, which are added up and the result returned.  While trivial,
Packit 6c4009
this function is sufficient to illustrate how to use the variable
Packit 6c4009
arguments facility.
Packit 6c4009
Packit 6c4009
@comment Yes, this example has been tested.
Packit 6c4009
@smallexample
Packit 6c4009
@include add.c.texi
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@node Null Pointer Constant
Packit 6c4009
@section Null Pointer Constant
Packit 6c4009
@cindex null pointer constant
Packit 6c4009
Packit 6c4009
The null pointer constant is guaranteed not to point to any real object.
Packit 6c4009
You can assign it to any pointer variable since it has type @code{void
Packit 6c4009
*}.  The preferred way to write a null pointer constant is with
Packit 6c4009
@code{NULL}.
Packit 6c4009
Packit 6c4009
@deftypevr Macro {void *} NULL
Packit 6c4009
@standards{ISO, stddef.h}
Packit 6c4009
This is a null pointer constant.
Packit 6c4009
@end deftypevr
Packit 6c4009
Packit 6c4009
You can also use @code{0} or @code{(void *)0} as a null pointer
Packit 6c4009
constant, but using @code{NULL} is cleaner because it makes the purpose
Packit 6c4009
of the constant more evident.
Packit 6c4009
Packit 6c4009
If you use the null pointer constant as a function argument, then for
Packit 6c4009
complete portability you should make sure that the function has a
Packit 6c4009
prototype declaration.  Otherwise, if the target machine has two
Packit 6c4009
different pointer representations, the compiler won't know which
Packit 6c4009
representation to use for that argument.  You can avoid the problem by
Packit 6c4009
explicitly casting the constant to the proper pointer type, but we
Packit 6c4009
recommend instead adding a prototype for the function you are calling.
Packit 6c4009
Packit 6c4009
@node Important Data Types
Packit 6c4009
@section Important Data Types
Packit 6c4009
Packit 6c4009
The result of subtracting two pointers in C is always an integer, but the
Packit 6c4009
precise data type varies from C compiler to C compiler.  Likewise, the
Packit 6c4009
data type of the result of @code{sizeof} also varies between compilers.
Packit 6c4009
ISO C defines standard aliases for these two types, so you can refer to
Packit 6c4009
them in a portable fashion.  They are defined in the header file
Packit 6c4009
@file{stddef.h}.
Packit 6c4009
@pindex stddef.h
Packit 6c4009
Packit 6c4009
@deftp {Data Type} ptrdiff_t
Packit 6c4009
@standards{ISO, stddef.h}
Packit 6c4009
This is the signed integer type of the result of subtracting two
Packit 6c4009
pointers.  For example, with the declaration @code{char *p1, *p2;}, the
Packit 6c4009
expression @code{p2 - p1} is of type @code{ptrdiff_t}.  This will
Packit 6c4009
probably be one of the standard signed integer types (@w{@code{short
Packit 6c4009
int}}, @code{int} or @w{@code{long int}}), but might be a nonstandard
Packit 6c4009
type that exists only for this purpose.
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
@deftp {Data Type} size_t
Packit 6c4009
@standards{ISO, stddef.h}
Packit 6c4009
This is an unsigned integer type used to represent the sizes of objects.
Packit 6c4009
The result of the @code{sizeof} operator is of this type, and functions
Packit 6c4009
such as @code{malloc} (@pxref{Unconstrained Allocation}) and
Packit 6c4009
@code{memcpy} (@pxref{Copying Strings and Arrays}) accept arguments of
Packit 6c4009
this type to specify object sizes.  On systems using @theglibc{}, this
Packit 6c4009
will be @w{@code{unsigned int}} or @w{@code{unsigned long int}}.
Packit 6c4009
Packit 6c4009
@strong{Usage Note:} @code{size_t} is the preferred way to declare any
Packit 6c4009
arguments or variables that hold the size of an object.
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
@strong{Compatibility Note:} Implementations of C before the advent of
Packit 6c4009
@w{ISO C} generally used @code{unsigned int} for representing object sizes
Packit 6c4009
and @code{int} for pointer subtraction results.  They did not
Packit 6c4009
necessarily define either @code{size_t} or @code{ptrdiff_t}.  Unix
Packit 6c4009
systems did define @code{size_t}, in @file{sys/types.h}, but the
Packit 6c4009
definition was usually a signed type.
Packit 6c4009
Packit 6c4009
@node Data Type Measurements
Packit 6c4009
@section Data Type Measurements
Packit 6c4009
Packit 6c4009
Most of the time, if you choose the proper C data type for each object
Packit 6c4009
in your program, you need not be concerned with just how it is
Packit 6c4009
represented or how many bits it uses.  When you do need such
Packit 6c4009
information, the C language itself does not provide a way to get it.
Packit 6c4009
The header files @file{limits.h} and @file{float.h} contain macros
Packit 6c4009
which give you this information in full detail.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Width of Type::           How many bits does an integer type hold?
Packit 6c4009
* Range of Type::           What are the largest and smallest values
Packit 6c4009
			     that an integer type can hold?
Packit 6c4009
* Floating Type Macros::    Parameters that measure the floating point types.
Packit 6c4009
* Structure Measurement::   Getting measurements on structure types.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Width of Type
Packit 6c4009
@subsection Width of an Integer Type
Packit 6c4009
@cindex integer type width
Packit 6c4009
@cindex width of integer type
Packit 6c4009
@cindex type measurements, integer
Packit 6c4009
@pindex limits.h
Packit 6c4009
Packit 6c4009
TS 18661-1:2014 defines macros for the width of integer types (the
Packit 6c4009
number of value and sign bits).  One benefit of these macros is they
Packit 6c4009
can be used in @code{#if} preprocessor directives, whereas
Packit 6c4009
@code{sizeof} cannot.  The following macros are defined in
Packit 6c4009
@file{limits.h}.
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item CHAR_WIDTH
Packit 6c4009
@itemx SCHAR_WIDTH
Packit 6c4009
@itemx UCHAR_WIDTH
Packit 6c4009
@itemx SHRT_WIDTH
Packit 6c4009
@itemx USHRT_WIDTH
Packit 6c4009
@itemx INT_WIDTH
Packit 6c4009
@itemx UINT_WIDTH
Packit 6c4009
@itemx LONG_WIDTH
Packit 6c4009
@itemx ULONG_WIDTH
Packit 6c4009
@itemx LLONG_WIDTH
Packit 6c4009
@itemx ULLONG_WIDTH
Packit 6c4009
@standards{ISO, limits.h}
Packit 6c4009
These are the widths of the types @code{char}, @code{signed char},
Packit 6c4009
@code{unsigned char}, @code{short int}, @code{unsigned short int},
Packit 6c4009
@code{int}, @code{unsigned int}, @code{long int}, @code{unsigned long
Packit 6c4009
int}, @code{long long int} and @code{unsigned long long int},
Packit 6c4009
respectively.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
Further such macros are defined in @file{stdint.h}.  Apart from those
Packit 6c4009
for types specified by width (@pxref{Integers}), the following are
Packit 6c4009
defined:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item INTPTR_WIDTH
Packit 6c4009
@itemx UINTPTR_WIDTH
Packit 6c4009
@itemx PTRDIFF_WIDTH
Packit 6c4009
@itemx SIG_ATOMIC_WIDTH
Packit 6c4009
@itemx SIZE_WIDTH
Packit 6c4009
@itemx WCHAR_WIDTH
Packit 6c4009
@itemx WINT_WIDTH
Packit 6c4009
@standards{ISO, stdint.h}
Packit 6c4009
These are the widths of the types @code{intptr_t}, @code{uintptr_t},
Packit 6c4009
@code{ptrdiff_t}, @code{sig_atomic_t}, @code{size_t}, @code{wchar_t}
Packit 6c4009
and @code{wint_t}, respectively.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
A common reason that a program needs to know how many bits are in an
Packit 6c4009
integer type is for using an array of @code{unsigned long int} as a
Packit 6c4009
bit vector.  You can access the bit at index @var{n} with:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
vector[@var{n} / ULONG_WIDTH] & (1UL << (@var{n} % ULONG_WIDTH))
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
Before @code{ULONG_WIDTH} was a part of the C language,
Packit 6c4009
@code{CHAR_BIT} was used to compute the number of bits in an integer
Packit 6c4009
data type.
Packit 6c4009
Packit 6c4009
@deftypevr Macro int CHAR_BIT
Packit 6c4009
@standards{C90, limits.h}
Packit 6c4009
This is the number of bits in a @code{char}.  POSIX.1-2001 requires
Packit 6c4009
this to be 8.
Packit 6c4009
@end deftypevr
Packit 6c4009
Packit 6c4009
The number of bits in any data type @var{type} can be computed like
Packit 6c4009
this:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
sizeof (@var{type}) * CHAR_BIT
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
That expression includes padding bits as well as value and sign bits.
Packit 6c4009
On all systems supported by @theglibc{}, standard integer types other
Packit 6c4009
than @code{_Bool} do not have any padding bits.
Packit 6c4009
Packit 6c4009
@strong{Portability Note:} One cannot actually easily compute the
Packit 6c4009
number of usable bits in a portable manner.
Packit 6c4009
Packit 6c4009
@node Range of Type
Packit 6c4009
@subsection Range of an Integer Type
Packit 6c4009
@cindex integer type range
Packit 6c4009
@cindex range of integer type
Packit 6c4009
@cindex limits, integer types
Packit 6c4009
Packit 6c4009
Suppose you need to store an integer value which can range from zero to
Packit 6c4009
one million.  Which is the smallest type you can use?  There is no
Packit 6c4009
general rule; it depends on the C compiler and target machine.  You can
Packit 6c4009
use the @samp{MIN} and @samp{MAX} macros in @file{limits.h} to determine
Packit 6c4009
which type will work.
Packit 6c4009
Packit 6c4009
Each signed integer type has a pair of macros which give the smallest
Packit 6c4009
and largest values that it can hold.  Each unsigned integer type has one
Packit 6c4009
such macro, for the maximum value; the minimum value is, of course,
Packit 6c4009
zero.
Packit 6c4009
Packit 6c4009
The values of these macros are all integer constant expressions.  The
Packit 6c4009
@samp{MAX} and @samp{MIN} macros for @code{char} and @w{@code{short
Packit 6c4009
int}} types have values of type @code{int}.  The @samp{MAX} and
Packit 6c4009
@samp{MIN} macros for the other types have values of the same type
Packit 6c4009
described by the macro---thus, @code{ULONG_MAX} has type
Packit 6c4009
@w{@code{unsigned long int}}.
Packit 6c4009
Packit 6c4009
@comment Extra blank lines make it look better.
Packit 6c4009
@vtable @code
Packit 6c4009
@item SCHAR_MIN
Packit 6c4009
@standards{ISO, limits.h}
Packit 6c4009
Packit 6c4009
This is the minimum value that can be represented by a @w{@code{signed char}}.
Packit 6c4009
Packit 6c4009
@item SCHAR_MAX
Packit 6c4009
@itemx UCHAR_MAX
Packit 6c4009
@standards{ISO, limits.h}
Packit 6c4009
Packit 6c4009
These are the maximum values that can be represented by a
Packit 6c4009
@w{@code{signed char}} and @w{@code{unsigned char}}, respectively.
Packit 6c4009
Packit 6c4009
@item CHAR_MIN
Packit 6c4009
@standards{ISO, limits.h}
Packit 6c4009
Packit 6c4009
This is the minimum value that can be represented by a @code{char}.
Packit 6c4009
It's equal to @code{SCHAR_MIN} if @code{char} is signed, or zero
Packit 6c4009
otherwise.
Packit 6c4009
Packit 6c4009
@item CHAR_MAX
Packit 6c4009
@standards{ISO, limits.h}
Packit 6c4009
Packit 6c4009
This is the maximum value that can be represented by a @code{char}.
Packit 6c4009
It's equal to @code{SCHAR_MAX} if @code{char} is signed, or
Packit 6c4009
@code{UCHAR_MAX} otherwise.
Packit 6c4009
Packit 6c4009
@item SHRT_MIN
Packit 6c4009
@standards{ISO, limits.h}
Packit 6c4009
Packit 6c4009
This is the minimum value that can be represented by a @w{@code{signed
Packit 6c4009
short int}}.  On most machines that @theglibc{} runs on,
Packit 6c4009
@code{short} integers are 16-bit quantities.
Packit 6c4009
Packit 6c4009
@item SHRT_MAX
Packit 6c4009
@itemx USHRT_MAX
Packit 6c4009
@standards{ISO, limits.h}
Packit 6c4009
Packit 6c4009
These are the maximum values that can be represented by a
Packit 6c4009
@w{@code{signed short int}} and @w{@code{unsigned short int}},
Packit 6c4009
respectively.
Packit 6c4009
Packit 6c4009
@item INT_MIN
Packit 6c4009
@standards{ISO, limits.h}
Packit 6c4009
Packit 6c4009
This is the minimum value that can be represented by a @w{@code{signed
Packit 6c4009
int}}.  On most machines that @theglibc{} runs on, an @code{int} is
Packit 6c4009
a 32-bit quantity.
Packit 6c4009
Packit 6c4009
@item INT_MAX
Packit 6c4009
@itemx UINT_MAX
Packit 6c4009
@standards{ISO, limits.h}
Packit 6c4009
Packit 6c4009
These are the maximum values that can be represented by, respectively,
Packit 6c4009
the type @w{@code{signed int}} and the type @w{@code{unsigned int}}.
Packit 6c4009
Packit 6c4009
@item LONG_MIN
Packit 6c4009
@standards{ISO, limits.h}
Packit 6c4009
Packit 6c4009
This is the minimum value that can be represented by a @w{@code{signed
Packit 6c4009
long int}}.  On most machines that @theglibc{} runs on, @code{long}
Packit 6c4009
integers are 32-bit quantities, the same size as @code{int}.
Packit 6c4009
Packit 6c4009
@item LONG_MAX
Packit 6c4009
@itemx ULONG_MAX
Packit 6c4009
@standards{ISO, limits.h}
Packit 6c4009
Packit 6c4009
These are the maximum values that can be represented by a
Packit 6c4009
@w{@code{signed long int}} and @code{unsigned long int}, respectively.
Packit 6c4009
Packit 6c4009
@item LLONG_MIN
Packit 6c4009
@standards{ISO, limits.h}
Packit 6c4009
Packit 6c4009
This is the minimum value that can be represented by a @w{@code{signed
Packit 6c4009
long long int}}.  On most machines that @theglibc{} runs on,
Packit 6c4009
@w{@code{long long}} integers are 64-bit quantities.
Packit 6c4009
Packit 6c4009
@item LLONG_MAX
Packit 6c4009
@itemx ULLONG_MAX
Packit 6c4009
@standards{ISO, limits.h}
Packit 6c4009
Packit 6c4009
These are the maximum values that can be represented by a @code{signed
Packit 6c4009
long long int} and @code{unsigned long long int}, respectively.
Packit 6c4009
Packit 6c4009
@item LONG_LONG_MIN
Packit 6c4009
@itemx LONG_LONG_MAX
Packit 6c4009
@itemx ULONG_LONG_MAX
Packit 6c4009
@standards{GNU, limits.h}
Packit 6c4009
These are obsolete names for @code{LLONG_MIN}, @code{LLONG_MAX}, and
Packit 6c4009
@code{ULLONG_MAX}.  They are only available if @code{_GNU_SOURCE} is
Packit 6c4009
defined (@pxref{Feature Test Macros}).  In GCC versions prior to 3.0,
Packit 6c4009
these were the only names available.
Packit 6c4009
Packit 6c4009
@item WCHAR_MAX
Packit 6c4009
@standards{GNU, limits.h}
Packit 6c4009
Packit 6c4009
This is the maximum value that can be represented by a @code{wchar_t}.
Packit 6c4009
@xref{Extended Char Intro}.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
The header file @file{limits.h} also defines some additional constants
Packit 6c4009
that parameterize various operating system and file system limits.  These
Packit 6c4009
constants are described in @ref{System Configuration}.
Packit 6c4009
Packit 6c4009
@node Floating Type Macros
Packit 6c4009
@subsection Floating Type Macros
Packit 6c4009
@cindex floating type measurements
Packit 6c4009
@cindex measurements of floating types
Packit 6c4009
@cindex type measurements, floating
Packit 6c4009
@cindex limits, floating types
Packit 6c4009
Packit 6c4009
The specific representation of floating point numbers varies from
Packit 6c4009
machine to machine.  Because floating point numbers are represented
Packit 6c4009
internally as approximate quantities, algorithms for manipulating
Packit 6c4009
floating point data often need to take account of the precise details of
Packit 6c4009
the machine's floating point representation.
Packit 6c4009
Packit 6c4009
Some of the functions in the C library itself need this information; for
Packit 6c4009
example, the algorithms for printing and reading floating point numbers
Packit 6c4009
(@pxref{I/O on Streams}) and for calculating trigonometric and
Packit 6c4009
irrational functions (@pxref{Mathematics}) use it to avoid round-off
Packit 6c4009
error and loss of accuracy.  User programs that implement numerical
Packit 6c4009
analysis techniques also often need this information in order to
Packit 6c4009
minimize or compute error bounds.
Packit 6c4009
Packit 6c4009
The header file @file{float.h} describes the format used by your
Packit 6c4009
machine.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Floating Point Concepts::     Definitions of terminology.
Packit 6c4009
* Floating Point Parameters::   Details of specific macros.
Packit 6c4009
* IEEE Floating Point::         The measurements for one common
Packit 6c4009
                                 representation.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Floating Point Concepts
Packit 6c4009
@subsubsection Floating Point Representation Concepts
Packit 6c4009
Packit 6c4009
This section introduces the terminology for describing floating point
Packit 6c4009
representations.
Packit 6c4009
Packit 6c4009
You are probably already familiar with most of these concepts in terms
Packit 6c4009
of scientific or exponential notation for floating point numbers.  For
Packit 6c4009
example, the number @code{123456.0} could be expressed in exponential
Packit 6c4009
notation as @code{1.23456e+05}, a shorthand notation indicating that the
Packit 6c4009
mantissa @code{1.23456} is multiplied by the base @code{10} raised to
Packit 6c4009
power @code{5}.
Packit 6c4009
Packit 6c4009
More formally, the internal representation of a floating point number
Packit 6c4009
can be characterized in terms of the following parameters:
Packit 6c4009
Packit 6c4009
@itemize @bullet
Packit 6c4009
@item
Packit 6c4009
@cindex sign (of floating point number)
Packit 6c4009
The @dfn{sign} is either @code{-1} or @code{1}.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
@cindex base (of floating point number)
Packit 6c4009
@cindex radix (of floating point number)
Packit 6c4009
The @dfn{base} or @dfn{radix} for exponentiation, an integer greater
Packit 6c4009
than @code{1}.  This is a constant for a particular representation.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
@cindex exponent (of floating point number)
Packit 6c4009
The @dfn{exponent} to which the base is raised.  The upper and lower
Packit 6c4009
bounds of the exponent value are constants for a particular
Packit 6c4009
representation.
Packit 6c4009
Packit 6c4009
@cindex bias (of floating point number exponent)
Packit 6c4009
Sometimes, in the actual bits representing the floating point number,
Packit 6c4009
the exponent is @dfn{biased} by adding a constant to it, to make it
Packit 6c4009
always be represented as an unsigned quantity.  This is only important
Packit 6c4009
if you have some reason to pick apart the bit fields making up the
Packit 6c4009
floating point number by hand, which is something for which @theglibc{}
Packit 6c4009
provides no support.  So this is ignored in the discussion that
Packit 6c4009
follows.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
@cindex mantissa (of floating point number)
Packit 6c4009
@cindex significand (of floating point number)
Packit 6c4009
The @dfn{mantissa} or @dfn{significand} is an unsigned integer which is a
Packit 6c4009
part of each floating point number.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
@cindex precision (of floating point number)
Packit 6c4009
The @dfn{precision} of the mantissa.  If the base of the representation
Packit 6c4009
is @var{b}, then the precision is the number of base-@var{b} digits in
Packit 6c4009
the mantissa.  This is a constant for a particular representation.
Packit 6c4009
Packit 6c4009
@cindex hidden bit (of floating point number mantissa)
Packit 6c4009
Many floating point representations have an implicit @dfn{hidden bit} in
Packit 6c4009
the mantissa.  This is a bit which is present virtually in the mantissa,
Packit 6c4009
but not stored in memory because its value is always 1 in a normalized
Packit 6c4009
number.  The precision figure (see above) includes any hidden bits.
Packit 6c4009
Packit 6c4009
Again, @theglibc{} provides no facilities for dealing with such
Packit 6c4009
low-level aspects of the representation.
Packit 6c4009
@end itemize
Packit 6c4009
Packit 6c4009
The mantissa of a floating point number represents an implicit fraction
Packit 6c4009
whose denominator is the base raised to the power of the precision.  Since
Packit 6c4009
the largest representable mantissa is one less than this denominator, the
Packit 6c4009
value of the fraction is always strictly less than @code{1}.  The
Packit 6c4009
mathematical value of a floating point number is then the product of this
Packit 6c4009
fraction, the sign, and the base raised to the exponent.
Packit 6c4009
Packit 6c4009
@cindex normalized floating point number
Packit 6c4009
We say that the floating point number is @dfn{normalized} if the
Packit 6c4009
fraction is at least @code{1/@var{b}}, where @var{b} is the base.  In
Packit 6c4009
other words, the mantissa would be too large to fit if it were
Packit 6c4009
multiplied by the base.  Non-normalized numbers are sometimes called
Packit 6c4009
@dfn{denormal}; they contain less precision than the representation
Packit 6c4009
normally can hold.
Packit 6c4009
Packit 6c4009
If the number is not normalized, then you can subtract @code{1} from the
Packit 6c4009
exponent while multiplying the mantissa by the base, and get another
Packit 6c4009
floating point number with the same value.  @dfn{Normalization} consists
Packit 6c4009
of doing this repeatedly until the number is normalized.  Two distinct
Packit 6c4009
normalized floating point numbers cannot be equal in value.
Packit 6c4009
Packit 6c4009
(There is an exception to this rule: if the mantissa is zero, it is
Packit 6c4009
considered normalized.  Another exception happens on certain machines
Packit 6c4009
where the exponent is as small as the representation can hold.  Then
Packit 6c4009
it is impossible to subtract @code{1} from the exponent, so a number
Packit 6c4009
may be normalized even if its fraction is less than @code{1/@var{b}}.)
Packit 6c4009
Packit 6c4009
@node Floating Point Parameters
Packit 6c4009
@subsubsection Floating Point Parameters
Packit 6c4009
Packit 6c4009
@pindex float.h
Packit 6c4009
These macro definitions can be accessed by including the header file
Packit 6c4009
@file{float.h} in your program.
Packit 6c4009
Packit 6c4009
Macro names starting with @samp{FLT_} refer to the @code{float} type,
Packit 6c4009
while names beginning with @samp{DBL_} refer to the @code{double} type
Packit 6c4009
and names beginning with @samp{LDBL_} refer to the @code{long double}
Packit 6c4009
type.  (If GCC does not support @code{long double} as a distinct data
Packit 6c4009
type on a target machine then the values for the @samp{LDBL_} constants
Packit 6c4009
are equal to the corresponding constants for the @code{double} type.)
Packit 6c4009
Packit 6c4009
Of these macros, only @code{FLT_RADIX} is guaranteed to be a constant
Packit 6c4009
expression.  The other macros listed here cannot be reliably used in
Packit 6c4009
places that require constant expressions, such as @samp{#if}
Packit 6c4009
preprocessing directives or in the dimensions of static arrays.
Packit 6c4009
Packit 6c4009
Although the @w{ISO C} standard specifies minimum and maximum values for
Packit 6c4009
most of these parameters, the GNU C implementation uses whatever values
Packit 6c4009
describe the floating point representation of the target machine.  So in
Packit 6c4009
principle GNU C actually satisfies the @w{ISO C} requirements only if the
Packit 6c4009
target machine is suitable.  In practice, all the machines currently
Packit 6c4009
supported are suitable.
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item FLT_ROUNDS
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
This value characterizes the rounding mode for floating point addition.
Packit 6c4009
The following values indicate standard rounding modes:
Packit 6c4009
Packit 6c4009
@need 750
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item -1
Packit 6c4009
The mode is indeterminable.
Packit 6c4009
@item 0
Packit 6c4009
Rounding is towards zero.
Packit 6c4009
@item 1
Packit 6c4009
Rounding is to the nearest number.
Packit 6c4009
@item 2
Packit 6c4009
Rounding is towards positive infinity.
Packit 6c4009
@item 3
Packit 6c4009
Rounding is towards negative infinity.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
Any other value represents a machine-dependent nonstandard rounding
Packit 6c4009
mode.
Packit 6c4009
Packit 6c4009
On most machines, the value is @code{1}, in accordance with the IEEE
Packit 6c4009
standard for floating point.
Packit 6c4009
Packit 6c4009
Here is a table showing how certain values round for each possible value
Packit 6c4009
of @code{FLT_ROUNDS}, if the other aspects of the representation match
Packit 6c4009
the IEEE single-precision standard.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
                0      1             2             3
Packit 6c4009
 1.00000003    1.0    1.0           1.00000012    1.0
Packit 6c4009
 1.00000007    1.0    1.00000012    1.00000012    1.0
Packit 6c4009
-1.00000003   -1.0   -1.0          -1.0          -1.00000012
Packit 6c4009
-1.00000007   -1.0   -1.00000012   -1.0          -1.00000012
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@item FLT_RADIX
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
This is the value of the base, or radix, of the exponent representation.
Packit 6c4009
This is guaranteed to be a constant expression, unlike the other macros
Packit 6c4009
described in this section.  The value is 2 on all machines we know of
Packit 6c4009
except the IBM 360 and derivatives.
Packit 6c4009
Packit 6c4009
@item FLT_MANT_DIG
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
This is the number of base-@code{FLT_RADIX} digits in the floating point
Packit 6c4009
mantissa for the @code{float} data type.  The following expression
Packit 6c4009
yields @code{1.0} (even though mathematically it should not) due to the
Packit 6c4009
limited number of mantissa digits:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
float radix = FLT_RADIX;
Packit 6c4009
Packit 6c4009
1.0f + 1.0f / radix / radix / @dots{} / radix
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
where @code{radix} appears @code{FLT_MANT_DIG} times.
Packit 6c4009
Packit 6c4009
@item DBL_MANT_DIG
Packit 6c4009
@itemx LDBL_MANT_DIG
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
This is the number of base-@code{FLT_RADIX} digits in the floating point
Packit 6c4009
mantissa for the data types @code{double} and @code{long double},
Packit 6c4009
respectively.
Packit 6c4009
Packit 6c4009
@comment Extra blank lines make it look better.
Packit 6c4009
@item FLT_DIG
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
Packit 6c4009
This is the number of decimal digits of precision for the @code{float}
Packit 6c4009
data type.  Technically, if @var{p} and @var{b} are the precision and
Packit 6c4009
base (respectively) for the representation, then the decimal precision
Packit 6c4009
@var{q} is the maximum number of decimal digits such that any floating
Packit 6c4009
point number with @var{q} base 10 digits can be rounded to a floating
Packit 6c4009
point number with @var{p} base @var{b} digits and back again, without
Packit 6c4009
change to the @var{q} decimal digits.
Packit 6c4009
Packit 6c4009
The value of this macro is supposed to be at least @code{6}, to satisfy
Packit 6c4009
@w{ISO C}.
Packit 6c4009
Packit 6c4009
@item DBL_DIG
Packit 6c4009
@itemx LDBL_DIG
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
Packit 6c4009
These are similar to @code{FLT_DIG}, but for the data types
Packit 6c4009
@code{double} and @code{long double}, respectively.  The values of these
Packit 6c4009
macros are supposed to be at least @code{10}.
Packit 6c4009
Packit 6c4009
@item FLT_MIN_EXP
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
This is the smallest possible exponent value for type @code{float}.
Packit 6c4009
More precisely, it is the minimum negative integer such that the value
Packit 6c4009
@code{FLT_RADIX} raised to this power minus 1 can be represented as a
Packit 6c4009
normalized floating point number of type @code{float}.
Packit 6c4009
Packit 6c4009
@item DBL_MIN_EXP
Packit 6c4009
@itemx LDBL_MIN_EXP
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
Packit 6c4009
These are similar to @code{FLT_MIN_EXP}, but for the data types
Packit 6c4009
@code{double} and @code{long double}, respectively.
Packit 6c4009
Packit 6c4009
@item FLT_MIN_10_EXP
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
This is the minimum negative integer such that @code{10} raised to this
Packit 6c4009
power minus 1 can be represented as a normalized floating point number
Packit 6c4009
of type @code{float}.  This is supposed to be @code{-37} or even less.
Packit 6c4009
Packit 6c4009
@item DBL_MIN_10_EXP
Packit 6c4009
@itemx LDBL_MIN_10_EXP
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
These are similar to @code{FLT_MIN_10_EXP}, but for the data types
Packit 6c4009
@code{double} and @code{long double}, respectively.
Packit 6c4009
Packit 6c4009
@item FLT_MAX_EXP
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
This is the largest possible exponent value for type @code{float}.  More
Packit 6c4009
precisely, this is the maximum positive integer such that value
Packit 6c4009
@code{FLT_RADIX} raised to this power minus 1 can be represented as a
Packit 6c4009
floating point number of type @code{float}.
Packit 6c4009
Packit 6c4009
@item DBL_MAX_EXP
Packit 6c4009
@itemx LDBL_MAX_EXP
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
These are similar to @code{FLT_MAX_EXP}, but for the data types
Packit 6c4009
@code{double} and @code{long double}, respectively.
Packit 6c4009
Packit 6c4009
@item FLT_MAX_10_EXP
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
This is the maximum positive integer such that @code{10} raised to this
Packit 6c4009
power minus 1 can be represented as a normalized floating point number
Packit 6c4009
of type @code{float}.  This is supposed to be at least @code{37}.
Packit 6c4009
Packit 6c4009
@item DBL_MAX_10_EXP
Packit 6c4009
@itemx LDBL_MAX_10_EXP
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
These are similar to @code{FLT_MAX_10_EXP}, but for the data types
Packit 6c4009
@code{double} and @code{long double}, respectively.
Packit 6c4009
Packit 6c4009
@item FLT_MAX
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
Packit 6c4009
The value of this macro is the maximum number representable in type
Packit 6c4009
@code{float}.  It is supposed to be at least @code{1E+37}.  The value
Packit 6c4009
has type @code{float}.
Packit 6c4009
Packit 6c4009
The smallest representable number is @code{- FLT_MAX}.
Packit 6c4009
Packit 6c4009
@item DBL_MAX
Packit 6c4009
@itemx LDBL_MAX
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
Packit 6c4009
These are similar to @code{FLT_MAX}, but for the data types
Packit 6c4009
@code{double} and @code{long double}, respectively.  The type of the
Packit 6c4009
macro's value is the same as the type it describes.
Packit 6c4009
Packit 6c4009
@item FLT_MIN
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
Packit 6c4009
The value of this macro is the minimum normalized positive floating
Packit 6c4009
point number that is representable in type @code{float}.  It is supposed
Packit 6c4009
to be no more than @code{1E-37}.
Packit 6c4009
Packit 6c4009
@item DBL_MIN
Packit 6c4009
@itemx LDBL_MIN
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
Packit 6c4009
These are similar to @code{FLT_MIN}, but for the data types
Packit 6c4009
@code{double} and @code{long double}, respectively.  The type of the
Packit 6c4009
macro's value is the same as the type it describes.
Packit 6c4009
Packit 6c4009
@item FLT_EPSILON
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
Packit 6c4009
This is the difference between 1 and the smallest floating point
Packit 6c4009
number of type @code{float} that is greater than 1.  It's supposed to
Packit 6c4009
be no greater than @code{1E-5}.
Packit 6c4009
Packit 6c4009
@item DBL_EPSILON
Packit 6c4009
@itemx LDBL_EPSILON
Packit 6c4009
@standards{C90, float.h}
Packit 6c4009
Packit 6c4009
These are similar to @code{FLT_EPSILON}, but for the data types
Packit 6c4009
@code{double} and @code{long double}, respectively.  The type of the
Packit 6c4009
macro's value is the same as the type it describes.  The values are not
Packit 6c4009
supposed to be greater than @code{1E-9}.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
@node IEEE Floating Point
Packit 6c4009
@subsubsection IEEE Floating Point
Packit 6c4009
@cindex IEEE floating point representation
Packit 6c4009
@cindex floating point, IEEE
Packit 6c4009
Packit 6c4009
Here is an example showing how the floating type measurements come out
Packit 6c4009
for the most common floating point representation, specified by the
Packit 6c4009
@cite{IEEE Standard for Binary Floating Point Arithmetic (ANSI/IEEE Std
Packit 6c4009
754-1985)}.  Nearly all computers designed since the 1980s use this
Packit 6c4009
format.
Packit 6c4009
Packit 6c4009
The IEEE single-precision float representation uses a base of 2.  There
Packit 6c4009
is a sign bit, a mantissa with 23 bits plus one hidden bit (so the total
Packit 6c4009
precision is 24 base-2 digits), and an 8-bit exponent that can represent
Packit 6c4009
values in the range -125 to 128, inclusive.
Packit 6c4009
Packit 6c4009
So, for an implementation that uses this representation for the
Packit 6c4009
@code{float} data type, appropriate values for the corresponding
Packit 6c4009
parameters are:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
FLT_RADIX                             2
Packit 6c4009
FLT_MANT_DIG                         24
Packit 6c4009
FLT_DIG                               6
Packit 6c4009
FLT_MIN_EXP                        -125
Packit 6c4009
FLT_MIN_10_EXP                      -37
Packit 6c4009
FLT_MAX_EXP                         128
Packit 6c4009
FLT_MAX_10_EXP                      +38
Packit 6c4009
FLT_MIN                 1.17549435E-38F
Packit 6c4009
FLT_MAX                 3.40282347E+38F
Packit 6c4009
FLT_EPSILON             1.19209290E-07F
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
Here are the values for the @code{double} data type:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
DBL_MANT_DIG                         53
Packit 6c4009
DBL_DIG                              15
Packit 6c4009
DBL_MIN_EXP                       -1021
Packit 6c4009
DBL_MIN_10_EXP                     -307
Packit 6c4009
DBL_MAX_EXP                        1024
Packit 6c4009
DBL_MAX_10_EXP                      308
Packit 6c4009
DBL_MAX         1.7976931348623157E+308
Packit 6c4009
DBL_MIN         2.2250738585072014E-308
Packit 6c4009
DBL_EPSILON     2.2204460492503131E-016
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@node Structure Measurement
Packit 6c4009
@subsection Structure Field Offset Measurement
Packit 6c4009
Packit 6c4009
You can use @code{offsetof} to measure the location within a structure
Packit 6c4009
type of a particular structure member.
Packit 6c4009
Packit 6c4009
@deftypefn {Macro} size_t offsetof (@var{type}, @var{member})
Packit 6c4009
@standards{ISO, stddef.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit 6c4009
@c This is no longer provided by glibc, but rather by the compiler.
Packit 6c4009
This expands to an integer constant expression that is the offset of the
Packit 6c4009
structure member named @var{member} in the structure type @var{type}.
Packit 6c4009
For example, @code{offsetof (struct s, elem)} is the offset, in bytes,
Packit 6c4009
of the member @code{elem} in a @code{struct s}.
Packit 6c4009
Packit 6c4009
This macro won't work if @var{member} is a bit field; you get an error
Packit 6c4009
from the C compiler in that case.
Packit 6c4009
@end deftypefn