Open In App

Length of Array in C

Improve
Improve
Like Article
Like
Save
Share
Report

The Length of an array in C refers to the number of elements in the array. 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 Programming language, we don’t have any pre-defined function to find the length of the array but we can manually determine it by using different methods mentioned below:

  1. Using sizeof() Operator
  2. Using Pointer Arithmetic
  3. Using Loop

1. Using sizeof() Operator

The sizeof operator is a compile-time unary operator that calculates the size of the variables and datatypes. It returns an integer value that represents the size of the expression or a variable in bytes. The sizeof operator is primarily used for dynamic memory allocation but it can also be used to find the length of an array.

The trick is to first find the size of the whole array in bytes and the size of a single element using the sizeof operator and then divide the size of the whole array by the size of a single element so that we can get the number of elements in the array.

Syntax:

data_type size = sizeof(Array_name) / sizeof(Array_name[index]);

In the above syntax,

  • data_type: It is the type of variable in which we want to store the length of the array.(like int, size_t, etc.).
  • Array_name: It is the name of the array you want to find the size of.
  • sizeof(Array_name): It is an operator that returns the size of the entire array in bytes.
  • sizeof(Array_name[index]): It returns the size of a single element in the array in bytes.
  • index: It is the index of any element in the array.

Example:

C




// C Program to calculate size of an array using sizeof()
// operator
#include <stdio.h>
 
int main()
{
 
    int Arr[] = { 1, 2, 3, 4, 5 };
   
    // variable to store size of Arr
    int length = sizeof(Arr) / sizeof(Arr[0]);
 
    printf("The length of the array is: %d\n", length);
 
    return 0;
}


Output

The length of the array is: 5

Time Complexity: O(1) as only constant time operations has been done.

Space Complexity: O(1) as no extra space has been used. 

2. Using Pointer Arithmetic

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

Syntax:

 data_type length = *(&arr + 1) - arr;

In the above syntax:

  • &arr: Pointer to an array of elements.
  • (&arr + 1): Address of memory ahead of the array as pointer type is a pointer to an array of integers.
  • *(&arr + 1) – arr: Inclusive difference between the start and the end of the array

Example:

C




// C Program to calculate size of an array using pointer arithmetic
#include <stdio.h>
 
int main()
{
 
    int Arr[] = { 1, 2, 3, 4, 5, 6 };
    // variable to store the size of Arr
    int length = *(&Arr + 1) - Arr;
 
    printf( "Number of elements in Arr[] is: %d", length);
    return 0;
}


Output

Number of elements in Arr[] is: 6

3. Using Loop

The loop method is used to calculate the length of an array in C. It iterates through all the elements of an array and increments the count.

Example:

C




// C Program to calculate size of an array using loop
#include <stdio.h>
  
int arr_length(int arr[])
{
    int i;
    int count = 0;
    for(i=0; arr[i]!='\0'; i++)
    {
        count++;
    }
    return count;
}
  
int main()
{
    int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int n;
  
    n = arr_length(arr);
    printf("Length of Array is: %d", n);
  
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


Output

Length of Array is: 10

Time Complexity: O(n) where n is the number of elements in the array.

Space Complexity: O(1) as no extra space has been used.

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.

The iterative methods of finding the length of the strings (array of characters) also cannot be applied to the array of other types as there is no end indicator in these array types in contrast to the ‘\0’ NULL character marking the end of the string.



Last Updated : 21 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads