Executive summary:

Copyint 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:

Copyint 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:

Copyint 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.

Copyint 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:

Copy#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
1757

Executive summary:

Copyint 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:

Copyint 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:

Copyint 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.

Copyint 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:

Copy#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:

Copy#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):

Copysizeof of array: 28
sizeof of parameter: 8
Length of array: 7
Length of parameter: 2

Output (in a 32-bit windows OS):

Copysizeof 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
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. Knowing the memory size of an array is great when you are working with larger programs that require ...
Discussions

Calculate size of a dynamic array in C: is this a reliable method of telling the size ?
C is case sensitive, and the operator you are thinking of is spelled sizeof, not sizeOf. sizeof(Person) is not valid C unless there's a typedef struct Person Person; somewhere that you failed to include in your post; you have to write sizeof(struct Person). myArray is not dynamic, it is either automatic or static (depending on whether this code is located within a function or at the top level) and its size is a compile-time constant. Initializing arraySize to sizeof(myArray) / structSize is not permitted at the top level since structSize is a regular variable and not a compile-time constant. It is however permitted in a function (in C99 and later). sizeof(array) / sizeof(array[0]) is a common construct to calculate the number of items in an automatic or static array at compile time, and is a compile-time constant. Some C implementations provide a nitems() macro which does this. There is no way to determine the size of a dynamic (i.e. allocated) array at compile time or at run time. If you need the size for later, you are simply going to have to store it somewhere when you allocate the array. Some allocators provide a way to retrieve the actual size of an allocation after the fact, but this may be larger than the requested size due to rounding. Small allocations are likely to get rounded up to the nearest power of two, or the nearest bucket size, while large allocations are likely to get rounded up to the nearest multiple of the virtual memory page size. More on reddit.com
🌐 r/C_Programming
29
11
November 9, 2025
Do you always have to declare the length or size of an array in C?
I just realized you said C, not ... specific size. You can however, make an array inside a loop that's created based on a number taken from user input. ... I know it is pretty easy to make a dynamic array in C++ but I am not sure if is in C. The best solution i could think of was said by ... More on teamtreehouse.com
🌐 teamtreehouse.com
3
August 12, 2014
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
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
🌐
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
🌐
Reddit
reddit.com › r/c_programming › calculate size of a dynamic array in c: is this a reliable method of telling the size ?
r/C_Programming on Reddit: Calculate size of a dynamic array in C: is this a reliable method of telling the size ?
November 9, 2025 -

Hi All !!

I'm playing a bit in C and one thing I cannot understand is how to calculate the size of an array dinamycally created.

Is this a reliable way of calculating the capacity of an array:

struct Person {

int id;
const char* name;
const char* surname;
int age;

} myArray[] = {

{1,"Tom","Burns",56},
{2,"Joe","Black",24}

};

int structSize = sizeOf(Person);

int arraySize = sizeOf(myArray) / structSize;

thanks a lot ! for your help !

Top answer
1 of 14
43
C is case sensitive, and the operator you are thinking of is spelled sizeof, not sizeOf. sizeof(Person) is not valid C unless there's a typedef struct Person Person; somewhere that you failed to include in your post; you have to write sizeof(struct Person). myArray is not dynamic, it is either automatic or static (depending on whether this code is located within a function or at the top level) and its size is a compile-time constant. Initializing arraySize to sizeof(myArray) / structSize is not permitted at the top level since structSize is a regular variable and not a compile-time constant. It is however permitted in a function (in C99 and later). sizeof(array) / sizeof(array[0]) is a common construct to calculate the number of items in an automatic or static array at compile time, and is a compile-time constant. Some C implementations provide a nitems() macro which does this. There is no way to determine the size of a dynamic (i.e. allocated) array at compile time or at run time. If you need the size for later, you are simply going to have to store it somewhere when you allocate the array. Some allocators provide a way to retrieve the actual size of an allocation after the fact, but this may be larger than the requested size due to rounding. Small allocations are likely to get rounded up to the nearest power of two, or the nearest bucket size, while large allocations are likely to get rounded up to the nearest multiple of the virtual memory page size.
2 of 14
20
This is not a dynamic array. The array's size is statically determined because it has 2 initalizers. It works in this case. Be careful with this usage because it will only work with arrays, not with decayed pointers.
🌐
Sentry
sentry.io › sentry answers › c › determine the size of an array in c
Determine the size of an array in C | Sentry
We can use the C sizeof operator to discover the size of expressions and data types in bytes. Using sizeof on an array will return the array’s size in bytes, as below:
Find elsewhere
🌐
IONOS
ionos.com › digital guide › websites › web development › c: array length
How to determine the length of an array in C
December 10, 2024 - Total size of the array: 20 bytes Size of a single element: 4 bytes Number of elements in the array: 5c · Pointers them­selves don’t contain in­for­ma­tion about the size or length of an array.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-arrays
Arrays in C - GeeksforGeeks
The sizeof() operator returns the size in bytes. sizeof(arr) returns the total number of bytes of the array. In an array, each element is of type int, which is 4 bytes. Therefore, we can calculate the size of the array by dividing the total ...
Published   October 17, 2025
🌐
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.
🌐
Sololearn
sololearn.com › en › Discuss › 2386662 › can-i-find-size-of-array-in-c-language
Can I find size of array in C language | Sololearn: Learn to code for FREE!
For charecter array, you can use length = strlen(array); For others, length=sizeof(array) /sizeof(array[0]) ; ... yes using strlen returns you the length of a char array, but it's time complexity is O(n) everytime you call it. and it does not ...
🌐
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 ...
🌐
Team Treehouse
teamtreehouse.com › community › do-you-always-have-to-declare-the-length-or-size-of-an-array-in-c
Do you always have to declare the length or size of an array in C? (Example) | Treehouse Community
August 12, 2014 - I just realized you said C, not Objective-C. In C, I don't believe there is a way to initialize an array without a specific size. You can however, make an array inside a loop that's created based on a number taken from user input. ... I know it is pretty easy to make a dynamic array in C++ but I am not sure if is in C. The best solution i could think of was said by Ricardo.
🌐
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.
🌐
Quora
quora.com › What-is-the-largest-array-size-in-C
What is the largest array size in C? - Quora
Answer: There is no fixed “largest permitted array size” in C. It's compiler, platform, physical memory size, virtual memory size and quite possibly phase-of-the-moon dependent.
🌐
Quora
quora.com › What-is-the-maximum-size-of-an-array-in-the-C-programming-language
What is the maximum size of an array in the C programming language? - Quora
From language perspective size of array is “size_t” type, which is 32 bit of 64 bit integer. Could be 16 bit on some platforms ( 64K elements ). 32 bit would give you 4 billions elemensts.
Top answer
1 of 13
10
The maximum size of an array is determined by the amount of memory that a program can access. On a 32-bit system, the maximum amount of memory that can be addressed by a pointer is 2^32 bytes which is 4 gigabytes. The actual limit may be less, depending on operating system implementation details. I believe that on most 32-bit Linux systems, 1 GB is reserved for the kernel, so the virtual address space of any given application for user data is 3 GB. All the application's code and data are mapped into this 3 GB address space, so the amount of contiguous address space available for a single array is even less. Your array amounts to about 2.6 GB, so I am not the least bit surprised that you ran into trouble. Note that this has nothing to do with the amount of physical memory you have available. Even on a machine with substantially less than 1 GB of RAM, you can allocate a 2 GB array... it's just going to be slow, as most of the array will be in virtual memory, swapped out to disk. The limitation you are up against is the 32-bit address space. Therefore, even if your machine has more than 4 GB of RAM that can be accessed through PAE, individual applications won't be able to access more than 4 (3) GB directly, only through using additional libraries that allow user-level access to to PAE. Given your question, however, you may be running into another issue. Your question indicates that you get an error while compiling your code. This suggests that your monster array is declared as local to a function. Local variables are allocated on the stack, and the stack size is usually much less than gigabytes, and while it is possible to increase the stack size, it's not necessarily a good idea. (Sorry, I don't know off the top of my head how GCC, which I presume is the compiler you use, determines the stack size and what command lines influence it.) So if you need a very large array, you are better off allocating it on the heap, either as a global variable, or allocating it dynamically (using malloc in C, or the new operator in C++).
2 of 13
0
Does it make a difference to 'malloc' the memory? Also, Python numpy has this awesome feature of memory maps, that basically create a file accessible as an array. Maybe one can create such structure in C, too.
🌐
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 simplest method to find the size of an array in C is by using sizeof operator.
🌐
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 - For some exercises, for example, repeating over its individuals, leading calculations, or powerfully assigning memory, knowing the length of a cluster is fundamental. This article looks at a few methodologies, for example, the sizeof() administrator, pointer number juggling, and circling developments, to decide the length of an array in C.
🌐
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.