Compare Two Arrays in C
To compare two arrays in C, we need to check if they have the same size and if all corresponding elements are equal. Since C does not have built-in functions for array comparison, we must manually compare elements using loops.
In this tutorial, we cover different methods for comparing two arrays with examples.
Examples of Comparing Two Arrays
1. Comparing Two Arrays Element by Element
In this example, we compare two integer arrays element by element using a for
loop. If all elements match, we consider the arrays equal; otherwise, they are different.
main.c
#include <stdio.h>
int compareArrays(int arr1[], int arr2[], int size) {
for (int i = 0; i < size; i++) {
if (arr1[i] != arr2[i]) {
return 0; // Arrays are not equal
}
}
return 1; // Arrays are equal
}
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {1, 2, 3, 4, 5};
int size = sizeof(arr1) / sizeof(arr1[0]);
if (compareArrays(arr1, arr2, size)) {
printf("Arrays are equal\n");
} else {
printf("Arrays are not equal\n");
}
return 0;
}
Explanation:
- We define a function
compareArrays()
that takes two arrays and their size as input. - Using a
for
loop, we compare corresponding elements of both arrays. - If any element is different, we return
0
, indicating the arrays are not equal. - If the loop completes without finding differences, we return
1
, meaning the arrays are equal. - In
main()
, we declare two integer arrays and determine their size usingsizeof()
. - We call
compareArrays()
and print whether the arrays are equal or not.
Output:
Arrays are equal
2. Comparing Two Arrays with Different Sizes
In this example, we first check if the arrays have the same size. If the sizes are different, we immediately conclude they are not equal; otherwise, we proceed with an element-by-element comparison.
main.c
#include <stdio.h>
int compareArrays(int arr1[], int size1, int arr2[], int size2) {
if (size1 != size2) {
return 0; // Arrays are not equal due to different sizes
}
for (int i = 0; i < size1; i++) {
if (arr1[i] != arr2[i]) {
return 0; // Arrays are not equal
}
}
return 1; // Arrays are equal
}
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {1, 2, 3, 4};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int size2 = sizeof(arr2) / sizeof(arr2[0]);
if (compareArrays(arr1, size1, arr2, size2)) {
printf("Arrays are equal\n");
} else {
printf("Arrays are not equal\n");
}
return 0;
}
Explanation:
- The function
compareArrays()
takes two arrays and their respective sizes. - We first check if the sizes of the arrays are different. If they are, we return
0
immediately. - If the sizes match, we compare elements using a
for
loop. - If any corresponding elements differ, we return
0
. - If all elements match, we return
1
. - In
main()
, we create two arrays of different sizes and pass them tocompareArrays()
. - The function determines that the arrays are not equal due to the size mismatch.
Output:
Arrays are not equal
Conclusion
In this tutorial, we explored different methods to compare two arrays in C:
- Element-by-element comparison: We used a loop to check if all corresponding elements are equal.
- Size check before comparison: We ensured that arrays of different sizes are immediately marked as unequal.
Since C does not have built-in array comparison functions, we rely on loops to check equality manually.