Home C wchar.h Functions wcslen() C wcslen() Function Definition In C, wcslen() counts wide characters before the null terminator. 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.
Call What it does wcslen(L"Hello")Counts wide characters before the null terminator in a typical call.
Include <wchar.h>. Add only the supporting headers actually used by your program.
Library declaration size_t wcslen(const wchar_t *ws);Parameters Parameter Description wsA wide-string buffer, source string, or format string.
Return Value Return Value Description size_t count The number of wide characters measured. Zero is a valid result.
C Examples 1. Count a wide string 2. Measure an empty string 3. Use a wide literal Example 1 — Count a wide string This example uses wcslen() to count a wide string, 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";
wprintf(L"length = %zu\n", wcslen(text));
return 0;
} How It Works Set up this case. The program prepares the exact input needed to count a wide string.Apply the wide-string operation. wcslen(L"Hello") 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. length = 5 confirms the behavior demonstrated by this example.Example 2 — Measure an empty string This example uses wcslen() to measure an empty string, then exposes the result so you can connect the call with its output.
#include <stdio.h>
#include <wchar.h>
int main(void) {
wprintf(L"empty = %zu\n", wcslen(L""));
return 0;
} How It Works Choose the edge case. This example changes the input so you can see how wcslen() behaves for measure an empty string.Apply the wide-string operation. wcslen(L"Hello") 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. empty = 0 confirms the behavior demonstrated by this example.Example 3 — Use a wide literal This example uses wcslen() to use a wide literal, 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 word[] = L"Café";
wprintf(L"%ls has %zu wide characters\n", word, wcslen(word));
return 0;
} Café has 4 wide characters How It Works Prepare a practical variation. The data is arranged specifically to use a wide literal.Apply the wide-string operation. wcslen(L"Hello") 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. Café has 4 wide characters confirms the behavior demonstrated by this example.Frequently Asked Questions What does wcslen() do in C? wcslen() counts wide characters before the null terminator. It is part of the standard wide-character library.
What parameters does wcslen() take? Its parameters are ws. ws: A wide-string buffer, source string, or format string.
What does wcslen() return? size_t count means the number of wide characters measured. Zero is a valid result.
Which header declares wcslen()? Include <wchar.h> for wcslen(). 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 wcslen() 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 wcsncat() 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