Next: Data Type Measurements, Previous: Null Pointer Constant, Up: C Language Facilities in the Library [Contents][Index]
The result of subtracting two pointers in C is always an integer, but the
precise data type varies from C compiler to C compiler. Likewise, the
data type of the result of sizeof
also varies between compilers.
ISO C defines standard aliases for these two types, so you can refer to
them in a portable fashion. They are defined in the header file
stddef.h.
This is the signed integer type of the result of subtracting two
pointers. For example, with the declaration char *p1, *p2;
, the
expression p2 - p1
is of type ptrdiff_t
. This will
probably be one of the standard signed integer types (short int
, int
or long int
), but might be a nonstandard
type that exists only for this purpose.
This is an unsigned integer type used to represent the sizes of objects.
The result of the sizeof
operator is of this type, and functions
such as malloc
(see Unconstrained Allocation) and
memcpy
(see Copying Strings and Arrays) accept arguments of
this type to specify object sizes. On systems using the GNU C Library, this
will be unsigned int
or unsigned long int
.
Usage Note: size_t
is the preferred way to declare any
arguments or variables that hold the size of an object.
Compatibility Note: Implementations of C before the advent of
ISO C generally used unsigned int
for representing object sizes
and int
for pointer subtraction results. They did not
necessarily define either size_t
or ptrdiff_t
. Unix
systems did define size_t
, in sys/types.h, but the
definition was usually a signed type.
Next: Data Type Measurements, Previous: Null Pointer Constant, Up: C Language Facilities in the Library [Contents][Index]