A.4 Important Data Types

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. Also, it helps to have a data type with the greatest fundamental alignment. ISO C defines standard aliases for these types, so you can refer to them in a portable fashion. They are defined in the header file stddef.h.

Data Type: ptrdiff_t

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.

Data Type: size_t

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.

Data Type: max_align_t

This is an object type with the greatest fundamental alignment, i.e., the greatest alignment needed by standard types. Code can use alignof (max_align_t) when calculating space needed for arbitrary collections of objects, so long as the objects’ types have a fundamental alignment and lack stricter alignment specifiers.

In the GNU C Library, the value of alignof (max_align_t) is 16 on most architectures. However, it is 8 on 32-bit architectures that do not require 16-byte alignment from malloc to support predefined types.

Compatibility Note: The C11 standard introduced max_align_t; older compilers may lack the type.