C Reference ยท Types and Literals
C size_t and ptrdiff_t
Reference for size_t and ptrdiff_t in C: what sizeof returns, the unsigned size type, signed pointer differences, %zu and %td, and common loop mistakes.
size_t is the unsigned integer type that holds the size of any object in memory. It is the type of the result of sizeof, the length parameter of malloc, memcpy and strlen, and the natural type for array indices. Its width matches the address space: 32 bits on a 32-bit system, 64 bits on a 64-bit one.
ptrdiff_t is its signed companion: the type of the difference of two pointers into the same array. Together they are the right types for "how many" and "how far".
#include <stddef.h> size_t n = sizeof(int[10]); ptrdiff_t gap = &arr[7] - &arr[2]; /* 5 */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
#include <stddef.h> /* size_t, ptrdiff_t, NULL, offsetof */ /* size_t is also defined by <stdio.h>, <stdlib.h>, <string.h>, <time.h> ... */
| Type | Signed? | Used for | printf |
Limit |
|---|---|---|---|---|
size_t |
unsigned | sizeof, object sizes, counts, array indices |
%zu |
SIZE_MAX (<stdint.h>) |
ptrdiff_t |
signed | difference of two pointers | %td |
PTRDIFF_MIN, PTRDIFF_MAX |
ssize_t (POSIX) |
signed | byte counts that may be -1, as returned by read() |
%zd |
SSIZE_MAX |
The standard requires size_t to be at least 16 bits. It is typically unsigned long on Linux 64-bit and unsigned long long on 64-bit Windows. The z and t length modifiers of printf/scanf exist for these types.
Errors / undefined behavior: subtracting pointers that do not point into the same array object is undefined behavior, and so is a pointer difference that does not fit in ptrdiff_t. Arithmetic on size_t wraps, which is defined but often a bug.
Usage notes
- Use
%zufor printingsize_tand%tdforptrdiff_t.%dor%luare mismatches that break on some platforms. size_tnever holds a negative number.for (size_t i = n - 1; i >= 0; i--)is an infinite loop; count down withi-- > 0.- Compare like with like.
i < nwhereiisintandnissize_tconvertsito unsigned, so a negativeilooks huge. GCC warns with-Wsign-compare. - Overflow in size calculations is a security bug.
malloc(count * size)can wrap; usecalloc, or check withckd_mulfrom stdckdint.h. sizeofyieldssize_t, and it is computed at compile time (except for variable-length arrays).- Pointer subtraction is in elements. Cast to
char *to get bytes, as in the example. ptrdiff_tcan be narrower thansize_t: objects larger thanPTRDIFF_MAXbytes are allowed by some compilers but lead to trouble with pointer differences.- Do not use
intfor sizes of data that may exceed two billion elements.