What is wcsrchr in C?

The wcsrchr function is used to locate the last occurrence of a wide character in a wide string. It is defined in the wchar.h header.

Parameters

wchar_t* wcsrchr (wchar_t* str, wchar_t ch);

The function takes two parameters of type wchar_t.

In the above snippet of code, ch is a wide character that will be searched in the wide string pointed by str.

Return Value

If ch is found in str, the function returns a pointer to its last occurrence. If ch is not found, the function returns NULL.

Example

#include<stdio.h>
#include<wchar.h>
void main() {
// declaring and initializing wide string
wchar_t str1[] = L"HELLO WORLD.";
// calling the function
wchar_t* ptr = wcsrchr(str1, L'O');
if(ptr == NULL) {
wprintf(L"Character not found! \n");
}
else {
// using the wprintf function to print the wide string
wprintf(L"RESULT: \n");
wprintf(L"ptr = %ls\n\n", ptr);
}
}

In the above snippet of code, we’re searching the last occurrence of ‘O’ in str1.

We can see from the output of the code that the function returned the last occurrence of ‘O’.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved