Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Check if Empty Array in C?

Improve Article
Save Article
  • Last Updated : 23 Dec, 2022
Improve Article
Save Article

Prerequisite: Array in C

The array is a type of Data-structure that can store homogeneous data-type elements. The size of the array is fixed.

Syntax:

int arr[N];

here, the data type of the array is an integer, the name of an array is arr, and the size of the array is N.

Example:

C




// C Program to declare an
// array and check it's size
#include <stdio.h>
 
int main()
{
    int arr[5] = { 1, 2, 3, 4, 5 };
 
    printf("Size of the array: %d bytes\n", sizeof(arr));
 
    return 0;
}


Output

Size of the array: 20 bytes

In the above program, we have created an array with some elements in it. This is the most common way of creating an array. Then, we used the sizeof() operator to print the size of the array. To know more about the sizeof() operator refer to sizeof() in C. 

The size of a data type varies from machine to machine. On our machine, the int data type consists of 4 bytes and the above array has 5 elements inside it, so the total size of the array is 5 x 4 = 20 bytes.

Now, we will create an array by specifying only its size. We will not insert any elements into it. Look at the below program:

Example:

C




// C Program to check
// above condition
#include <stdio.h>
 
int main()
{
    int arr[5] = {};
 
    printf("Size of the array: %d bytes\n", sizeof(arr));
 
    return 0;
}


Output

Size of the array: 20 bytes

The above output says that the size of the array is 20 bytes.

Reason: The reason is that we have mentioned the size of the array. So, the compiler automatically creates an array that can hold 5 values, despite the fact that we haven’t declared any values yet. In this case, some pre-determined values are put into the array to fill its positions. That’s why the output shows that the array has a size of 20 bytes. It’s just the fact that we haven’t inserted any values yet.

Empty Array:

An array can be said only when the size of the array is 0. And it is only possible during initialization. So, let’s check how to check when an array is empty.

Example:

C




// C Program to check array is
// empty or not
#include <stdio.h>
 
int main()
{
    // Method1 to declare empty array
    int arr1[] = {};
 
    // Method2 to declare empty array
    int arr2[0];
 
    printf("Size of the array1: %d bytes\n", sizeof(arr1));
    printf("Size of the array2: %d bytes\n", sizeof(arr1));
 
    return 0;
}


Output

Size of the array1: 0 bytes
Size of the array2: 0 bytes

Method 1: Here, we have declared an array but haven’t specified its size or inserted any elements into it. This means that the compiler doesn’t know either the size of the array or its elements. So, it can’t use any of them to make the array have a particular size. As a result, the array hasn’t got any space to contain elements, which is why the above output shows that the size of the array is 0 bytes.

Method 2: As the size of the array is already 0 so no memory is allocated to it. So, it is also empty


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!