Blame manual/lang.texi

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