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
🌐
W3Schools
w3schools.com › c › c_arrays_size.php
C Get the Size of an Array
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Compiler C Syllabus C Study Plan C Interview Q&A C Certificate · ❮ Previous Next ❯ · To get the size of an array, you can use the sizeof operator: int myNumbers[] = ...
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
loops - In C++ how does "sizeof(array)/sizeof(array[0])" give the number of elements in an array? Why isn't it affected by the length of each element? - Computer Science Stack Exchange
I'm playing around with C++ and was looking at how to loop through an array of unknown length. The following code was suggested and works perfectly from the little testing I've done: std::string wo... More on cs.stackexchange.com
🌐 cs.stackexchange.com
May 5, 2021
How to find size of an array in C without sizeof
For example, if the array is at the end of the stack frame then doing this will cause a segfault that would be impossible to catch because you probably bound it to a macro for readability. ... Wow that is really dangerous. The type of *(&foo + 1) - foo is ptrdiff_t which could be much much larger than int. Edit: This next part might never be true on any system now that I think about it as size_t is always larger than the stack. Also, the sizeof ... More on reddit.com
🌐 r/programming
95
239
December 27, 2016
how do i find size of an array in a function
You cannot. void f( int arr[] ) is exactly equivalent to void f( int* arr ), meaning you only have a pointer to the array. C-style/raw arrays get implicitly converted into pointers in most situations (commonly called decay). Once you only have a pointer, you have lost all information about the array size, or whether it was an array to begin with. This and the fact that raw arrays do not have value semantics is why they are discouraged in modern C++. The alternatives are: For a fixed size array use std::array For a dynamically sized array, use std::vector For a function parameter when you dont want a copy, use std::span So in your case, your function should be int binary_search( const std::span arr, const int target ) { int start = 0; int end = arr.size(); } On another note: This sizeof(arr)/sizeof(arr[0]) trick is simply never the correct solution. As you have experienced here, it will give you a nonsense answer without any warning. Dont ever use it. Use std::size(arr). That will either work, or it will cause a compilation error if its not possible to determine the size. More on reddit.com
🌐 r/cpp_questions
14
4
January 10, 2023
🌐
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 ... to find the length of an array is by using sizeof() operator to get the total size of the array and then divide it by the size of one element....
Published   October 17, 2025
🌐
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 - C does not provide a built-in way to get the size of an array. With that said, it does have the built-in sizeof operator, which you can use to determine the size.
🌐
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 this example, we’re going to use sizeof() to determine the size of the array myArray as well as the size of a single element. With this in­for­ma­tion, we can then calculate the number of elements in the array by dividing the total size by the size of a single element.
Find elsewhere
🌐
Quora
quora.com › How-do-I-take-the-size-of-an-array-from-a-user-in-C
How to take the size of an array from a user in C - Quora
Answer (1 of 3): Arrays have fixed lengths that are known within the scope of their declarations. Nevertheless, it is possible and sometimes convenient to calculate array lengths. In particular, this can make code more flexible when the array length is determined automatically from an initializer...
🌐
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:
🌐
Quora
quora.com › What-is-an-alternative-method-to-using-sizeof-to-find-the-length-of-an-array-in-a-C-program
What is an alternative method to using sizeof to find the length of an array in a C program? - Quora
Answer (1 of 3): In general terms, there’s not a complete replacement to[code ] sizeof [/code]in the C language:[code ] sizeof [/code]not even a function, but an operator of the language, just as plus[code ] + [/code]or less[code ] -[/code] A possible idea would be to subtract the address ...
🌐
W3Schools
w3schools.com › cpp › cpp_arrays_size.asp
C++ Get the Size of an Array
It is because the sizeof() operator returns the size of a type in bytes. You learned from the Data Types chapter that an int type is usually 4 bytes, so from the example above, 4 x 5 (4 bytes x 5 elements) = 20 bytes. To find out how many elements an array has, you have to divide the size of the array by the size of the first element in the array:
🌐
CodeChef
codechef.com › learn › course › c › BSNOJS › problems › NRJAYK13
Array sizeof() Property in C in C
Test your Learn C knowledge with our Array sizeof() Property in C practice problem. Dive into the world of c challenges at CodeChef.
🌐
W3Schools
w3schools.com › c › c_data_types_sizeof.php
C sizeof operator
Which means if you have an array of 1000 char values, it will occupy 1000 bytes (1 KB) of memory. Using the right data type for the right purpose will save memory and improve the performance of your program. You will learn more about the sizeof operator later in this tutorial, and how to use ...
🌐
Sanfoundry
sanfoundry.com › c-tutorials-size-array-using-sizeof-operator-strlen-function
Size of an Array using sizeof Operator OR strlen() Function in C - Sanfoundry
May 15, 2025 - This program calculates the total memory used by an integer array and the number of elements it contains. The sizeof(mcqs) returns the total size in bytes (20 bytes for 5 integers), and dividing it by the size of one element gives the count (5).
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-language › sizeof-operator-c
sizeof Operator (C) | Microsoft Learn
August 3, 2021 - The result is an unsigned integral constant. The standard header STDDEF.H defines this type as size_t. When you apply the sizeof operator to an array identifier, the result is the size of the entire array rather than the size of the pointer represented by the array identifier.
🌐
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 - 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. It is possible to find the length of an array in multiple ways.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › using-sizof-operator-with-array-paratmeters-in-c
Do Not Use sizeof For Array Parameters in C - GeeksforGeeks
July 23, 2025 - So, the expression sizeof(arr)/sizeof(arr[0]) becomes sizeof(int *)/sizeof(int) which results in 2 (size of int* is 8 bytes because its an pointer and pointer occupies the 8 bytes of memory and int is 4) and the for loop inside fun() is executed only two times irrespective of the size of the array. Therefore, sizeof should not be used to get a number of elements in such cases.
🌐
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
Embed size in a struct or use sentinel value/terminator. Use conventions like null-termination and strlen if appropriate. ... sizeof gives bytes, not "used characters" for strings; for wide-char arrays use sizeof(arr)/sizeof(arr[0]).
🌐
Wikipedia
en.wikipedia.org › wiki › Sizeof
sizeof - Wikipedia
January 11, 2026 - Here, sizeof buffer is equivalent to 10 * sizeof buffer[0], which evaluates to 10, because the size of the type char is defined as 1. C99 adds support for flexible array members to structures. This form of array declaration is allowed as the last element in structures only, and differs from normal arrays in that no length is specified to the compiler.
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_arrays.htm
Arrays in C
The compiler allocates a continuous block of memory. The size of the allocated memory depends on the data type of the array. If an integer array of 5 elements is declared, the array size in number of bytes would be "sizeof(int) x 5"