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
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
🌐
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
Calculate Length of Array in C by Using Function - Stack Overflow
I want to make a FUNCTION which calculates size of passed array. I will pass an Array as input and it should return its length. I want a Function int ArraySize(int * Array /* Or int Array[] */)... More on stackoverflow.com
🌐 stackoverflow.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
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:
🌐
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­...
🌐
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]); ...
Find elsewhere
🌐
WsCube Tech
wscubetech.com › resources › c-programming › length-of-array
How to Get Length (Size) of Array in C? With Examples
3 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.
🌐
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.
🌐
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 ...
🌐
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.
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.
🌐
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.
🌐
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 ...
Top answer
1 of 9
34

You cannot calculate the size of an array when all you've got is a pointer.

The only way to make this "function-like" is to define a macro:

#define ARRAY_SIZE( array ) ( sizeof( array ) / sizeof( array[0] ) )

This comes with all the usual caveats of macros, of course.

Edit: (The comments below really belong into the answer...)

  1. You cannot determine the number of elements initialized within an array, unless you initialize all elements to an "invalid" value first and doing the counting of "valid" values manually. If your array has been defined as having 8 elements, for the compiler it has 8 elements, no matter whether you initialized only 5 of them.
  2. You cannot determine the size of an array within a function to which that array has been passed as parameter. Not directly, not through a macro, not in any way. You can only determine the size of an array in the scope it has been declared in.

The impossibility of determining the size of the array in a called function can be understood once you realize that sizeof() is a compile-time operator. It might look like a run-time function call, but it isn't: The compiler determines the size of the operands, and inserts them as constants.

In the scope the array is declared, the compiler has the information that it is actually an array, and how many elements it has.

In a function to which the array is passed, all the compiler sees is a pointer. (Consider that the function might be called with many different arrays, and remember that sizeof() is a compile-time operator.

You can switch to C++ and use <vector>. You can define a struct vector plus functions handling that, but it's not really comfortable:

#include <stdlib.h>

typedef struct
{
    int *  _data;
    size_t _size;
} int_vector;

int_vector * create_int_vector( size_t size )
{
    int_vector * _vec = malloc( sizeof( int_vector ) );
    if ( _vec != NULL )
    {
        _vec._size = size;
        _vec._data = (int *)malloc( size * sizeof( int ) );
    }
    return _vec;
}

void destroy_int_vector( int_vector * _vec )
{
    free( _vec->_data );
    free( _vec );
}

int main()
{
    int_vector * myVector = create_int_vector( 8 );
    if ( myVector != NULL && myVector->_data != NULL )
    {
        myVector->_data[0] = ...;
        destroy_int_vector( myVector );
    }
    else if ( myVector != NULL )
    {
        free( myVector );
    }
    return 0;
}

Bottom line: C arrays are limited. You cannot calculate their length in a sub-function, period. You have to code your way around that limitation, or use a different language (like C++).

2 of 9
12

You can't do this once the array has decayed to a pointer - you'll always get the pointer size.

What you need to do is either:

  • use a sentinel value if possible, like NULL for pointers or -1 for positive numbers.
  • calculate it when it's still an array, and pass that size to any functions.
  • same as above but using funky macro magic, something like:
    #define arrSz(a) (sizeof(a)/sizeof(*a)).
  • create your own abstract data type which maintains the length as an item in a structure, so that you have a way of getting your Array.length().
🌐
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", ...