What is wmemcmp in C?

The wmemcmp function compares the characters of two wide strings, until a specific number of characters or a character mismatch. It is defined in the wchar.h header file.

Parameters

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

In the above snippet of code, str1 and str2 are the pointers to wide strings that will be compared. The strings will be compared for the count number of characters. If a character mismatch occurs before this, the function stops.

Return value

If both the strings are the same till the specified number of characters, the function returns 0.

A positive number is returned if the character from str1 is greater than the corresponding character of str2 in case of a mismatch. A negative number is returned if the character from str1 is lesser than the corresponding character of str2.

Difference between wmemcmp and wcsncmp

The primary difference between these functions is that wcsncmp compares two wide strings, whereas wmemcmp compares two memory blocks. In simple words, wcsncmp functions stop the comparison when it reads a null wide character, but wmemcmp continues the comparison until it compares the count number of wide characters.

Example 1

#include<stdio.h>
#include<wchar.h>
void main() {
// declaing and initializing two same wide strings
wchar_t str1[] = L"HELLO WORLD";
wchar_t str2[] = L"HELLO WORLD";
// calling the function and storing the results in variables
int returnedVal0 = wmemcmp(str1, str2, 11);
int returnedVal1 = wmemcmp(str1, str2, 15);
// checking if the strings were found to be same or not
printf("RESULT AFTER COMPARING 11 CHARACTERS: \n");
if(returnedVal0 == 0) {
printf("Both strings are the same");
}
else {
printf("Strings are not same!");
}
printf("\n\nRESULT AFTER COMPARING 15 CHARACTERS: \n");
if(returnedVal1 == 0) {
printf("Both strings are the same");
}
else {
printf("Strings are not same!");
}
}

In the above snippet of code, we compare two same strings using the wmemcmp function twice. We’re calling the function once for 11 characters and then for 15 characters. It is clear by the output that the strings are same for the first 11 characters but wmemcmp finds a mismatch when 15 characters are compared. This is because it continues the comparison even after the null wide character.

Example 2

#include<stdio.h>
#include<wchar.h>
void main() {
// declaing and initializing two same wide strings
wchar_t str1[] = L"HELLO WORLD";
wchar_t str2[] = L"HELLO WORLD";
// calling the function and storing the results in variables
int returnedVal0 = wcsncmp(str1, str2, 15);
int returnedVal1 = wmemcmp(str1, str2, 15);
// checking if the strings were found to be same or not
printf("RESULT AFTER COMPARING WITH wcsncmp: \n");
if(returnedVal0 == 0) {
printf("Both strings are the same");
}
else {
printf("Strings are not same!");
}
printf("\n\nRESULT AFTER COMPARING wmemcmp: \n");
if(returnedVal1 == 0) {
printf("Both strings are the same");
}
else {
printf("Strings are not same!");
}
}

In the above snippet of code, we compare the same strings using wcsncmp and wmemcmp functions. It is evident by the output that wcsncmp stops the comparison when the null wide character is encountered and finds the strings to be the same. On the other hand, wmemcmp continues the comparison even after null wide characters and finds the strings to be different.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved