In C, wmemchr() finds a wide character in a counted block. 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
wmemchr(block, L'B', 4);
Call
What it does
wmemchr(block, L'B', 4)
Finds a wide character in a counted block in a typical call.
Include <wchar.h>. Add only the supporting headers actually used by your program.
State pointer used by wcstok, or a counted wide-character block.
wc
The wide character to write, find, fill, or convert.
count
Maximum number of wchar_t elements to inspect, copy, append, or transform.
Return Value
Return Value
Description
Pointer
Address of the match or token.
NULL
No match or no more tokens.
C Examples
Example 1 — Find a match
This example uses wmemchr() to find a match, then exposes the result so you can connect the call with its output.
C
#include <stdio.h>
#include <wchar.h>
int main(void) {
const wchar_t *text = L"Hello World";
const wchar_t *found = wmemchr(text, L'o', 11);
wprintf(L"%ls\n", found ? found : L"not found");
return 0;
}
Output
o World
How It Works
Set up this case. The program prepares the exact input needed to find a match.
Process a counted block.wmemchr(block, L'B', 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.o World confirms the behavior demonstrated by this example.
Example 2 — Check the returned pointer
This example uses wmemchr() to check the returned pointer, then exposes the result so you can connect the call with its output.
C
#include <stdio.h>
#include <wchar.h>
int main(void) {
const wchar_t *text = L"Hello World";
const wchar_t *found = wmemchr(text, L'o', 11);
wprintf(L"%ls\n", found ? found : L"not found");
return 0;
}
Output
o World
How It Works
Choose the edge case. This example changes the input so you can see how wmemchr() behaves for check the returned pointer.
Process a counted block.wmemchr(block, L'B', 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.o World confirms the behavior demonstrated by this example.
Example 3 — Handle no match
This example uses wmemchr() to handle no match, then exposes the result so you can connect the call with its output.
C
#include <stdio.h>
#include <wchar.h>
int main(void) {
const wchar_t *text = L"Hello World";
const wchar_t *found = wmemchr(text, L'o', 11);
wprintf(L"%ls\n", found ? found : L"not found");
return 0;
}
Output
o World
How It Works
Prepare a practical variation. The data is arranged specifically to handle no match.
Process a counted block.wmemchr(block, L'B', 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.o World confirms the behavior demonstrated by this example.
Frequently Asked Questions
wmemchr() finds a wide character in a counted block. It is part of the standard wide-character library.
Its parameters are ptr, wc, count. ptr: State pointer used by wcstok, or a counted wide-character block. wc: The wide character to write, find, fill, or convert. count: Maximum number of wchar_t elements to inspect, copy, append, or transform.
Pointer means address of the match or token. NULL means no match or no more tokens.
Include <wchar.h> for wmemchr(). 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'.