HOWTO · C
How to Get the Size of Array in C
This tutorial introduces methods in C to determine the size of an array. Learn how to effectively use the sizeof() operator and explore alternative methods like macros and functions. Mastering these techniques is essential for efficient memory management and array handling in C programming.
On this page
For a fixed-size array in the same scope, divide its total byte size by the byte size of one element: sizeof array / sizeof array[0]. The result is the number of elements. Inside a function, pass that count separately because an array parameter is treated as a pointer.
Use sizeof to Count Elements in a Fixed-Size Array
sizeof returns a size in bytes. For an array visible in the current scope, sizeof values is the size of the whole array, while sizeof values[0] is the size of one element. Their quotient gives the element count without assuming that an int has a particular byte size.
This complete C example counts a fixed-size array and passes the result to a function. The values and function call show how to adapt the pattern to another element type or array.
#include <stdio.h>
#include <stddef.h>
void print_values(const int *values, size_t count)
{
size_t i;
printf("Element count: %d\n", (int)count);
for (i = 0; i < count; ++i) {
printf("%d ", values[i]);
}
putchar('\n');
}
int main(void)
{
int values[] = {4, 8, 15, 16};
size_t count = sizeof values / sizeof values[0];
printf("Array count: %d\n", (int)count);
print_values(values, count);
return 0;
}
Array count: 4
Element count: 4
4 8 15 16
For this demonstration, the cast to int is safe because the array has four elements. For a general count, use a format specifier appropriate for size_t on the target C library.
Why sizeof Gives the Pointer Size Inside a Function
In a function parameter declaration, int values[] is adjusted to int *values. Therefore, sizeof values in that function measures the pointer, not the caller’s array. Dividing that value by sizeof values[0] does not recover the original number of elements, and the pointer size can vary by platform.
Keep the array-count calculation in the scope where the array is declared, then pass the count to any function that needs it. This applies to ordinary array parameters as well as parameters written with pointer syntax.
Use a Macro for Arrays in the Same Scope
A macro can make the same-scope calculation reusable: #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])). Use it where array is an actual array, such as ARRAY_SIZE(values) in the scope that declares values. When an array is passed to a function, the macro sees a pointer and cannot determine the original array length. For multidimensional arrays, the expression counts elements in the outer dimension.
Pass the Element Count to a Function
Declare the function parameter as a pointer (or array parameter) and provide a separate size_t count, as print_values(values, count) does above. The caller computes the count while the complete array is in scope; the function uses that count as its loop bound. This pattern works for fixed-size arrays and dynamically allocated storage as long as the caller supplies the number of elements. Keep the pointer and count together so the function can process exactly the intended elements.