C Program to count the number of elements in an array
Learn how to count the number of elements in a C array using the sizeof operator and a loop, with examples, limitations, and complexity analysis.
C does not provide a built-in array length property. For an array declared in the current scope, we can calculate its number of elements by dividing the total size of the array by the size of one element.
The formula is:
Number of elements = Size of the complete array / Size of one element
For example, if an integer array occupies 24 bytes and each integer occupies 4 bytes, the array contains 24 / 4 = 6 elements.
C Program to Count the Number of Array Elements
#include <stdio.h>
int main(void) {
int numbers[] = {12, 7, 25, 4, 18, 9};
size_t arraySize = sizeof(numbers);
size_t elementSize = sizeof(numbers[0]);
size_t elementCount = arraySize / elementSize;
printf("Size of the array = %zu bytes\n", arraySize);
printf("Size of one element = %zu bytes\n", elementSize);
printf("Number of elements = %zu\n", elementCount);
return 0;
}
Sample Output
On a system where an int occupies 4 bytes, the output is:
Size of the array = 24 bytes
Size of one element = 4 bytes
Number of elements = 6
The size of an int can vary between systems, but the calculated element count remains 6 because both values in the division use the same element type.
How the Program Works
The expression sizeof(numbers) returns the number of bytes occupied by the complete array. For six integers of 4 bytes each, it returns 24.
The expression sizeof(numbers[0]) returns the size of one array element. It returns the size of an int because numbers[0] has type int.
The division gives the element count:
elementCount = 24 / 4
elementCount = 6
The program uses size_t for sizes and counts because sizeof returns a value of type size_t. The %zu format specifier prints a size_t value correctly.
Shorter Form of the Calculation
The same calculation can be written in one statement inside main():
size_t elementCount = sizeof(numbers) / sizeof(numbers[0]);
Using numbers[0] instead of writing a type such as int makes the expression safer. If the array type later changes from int to double, the calculation still works without any other changes.
For example:
double prices[] = {19.99, 5.50, 12.75, 8.25};
size_t elementCount = sizeof(prices) / sizeof(prices[0]);
printf("Number of elements = %zu\n", elementCount);
This prints:
Number of elements = 4
Count the Number of Array Elements Using a Loop
A loop can count elements by increasing a counter once for every position it visits. However, a raw C array does not contain an automatic end marker. The loop must already have a valid boundary so that it does not read beyond the array.
For an array declared inside main(), we can obtain that safe boundary with sizeof and then demonstrate the loop-based count:
#include <stdio.h>
int main(void) {
int numbers[] = {12, 7, 25, 4, 18, 9};
size_t arrayLength = sizeof(numbers) / sizeof(numbers[0]);
size_t elementCount = 0;
for (size_t index = 0; index < arrayLength; index++) {
elementCount++;
}
printf("Number of elements = %zu\n", elementCount);
return 0;
}
Output
Number of elements = 6
How the Loop Counts the Elements
The loop starts at index 0 and continues while index < arrayLength. During every iteration, elementCount is increased by one.
| Iteration | Visited index | elementCount |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 3 |
| 4 | 3 | 4 |
| 5 | 4 | 5 |
| 6 | 5 | 6 |
After index 5 is visited, index becomes 6. The condition 6 < 6 is false, so the loop ends and the counter contains 6.
For a fixed array, this loop is mainly useful for learning how counting during traversal works—the direct sizeof formula already provides the answer more efficiently. Loop-based counting becomes useful when values are being read or processed one at a time. In that situation, increase the counter only after an element has been successfully stored.
The loop still cannot discover the end of an ordinary array on its own. Writing a condition without a known boundary or a deliberately designed sentinel can access memory outside the array and cause undefined behavior.
Important Limitation of the sizeof Method
This method works when sizeof can see the actual array declaration. It should be used in the same scope in which the array is declared, as shown in the program above.
When an array is passed to another function, its parameter behaves like a pointer. Applying sizeof to that parameter returns the size of the pointer, not the total size of the original array. That result cannot be used to determine the array's element count.
For this reason, C programs normally pass the element count separately whenever an array is passed to a function. The complete program in this article performs the calculation directly inside main() and does not use a function-based implementation.
Declared Capacity and Entered Elements Are Different
Consider this declaration:
int numbers[100];
The sizeof formula reports 100 because that is the array's declared capacity. If a user enters only 5 values, the number of entered values is still 5 and must be tracked in a separate variable such as size.
int numbers[100];
int size = 5;
Here, sizeof(numbers) / sizeof(numbers[0]) is 100, while size is the number of elements currently being used. The sizeof expression cannot determine how many positions contain meaningful user input.
Time and Space Complexity
- Direct
sizeofmethod:O(1)time because it determines the count without traversing the array. - Loop method:
O(n)time because it visits allnarray positions. - Extra space complexity:
O(1)for both methods because they use only a few size or counter variables.
Common Mistakes
- Using only
sizeof(array), which gives the size in bytes rather than the number of elements. - Dividing by a hard-coded type size instead of
sizeof(array[0]). - Applying the formula to a pointer and expecting it to behave like an array.
- Running a counting loop without a valid boundary and reading beyond the array.
- Confusing the array's declared capacity with the number of elements currently in use.
- Printing a
size_tvalue with%dinstead of the correct%zuformat specifier.
For an array declared in the current scope, sizeof(array) / sizeof(array[0]) is the most concise way to count its elements. A loop can maintain a counter while traversing the array, but it must always be given a safe and known boundary.