Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

How to Find the Length of an Array in C

In this blog, we need the length of arrays for a variety of actions, including iterating over the array, copying the array, and more. In this post, we’ll look at the many ways to obtain an C array length.

The C array length is fixed and is stated in the declaration of the array. There is no built-in way to determine an array’s size in C. By using C’s built-in techniques or pointers, getting the array’s length requires a little bit of work. There are several approaches to determining C array length.
Two methods will be examined in this article:

  • Using the sizeof() operator.
  • Using pointer arithmetic.

Programming Logic to Calculate the Length of an Array in C

  • Use the sizeof operator, such as sizeof, to determine an array’s size (arr).
  • Using the sizeof operator, determine the size of a datatype value (datatype).
  • Divide the overall array size by the size of one of the datatypes you are using to get the length of the array (the number of elements).

Thus, the reasoning would be as follows:

  • The size of the array divided by the size of the datatype you are using to describe it gives the C array length.
  • The next section, titled "Using sizeof()," provides a more programmatic elaboration of the concept.

1. Using sizeof()

I’d want to ask you a query. Could you please provide the length of the following array?

//C sizeof array

int arr[] = {19,25,10,3,1,8,22,17,7,84,9,19,25,10,3,1,
             8,22,17,7,84, 9,19,25,10,3,1,8,22,17,7,84
            ,9,19,25,10,3,1,8,22,17,7,84,9,19,25,10,33,
             1,8,22,17,7,84,9,19,25,10,3,1,8,22,17,7,84,
             9,19,25,10,3,1,8,22,17,7,84,9,11,23,45,5,3};

You may respond to this question after spending some time on it, but if the array you get is 10 times as large as the one above, it will be challenging and time-consuming. Instead, consider why you would need to manually count the elements when the C language has the sizeof() operator.

So let me show you how to count C Array length of elements using the sizeof() operator.

In C, the sizeof() operator determines the size in bytes of any provided variables or datatypes. Since sizeof() cannot determine the array’s size directly, we must apply the programming logic outlined above to determine its length.

Syntax to calculate C array length using sizeof():

datatype arr_size = sizeof(name_of_array)/sizeof(name_of_array[index]);

In the above syntax:

  • arr_size: A variable used to hold an array’s size.
  • sizeof(name_of_array): In C sizeof array we are measuring the size of was given a straightforward name.
  • sizeof(name_of_array[index]): The size of a single element of the array.

Let’s now examine why this algorithm for determining an array’s length is effective:

We divide the size of one array element by the number of array elements to determine the size of the entire array. We then divide the size of the entire array, sizeof(name of the C array), by the size of a single array element, sizeof(name of array[index]), to determine the length of the array. Consequently, we are given the array’s length.

Using sizeof(), a C program may determine the length of an integer array
Let’s now have a look at a C program that uses the sizeof() operator to determine the length of an integer array.

Calculate the length of an array using the sizeof() operator in C language.
Below is the code Implementation:

#include 
int main() {
    //simple C sizeof array
    int arr[] = {19,25,10,3,1,8,22,17,7,84,9,19,25,10,3,1,
             8,22,17,7,84, 9,19,25,10,3,1,8,22,17,7,84,
             9,19,25,10,3,1,8,22,17,7,84,9,19,25,10,33,
             1,8,22,17,7,84,9,19,25,10,3,1,8,22,17,7,84,
             9,19,25,10,3,1,8,22,17,7,84,9,11,23,45,5,3,4};
    //using the sizeof() operator to get the length of the array
    printf(" The length of int array is : %d ", sizeof(arr) / sizeof(arr[0]));
}

Output:

The length of the int array is: 83

C Program to Calculate the Length of char Array Using sizeof()
Let’s now have a look at a C program that uses the sizeof() operator to determine the length of a char array.

#include <stdio.h>
int main() {
    //simple C sizeof array
    char arr[] = {'d', 'd', 'c', 'a', 'v', 'b', 's', 'b', 'z', 'v'};

    //using the sizeof() operator to get the length of the char array
    printf(" The length of char array is : %d", sizeof(arr) / sizeof(arr[0]));
}

2. Using Pointer Arithmetic

Using pointer arithmetic as a solution in C to get the length of an array. In comparison to the sizeof() operator, the approach is straightforward.
The length of an array is calculated using the following expression:

datatype size_variable = *(&array_name + 1) - array_name;
  • Size_variable: Used to keep track of an array’s size.
  • &array_name: It returns a pointer
  • (&array_name + 1)= address of elements ahead.
  • (&array_name + 1) – array_name= The length of the array is determined by the difference in the address of the elements ahead of it.

Let’s look at some C programs to grasp this better:

Calculate the Length of int Array Using Pointer Arithmetic in C language
Below is the code Implementation

#include <stdio.h>

int main() {
   // simple array 
   int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
   int arr_length = *(&arr + 1) - arr; // using pointer arithmetic
   printf("The length of the integer array is: %d", arr_length);
   return 0;
}

Output

The length of the int array is: 9

Calculate the Length of char Array Using Pointer Arithmetic in C language
Below is the code Implementation

#include <stdio.h>

int main() {
   // simple array
   char arr[] = {'a', 'b', 'b', 'v', 'c', 'f'};
   int arr_length = *(&arr + 1) - arr; // using pointer arithmetic.
   printf("The length of the character array is: %d", arr_length);
   return 0;
}

Output

The length of the char array is: 6

Conclusion
This blog taught you the understanding of how to find the size of an array in C programming language using sizeof operator.

Leave a Reply

Your email address will not be published. Required fields are marked *