strtok(3)

๐Ÿ’– strtok(3) โ€” string tokenize ๐Ÿ’–

okay bestie so this is strtok, from the Library Functions Manual (yes that's the manual name, strtok(3), she's an icon, she's famous). She lives in the Standard C library โ€” that's libc, link her in with -lc. And she splits strings into lil pieces. Let's get into it, no cap ๐ŸŽ€


โœจ SYNOPSIS โœจ

#include <string.h>

char *strtok(char *_Nullable restrict str, const char *restrict delim);

that's the whole thing. two args. so simple. so chaotic. ๐Ÿฆ‹


๐ŸŒธ DESCRIPTION ๐ŸŒธ

So the way that strtok() works is: she breaks a string into a sequence of zero or more nonempty tokens. Zero or more. Nonempty. Remember that part, it comes back ๐Ÿ’…

On the first call to strtok(), the string you wanna parse goes in str. Then for each subsequent call that parses that same string, str must be NULL. That's the vibe โ€” first call you hand her the string, after that you just say "same one bestie" with a NULL.

The delim argument sets the set of bytes that delimit the tokens in the parsed string. And get this โ€” the caller may specify different strings in delim in successive calls that parse the same string. So you get to change the rules mid-game. iconic honestly.

Each call to strtok() returns a pointer to a null-terminated string containing the next token. That string does not include the delimiting byte. And if there are no more tokens? strtok() returns NULL. boom, done.

Now the mechanics, because this is the part everyone gets wrong:

A sequence of calls to strtok() operating on the same string maintains a pointer that decides where to start searching for the next token. The first call sets this pointer to point to the first byte of the string. Then the start of the next token is found by scanning forward for the next nondelimiter byte in str. If she finds one, that's the start of the next token. If she finds no such byte, then there are no more tokens and strtok() returns NULL. (so like โ€” a string that's empty, or that contains only delimiters, will make strtok() return NULL on the very first call. rude but fine.)

The end of each token gets found by scanning forward until either the next delimiter byte shows up or the terminating null byte ('\0') is hit. If a delimiter byte is found, it gets overwritten with a null byte to terminate the current token (!!! she edits your string in place, girl is writing in the group chat), and strtok() saves a pointer to the following byte โ€” that pointer becomes the starting point when searching for the next token. In that case strtok() returns a pointer to the start of the found token.

So from all that it follows that a sequence of two or more contiguous delimiter bytes in the parsed string counts as a single delimiter, and delimiter bytes at the start or end of the string are ignored. Put another way: the tokens returned by strtok() are always nonempty strings. periodt ๐Ÿ’…

For example โ€” and this is straight from the source, bestie โ€” given the string:

"aaa;;bbb,"

successive calls to strtok() with the delimiter string ";," would return "aaa" and "bbb", and then a null pointer. ate and left no crumbs โœจ


โญ RETURN VALUE โญ

strtok() returns a pointer to the next token, or NULL if there are no more tokens. that's it that's the section ๐Ÿซถ


๐Ÿ’œ ATTRIBUTES ๐Ÿ’œ

for an explanation of the terms used in this section, peep attributes(7) โ€” she's got the glossary.

  • Interface: strtok()
  • Attribute: Thread safety
  • Value: MT-Unsafe race:strtok
โš ๏ธ yeah. MT-Unsafe race:strtok. she is NOT thread safe and the attribute table is not being subtle about it. as in โ€” the interface is strtok() and the value is literally the words MT-Unsafe race:strtok. ๐Ÿ˜ญ

๐ŸŒˆ STANDARDS ๐ŸŒˆ

  • C11
  • POSIX.1-2008

๐Ÿฆ‹ HISTORY ๐Ÿฆ‹

  • POSIX.1-2001
  • C89
  • SVr4
  • 4.3BSD

she's ancient. she's been here forever. she's still in every codebase. let that sink in ๐Ÿ’€


๐Ÿ•ฏ๏ธ CAVEATS ๐Ÿ•ฏ๏ธ

Be cautious when using this function. and if you do use it, note that:

  • the identity of the delimiting byte is lost. gone. she overwrote it with '\0', remember? that byte is not coming back. it's giving irreversible.
  • the strtok() function uses a static buffer while parsing, so it's not thread safe. if that matters to you (it should), use strtok_r(3) instead. bestie, for real, look out for yourself ๐Ÿซถ

๐ŸŽ€ EXAMPLES ๐ŸŽ€

see getaddrinfo_a(3). that's where the examples live. go visit, tell them I said hi ๐Ÿ’–


๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘

string(3), strsep(3), strtok_r(3), wcstok(3)


and that's the whole page bestie ๐Ÿงš this was Linux man-pages 6.20, dated 2026-08-10, and yeah the page itself is strtok(3) โ€” go split some strings responsibly, and honestly maybe just use strtok_r(3). 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: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms