C wcstok() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
wchar.h

Definition

In C, wcstok() splits a writable wide string into tokens. 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
wcstok(text, L",", &state);
CallWhat it does
wcstok(text, L",", &state)Splits a writable wide string into tokens in a typical call.

Include <wchar.h>. Add only the supporting headers actually used by your program.

Library declaration
wchar_t *wcstok(wchar_t *restrict str, const wchar_t *restrict delim, wchar_t **restrict ptr);

Parameters

ParameterDescription
strWritable wide string on the first token call, then NULL on later calls.
delimWide string containing delimiter characters.
ptrState pointer used by wcstok, or a counted wide-character block.

Return Value

Return ValueDescription
PointerAddress of the match or token.
NULLNo match or no more tokens.

C Examples

Example 1 — Split a writable string

This example uses wcstok() to split a writable string, then exposes the result so you can connect the call with its output.

C
#include <stdio.h>
#include <wchar.h>

int main(void) {
    wchar_t text[] = L"red,green,blue"; wchar_t *state;
    for (wchar_t *p = wcstok(text, L",", &state); p; p = wcstok(NULL, L",", &state))
        wprintf(L"%ls\n", p);
    return 0;
}

How It Works

  1. Set up this case. The program prepares the exact input needed to split a writable string.
  2. Apply the wide-string operation. wcstok(text, L",", &state) works in wchar_t elements and respects the ending L'\0'.
  3. Read the normal result. The displayed output shows the direct result of the call for this input.
  4. Interpret the output. red / green / blue confirms the behavior demonstrated by this example.

Example 2 — Continue with NULL

This example uses wcstok() to continue with null, then exposes the result so you can connect the call with its output.

C
#include <stdio.h>
#include <wchar.h>

int main(void) {
    wchar_t text[] = L"red,green,blue"; wchar_t *state;
    for (wchar_t *p = wcstok(text, L",", &state); p; p = wcstok(NULL, L",", &state))
        wprintf(L"%ls\n", p);
    return 0;
}

How It Works

  1. Choose the edge case. This example changes the input so you can see how wcstok() behaves for continue with null.
  2. Apply the wide-string operation. wcstok(text, L",", &state) works in wchar_t elements and respects the ending L'\0'.
  3. Inspect the status or pointer. The program checks what the function returned before relying on output data.
  4. Interpret the output. red / green / blue confirms the behavior demonstrated by this example.

Example 3 — Keep separate state

This example uses wcstok() to keep separate state, then exposes the result so you can connect the call with its output.

C
#include <stdio.h>
#include <wchar.h>

int main(void) {
    wchar_t text[] = L"red,green,blue"; wchar_t *state;
    for (wchar_t *p = wcstok(text, L",", &state); p; p = wcstok(NULL, L",", &state))
        wprintf(L"%ls\n", p);
    return 0;
}

How It Works

  1. Prepare a practical variation. The data is arranged specifically to keep separate state.
  2. Apply the wide-string operation. wcstok(text, L",", &state) works in wchar_t elements and respects the ending L'\0'.
  3. Notice the safety rule. The example keeps the count, destination capacity, stream state, and terminator requirements visible.
  4. Interpret the output. red / green / blue confirms the behavior demonstrated by this example.

Frequently Asked Questions

wcstok() splits a writable wide string into tokens. It is part of the standard wide-character library.
Its parameters are str, delim, ptr. str: Writable wide string on the first token call, then NULL on later calls. delim: Wide string containing delimiter characters. ptr: State pointer used by wcstok, or a counted wide-character block.
Pointer means address of the match or token. NULL means no match or no more tokens.
Include <wchar.h> for wcstok(). Include only the supporting headers used by your program, such as <stdio.h> for FILE or <stdarg.h> for va_list.
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 wcstol() Function

Continue learning C wide-character functions.

Next tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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