Executive summary:

int a[17];
size_t n = sizeof(a)/sizeof(a[0]);

Full answer:

To determine the size of your array in bytes, you can use the sizeof operator:

int a[17];
size_t n = sizeof(a);

On my computer, ints are 4 bytes long, so n is 68.

To determine the number of elements in the array, we can divide the total size of the array by the size of the array element. You could do this with the type, like this:

int a[17];
size_t n = sizeof(a) / sizeof(int);

and get the proper answer (68 / 4 = 17), but if the type of a changed you would have a nasty bug if you forgot to change the sizeof(int) as well.

So the preferred divisor is sizeof(a[0]) or the equivalent sizeof(*a), the size of the first element of the array.

int a[17];
size_t n = sizeof(a) / sizeof(a[0]);

Another advantage is that you can now easily parameterize the array name in a macro and get:

#define NELEMS(x)  (sizeof(x) / sizeof((x)[0]))

int a[17];
size_t n = NELEMS(a);
Answer from Mark Harrison on Stack Overflow
Top answer
1 of 7
1759

Executive summary:

int a[17];
size_t n = sizeof(a)/sizeof(a[0]);

Full answer:

To determine the size of your array in bytes, you can use the sizeof operator:

int a[17];
size_t n = sizeof(a);

On my computer, ints are 4 bytes long, so n is 68.

To determine the number of elements in the array, we can divide the total size of the array by the size of the array element. You could do this with the type, like this:

int a[17];
size_t n = sizeof(a) / sizeof(int);

and get the proper answer (68 / 4 = 17), but if the type of a changed you would have a nasty bug if you forgot to change the sizeof(int) as well.

So the preferred divisor is sizeof(a[0]) or the equivalent sizeof(*a), the size of the first element of the array.

int a[17];
size_t n = sizeof(a) / sizeof(a[0]);

Another advantage is that you can now easily parameterize the array name in a macro and get:

#define NELEMS(x)  (sizeof(x) / sizeof((x)[0]))

int a[17];
size_t n = NELEMS(a);
2 of 7
1121

The sizeof way is the right way iff you are dealing with arrays not received as parameters. An array sent as a parameter to a function is treated as a pointer, so sizeof will return the pointer's size, instead of the array's.

Thus, inside functions this method does not work. Instead, always pass an additional parameter size_t size indicating the number of elements in the array.

Test:

#include <stdio.h>
#include <stdlib.h>

void printSizeOf(int intArray[]);
void printLength(int intArray[]);

int main(int argc, char* argv[])
{
    int array[] = { 0, 1, 2, 3, 4, 5, 6 };

    printf("sizeof of array: %d\n", (int) sizeof(array));
    printSizeOf(array);

    printf("Length of array: %d\n", (int)( sizeof(array) / sizeof(array[0]) ));
    printLength(array);
}

void printSizeOf(int intArray[])
{
    printf("sizeof of parameter: %d\n", (int) sizeof(intArray));
}

void printLength(int intArray[])
{
    printf("Length of parameter: %d\n", (int)( sizeof(intArray) / sizeof(intArray[0]) ));
}

Output (in a 64-bit Linux OS):

sizeof of array: 28
sizeof of parameter: 8
Length of array: 7
Length of parameter: 2

Output (in a 32-bit windows OS):

sizeof of array: 28
sizeof of parameter: 4
Length of array: 7
Length of parameter: 1
🌐
W3Schools
w3schools.com › c › c_arrays_size.php
C Get the Size of an Array
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: int myNumbers[] = {10, 25, 50, 75, 100}; int length = sizeof(myNumbers) / sizeof(myNumbers[0]); ...
Discussions

How does C know the size of an array?
The allocator needs to keep that information itself. There's a couple of approaches at doing this. The straight-forward way is for the memory allocator to keep a small amount of metadata for of each memory allocation. That metadata would contain the size of the allocation (the size you passed to malloc rounded up to a more convenient value). When you pass back a pointer to free, it can use that size to know how big the allocation was. A common approach is for the metadata to be placed in memory immediately before the pointer given to the program in malloc. Another approach is for the memory to be allocated from a slab of equal-sized blocks. There might be a slab that allocates 8-byte blocks, a slab for 16-byte blocks, a slab for 32-byte blocks, and so on. If you malloc(12), say, the allocator gives the program one of the 16-byte blocks. When the pointer is passed back to free, the memory allocator knows how big the allocation was since it can determine which slab the pointer came from. The allocator still needs to keep some metadata to know which blocks within a slab have been allocated, but not as much as would be needed to track the size of each allocation individually. C is fairly agnostic as to what a pointer actually is, so I suppose you could even have a C implementation with fat pointers that also encoded the sizes (or perhaps their bounds) of their allocations. I'm not sure if any system ever worked this way though. More on reddit.com
🌐 r/C_Programming
24
30
January 6, 2022
Why do C arrays not keep track of their length? - Software Engineering Stack Exchange
What was the reasoning behind not explicitly storing an array's length with an array in C? The way I see it, there are overwhelming reasons to do so but not very many in support of the standard (C... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
April 28, 2014
C Program Length of Array
int is a 32-bit (= 4 byte) data type, so sizeof(array) returns the number of elements times the size in bytes of a single object. A common way of getting the length of an array in C is sizeof(array)/sizeof(array[0]). More on reddit.com
🌐 r/code
6
3
February 21, 2022
In C/C++, why can't we (in theory) grab the size of an array in a function?
C was designed for extremely small memory sizes. The original PDP-7 had 8KB of RAM. With these limitations, every byte matters; like, if you need to pass two arrays of the same size into a function, you would want to have only one variable with the size, not two of them; and if it has a fixed size - don't have a size variable at all. The compiler knows the size of the array during compilation; but in the run time, it's programmers choice how to store the size (and to store it at all or not). More on reddit.com
🌐 r/AskProgramming
23
2
March 6, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › c language › length-of-array-in-c
Length of Array in C - GeeksforGeeks
The Length of an array in C refers to the maximum number of elements that an array can hold.
Published: October 17, 2025
🌐
Reddit
reddit.com › r/c_programming › how does c know the size of an array?
r/C_Programming on Reddit: How does C know the size of an array?
January 6, 2022 -

A char array has the terminating \0 byte at the last index, so you can easily iterate through it. I can access the array by using it's address, stored in a pointer variable. The pointer is just a number, and by adding digits i can get any array index i want.

Integer arrays do not have a terminating byte and there is no information about the size in the pointer address. So how does for example free() know what to free?

Top answer
1 of 7
35
The allocator needs to keep that information itself. There's a couple of approaches at doing this. The straight-forward way is for the memory allocator to keep a small amount of metadata for of each memory allocation. That metadata would contain the size of the allocation (the size you passed to malloc rounded up to a more convenient value). When you pass back a pointer to free, it can use that size to know how big the allocation was. A common approach is for the metadata to be placed in memory immediately before the pointer given to the program in malloc. Another approach is for the memory to be allocated from a slab of equal-sized blocks. There might be a slab that allocates 8-byte blocks, a slab for 16-byte blocks, a slab for 32-byte blocks, and so on. If you malloc(12), say, the allocator gives the program one of the 16-byte blocks. When the pointer is passed back to free, the memory allocator knows how big the allocation was since it can determine which slab the pointer came from. The allocator still needs to keep some metadata to know which blocks within a slab have been allocated, but not as much as would be needed to track the size of each allocation individually. C is fairly agnostic as to what a pointer actually is, so I suppose you could even have a C implementation with fat pointers that also encoded the sizes (or perhaps their bounds) of their allocations. I'm not sure if any system ever worked this way though.
2 of 7
33
How does C know the size of an array? It does not, because it can not know, since C arrays are just pointers to the memory address of the first element. With other words, you have to tell it the size. This because there is no array data type in the machine. C models memory as a linear space of addresses starting from 0 to N and counting bytes. An array is just chunk or consecutive memory addresses. If you need the size of an array, you usually keep that in some variable. The pointer is just a number, and by adding digits i can get any array index i want. You are not adding digits; you are adding offsets. C has "pointer arithmetic", i.e. pointers are an intrinsic data type in C and have types, so int* is not the same as char*. That, so you can actually work with indexes and not with bytes. If you have an int32_t *i, and a int8_t *c; when you do i+1 and c+1, the compiler will know how many bytes to add, 4 or 1, so that you get address of next element in memory, otherwise you would have to do yourself this low arithmetic to get correct offset to next element. A char array has the terminating \0 byte at the last index, so you can easily iterate through it. Not really. A char array would be just an array of characters, i.e. of integers, since there is no char data type in a machine either. What you think of is a null-terminated string, typically a const char*. If you declare a string as a literal, something like char* s = "hello world", the compiler will add that terminating null for you. If you declare char[10], an array of 10 chars, there would be no terminating null at the end. If you wish to store a string in that array for use with string functions from string.h, you would have to put terminating null in it yourself. That terminating null is added because arrays are not intrinsic data types in C either, but pointers, and do not record length automatically. So how does for example free() know what to free? free is an API into a memory allocation/deallocation routines. It keeps internal structures that keeps track of allocated memory.
🌐
IONOS
ionos.com › digital guide › websites › web development › c: array length
How to determine the length of an array in C
December 10, 2024 - In C, there is no built-in function for de­ter­min­ing array length, so you have to determine it manually. ... sizeof()is an operator in C. It de­ter­mines the size of a data type or a variable in bytes during the compile time.
Top answer
1 of 10
108

C arrays do keep track of their length, as the array length is a static property:

int xs[42];  /* a 42-element array */

You can't usually query this length, but you don't need to because it's static anyway – just declare a macro XS_LENGTH for the length, and you're done.

The more important issue is that C arrays implicitly degrade into pointers, e.g. when passed to a function. This does make some sense, and allows for some nice low-level tricks, but it loses the information about the length of the array. So a better question would be why C was designed with this implicit degradation to pointers.

Another matter is that pointers need no storage except the memory address itself. C allows us to cast integers to pointers, pointers to other pointers, and to treat pointers as if they were arrays. While doing this, C is not insane enough to fabricate some array length into existence, but seems to trust in the Spiderman motto: with great power the programmer will hopefully fulfill the great responsibility of keeping track of lengths and overflows.

2 of 10
39

A lot of this had to do with the computers available at the time. Not only did the compiled program have to run on a limited resource computer, but, perhaps more importantly, the compiler itself had to run on these machines. At the time Thompson developed C, he was using a PDP-7, with 8k of RAM. Complex language features that didn't have an immediate analog on the actual machine code were simply not included in the language.

A careful read through the history of C yields more understanding into the above, but it wasn't entirely a result of the machine limitations they had:

Moreover, the language (C) shows considerable power to describe important concepts, for example, vectors whose length varies at run time, with only a few basic rules and conventions. ... It is interesting to compare C's approach with that of two nearly contemporaneous languages, Algol 68 and Pascal [Jensen 74]. Arrays in Algol 68 either have fixed bounds, or are `flexible:' considerable mechanism is required both in the language definition, and in compilers, to accommodate flexible arrays (and not all compilers fully implement them.) Original Pascal had only fixed-sized arrays and strings, and this proved confining [Kernighan 81].

C arrays are inherently more powerful. Adding bounds to them restricts what the programmer can use them for. Such restrictions may be useful for programmers, but necessarily are also limiting.

Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › how-to-find-the-size-of-an-array-in-c-with-the-sizeof-operator
How to Find the Size of an Array in C with the sizeof Operator
December 5, 2022 - To find the length of the array, you need to divide the total amount of memory by the size of one element - this method works because the array stores items of the same type. So, you can divide the total number of bytes by the size of the first ...
🌐
Sentry
sentry.io › sentry answers › c › determine the size of an array in c
Determine the size of an array in C | Sentry
size_t list_length = sizeof(my_array) / sizeof(my_array[0]); In the background, size_t is an unsigned integer or unsigned long. Therefore, unless the size of our array is greater than INT_MAX, we can safely declare list_length as an int:
🌐
Reddit
reddit.com › r/askprogramming › in c/c++, why can't we (in theory) grab the size of an array in a function?
r/AskProgramming on Reddit: In C/C++, why can't we (in theory) grab the size of an array in a function?
March 6, 2024 -

I'm learning C/C++, and it has been drilled into me that whenever I pass an array to a function, I should also pass its length alongside it, since what actually gets passed is a pointer to the first element of the array. Previously, I just accepted this fact at face value, since I assumed that in an array declaration, the compiler would allocate the necessary memory for the array, but only store the starting location (similar to what happens when I use malloc). However, I recently realized that this is not true. The compiler clearly knows how long each array is, when in the right scope:

int func(int array[]) {return sizeof(array);}

int main() {
    int array[5] = {0, 1, 2, 3, 4};
    printf("Sizeof array in main: %d, sizeof array in func: %d", sizeof(array), func(array));
    //Sizeof array in main: 20, sizeof array in func: 8
    return 0;
}

This, however, means that my previous reasoning for why only the starting pointer of the array gets passed was nonsense. This means we could implement a compiler which would know the size of an array when it gets passed to a function, and the fact that C and C++ don't do this is a design choice, not a necessity. My question is therefore: Why were these languages implemented in this way? Providing an array's size alongside its starting pointer by default would save programmers some headache, and make over-indexing much less likely.

Edit: Formatting.

🌐
DigitalOcean
digitalocean.com › community › tutorials › find-array-length-in-c-plus-plus
How to Find the Length of an Array in C++ | DigitalOcean
Learn how to get the length of arrays in C++ using simple methods. Explore examples with pointers, STL containers, and best practices for beginners.
🌐
Code with C
codewithc.com › code with c › c++ tutorial › c++ array length: determining size in c++ arrays
C++ Array Length: Determining Size In C++ Arrays - Code With C
January 11, 2024 - Before we delve into the length of the entire array, it’s crucial to comprehend the size of individual elements within the array. Each element’s size can be obtained by using sizeof() followed by the data type of the array.
🌐
Arduino Forum
forum.arduino.cc › t › how-do-get-the-array-length › 229483
How do get the array length? - Projects / Programming - Arduino Forum
April 25, 2014 - Arrays are not passed to functions. Pointers are. In the function, there is no way to get the length of the array that the pointer points to, unless the array contains some marker to indicate the end, as the NULL in a char array does.
🌐
Quora
quora.com › How-do-you-get-the-size-of-a-char-array-in-C
How to get the size of a char array in C - Quora
Answer (1 of 12): Find the length of a char array using the sizeof operator. Regardless of the data type of an element, the size of operator can be used to calculate an array's size. However, if the inner details are disregarded when measuring the array's size, there can be horrifying blunders. ...
🌐
W3Schools
w3schools.com › cpp › cpp_arrays_size.asp
C++ Get the Size of an Array
To get the size of an array, you can use the sizeof() operator.
🌐
Codemia
codemia.io › home › knowledge hub › how do i determine the size of my array in c?
How do I determine the size of my array in C? | Codemia
December 28, 2024 - It's vital to note that in standard ... mechanisms like vectors or ArrayLists. The most common approach to determine the size of an array in C is using the sizeof operator....
🌐
cppreference.com
en.cppreference.com › c › language › array
Array declaration - cppreference.com
The number of those objects (the array size) never changes during the array lifetime.
🌐
W3Schools
w3schools.com › c › c_arrays_loop.php
C Arrays and Loops
Now we can use that to write loops that work for arrays of any size. This is more flexible and sustainable: int myNumbers[] = {25, 50, 75, 100}; int length = sizeof(myNumbers) / sizeof(myNumbers[0]); int i; for (i = 0; i < length; i++) { printf("%d\n", myNumbers[i]); } Try it Yourself » ·
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › cpp-arrays
Arrays in C++ - GeeksforGeeks
Size of arr[0]: 1 Size of arr: 5 Length of an array: 5 · Practice Problems: Print Array in Reverse, Count the Zeros in an Array, Search an Element in an Array, First Repeating Element, Sum of All Array Elements, Count Smaller Than X, Largest Element in Array ·
Published: 3 weeks ago