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 16
1753

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 16
1120

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
🌐
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. It must be specified at the time of declaration. It is also known as the size of an array that is used to determine the memory required to store all ...
Published   October 17, 2025
Discussions

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
Why do C arrays not keep track of their length? - Software Engineering Stack Exchange
A Java-style arr.length is both clear and avoids the programmer from having to maintain many ints on the stack if dealing with several arrays · Function parameters become more cogent. More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
How to find the length of an array
The size information is lost when passing the array to a function. You need an additional parameter for printCharr where you'll pass the size, which you calculate inside main (where the array has been defined). More on reddit.com
🌐 r/C_Programming
12
19
November 16, 2018
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
People also ask

How do you find the length of an array in C?
You can find the length of an array in C using methods like the `sizeof()` operator or by iterating through the array with a loop. For character arrays, a common approach is to use the null terminator `'\0'` to indicate the end, while for integer arrays, you might use a sentinel value or pointer arithmetic.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › length of an array in c
Find the Length of an Array in C: Methods & Examples
What is the length of an empty array in C?
C does not allow truly empty arrays with size 0. You must specify a positive size during declaration.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › length-of-array
How to Get Length (Size) of Array in C? With Examples
Can I use sizeof() to get the length of an array inside a function?
No, you cannot. When you pass an array to a function, it decays into a pointer. sizeof() will then return the size of the pointer, not the actual array.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › length-of-array
How to Get Length (Size) of Array in C? With Examples
🌐
Sentry
sentry.io › sentry answers › c › determine the size of an array in c
Determine the size of an array in C | Sentry
May 15, 2023 - 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:
🌐
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]); ...
🌐
IONOS
ionos.com › digital guide › websites › web development › c: array length
How to determine the length of an array in C
December 10, 2024 - When measuring the length of an array in C, you are measuring the number of elements the array contains. This in­for­ma­tion is crucial for accessing in­di­vid­ual elements in the array, tra­vers­ing it or per­form­ing ma­nip­u­...
Find elsewhere
🌐
WsCube Tech
wscubetech.com › resources › c-programming › length-of-array
How to Get Length (Size) of Array in C? With Examples
2 weeks ago - Learn in this tutorial how to find the length (size) of an array in C with examples. Understand the concept clearly and improve your C programming skills.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › length of an array in c
Find the Length of an Array in C: Methods & Examples
April 30, 2025 - You can find the length of an array in C using methods like the `sizeof()` operator or by iterating through the array with a loop. For character arrays, a common approach is to use the null terminator `'\0'` to indicate the end, while for integer ...
🌐
Scaler
scaler.com › home › topics › how to find the length of an array in c?
How to Find the Length of an Array in C? - Scaler Topics
August 16, 2022 - Length of Array = size of array/size of 1 datatype that you are using to define an array. The logic is elaborated further programmatically in the below section [Using sizeof()]. Let me ask you a question.
🌐
GNU
gnu.org › software › c-intro-and-ref › manual › html_node › Arrays-of-Variable-Length.html
Arrays of Variable Length (GNU C Language Manual)
Calling the function does not allocate the array, so there’s no particular danger of stack overflow in using this construct. To pass the array first and the length afterward, use a forward declaration in the function’s parameter list (another GNU extension).
🌐
Quora
quora.com › How-do-you-find-an-arrays-length-or-size-in-C
How to find an array's length or size in C - Quora
You can then divide this size by the size of a single element to get the number of elements (length). ... Dividing the total size by the size of one element gives you the number of elements in the array. 2. Array passed as a function argument or referenced by a pointer:
🌐
Quora
quora.com › How-is-the-length-of-an-array-in-C-determined
How is the length of an array in C determined? - Quora
For arrays explicitly declared with a size within a function, you can use the sizeof operator to determine the total size of the array in bytes. Then, by dividing this size by the size of a single element (obtained using sizeof on an element ...
🌐
Medium
medium.com › @future_fanatic › array-length-calculation-in-c-a-complete-guide-50d71d79f44a
Array Length Calculation in C: A Complete Guide | by Future Fanatic | Medium
March 10, 2024 - Explore practical scenarios where each method is applied to determine the length of arrays of varying data types and dimensions. From single-dimensional arrays to multidimensional matrices, these examples will illuminate the versatility of array length calculation in C.
🌐
TutorialKart
tutorialkart.com › c-programming › c-array-length
How to find Array Length in C Language? Examples
October 22, 2024 - #include <stdio.h> int main() { int arr[] = {2, 4, 6, 8}; int arrLength = sizeof arr / sizeof arr[0]; printf("Array Length : %d\n", arrLength); return 0; } ... In this C Tutorial, we learned how to find the length of an array programmatically ...
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.

🌐
Quora
quora.com › How-do-I-return-an-array-and-its-length-to-a-function-in-C
How to return an array and its length to a function in C - Quora
It returns the size (length) of the array. In the main function, we call getArray and pass the address of myArray and its size calculated using sizeof.
🌐
TutorialsPoint
tutorialspoint.com › article › how-do-i-find-the-length-of-an-array-in-c-cplusplus
How do I find the length of an array in C/C++?
April 9, 2025 - When an array is passed to a function, it decays to a pointer, losing size information. For dynamic arrays (malloc), you must manually track the size. The sizeof() operator is the most common and reliable method for finding array length in C.
🌐
w3resource
w3resource.com › c-programming-exercises › c-snippets › how-to-get-the-length-of-an-array-in-c.php
C - Size of an array
November 1, 2025 - C - Size of an array · Code: int size = sizeof(arr)/sizeof(arr[0]); Example: #include <stdio.h> int main() { int arr[] = { 10, 20, 30, 40, 50, 60 }; int size_arra = (arr, sizeof arr / sizeof *arr); printf("Number of elements in arr[]: %d", ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › find-array-length-in-c-plus-plus
How to Find the Length of an Array in C++ | DigitalOcean
April 17, 2025 - Here, we can see the difference between the return values of the two functions end() and begin() gives us the size or length of the given array arr. In this case, the difference is 4, which is the length of arr.
🌐
Sololearn
sololearn.com › en › Discuss › 2562093 › how-to-find-array-length-in-c-programming-language
How to find array length in C Programming Language? | Sololearn: Learn to code for FREE!
October 26, 2020 - Here is mine program:: https://code.sololearn.com/cb1dF7V2ZVx9/?ref=app PLEASE HELP! ... Yes, arrays are of fixed length. You might need to use dynimic arrays or another dynamic length data structure. a [3] = 55 is not adding a new element to the array. In order to understand it you should learn more about pointers and memory.