The sizeof Operator
sizeof is a compile-time operator that reports how many bytes a type or expression occupies in memory. It can be applied to a type name in parentheses, such as sizeof(int), or directly to an expression, such as sizeof x (parentheses are optional for expressions, but commonly used for clarity).C
#include <stdio.h>
int main(void) {
int x = 42;
printf("%zu\n", sizeof(int)); // typically 4
printf("%zu\n", sizeof(double)); // typically 8
printf("%zu\n", sizeof x); // same as sizeof(int) here, since x is an int
return 0;
}sizeof Returns size_t
size_t is unsigned — print it with %zu
The result of
sizeof has type size_t, an unsigned integer type defined in <stddef.h> (and pulled in by most standard headers). Printing it with the wrong format specifier, such as %d, is undefined behavior on platforms where size_t is wider than int. The correct, portable format specifier is %zu (available since C99).C
size_t s = sizeof(int);
printf("%zu\n", s); // correct
/* printf("%d\n", s); */ // wrong type — avoid, especially on 64-bit systemsEvaluated at Compile Time
Except for variable-length arrays
For ordinary types and expressions,
sizeof is resolved entirely at compile time — the operand is not actually evaluated at runtime (so sizeof(x++) does not increment x). The one exception is C99's variable-length arrays (VLAs), where the size genuinely depends on a runtime value, so the compiler must generate code to compute it at runtime.C
int n = 10;
int vla[n]; // a VLA — its size is only known at runtime
printf("%zu\n", sizeof(vla)); // computed at runtime: n * sizeof(int)
int x = 5;
printf("%zu\n", sizeof(x++)); // x++ is NOT executed; x is still 5 afterwardCounting Array Elements
The most common practical use of
sizeof is the classic C idiom for computing how many elements an array holds: sizeof(arr) / sizeof(arr[0]). This divides the total size of the array by the size of a single element.C
#include <stdio.h>
int main(void) {
int numbers[] = {10, 20, 30, 40, 50};
size_t count = sizeof(numbers) / sizeof(numbers[0]);
printf("The array has %zu elements\n", count); // 5
for (size_t i = 0; i < count; i++) {
printf("%d\n", numbers[i]);
}
return 0;
}This idiom breaks once the array decays to a pointer
sizeof(arr) / sizeof(arr[0]) only works while arr is still a real array in the current scope. As soon as an array is passed to a function, it decays into a plain pointer to its first element, and sizeof on that parameter returns the size of the pointer (commonly 8 bytes on 64-bit systems) — not the size of the original array. Inside a function, you must pass the element count separately.C
#include <stdio.h>
void printCount(int arr[]) {
/* BUG: arr has decayed to int*, so sizeof(arr) is the pointer size,
not the array size — this does NOT compute the element count. */
printf("%zu\n", sizeof(arr) / sizeof(arr[0])); // wrong inside a function
}
void printCountCorrect(int *arr, size_t count) {
printf("%zu\n", count); // correct — pass the count explicitly
}
int main(void) {
int numbers[] = {1, 2, 3, 4, 5};
printCount(numbers);
printCountCorrect(numbers, sizeof(numbers) / sizeof(numbers[0]));
return 0;
}sizeof(type)orsizeof exprboth give a byte size, resolved at compile time (VLAs are the exception)The result type is
size_t(unsigned) — always print it with%zusizeof(arr) / sizeof(arr[0])counts array elements, but only wherearris still a real array, not a decayed pointer parameter