Length of Array in C Last Updated : 17 Jan, 2025 Comments Improve Suggest changes Like Article Like Report The Length of an array in C refers to the maximum number of elements that an array can hold. It must be specified at the time of declaration. It is also known as the size of an array that is used to determine the memory required to store all of its elements.In C language, we don't have any pre-defined function to find the length of the array instead, you must calculate the length manually using techniques based on how the array is declared and used.The simplest method 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. Take a look at the below example: C #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; } Output5Explanation: 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 = 5Using Pointer Arithmetic TrickWe can also calculate the length of an array in C using pointer arithmetic. This solution of using a pointer is just a hack that is used to find the number of elements in an array. C #include <stdio.h> int main(){ int arr[5] = { 1, 2, 3, 4, 5 }; // Find the size of array arr int n = *(&arr + 1) - arr; printf( "%d", n); return 0; } Output5Explanation: In the above code, in the expression: *(&arr + 1) - arr; is the main logic. Here is how it works,&arr gives the address of the entire array.&arr + 1 moves the pointer to the address just after the end of the array arr.*(&arr + 1) dereferences this pointer to get the memory location just after the array.Subtracting the base address of the array (arr) from this value gives the total number of elements in the array, as pointer arithmetic works based on the size of the elements (e.g., int in this case).Note: Please note that these methods only works when the array is declared in the same scope. These methods will fail if we try them on an array which is passed as a pointer. This happens due to Array Decay. So, it is recommended to keep a variable that tracks the size of the array.For more insights into array handling and other key data structures in C, our C programming course offers comprehensive lessons on arrays, pointers, and memory management. Comment More infoAdvertise with us Next Article Multidimensional Arrays in C - 2D and 3D Arrays kamleshjoshi18 Follow Improve Article Tags : C Language c-array Similar Reads C Arrays An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It can be used to store the collection of primitive data types such as int, char, float, etc., as well as derived and user-defined data types such as pointers, structures, etc. Creating an Array in 7 min read Properties of Array in C An array in C is a fixed-size homogeneous collection of elements stored at a contiguous memory location. It is a derived data type in C that can store elements of different data types such as int, char, struct, etc. It is one of the most popular data types widely used by programmers to solve differe 8 min read Length of Array in C The Length of an array in C refers to the maximum number of elements that an array can hold. It must be specified at the time of declaration. It is also known as the size of an array that is used to determine the memory required to store all of its elements.In C language, we don't have any pre-defin 3 min read Multidimensional Arrays in C - 2D and 3D Arrays A multi-dimensional array in C can be defined as an array that has more than one dimension. Having more than one dimension means that it can grow in multiple directions. Some popular multidimensional arrays include 2D arrays which grows in two dimensions, and 3D arrays which grows in three dimension 8 min read Initialization of Multidimensional Array in C In C, multidimensional arrays are the arrays that contain more than one dimensions. These arrays are useful when we need to store data in a table or matrix-like structure. In this article, we will learn the different methods to initialize a multidimensional array in C. The easiest method for initial 4 min read Jagged Array or Array of Arrays in C with Examples Prerequisite: Arrays in CJagged array is array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D array but with a variable number of columns in each row. These type of arrays are also known as Jagged arrays. Example:arr[][] = { {0, 1, 2}, {6, 4}, {1, 7, 6, 8, 9}, 3 min read Pass Array to Functions in C Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C.In C, arrays are always passed to function as pointers. They cannot be passed by value because of the array decay due to which, whe 3 min read How to pass a 2D array as a parameter in C? A 2D array is essentially an array of arrays, where each element of the main array holds another array. In this article, we will see how to pass a 2D array to a function.The simplest and most common method to pass 2D array to a function is by specifying the parameter as 2D array with row size and co 3 min read How to pass an array by value in C ? In C programming, arrays are always passed as pointers to the function. There are no direct ways to pass the array by value. However, there is trick that allows you to simulate the passing of array by value by enclosing it inside a structure and then passing that structure by value. This will also p 2 min read Variable Length Arrays (VLAs) in C In C, variable length arrays (VLAs) are also known as runtime-sized or variable-sized arrays. The size of such arrays is defined at run-time.Variably modified types include variable-length arrays and pointers to variable-length arrays. Variably changed types must be declared at either block scope or 2 min read Like