ptrdiff_t is the signed integer type for differences between pointers to elements of the same array. Pointer subtraction produces this type, and the result equals the difference between the elements’ array indices. section 11.1.3
Subtracting pointers
double A[4] = { 0.0, 1.0, 2.0, -3.0 }; double* p = &A[1]; double* q = &A[3]; assert(p - q == -2); The difference is signed because q is two positions after p. Store such differences in ptrdiff_t rather than assuming that int is wide enough. section 11.1.3
Use ptrdiff_t when:
- A difference between positions or sizes may be positive or negative. section 5.2 section 11.1.3
- Pointer subtraction measures positions within one array object. section 11.1.3
- Do not subtract pointers that do not refer to elements of the same array. section 11.1.3
Table 5.2 Some semantic arithmetic types for specialized use cases
| Type | Header | Context of definition | Meaning |
size_t |
<stddef.h> |
Type for “sizes” and cardinalities | |
ptrdiff_t |
<stddef.h> |
Type for size differences | |
uintmax_t |
<stdint.h> |
Unsigned integer type of preprocessor | |
intmax_t |
<stdint.h> |
Signed integer type of preprocessor | |
time_t |
<time.h> |
time(0), difftime(t1, t0) |
Calendar time in seconds since epoch |
clock_t |
<time.h> |
clock() |
Processor time |
Compare ptrdiff_t with size_t and other semantic arithmetic types, including their headers and specialized meanings. section 5.2

