wmemcmp() function in C/C++

The wmemcmp() function in C/C++ compare two wide characters. This function compares the first num wide characters of two wide characters pointed by str1 and str2, returns zero if both the strings are equal or different value from zero if they are not.

Syntax:

int wmemcmp (const wchar_t* str1, const wchar_t* str2, size_t num);

Parameters:

Return value: This function returns three different values which defines the relationship between two strings:



Below programs illustrate the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// wmemcmp() function
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialize two strings
    wchar_t str1[] = L"geekforgeeks";
    ;
    wchar_t str2[] = L"geekforgeeks";
  
    // this function will compare these two strings
    // till 12 characters, if there would have been
    // more than 12 characters, it will compare
    // even more than the length of the strings
    int print = wmemcmp(str1, str2, 12);
  
    // print if it's equal or not equal( greater or smaller)
    wprintf(L"wmemcmp comparison: %ls\n",
            print ? L"not equal" : L"equal");
  
    return 0;
}
chevron_right

Output:
wmemcmp comparison: equal

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// wmemcmp() function
// Comparing two strings with the same type of function
// wcsncmp() and wmemcmp()
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialize two strings
    wchar_t str1[] = L"geekforgeeks";
    ;
    wchar_t str2[] = L"geekforgeeks";
  
    // wcsncmp() function compare characters
    // until the null character is encountered ('\0')
    int first = wcsncmp(str1, str2, 20);
  
    // but wmemcmp() function compares 20 characters
    // even after encountering null character
    int second = wmemcmp(str1, str2, 20);
  
    // print if it's equal or not equal( greater or smaller)
    wprintf(L"wcsncmp comparison: %ls\n",
            first ? L"not equal" : L"equal");
    wprintf(L"wmemcmp comparison: %ls\n",
            second ? L"not equal" : L"equal");
  
    return 0;
}
chevron_right

Output:
wcsncmp comparison: equal
wmemcmp comparison: not equal

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai

Article Tags :
C
Practice Tags :