Open In App

How to Find the Size of an Array in C?

Last Updated : 01 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C, the size of the array is the number of elements present in the array. In this article, we will learn how to find the size of an array in C.

Example:

Input: int arr[]={1, 2, 3, 4, 5};
Output: Size of the array is: 5

Size of an Array in C

The size of the array, also known as the length can be determined using the sizeof operator. The sizeof operator returns the size of the object in bytes. We can divide the size of the whole array in bytes by the size of each element in bytes to get the number of elements in the array.

Syntax to Find the Size of an Array in C

Use the below syntax to find the size of an array:

size_t arraySize = sizeof(arrayName) / sizeof(arrayName[index]);

Here,

  • arrayName is the name of the array.
  • sizeof(arrayName) returns the total size of array in bytes.
  • sizeof(arrayName[index]) returns the size of one element of array.

C Program to Find the Size of an Array

The below example demonstrate how we can find the size of a given array in C.

C




// C program to illustrate how to find size of an array
#include <stdio.h>
  
int main()
{
  
    // initializing an array of 5 elements
    int myArray[] = { 1, 2, 3, 4, 5 };
  
    // finding size of an array
    int arraySize = sizeof(myArray) / sizeof(myArray[0]);
  
    // printing the size of an array
    printf("The size of the array is: %d\n", arraySize);
  
    return 0;
}


Output

The size of the array is: 5

Time Complexity: O(1)
Space Complexity: O(1)

Time complexity: O(1)
Auxilliary Space: O(1)


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads