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
🌐
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]); ...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › length-of-array-in-c
Length of Array in C - GeeksforGeeks
Explanation: In this program, the total size of the array arr (20 bytes) is divided by the size of a single element in the array (4 bytes). This gives the number of elements in the array, which is 20/4 = 5 · We can also calculate the length ...
Published   October 17, 2025
Discussions

c++ - Common array length macro for C? - Stack Overflow
Closed 7 years ago. I've seen several macros for array length floating around: More on stackoverflow.com
🌐 stackoverflow.com
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
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

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
Is sizeof(array) and sizeof(&array) the same?
No. sizeof(array) gives the total size of the array in bytes, while sizeof(&array) gives the size of the pointer to the array
🌐
wscubetech.com
wscubetech.com › resources › c-programming › length-of-array
How to Get Length (Size) of Array in C? With Examples
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
Top answer
1 of 3
83

Here's a better C version (from Google's Chromium project):

#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))

It improves on the array[0] or *array version by using 0[array], which is equivalent to array[0] on plain arrays, but will fail to compile if array happens to be a C++ type that overloads operator[]().

The division causes a divide-by-zero operation (that should be caught at compile time since it's a compile-time constant expression) for many (but not all) situations where a pointer is passed as the array parameter.

See Is there a standard function in C that would return the length of an array? for more details.

There's a better option for C++ code. See Compile time sizeof_array without using a macro for details.

2 of 3
18
  1. What's the difference between those using array[0] and *array?
  2. Why should either be preferred?
  3. Do they differ in C++?

(1) No difference in C. No difference for an actual raw array in C++.

(2) No technical grounds to prefer one or the other, but newbies might be confused by the pointer dereference.

(3) In C++ you would normally not use the macro, because it's very unsafe. If you pass in a pointer instead of an actual raw array, code will compile but yield incorrect result. So in C++ you would/should instead use a function template, like …

#include <stddef.h>

typedef ptrdiff_t Size;

template< class Type, Size n >
Size countOf( Type (&)[n] ) { return n; }

This only accepts actual raw array as argument.

It's part of a triad of functions startOf, endOf and countOf that it's very convenient to define so that they can be applied to both raw arrays and standard library containers. As far as I know this triad was first identified by Dietmar Kuehl. In C++0x startOf and endOf will most probably be available as std::begin and std::end.

Cheers & hth.,

🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-find-size-of-an-array-in-c
How to Find the Size of an Array in C? - GeeksforGeeks
July 23, 2025 - The idea is to use pointer arithmetic to find the difference between the memory addresses of the first element of the array and the beyond the last which will be then automatically scaled by the compiler according to the type of array.
Find elsewhere
🌐
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.
🌐
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.
🌐
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!
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_variable_length_arrays.htm
Variable Length Arrays in C
Enter the size of the array: 5 The array elements are: 1 2 3 4 5 .... The following code fills the variable length array with randomly generated numbers using the functions srand() and rand() from the stdlib.h header file.
🌐
Reddit
reddit.com › r/c_programming › how does c know the size of an array?
r/C_Programming on Reddit: How does C know the size of an array?
January 6, 2022 -

A char array has the terminating \0 byte at the last index, so you can easily iterate through it. I can access the array by using it's address, stored in a pointer variable. The pointer is just a number, and by adding digits i can get any array index i want.

Integer arrays do not have a terminating byte and there is no information about the size in the pointer address. So how does for example free() know what to free?

Top answer
1 of 7
35
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.
2 of 7
33
How does C know the size of an array? It does not, because it can not know, since C arrays are just pointers to the memory address of the first element. With other words, you have to tell it the size. This because there is no array data type in the machine. C models memory as a linear space of addresses starting from 0 to N and counting bytes. An array is just chunk or consecutive memory addresses. If you need the size of an array, you usually keep that in some variable. The pointer is just a number, and by adding digits i can get any array index i want. You are not adding digits; you are adding offsets. C has "pointer arithmetic", i.e. pointers are an intrinsic data type in C and have types, so int* is not the same as char*. That, so you can actually work with indexes and not with bytes. If you have an int32_t *i, and a int8_t *c; when you do i+1 and c+1, the compiler will know how many bytes to add, 4 or 1, so that you get address of next element in memory, otherwise you would have to do yourself this low arithmetic to get correct offset to next element. A char array has the terminating \0 byte at the last index, so you can easily iterate through it. Not really. A char array would be just an array of characters, i.e. of integers, since there is no char data type in a machine either. What you think of is a null-terminated string, typically a const char*. If you declare a string as a literal, something like char* s = "hello world", the compiler will add that terminating null for you. If you declare char[10], an array of 10 chars, there would be no terminating null at the end. If you wish to store a string in that array for use with string functions from string.h, you would have to put terminating null in it yourself. That terminating null is added because arrays are not intrinsic data types in C either, but pointers, and do not record length automatically. So how does for example free() know what to free? free is an API into a memory allocation/deallocation routines. It keeps internal structures that keeps track of allocated memory.
🌐
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:
🌐
Medium
medium.com › @infinator › c-programming-for-beginners-length-of-an-array-723d583be70b
C Programming for Beginners : Length of an Array | by Suraj Das | Medium
December 11, 2021 - The length of the above the above is 4(cuz there are three letters). And the size is 4 too. For a string array, the length is same as the array size. But can be different for other types of arrays.
🌐
W3Schools
w3schools.com › c › c_arrays.php
C Arrays
Using this method, you should know the number of array elements in advance, in order for the program to store enough memory. You are not able to change the size of the array after creation.
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.

🌐
GeeksforGeeks
geeksforgeeks.org › c++ › how-to-find-the-length-of-an-array-in-cpp
How to Find the Length of an Array in C++? - GeeksforGeeks
The simplest way to find the length of an array is by using the sizeof operator. First calculate the size of the array in bytes and divide it by the size of the first element to get the length of the array.
Published   July 23, 2025
🌐
Arduino Forum
forum.arduino.cc › projects › programming
How do you check the Length of an Array? - Programming - Arduino Forum
February 4, 2012 - I was just curious if there was a way to check the length of an array? I didn't see any direct answers to this on the forums and the Array function page on the website didn't say anything about checking the length either. I was hoping to see something like sizeof(array_name), but sizeof() doesn't ...
🌐
Oreate AI
oreateai.com › blog › function-to-determine-the-number-of-elements-in-an-array-in-c › 265396ac4eee2a96483e6324e4a2b385
Function to Determine the Number of Elements in an Array in C - Oreate AI Blog
December 22, 2025 - The sizeof operator returns the total byte size of an array, which we can divide by the byte size of a single element to get the number of elements in that array. Here’s an example function using sizeof to determine the number of elements: ...