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

Last Updated on June 8, 2023 by Mayank Dham

This blog discusses the length of arrays for a number of operations, such as iterating over the array, copying the array, and more. The many methods for obtaining a C array length will be discussed in this post.
The declaration of the array specifies the array’s fixed length in C. The size of an array cannot be calculated automatically in C. Obtaining the array’s length involves a little amount of work using pointers or built-in C methods. The length of a C array may be calculated using a variety of methods.

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
In conclusion, finding the length of an array in C is a fundamental operation that allows you to determine the number of elements present in an array. The length of an array is crucial for efficient array manipulation and traversal. By understanding the various methods to find the length of an array in C and considering some frequently asked questions (FAQs) on the topic, you can confidently work with arrays in your C programs.

Frequently Asked Questions (FAQs) related to How to Find the Length of an Array in C:

Q1. How can I find the length of an array in C?
There are a few methods to find the length of an array in C:
Method 1: If you know the number of elements in advance, you can simply use that value.
Method 2: You can divide the total size of the array by the size of one element. For example, if you have an array of integers with 20 elements, you can find the length by dividing the total size (in bytes) by the size of one integer element (typically 4 bytes).
Method 3: For null-terminated strings (character arrays), you can use the strlen() function from the standard library. It returns the length of the string excluding the null terminator.

Q2. Can I use the sizeof() operator to find the length of an array in C?
Yes, the sizeof() operator can be used to find the total size of an array in bytes. However, to determine the length (number of elements), you need to divide the total size by the size of one element. For example, if you have an array of integers with a total size of 40 bytes, and each integer occupies 4 bytes, the length of the array would be 40 / 4 = 10 elements.

Q3. Why is it important to find the length of an array?
Finding the length of an array is important for various reasons, such as:
Iterating over the elements of the array using loops.
Performing array manipulation operations, like sorting or searching.
Avoiding accessing elements beyond the array boundaries, which can lead to memory corruption and unexpected behavior.

Q4. Can I pass an array as a function argument without explicitly specifying its length?
No, you cannot pass an array as a function argument without explicitly specifying its length. In C, arrays decay into pointers when passed to functions. Therefore, you need to pass the length of the array as a separate parameter to enable proper array processing within the function.

Q5. Is the length of an array dynamically adjustable in C?
No, the length of an array in C is fixed at the time of declaration. Once an array is created, its length cannot be changed dynamically. If you require a dynamic length, you should consider using dynamic memory allocation functions like malloc() and realloc() to work with arrays.

Leave a Reply

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