memcmp() in C++

memcmp(): This function is available in the file string.h. This function Compares the first num bytes of the block of memory pointed by (first pointer) to the first num bytes pointed by (second pointer), returning zero if they all match or a value different from zero representing which is greater if they do not.

Return Values:

Case-1. If both are the same then it returns 0 (Zero).

Case -2. If the first byte does not match in both memory and has less value in the first memory block then it will return (less than Zero) means any negative value.

Case-3. If the first byte does not match in both memory blocks and has more value in the first memory block then it will returns (greater than Zero) means any positive value.

Syntax:

int memcmp(const void *_Buf1, const void *_Buf2, size_t _Size)

Parameters: Three parameters are required for this function.

_Buf1: Pointer to block of memory.

_Buf2:Pointer to block of memory.

_Size: Number of bytes to compare.

For Example:

_Buf1 = "Hello"

_Buf2= "Abc"

memcmp(_Buf1,_Buf2,sizeof(_Buf1))  = It will returns positive value.

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    char _Buf1[] = "Hello";
    char _Buf2[] = "Abc";

    int val = memcmp(_Buf1_Buf2sizeof(_Buf1));

    cout << val << "\n";

    return 0;
}


No comments:

Post a Comment