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