size_t
size_t is the unsigned integer type of the result of sizeof. It is wide enough to count every byte in the largest object the implementation can create, and it is the type of array lengths, malloc arguments, and most library size parameters.
Detailed Explanation
<stddef.h>, <stdio.h>, <stdlib.h>, <string.h>, and several other headers all declare size_t. You do not invent it. You include a header that already needs it. Its unsigned nature is deliberate: a size cannot be negative, and the extra bit of range matters on machines where the address space is the full width of a register.
sizeof x has type size_t. So do strlen, strlen lookalikes, the n in memcpy(dest, src, n), the argument to malloc, and the return of fread. When you write a function that takes a buffer and a length, the length should be size_t unless you have a specific reason to use a signed type — for example, a return that uses -1 as an error sentinel.
The matching signed type is ptrdiff_t, also in <stddef.h>. Subtracting two pointers into the same array yields ptrdiff_t. Mixing size_t and ptrdiff_t in one expression re-enters the signed-versus-unsigned conversion rules. A common pattern is to keep lengths as size_t and convert to ptrdiff_t only at the point you form a pointer difference you intend to treat as signed.
Printing size_t portably uses %zu. Printing it with %d or %lu is a format mismatch and undefined behavior on some ABIs. Scanning it uses %zu with a size_t *. Older Microsoft compilers wanted %Iu; current ones accept %zu.
The cost of size_t is the countdown loop. for (size_t i = n - 1; i >= 0; i--) does not terminate. The test i >= 0 is true for every unsigned value. Walk downward with for (size_t i = n; i-- > 0; ) or iterate forward. Subtracting 1 from 0 as size_t wraps to SIZE_MAX, which is why buf[len - 1] is undefined when len is 0 even before you think about the index.
On a 32-bit host, size_t is 32 bits and cannot describe a 4 GiB-plus object. On a 64-bit host it is 64 bits. Code that writes size_t into an unsigned int or an int will truncate when those types are narrower. Store sizes as size_t all the way to the system call.
Code Examples
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char *repeat(const char *s, size_t times)
{
size_t n = strlen(s);
if (times != 0 && n > (size_t)-1 / times) { /* overflow check before multiply */
return NULL;
}
size_t bytes = n * times;
char *out = malloc(bytes + 1);
if (out == NULL) {
return NULL;
}
for (size_t i = 0; i < times; i++) {
memcpy(out + i * n, s, n);
}
out[bytes] = '\0';
return out;
}
int main(void)
{
char *s = repeat("ab", 3);
if (s == NULL) {
return 1;
}
printf("%s %zu\n", s, strlen(s)); /* prints: ababab 6 */
free(s);
return 0;
}#include <stdio.h>
#include <stddef.h>
int main(void)
{
int a[] = { 10, 20, 30, 40 };
size_t n = sizeof a / sizeof a[0]; /* 4, type size_t */
printf("n=%zu last=%d\n", n, a[n - 1]); /* prints: n=4 last=40 */
/* Safe empty-length guard. a[n - 1] is illegal when n == 0. */
size_t empty = 0;
if (empty > 0) {
printf("%d\n", a[empty - 1]);
}
return 0;
}Common Gotchas
`%d` is the wrong conversion for `size_t`
use `%zu`. A mismatch is undefined behavior and shows up as garbage or a crash when `size_t` is wider than `int`.
`for (size_t i = n; i >= 0; i--)` is an infinite loop
unsigned comparison to zero is always true. Decrement in the condition: `for (size_t i = n; i-- > 0; )`.
`sizeof` a VLA is evaluated at run time and still has type `size_t`
the value can be zero. Do not assume it is a compile-time constant.