Nearby lessons

56 of 124

C - Array Size

How to find the size of an array in C with the sizeof(arr) / sizeof(arr[0]) idiom — and the critical reason it stops working once the array is passed to a function.

sizeof Gives Bytes, Not Elements

sizeof reports how many bytes something occupies. For an array that is the element count multiplied by the size of one element:

Example01
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int nums[5];
6 char letters[5];
7 double values[5];
8 
9 printf("int nums[5] : %zu bytes\n", sizeof(nums));
10 printf("char letters[5]: %zu bytes\n", sizeof(letters));
11 printf("double values[5]: %zu bytes\n", sizeof(values));
12 return 0;
13}
Output
int    nums[5]  : 20 bytes
char   letters[5]: 5 bytes
double values[5]: 40 bytes

The Standard Idiom

Divide the total size by the size of one element and the units cancel, leaving the count:

Example02
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int nums[] = {10, 20, 30, 40, 50, 60, 70};
6 
7 int total = sizeof(nums);
8 int oneElem = sizeof(nums[0]);
9 int count = total / oneElem;
10 
11 printf("Total bytes : %d\n", total);
12 printf("Bytes each : %d\n", oneElem);
13 printf("Element count : %d\n", count);
14 return 0;
15}
Output
Total bytes   : 28
Bytes each    : 4
Element count : 7

Why Divide by arr[0] and Not sizeof(int)?

Both give the right answer today. Only one keeps working when you change the array's type tomorrow:

Example03
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 double nums[] = {1.5, 2.5, 3.5}; /* changed from int to double */
6 
7 /* FRAGILE - hard-codes the old type */
8 printf("Wrong : %zu\n", sizeof(nums) / sizeof(int));
9 
10 /* ROBUST - adapts automatically */
11 printf("Correct: %zu\n", sizeof(nums) / sizeof(nums[0]));
12 return 0;
13}
Output
Wrong  : 6
Correct: 3

A Reusable Macro

Because the idiom is used so often, most C projects define a macro for it:

Example04
CCode Cell
1#include <stdio.h>
2 
3#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
4 
5int main()
6{
7 int a[] = {1, 2, 3, 4};
8 char b[] = {'x', 'y', 'z'};
9 double c[10];
10 
11 printf("a: %zu elements\n", ARRAY_SIZE(a));
12 printf("b: %zu elements\n", ARRAY_SIZE(b));
13 printf("c: %zu elements\n", ARRAY_SIZE(c));
14 return 0;
15}
Output
a: 4 elements
b: 3 elements
c: 10 elements

The Trap — sizeof Inside a Function

This is the single most important thing to know about array size in C. When an array is passed to a function, it decays into a pointer, and the size information is lost:

Example05
CCode Cell
1#include <stdio.h>
2 
3void printSize(int arr[])
4{
5 /* arr is really an int* here, not an array */
6 printf("Inside function : %zu bytes\n", sizeof(arr));
7 printf("Computed count : %zu <-- WRONG\n",
8 sizeof(arr) / sizeof(arr[0]));
9}
10 
11int main()
12{
13 int nums[5] = {10, 20, 30, 40, 50};
14 
15 printf("In main : %zu bytes\n", sizeof(nums));
16 printf("Computed count : %zu <-- correct\n",
17 sizeof(nums) / sizeof(nums[0]));
18 printf("\n");
19 printSize(nums);
20 return 0;
21}
Output
In main         : 20 bytes
Computed count  : 5  <-- correct

Inside function : 8 bytes
Computed count  : 2  <-- WRONG

Why the Function Sees 8 Bytes

In simple words: a function never receives the array itself — only the address of its first element. On a 64-bit machine an address is 8 bytes, so sizeof(arr) reports 8. Dividing 8 by 4 gives 2, which has nothing to do with the real length.

The parameter forms int arr[], int arr[5] and int *arr are all treated identically by the compiler. Even writing the size in the brackets does not preserve it.

The Fix — Pass the Size

Compute the size in the scope where the array was declared, then hand it over as a parameter:

Example07
CCode Cell
1#include <stdio.h>
2 
3void printArray(int arr[], int size)
4{
5 int i;
6 for (i = 0; i < size; i++)
7 printf("%d ", arr[i]);
8 printf("\n");
9}
10 
11int sumArray(int arr[], int size)
12{
13 int i, total = 0;
14 for (i = 0; i < size; i++) total += arr[i];
15 return total;
16}
17 
18int main()
19{
20 int nums[] = {10, 20, 30, 40, 50};
21 int size = sizeof(nums) / sizeof(nums[0]); /* computed HERE */
22 
23 printArray(nums, size);
24 printf("Sum: %d\n", sumArray(nums, size));
25 return 0;
26}
Output
10 20 30 40 50
Sum: 150

Common Mistakes

MistakeResult
Using sizeof(arr) as the countLoops 20 times over a 5-element array
Calling sizeof on a parameterGives the pointer size, not the array size
Dividing by sizeof(int)Breaks silently when the type changes
Using strlen on a number arraystrlen is for strings only
Trusting int arr[5] as a parameterThe 5 is ignored by the compiler
Trainer's Note: This is why almost every C standard library function that takes an array also takes a length — memcpy(dst, src, n), fgets(buf, n, stdin), qsort(base, n, size, cmp). The language cannot tell them, so you must.
📝 Key Takeaways
  • sizeof(arr) gives the total bytes, not the element count.
  • Element count = sizeof(arr) / sizeof(arr[0]).
  • Divide by arr[0], not by sizeof(int), so it survives a type change.
  • Inside a function the array decays to a pointer and sizeof gives the pointer size.
  • Always pass the size as a second parameter.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4