MEMCMP()

this function is used to compare the contents of two arrays. it compares the arrays byte by byte and helps determine whether they are identical or different. this is useful when dealing with arrays of primitive data types such as int, char, or float.

int memcmp(const void *arr1, const void *arr2, size_t size);

 * arr1 & arr2 are the arrays being compared
 * size is the number of bytes to compare (usually sizeof(array))
    - a return value of 0 means the arrays are equal
    - negative values represent arr1 is less than arr2
    - positive values represent arr1 is greater then arr2 (based on first differing byte)
#include <stdio.h>
#include <string.h>

int main() {
    
    // Ttwo array to store 5 characters
    char str1[5];  
    char str2[5]; 

    // Input 5 characters for the first array here
    for (int i = 0; i < 5; i++){
        scanf(" %c", &str1[i]);
    }
    // Input 5 characters for the second array here

    for (int i = 0; i < 5; i++){
        scanf(" %c", &str2[i]);
    }

    


    // Compare the two arrays (5 characters each) here
   if (memcmp(str1, str2, sizeof(str1)) == 0){
        printf("Both character arrays are equal\n");  // All elements match
    } else {
        printf("The character arrays are different\n");  // At least one mismatch
    }

    return 0;  
}

Last updated