memcmp() function in C
×


memcmp() function in C

253

 The memcmp() function in C, available in the header, is used to compare two memory blocks.

 It compares the first n bytes of the memory block pointed to by ptr1 with the first n bytes of the memory block pointed to by ptr2.

Syntax of memcmp() function in C:

#include<string.h>
int memcmp(const void *ptr1, const void *ptr2, size_t n);

 ptr1: Pointer to the first memory block.

 ptr2: Pointer to the second memory block.

 n: Number of bytes to compare.

Return Value:

 Returns an integer less than, equal to, or greater than zero if the first n bytes of ptr1 are found, respectively, to be less than, equal to, or greater than the first n bytes of ptr2.

Implementation:

The memcmp() function typically performs a byte-by-byte comparison of the memory blocks pointed to by ptr1 and ptr2.

It compares each byte in the memory blocks until either a difference is found or n bytes have been compared.

It then returns the result of the comparison based on the byte values encountered.

Example:

Program 1: Basic Usage

// Program for basic usage
#include<stdio.h> 
#include<string.h> 

int main() {
    char str1[] = "hello";
    char str2[] = "world";
    int result = memcmp(str1, str2, 5); // Compare first 5 characters
    if (result == 0) {
        printf("The strings are equal.\n");
    } else if (result < 0) {
        printf("The first string is less than the second.\n");
    } else {
        printf("The first string is greater than the second.\n");
    }
    return 0;
}	

Output:

The first string is less than the second.

Program 2: Using memcmp() for Array Comparison

// program for using memcmp() for array comparison
#include<stdio.h> 
#include<string.h> 

int main() {
    int arr1[] = {1, 2, 3};
    int arr2[] = {1, 2, 4};
    int result = memcmp(arr1, arr2, 3 * sizeof(int)); // Compare entire arrays
    if (result == 0) {
        printf("The arrays are equal.\n");
    } else if (result < 0) {
        printf("The first array is less than the second.\n");
    } else {
        printf("The first array is greater than the second.\n");
    }
    return 0;
}	

Output:

The first array is less than the second.

Program 3: Using memcmp() for Struct Comparison

// program for using memcmp() for struct comparison 
#include<stdio.h> 
#include<string.h> 

struct Point {
    int x;
    int y;
};

int main() {
    struct Point p1 = {1, 2};
    struct Point p2 = {1, 3};
    int result = memcmp(&p1, &p2, sizeof(struct Point)); // Compare entire structs
    if (result == 0) {
        printf("The structs are equal.\n");
    } else if (result < 0) {
        printf("The first struct is less than the second.\n");
    } else {
        printf("The first struct is greater than the second.\n");
    }
    return 0;
}

Output:

The first struct is less than the second.

These examples showcase the versatility of memcmp() in comparing strings, arrays, and structs, providing insight into its implementation and usage.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments