Question
In C, how can I determine the size of an array, meaning the number of elements it can hold?
For example, if I declare an array, how do I calculate how many items fit in it rather than just its memory size in bytes?
Short Answer
By the end of this page, you will understand how to get the number of elements in a C array using sizeof, why this works only in certain contexts, and what changes when arrays are passed to functions.
Concept
In C, an array stores multiple values of the same type in contiguous memory. Two related ideas are important:
- Total array size in bytes
- Number of elements in the array
C does not provide a built-in .length property like some other languages. Instead, for a real array declared in the same scope, you usually calculate its element count like this:
sizeof(array) / sizeof(array[0])
Why this works:
sizeof(array)gives the total number of bytes used by the entire array.sizeof(array[0])gives the number of bytes used by one element.- Dividing them gives the number of elements.
Example:
int numbers[10];
If int is 4 bytes on your system:
sizeof(numbers)is40sizeof(numbers[0])is440 / 4 = 10
This matters because C gives you low-level memory control, but that also means you must often track array sizes yourself. Understanding this pattern is essential for loops, validation, copying data, and avoiding out-of-bounds access.
A very important limitation: this only works when the variable is truly an array in that scope. If you pass the array to a function, it usually becomes a pointer, and sizeof will no longer give the element count.
Mental Model
Think of an array like a row of identical boxes in a warehouse:
- The whole shelf width is
sizeof(array). - The width of one box is
sizeof(array[0]). - If you divide shelf width by box width, you get how many boxes fit on the shelf.
total shelf width / one box width = number of boxes
This works only when you are looking at the actual shelf. If someone gives you just a note pointing to the first box, you no longer know how long the whole shelf is. That is what happens when an array is passed to a function in C: it decays into a pointer.
Syntax and Examples
The standard pattern is:
sizeof(array) / sizeof(array[0])
Example 1: Integer array
#include <stdio.h>
int main(void) {
int numbers[] = {10, 20, 30, 40, 50};
size_t count = sizeof(numbers) / sizeof(numbers[0]);
printf("Number of elements: %zu\n", count);
return 0;
}
Output:
Number of elements: 5
Explanation:
sizeof(numbers)returns the total bytes of the whole array.sizeof(numbers[0])returns the bytes of oneint.- Dividing gives the element count.
Step by Step Execution
Consider this program:
#include <stdio.h>
int main(void) {
int values[] = {5, 10, 15, 20};
size_t count = sizeof(values) / sizeof(values[0]);
printf("%zu\n", count);
return 0;
}
Step by step:
-
int values[] = {5, 10, 15, 20};- C creates an array with 4 elements.
- Each element is an
int.
-
sizeof(values)- This asks for the total size of the whole array in bytes.
- If
intis 4 bytes, then the result is 16.
-
sizeof(values[0])- This asks for the size of one element.
- If
intis 4 bytes, then the result is 4.
Real World Use Cases
Knowing array size is useful in many practical situations:
Looping through fixed arrays
for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); i++) {
printf("%d\n", values[i]);
}
Validating buffer limits
If you have a fixed-size array, you need its length to avoid writing past the end.
Processing lookup tables
Programs often use arrays for:
- menu options
- sensor calibration values
- status code tables
- test data sets
Embedded systems
In embedded C, fixed-size arrays are common because memory is limited and controlled carefully.
String-like character buffers
Even though strings in C use a null terminator, character arrays still have a fixed capacity that matters for safe input and output.
Real Codebase Usage
In real C projects, developers commonly use array sizes in a few predictable ways.
Pass length into functions
void print_numbers(const int arr[], size_t length) {
for (size_t i = 0; i < length; i++) {
printf("%d\n", arr[i]);
}
}
This is the standard pattern because the function cannot reliably determine the original array length by itself.
Use helper macros
#define ARRAY_LEN(arr) (sizeof(arr) / sizeof((arr)[0]))
This is often used for local static arrays.
Guard against out-of-bounds access
if (index < length) {
printf("%d\n", arr[index]);
}
Configuration tables
Arrays are often used to store fixed settings or mappings:
const char *days[] = {"Mon", "Tue", "Wed", "Thu", };
day_count = (days) / (days[]);
Common Mistakes
1. Using sizeof on an array parameter
Broken example:
void f(int arr[]) {
size_t n = sizeof(arr) / sizeof(arr[0]);
printf("%zu\n", n);
}
Problem:
arris not the full array here.- It behaves like
int *arr. sizeof(arr)is the size of a pointer.
Fix:
void f(int arr[], size_t n) {
printf("%zu\n", n);
}
2. Confusing bytes with element count
Broken example:
int nums[10];
printf("%zu\n", sizeof(nums));
Problem:
Comparisons
| Concept | What it gives you | Works for real arrays? | Works for pointers? |
|---|---|---|---|
sizeof(array) | Total bytes of the full array | Yes | No |
sizeof(array[0]) | Bytes of one element | Yes | Yes |
sizeof(array) / sizeof(array[0]) | Number of elements | Yes | No |
Stored length variable | Number of elements | Yes | Yes |
Array vs pointer
| Feature |
|---|
Cheat Sheet
sizeof(array) / sizeof(array[0])
Rules
- Use this only when
arrayis an actual array in the current scope. sizeof(array)returns total bytes.sizeof(array[0])returns bytes per element.- The result type is usually
size_t. - Print
size_twith%zu.
Good example
int nums[] = {1, 2, 3, 4};
size_t len = sizeof(nums) / sizeof(nums[0]);
Function pattern
void process(const int arr[], size_t len) {
for (size_t i = ; i < len; i++) {
}
}
FAQ
How do I get the length of an array in C?
Use:
sizeof(array) / sizeof(array[0])
This gives the number of elements, not the byte size.
Why doesn't C have a built-in array length property?
C is a low-level language. Arrays are closely tied to memory layout, and the language does not attach a runtime length property to them.
Does sizeof return the number of elements?
No. sizeof returns a size in bytes. You divide by the size of one element to get the element count.
Why does this stop working in functions?
Because array parameters are treated as pointers. A pointer does not know how many elements are in the original array.
Can I use this with dynamically allocated arrays?
No. For memory allocated with malloc, you must store the length yourself.
What type should I use for array length in C?
Use size_t, because it is the standard type for sizes and counts related to memory.
Is sizeof(array) / sizeof(*array) also valid?
Yes. This is equivalent and often used:
Mini Project
Description
Build a small C program that stores a fixed list of temperatures in an array, calculates how many readings exist, and prints each value. This demonstrates the most common real use of array length: safe iteration without hard-coding the number of elements.
Goal
Create a program that calculates an array's element count with sizeof and uses that count to loop through the array safely.
Requirements
- Declare a fixed-size array of integers with sample temperature values.
- Compute the number of elements using
sizeof(array) / sizeof(array[0]). - Print the total number of readings.
- Use a loop to print each reading.
- Use
size_tfor the count and loop index where appropriate.
Keep learning
Related questions
Building More Fault-Tolerant Embedded C++ Applications for Radiation-Prone ARM Systems
Learn practical C++ and compile-time techniques to reduce soft-error damage in embedded ARM systems exposed to radiation.
Definition vs Declaration in C and C++: What’s the Difference?
Learn the difference between declarations and definitions in C and C++ with simple examples, common mistakes, and practical usage.
Difference Between #include <...> and #include "..." in C and C++
Learn the difference between #include with angle brackets and quotes in C and C++, including search paths, examples, and common mistakes.