Skip to content
Tutorials Center
My learning

Topics

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".

C
#include <stddef.h>

size_t n = sizeof(int[10]);
ptrdiff_t gap = &arr[7] - &arr[2];   /* 5 */

Try it

C size_t in everyday code

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.

C ptrdiff_t and the unsigned trap

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

C
#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 %zu for printing size_t and %td for ptrdiff_t. %d or %lu are mismatches that break on some platforms.
  • size_t never holds a negative number. for (size_t i = n - 1; i >= 0; i--) is an infinite loop; count down with i-- > 0.
  • Compare like with like. i < n where i is int and n is size_t converts i to unsigned, so a negative i looks huge. GCC warns with -Wsign-compare.
  • Overflow in size calculations is a security bug. malloc(count * size) can wrap; use calloc, or check with ckd_mul from stdckdint.h.
  • sizeof yields size_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_t can be narrower than size_t: objects larger than PTRDIFF_MAX bytes are allowed by some compilers but lead to trouble with pointer differences.
  • Do not use int for sizes of data that may exceed two billion elements.