How to Check if Empty Array in C?
Last Updated :
09 Mar, 2023
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;
}
OutputSize 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;
}
OutputSize 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(arr2));
return 0;
}
OutputSize 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
Similar Reads
How to Empty a Char Array in C? Prerequisite: Char Array in C Char arrays are used for storing multiple character elements in a continuous memory allocated. In a few questions, we have the necessity to empty the char array but You canât clear a char array by setting the individual members to some value indicating that they donât c
4 min read
How to check for empty string in PHP ? In this article, we will see how to check for empty string in PHP. String is a set of characters. A string is said to be empty, if it contains no characters. We can use empty() function to check whether a string is empty or not.here we have some common approachesTable of ContentUsing empty() functio
4 min read
Check if an array is empty or not in JavaScript These are the following ways to check whether the given array is empty or not:1. The length Property - mostly usedThe length property can be used to get the length of the given array if it returns 0 then the length of the array is 0 means the given array is empty else the array have the elements.Jav
1 min read
Check if an Array is Empty or not in TypeScript In TypeScript, while performing any operation on the array, it is quite important that there should be elements or data in the array, else no operation can be done. We can check whether the array contains the elements or not by using the below-listed methods: Table of Content Using length PropertyUs
2 min read
How to check if an Array contains a value or not? There are many ways for checking whether the array contains any specific value or not, one of them is: Examples: Input: arr[] = {10, 30, 15, 17, 39, 13}, key = 17Output: True Input: arr[] = {3, 2, 1, 7, 10, 13}, key = 20Output: False Approach: Using in-built functions: In C language there is no in-b
3 min read