wcstok(3)

✨ wcstok(3) β€” the wide-character glow-up of strtok(3) ✨

okay bestie, settle in, because we're splitting wide-character strings today and honestly? it's giving main character energy. this page lives in the Library Functions Manual, section 3, and it's all about the standard C library (that's libc, aka -lc) πŸ’–


🌸 NAME 🌸

wcstok β€” split a wide-character string into tokens πŸŽ€


🧚 LIBRARY 🧚

Standard C library (libc, -lc) β€” yes queen, the regular one, nothing fancy 🫢


✨ SYNOPSIS ✨

#include <wchar.h>

wchar_t *wcstok(wchar_t *restrict wcs, const wchar_t *restrict delim,
                wchar_t **restrict ptr);

that's it that's the whole vibe πŸ’…


πŸ’œ DESCRIPTION πŸ’œ

so wcstok() is literally the wide-character equivalent of strtok(3), except it got a glow-up: it comes with an extra argument that makes it multithread-safe πŸ”₯ iconic behavior, no notes.

you use it to split a wide-character string wcs into tokens, and a token is defined as a substring that contains none of the wide-characters from delim. simple as that πŸ¦‹

now here's how the search goes, bestie, pay attention:

  • the search starts at wcs β€” but only if wcs is not NULL
  • otherwise it starts at *ptr β€” that's when wcs is NULL

then? first, any delimiter wide-characters get skipped. like, the pointer just advances past anything in delim, bye πŸ‘‹

if the end of the wide-character string is reached at that point, wcstok() returns NULL to say "no tokens found, bestie, it's over", and it stores an appropriate value in *ptr so that subsequent calls to wcstok() will just keep returning NULL. the search has left the chat πŸ’€

otherwise β€” and this is the part where it eats β€” wcstok() recognizes the beginning of a token and returns a pointer to it. but before it does that, it zero-terminates the token, replacing the next wide-character that occurs in delim with a null wide character (L'\0'), and it updates *ptr so subsequent calls continue searching after the end of the recognized token ✨

⚠️ lowkey important: this is destructive, we get to that in NOTES, don't say I didn't warn you πŸ™ˆ

πŸŽ€ RETURN VALUE πŸŽ€

wcstok() returns a pointer to the next token, or NULL if no further token was found. that's the whole plot. 🌈


πŸ‘‘ ATTRIBUTES πŸ‘‘

for an explanation of the terms used in this section, go peek at attributes(7) β€” it's the lore doc πŸ“–

  • Interface: wcstok()
  • Attribute: Thread safety
  • Value: MT-Safe πŸ”₯

🌟 STANDARDS 🌟

C11, POSIX.1-2008 β€” certified, periodt πŸ’…


πŸ’Œ HISTORY πŸ’Œ

POSIX.1-2001, C99. it's been here for a minute, an elder icon honestly πŸ’–


πŸ“ NOTES πŸ“

the original wcs wide-character string is destructively modified during the operation. like, it will not survive the encounter bestie. it's being edited in place. if you needed the original, too late, you should've made a copy 🫠


πŸ¦‹ EXAMPLES πŸ¦‹

the following code loops over the tokens contained in a wide-character string. look at her go:

wchar_t *wcs = ...;
wchar_t *token;
wchar_t *state;
for (token = wcstok(wcs, L" \t\n", &state);
    token != NULL;
    token = wcstok(NULL, L" \t\n", &state)) {
    ...
}

see how wcs is passed on the first call and NULL on every call after? and &state carries the *ptr drip forward? that's the whole trick. chef's kiss πŸ§‘β€πŸ³πŸ’‹


🌈 SEE ALSO 🌈


and that's the tea bestie πŸ’œ this page was brought to you by Linux man-pages 6.20, dated 2026-02-08, and filed under wcstok(3). go forth and tokenize responsibly, mwah πŸ«ΆπŸŽ€βœ¨

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: GPL-2.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms