Size t
Encyclopedia
size_t
is an unsigned data type defined by several C and C++ standards (e.g., the C99C99
C99 is a modern dialect of the C programming language. It extends the previous version with new linguistic and library features, and helps implementations make better use of available computer hardware and compiler technology.-History:...
ISO/IEC 9899 standard) that is defined in
stddef.h
. It can be further imported by inclusion of stdlib.h
as this file internally sub includes stddef.h
.This type is used to represent the size of an object or the maximum size of an array. Library functions that take or return sizes expect them to be of this type or have the return type of
size_t
. Further, the most frequently used compiler-based operator sizeofSizeofIn the programming languages C and C++, the unary operator sizeof is used to calculate the sizes of datatypes, in number of bytes. A byte in this context is the same as an unsigned char, and may be larger than the standard 8 bits, although that is uncommon in modern implementations...
should evaluate to a value that is compatible with size_t
.Range and storage size of size_t
The actual type ofsize_t
is platform-dependent; a common mistake is to assume size_t
is the same as unsigned int
, which can lead to programming errors for example when moving from 32 to 64-bit architectures.The C99 standard introduced the
SIZE_MAX
constant, defined in the stdint.h header, which specifies the maximum value a size_t
can hold. The actual value for SIZE_MAX
is implementation-defined, but the standard requires it to be at least 65535.Signed variant ssize_t
On platforms supporting the POSIX 1003.1-1996 API standard, which includes most Unix-like systems, a signed variant ofsize_t
named ssize_t
is available, which was not part of the ANSI or ISO C standards. The ssize_t
type follows the same rules as size_t
for its storage size, location and inclusion, and uses the implementation's usual representation of signed values—2's complement in most, if not all, cases. It is frequently used for pointer differences (but see also ptrdiff t), or for returning sizes in cases where negative values are used as an error value.