Table of contents
1.
Introduction
2.
Using sizeof() Operator
2.1.
Example
2.2.
C
3.
Using Pointer Arithmetic
3.1.
Example
3.2.
C
4.
Using Loop
4.1.
Example
4.2.
C
5.
Frequently Asked Questions
5.1.
Can I use the sizeof() operator to find the length of an array passed as a function argument?
5.2.
Is it necessary to use a null terminator character when finding the length of a string using a loop?
5.3.
What is the time complexity of finding the length of an array using the sizeof() operator or pointer arithmetic?
5.4.
How to find the length of an array in C without using sizeof?
6.
Conclusion 
Last Updated: Jan 18, 2025
Easy

How to Find Length of Array in C

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Arrays are a basic but one of the most important concept in programming. It allows you to store & manipulate multiple values of the same data type. In C, arrays are widely used to efficiently handle collections of data. One important aspect of working with arrays is determining their length or size. Knowing the length of an array is crucial for performing various operations, such as iterating over its elements or allocating memory dynamically. 

How to Find Length of Array in C

In this article, we will discuss different methods to find the length of an array in C, including the use of the sizeof() operator, pointer arithmetic & loops. 

Using sizeof() Operator

The sizeof() operator in C is a powerful tool that allows you to determine the size of a data type or an expression in bytes. When applied to an array, sizeof() returns the total size of the array in bytes. To find the length of an array using sizeof(), you can divide the total size of the array by the size of a single element.

Here's the syntax to find the length of an array using sizeof():

int length = sizeof(array) / sizeof(array[0]);


Let's understand this expression:

  • sizeof(array) returns the total size of the array in bytes.
     
  • sizeof(array[0]) returns the size of a single element in the array.
     
  • Dividing the total size by the size of a single element gives us the number of elements in the array, which represents its length.

Example

  • C

C

#include <stdio.h>

int main() {

   int numbers[] = {1, 2, 3, 4, 5};

   int length = sizeof(numbers) / sizeof(numbers[0]);

   printf("Length of the array: %d\n", length);

  

   return 0;

}
You can also try this code with Online C Compiler
Run Code

 

Output

Length of the array: 5


In this example, we have an integer array numbers containing five elements. By using the sizeof() operator, we calculate the total size of the array and divide it by the size of a single element to obtain the length of the array. The resulting length is then printed to the console.

The sizeof() operator provides a convenient & reliable way to determine the length of an array in C. It eliminates the need for manually counting the elements & ensures that the length is calculated accurately, regardless of any modifications made to the array.

Using Pointer Arithmetic

Pointer arithmetic is another technique that can be used to find the length of an array in C. In C, an array name is essentially a pointer to the first element of the array. By subtracting the pointer to the first element from the pointer to the element beyond the last element, we can determine the length of the array.

Here's the syntax to find the length of an array using pointer arithmetic:

int length = &array[size] - array;


Let's understand how this works:

  • array represents the pointer to the first element of the array.
     
  • &array[size] represents the pointer to the element beyond the last element of the array, where size is the total number of elements in the array.
     
  • Subtracting the pointer to the first element from the pointer to the element beyond the last element gives us the length of the array.

Example

  • C

C

#include <stdio.h>

int main() {

   char str[] = "Hello, World!";

   int length = &str[sizeof(str)] - str;

   printf("Length of the string: %d\n", length);

   return 0;

}
You can also try this code with Online C Compiler
Run Code

 

Output

Length of the string: 13


In this example, we have a character array str containing the string "Hello, World!". By subtracting the pointer to the first element (str) from the pointer to the element beyond the last element (&str[sizeof(str)]), we calculate the length of the array. The resulting length includes the null terminator character ('\0') at the end of the string.

Pointer arithmetic provides a low-level approach to finding the length of an array. It relies on the relationship between pointers & array elements. However, it's important to note that this method may not work accurately if the array decays to a pointer in certain contexts, such as when passing an array to a function.

Using Loop

Another approach to finding the length of an array in C is by using a loop to iterate over the elements of the array until a specific condition is met. This method is particularly useful when the array contains a sentinel value or a terminating character that indicates the end of the array.

Example

  • C

C

#include <stdio.h>

int main() {

   char str[] = "Hello, World!";

   int length = 0;

  

   while (str[length] != '\0') {

       length++;

   }  

   printf("Length of the string: %d\n", length);

  

   return 0;

}
You can also try this code with Online C Compiler
Run Code

 

Output

Length of the string: 13


In this example, we have a character array str containing the string "Hello, World!". We initialize a variable length to 0, which will keep track of the length of the string. We then use a while loop to iterate over the characters of the string until we encounter the null terminator character ('\0'). For each character, we increment the length variable.

By the end of the loop, the length variable will hold the length of the string, excluding the null terminator. The resulting length is then printed to the console.

Using a loop to find the length of an array is particularly useful when the array doesn't have a fixed size or when the size is not known beforehand. It allows you to dynamically determine the length based on the actual content of the array.

However, it's important to note that this method relies on the presence of a sentinel value or a terminating character to indicate the end of the array. If the array doesn't have such an indicator, using a loop may result in accessing elements beyond the array's bounds, leading to undefined behavior.

Frequently Asked Questions

Can I use the sizeof() operator to find the length of an array passed as a function argument?

No, the sizeof() operator will return the size of the pointer instead of the array when used inside a function.

Is it necessary to use a null terminator character when finding the length of a string using a loop?

Yes, the null terminator character ('\0') is used to mark the end of a string and is essential for determining its length using a loop.

What is the time complexity of finding the length of an array using the sizeof() operator or pointer arithmetic?

The time complexity of finding the length of an array using sizeof() or pointer arithmetic is O(1) since it involves a constant-time operation.

How to find the length of an array in C without using sizeof?

To find the length of an array in C without using sizeof, you can iterate through the array with a loop, counting the elements until you reach the array's end (null).

Conclusion 

In this article, we learned three methods to find the length of an array in C: using the sizeof() operator, pointer arithmetic & loops. The sizeof() operator provides a simple way to calculate the length, while pointer arithmetic allows us to determine the length by subtracting pointers. Loops can be used to find the length of arrays with sentinel values or terminating characters. Learning all these techniques enables you to efficiently work with arrays in C.

Live masterclass