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
#include <stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 }; // Find the size of the array arr int n = sizeof(arr) / sizeof(arr[0]); printf("%d", n); return 0; } ... Explanation: In this program, the total size of the array arr (20 bytes) ...
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
🌐
IONOS
ionos.com › digital guide › websites › web development › c: array length
How to determine the length of an array in C
December 10, 2024 - Another method for de­ter­min­ing array lengths in C is to use a for loop. This approach involves iterating through the array and counting the number of elements it contains.
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
🌐
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 - During each iteration, the pointer is advanced and the length variable is incremented. Once the loop ends, length holds the number of characters in the string, effectively calculating the length of the character array without including the null ...
🌐
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!
🌐
Know Program
knowprogram.com › home › how to find length of array in c
How to Find Length of Array in C - Know Program
February 6, 2020 - How to Find the Length of Array in C? To find the length of the array we have to use sizeof() function. Length = sizeof(arr) / sizeof(arr[0]);
Find elsewhere
🌐
W3Schools
w3schools.com › c › c_arrays_size.php
C Get the Size of an Array
If you want to find out how many ... one element: int myNumbers[] = {10, 25, 50, 75, 100}; int length = sizeof(myNumbers) / sizeof(myNumbers[0]); printf("%d", length); // Prints 5 ... double myValues[] = {1.1, 2.2, 3.3}; int ...
🌐
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 - Arrays have a fixed length that is specified in the declaration of the array. In C, there is no built-in method to get the size of an array. A little effort is required to get the length of the array by utilizing the in-built methods or pointers in C.
🌐
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 - However, the code above doesn't calculate the size of the array directly. You will need some extra programming logic, which will look something like this: array_length = (total size of the array) / (size of the first element in the array)
🌐
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 - OutputThe array is: 1 2 3 4 5 6 7 8 9 0 The length of the given Array is: 10 · In this code, we traverse the array arr using a for-each loop with i as the iterator. As the loop traverses, c is incremented.
🌐
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
Answer (1 of 8): [code c++]// Number of elements in the array size_t array_size = *(&array + 1) - array;[/code] Considering the array arr[] : 1. &arr is a pointer to the array. It points at the same memory address as arr. But arr decays to a pointer to first element. 2. Hence &arr + 1 points at ...
🌐
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.
🌐
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 - #include <stddef.h> #include <stdio.h> ... printf("Size of the array: %d\n",ARRAYSIZE(values)); for (i = 0; i < ARRAYSIZE(values); i++) { printf("%d ", values[i]); } return 0; }...
🌐
Quora
quora.com › How-is-the-length-of-an-array-in-C-determined
How is the length of an array in C determined? - Quora
The general formula to calculate the length of an array is ... where upper_bound is the index of the last element and lower_bound is the index of the first element in the array. ... B.S. Physics, University of California, Los Angeles (2022) ...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-arrays
Arrays in C - GeeksforGeeks
#include <stdio.h> int main() { int arr[5] = {2, 4, 8, 12, 16}; // Print each element of // array using loop printf("Printing Array Elements\n"); for(int i = 0; i < 5; i++){ printf("%d ", arr[i]); } printf("\n"); // Printing array element in reverse printf("Printing Array Elements in Reverse\n"); for(int i = 4; i>=0; i--){ printf("%d ", arr[i]); } return 0; }
Published   October 17, 2025
🌐
Arduino Forum
forum.arduino.cc › forum 2005-2010 (read only) › software › syntax & programs
get the length of an Array - Syntax & Programs - Arduino Forum
June 17, 2008 - In C you can get the size of your array in bytes using sizeof(myarray); To get the number of elements you divide this by the size of a single element, like this: · One thing to note about the sizeof function is that it will not work on arrays ...