C program to check two arrays is equal in size or not

Ans: 

In this tutorial, you will learn how to write C program to compare two arrays and check that they are equal in size or not.

Below are the approach which we will be follow to achieve our goal:

  • Our approach is to check two given arrays are equal in size or not is first we will find the size of both array.
  • And then compare the size. If the size are equal then it will print “size of both arrays are equal” and if the size is not equal then it will print “size of arrays are not equal”.

As we have already seen above our logic to check the size of given two arrays is equal or not.

In this program we have already two arrays. After the execution of the program it will print the output as per array size after the calculation.

Program to check the size of given two arrays are equal or not?

#include<stdio.h>
#include<conio.h>
int main(){
    int arr1[]={1,2,5,3,4,5};
    int arr2[]={2,3,1,9,5};
    int size1=sizeof(arr1)/sizeof(arr1[0]);
    int size2=sizeof(arr2)/sizeof(arr2[0]);
    if(size1 == size2){
        printf("size of both arrays are equal");
    }else{
        printf("size of arrays are not equal");
    }
    getch();    
} 

Output:

compare two arrays in c

Explanation of the above

  • In the above program we have two arrays arr1 and arr2 of type integer.
  • Elements in arr1 are 1, 2, 5, 3, 4, and 5.
  • Elements in arr2 are 2, 3, 1, 9, and 5.
  • Now we have calculated the size of both array separately. 
  • size1 variable has hold the size of array arr1 and size2 variable has hold the size of array arr2.
  • If the size of both array is equal which we have checked using if statement then print “size of both arrays are equal” otherwise print “size of arrays are not equal”.

This was the all logic to check whether a size of two arrays are equal or not.

Leave a Comment