C Array Size
Get Array Size
To get the size of an array, you can use the sizeof
operator:
Example
int myNumbers[] = {10, 25, 50, 75, 100};
printf("%zu", sizeof(myNumbers)); // Prints 20
Note: Why did the result show 20
instead of 5
, when the array contains 5 elements?
- It is because the sizeof
operator returns the size of a type in
bytes.
You learned from the Data Types chapter that an int
type is usually 4 bytes, so from the example above, 4 x 5
(4 bytes x 5 elements) = 20 bytes.
Knowing the memory size of an array is great when you are working with larger programs that require good memory management.
But what if you just wanted to know how many elements the array was storing?
Get the Number of Elements
If you want to find out how many elements an array has, you can use this formula, which divides the total size of the array by the size of one element:
Example
int myNumbers[] = {10, 25, 50, 75, 100};
int length = sizeof(myNumbers) / sizeof(myNumbers[0]);
printf("%d", length); // Prints 5
The sizeof
formula works for arrays of any type and any size:
Example
double myValues[] = {1.1, 2.2, 3.3};
int length = sizeof(myValues) / sizeof(myValues[0]);
printf("%d", length); // Prints 3
In the next chapter, you will see how to use this formula to make loops that automatically adapt to the array size.