Home C wchar.h Functions wcsstr() C wcsstr() Function Definition In C, wcsstr() finds a wide substring. 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.
wcsstr(L"Hello World", L"World"); Call What it does wcsstr(L"Hello World", L"World")Finds a wide substring in a typical call.
Include <wchar.h>. Add only the supporting headers actually used by your program.
Library declaration wchar_t *wcsstr(const wchar_t *haystack, const wchar_t *needle);Parameters Parameter Description haystackWide string to search. needleWide substring to find.
Return Value Return Value Description Pointer Address of the match or token. NULL No match or no more tokens.
C Examples 1. Find a match 2. Check the returned pointer 3. Handle no match Example 1 — Find a match This example uses wcsstr() to find a match, then exposes the result so you can connect the call with its output.
#include <stdio.h>
#include <wchar.h>
int main(void) {
const wchar_t *text = L"Hello World";
const wchar_t *found = wcsstr(text, L"World");
wprintf(L"%ls\n", found ? found : L"not found");
return 0;
} How It Works Set up this case. The program prepares the exact input needed to find a match.Apply the wide-string operation. wcsstr(L"Hello World", L"World") works in wchar_t elements and respects the ending L'\0'.Read the normal result. The displayed output shows the direct result of the call for this input.Interpret the output. World confirms the behavior demonstrated by this example.Example 2 — Check the returned pointer This example uses wcsstr() to check the returned pointer, then exposes the result so you can connect the call with its output.
#include <stdio.h>
#include <wchar.h>
int main(void) {
const wchar_t *text = L"Hello World";
const wchar_t *found = wcsstr(text, L"World");
wprintf(L"%ls\n", found ? found : L"not found");
return 0;
} How It Works Choose the edge case. This example changes the input so you can see how wcsstr() behaves for check the returned pointer.Apply the wide-string operation. wcsstr(L"Hello World", L"World") works in wchar_t elements and respects the ending L'\0'.Inspect the status or pointer. The program checks what the function returned before relying on output data.Interpret the output. World confirms the behavior demonstrated by this example.Example 3 — Handle no match This example uses wcsstr() to handle no match, then exposes the result so you can connect the call with its output.
#include <stdio.h>
#include <wchar.h>
int main(void) {
const wchar_t *text = L"Hello World";
const wchar_t *found = wcsstr(text, L"World");
wprintf(L"%ls\n", found ? found : L"not found");
return 0;
} How It Works Prepare a practical variation. The data is arranged specifically to handle no match.Apply the wide-string operation. wcsstr(L"Hello World", L"World") works in wchar_t elements and respects the ending L'\0'.Notice the safety rule. The example keeps the count, destination capacity, stream state, and terminator requirements visible.Interpret the output. World confirms the behavior demonstrated by this example.Frequently Asked Questions What does wcsstr() do in C? wcsstr() finds a wide substring. It is part of the standard wide-character library.
What parameters does wcsstr() take? Its parameters are haystack, needle. haystack: Wide string to search. needle: Wide substring to find.
What does wcsstr() return? Pointer means address of the match or token. NULL means no match or no more tokens.
Which header declares wcsstr()? Include <wchar.h> for wcsstr(). Include only the supporting headers used by your program, such as <stdio.h> for FILE or <stdarg.h> for va_list.
What is a common wcsstr() mistake? Inputs must be valid null-terminated wide strings unless the API explicitly uses a count. Writable destinations need room for the result and L'\0'.
Next: C wcstod() Function Continue learning C wide-character functions.
Next tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion