In C, wmemcmp() compares counted wide-character blocks. It belongs to the wide-character tools declared in <wchar.h>.
Use wchar_t for wide characters and prefix wide literals with L, as in L"Hello". The examples below use simple C Programming Tutorial patterns and print wide strings with %ls.
Syntax
The following concrete call shows the argument shape before the formal declaration.
C
wmemcmp(left, right, 4);
Call
What it does
wmemcmp(left, right, 4)
Compares counted wide-character blocks in a typical call.
Include <wchar.h>. Add only the supporting headers actually used by your program.
Library declaration
int wmemcmp(const wchar_t *lhs, const wchar_t *rhs, size_t count);
Parameters
Parameter
Description
lhs
Left-hand value in the comparison.
rhs
Right-hand value in the comparison.
count
Maximum number of wchar_t elements to inspect, copy, append, or transform.
Return Value
Return Value
Description
Negative / 0 / Positive
The left value sorts before, equals, or sorts after the right value.
C Examples
Example 1 — Different values
This example uses wmemcmp() to different values, then exposes the result so you can connect the call with its output.
C
#include <stdio.h>
#include <wchar.h>
int main(void) {
int result = wmemcmp(L"cat", L"car", 3);
wprintf(L"result %ls 0\n", result < 0 ? L"<" : result > 0 ? L">" : L"=");
return 0;
}
Output
result > 0
How It Works
Set up this case. The program prepares the exact input needed to different values.
Process a counted block.wmemcmp(left, right, 4) uses a count of wchar_t elements rather than bytes.
Read the normal result. The displayed output shows the direct result of the call for this input.
Interpret the output.result > 0 confirms the behavior demonstrated by this example.
Example 2 — Equal values
This example uses wmemcmp() to equal values, then exposes the result so you can connect the call with its output.
C
#include <stdio.h>
#include <wchar.h>
int main(void) {
int result = wmemcmp(L"cat", L"cat", 3);
wprintf(L"result %ls 0\n", result < 0 ? L"<" : result > 0 ? L">" : L"=");
return 0;
}
Output
result = 0
How It Works
Choose the edge case. This example changes the input so you can see how wmemcmp() behaves for equal values.
Process a counted block.wmemcmp(left, right, 4) uses a count of wchar_t elements rather than bytes.
Inspect the status or pointer. The program checks what the function returned before relying on output data.
Interpret the output.result = 0 confirms the behavior demonstrated by this example.
Example 3 — Check the sign
This example uses wmemcmp() to check the sign, then exposes the result so you can connect the call with its output.
C
#include <stdio.h>
#include <wchar.h>
int main(void) {
int result = wmemcmp(L"cat", L"car", 3);
wprintf(L"result %ls 0\n", result < 0 ? L"<" : result > 0 ? L">" : L"=");
return 0;
}
Output
result > 0
How It Works
Prepare a practical variation. The data is arranged specifically to check the sign.
Process a counted block.wmemcmp(left, right, 4) uses a count of wchar_t elements rather than bytes.
Notice the safety rule. The example keeps the count, destination capacity, stream state, and terminator requirements visible.
Interpret the output.result > 0 confirms the behavior demonstrated by this example.
Frequently Asked Questions
wmemcmp() compares counted wide-character blocks. It is part of the standard wide-character library.
Its parameters are lhs, rhs, count. lhs: Left-hand value in the comparison. rhs: Right-hand value in the comparison. count: Maximum number of wchar_t elements to inspect, copy, append, or transform.
Negative / 0 / Positive means the left value sorts before, equals, or sorts after the right value.
Include <wchar.h> for wmemcmp(). Include only the supporting headers used by your program, such as <stdio.h> for FILE or <stdarg.h> for va_list.
The count is measured in wchar_t elements, not bytes. Counted memory functions do not automatically append L'\0'.