CKTIS / C
size_t — a type for sizes and counts
size_t is the unsigned integer type returned by sizeof. Use it for object sizes and element counts.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <stddef.h>
size_t count = sizeof object;size_t, available through <stddef.h>, names the unsigned integer type of a sizeof result. Sizes reported by sizeof are measured in C bytes. Do not assume a particular width or equivalence to unsigned long.
Use %zu to print a size_t value with printf. The type cannot represent negative numbers. Before converting a signed value to size_t, check that it is nonnegative and does not exceed SIZE_MAX (from <stdint.h>). You can omit the upper-bound check if the source type guarantees that the value cannot exceed that limit. A positive value above SIZE_MAX converts to a smaller value, which can lead to an undersized allocation.
In the example, sizeof values / sizeof values[0] calculates the array's element count. This formula does not determine an array's length from a pointer.
Examples
#include <stddef.h>
#include <stdio.h>
int main(void)
{
int values[] = {10, 20, 30};
size_t count = sizeof values / sizeof values[0];
printf("count=%zu\n", count);
return 0;
}
Result
count=3
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.