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
🌐
GeeksforGeeks
geeksforgeeks.org › c language › length-of-array-in-c
Length of Array in C - GeeksforGeeks
In C, we don't have any pre-defined function to find the length of the array instead, you must calculate the length manually using techniques based on how the array is declared and used.
Published   October 17, 2025
Top answer
1 of 16
1754

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
Discussions

(C#) find length of a List<String>?
This is a common error. But stop to think about it. Let's say I have this: List mylist = new List(); mylist.Add("test1"); mylist.Add("test2"); How many items are there? 2 right. And since indexes are zero based it would be: mylist[0] = "test1" mylist[1] = "test2" Now, take a look at your look again and see if you can find the problem. More on reddit.com
🌐 r/learnprogramming
10
7
January 13, 2012
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
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
Why do C arrays not keep track of their length? - Software Engineering Stack Exchange
Great link, they also explicitly ... on the length of a string caused by holding the count in an 8- or 9-bit slot, and partly because maintaining the count seemed, in our experience, less convenient than using a terminator - well so much for that :-) ... The unterminated arrays also fits with the bare metal approach of C. Remember that the K&R C book is less than 300 pages with a language tutorial, reference and a list of the standard ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
April 28, 2014
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
Why is `sizeof` used to determine the length of an array in C?
The `sizeof` operator returns the size of a variable or data type in bytes. For an array, `sizeof(array)` gives the total number of bytes the array occupies in memory. To get the number of elements, divide by `sizeof(element)`, which gives the size of each element in the array.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › length of an array in c
Find the Length of an Array in C: Methods & Examples
🌐
W3Schools
w3schools.com › c › c_arrays_size.php
C Get the Size of an Array
double myValues[] = {1.1, 2.2, 3.3}; int length = sizeof(myValues) / sizeof(myValues[0]); printf("%d", length); // Prints 3 · Try it Yourself » · In the next chapter, you will see how to use this formula to make loops that automatically adapt to the array size.
🌐
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 - Being of the same type, all elements in a C array will also be of the same length, so we can use the first element (my_array[0]) to do this: size_t list_length = sizeof(my_array) / sizeof(my_array[0]);
🌐
Quora
quora.com › What-is-the-length-of-a-list-in-C
What is the length of a list in C++? - Quora
This can be determined using the size() member function for standard containers like std::list or std::vector. ... In C++, you can use the `size()` function to get the length of a list.
🌐
Reddit
reddit.com › r/learnprogramming › (c#) find length of a list?
r/learnprogramming on Reddit: (C#) find length of a List<String>?
January 13, 2012 -

I am attempting to grab a List<String> and iterate through each string in the list then Iterate through each Character in each string.

I can get the length of the list, but I am having a hard time getiing the length of the String.

List<String> myList;
 for (int y = 0; y <= myList.Count(); y++)
        {
 for (int x = 0; x <= myList[y].Length ; x++)
{

And I keep getting an index out of bounds error. I know I am probably going about this all wrong. So I will ask here for advice and come back tomorrow with a fresh head and see if it makes any more sense.

Thanks!

Find elsewhere
🌐
WsCube Tech
wscubetech.com › resources › c-programming › length-of-array
How to Get Length (Size) of Array in C? With Examples
August 29, 2025 - Learn how to find the length (size) of an array in C with simple examples. Understand the concept clearly and improve your coding skills. Read now!
🌐
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 - In C, utilizing the sizeof() administrator is a standard method for getting an array length. To view a variable's or information type's size in bytes, utilize the sizeof() administrator.
🌐
Cplusplus
cplusplus.com › reference › list › list › size
std::list::size
Returns the number of elements in the list container.
🌐
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 you declare an array in C, the memory for the elements in the array is allocated in the RAM con­tigu­ous­ly (i.e., in a se­quen­tial manner without gaps). In C, there is no built-in function for de­ter­min­ing array length, so you have to determine it manually.
🌐
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 - The sizeof() operator in C calculates the size of passed variables or datatype in bytes. We cannot calculate the size of the array directly using sizeof(), we will use the programming logic defined above to find the length.
🌐
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 - The size or length of the array here is equal to the total number of elements in it - which is 5. There are a few methods through which we can determine the length of an array in C++ language.
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.

🌐
PrepBytes
prepbytes.com › home › c programming › how to find the length of an array in c
How to Find the Length of An Array in C
June 8, 2023 - Divide the overall array size by the size of one of the datatypes you are using to get the length of the array (the number of elements). ... The size of the array divided by the size of the datatype you are using to describe it gives the C array ...
🌐
Verve AI
vervecopilot.com › interview-questions › why-understanding-c-sharp-list-length-might-be-your-edge-in-technical-interviews
Why Understanding C Sharp List Length Might Be Your Edge In Technical Interviews
When we talk about "c sharp list length," we're specifically referring to the Count property of the List collection in C#. Unlike arrays, which have a fixed Length property, a List is a dynamic, resizable collection of elements. The Count property returns the number of elements currently contained in the List.
🌐
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 - In programming and Computer Science in general, indexing always starts at 0, so the first element in an array will always have an index of 0. #include <stdio.h> int main() { int faveNumbers[] = {7, 33, 13, 9, 29}; size_t size = sizeof(faveNumbers) / sizeof(faveNumbers[0]); printf("The length of the array is %d \n", size); } // output // The length of the array is 5
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › variable-length-arrays-in-c-and-c
Variable Length Arrays (VLAs) in C - GeeksforGeeks
October 7, 2025 - A Variable Length Array is an array whose size is not fixed at compile-time, but instead is decided at runtime.