Computing array lengths, in C, is problematic at best.

The issue with your code above, is that when you do:

int array_length(int a[]){ 
    return sizeof(a)/sizeof(int); 
} 

You're really just passing in a pointer as "a", so sizeof(a) is sizeof(int*). If you're on a 64bit system, you'll always get 2 for sizeof(a)/sizeof(int) inside of the function, since the pointer will be 64bits.

You can (potentially) do this as a macro instead of a function, but that has it's own issues... (It completely inlines this, so you get the same behavior as your int k =... block, though.)

Answer from Reed Copsey 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]); ...
🌐
Ashn
ashn.dev › blog › 2020-01-06-c-array-length.html
The ARRAY_LENGTH Macro in C
Another use of the sizeof operator is to compute the number of elements in an array: ... Say we have an array type T (for example T as type int[3]). Section 6.5.3.4.3 tells us that sizeof T will yield the number of bytes in an array with type T. Expression T[0] has type "element of T", so sizeof ...
Discussions

arrays - array_length in C - Stack Overflow
Although some people below have provided a macro that works, it's important to internalize that C is different from other languages (Java, C#, etc.), arrays are really just pointers to a block of memory, and it's up to you to keep track of the length. More on stackoverflow.com
🌐 stackoverflow.com
How do I determine the size of my array in C? - Stack Overflow
You can do the following to find the length of an array: ... This question already has many answers. What does this answer add that the accepted answer does not have? 2022-09-13T05:20:11.583Z+00:00 ... Note: This thing only works if the array is not defined at run time (like malloc) and the array is not passed in ... More on stackoverflow.com
🌐 stackoverflow.com
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 ... the array, which is 20/4 = 5 · We can also calculate the length of an array in C using pointer arithmetic....
Published   October 17, 2025
🌐
ByteHide
bytehide.com › home › array length in c#: step-by-step guide
Array Length in C#: Step-by-Step Guide (2026)
December 26, 2023 - This code effortlessly reveals the length of ‘array1’ as 5. The ‘Length’ property in C# acts as a fundamental tool when dealing with arrays. No cloak and dagger act, just plain and simple usage to get the total number of elements present in an array. Have you ever wondered how it works?
🌐
Codemia
codemia.io › knowledge-hub › path › how_do_i_determine_the_size_of_my_array_in_c
How do I determine the size of my array in C?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
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 - The sizeof() operator returns the total size in bytes. To get the array length, divide the total size by the size of one element
Find elsewhere
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
🌐
The Valley of Code
thevalleyofcode.com › lesson › c-advanced › array-length
C Advanced: Array length
The simplest procedural way to get the value of the length of an array is by using the sizeof operator. First you need to determine the size of the array. Then you need to divide it by the size of one element.
🌐
Sabe
sabe.io › blog › c-array-length
How to get the Length of an Array in C | Sabe
January 16, 2022 - Unfortunately, C does not have a built-in function to get the length of an array.
🌐
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.
🌐
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 - Note : Do not forget to specify the data type of the array as int. We can see the length of this array is 3 as it has three numbers.
🌐
ScriptVerse
scriptverse.academy › tutorials › c-program-array-length.html
C Program: Find the Length (or Size) of an Array
#include <stdio.h> int main() { char c[] = "hello"; int n[] = {-4, -3, -2, -1, 0, 1, 2, 3, 4}; float f[] = {3.14, 2.71}; double d[] = {3.14, 2.71, 1.61}; printf("Size of the character array is: %lu", sizeof(c)/sizeof(char)); printf("\n"); printf("Size of the integer array is: %lu", sizeof(n)/sizeof(int)); printf("\n"); printf("Size of the float array is: %lu", sizeof(f)/sizeof(float)); printf("\n"); printf("Size of the double array is: %lu", sizeof(d)/sizeof(double)); printf("\n"); return 0; } We can also find the length of an array by using pointer arithmetic.
🌐
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.
🌐
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.
🌐
Code with C
codewithc.com › code with c › c++ tutorial › c++ array length: determining size in c++ arrays
C++ Array Length: Determining Size In C++ Arrays - Code With C
January 11, 2024 - 🚀 · Determining the length of an array is a fundamental aspect of handling arrays in C++. One common approach is utilizing the sizeof() function. This nifty function enables us to grasp the size of an array in bytes.
🌐
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 - The size or length of the array here is equal to the total number of elements in it - which is 5. There are a few methods through which we can determine the length of an array in C++ language.
🌐
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 - ... 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.
🌐
Quora
quora.com › How-is-the-length-of-an-array-in-C-determined
How is the length of an array in C determined? - Quora
Finally, we can subtract the pointer to the first element to get the length of the array: *(&arr + 1) - arr. (Remember this is pointer arithmetic; Subtraction gives the total number of objects between them). Reference : Anders Kaseorg's answer to In C or C++, what are your favorite pointer tricks?