Use strtok from <string.h> to split a string into tokens separated by delimiter characters: call it once with the string, then repeatedly with NULL to get the next token. It modifies the original string by writing '\0' over each delimiter, so you must pass a writable array, not a string literal. If you search for "strtok in c", the key facts are that it is destructive, not thread-safe, and that strtok_r or C23's strtok_s variants fix the state problem.
This guide builds on how char arrays and null terminators work.
The signature is:
On the first call, pass the string to split. On every later call, pass NULL and strtok continues from where it stopped. Each call returns a pointer to the next token, or NULL when no tokens are left. The delim argument is a set of characters, not a substring: any one of them ends a token.
Here ,, ; and a space are all delimiters, so one call handles all of them.
strtok keeps a hidden pointer between calls and replaces each delimiter it finds with '\0'. After the loop above, line no longer holds the original text:
This has two consequences:
char *s = "a,b,c"; points to a string literal, and calling strtok(s, ",") is undefined behaviour (usually a crash). Use char s[] = "a,b,c"; instead.strdup (standard since C23, POSIX before that) or memcpy.The tokens returned are pointers into the original buffer, not copies. They stay valid only as long as that buffer does.
strtok treats a run of delimiters as a single separator and never returns empty tokens:
The two empty fields were skipped. That makes strtok a bad choice for CSV where empty columns matter. If you need empty fields, use strsep (a BSD and glibc extension, not standard C) or write the loop yourself with strchr or strpbrk.
The hidden pointer is static state shared by the whole program. Two consequences follow:
strtok at the same time corrupt each other's position.The re-entrant alternative takes the state as an argument. POSIX provides strtok_r, and Windows provides strtok_s. Annex K of C11 also defines a strtok_s with a different signature, but it is optional and not available in most Unix libraries.
Each loop has its own state pointer, so nesting works. This code needs a POSIX system (Linux, macOS, BSD).
Because strtok returns NULL when tokens run out, always test the pointer before you dereference it. For input like "key" without an =, the second strtok_r above returns NULL, and passing that to printf("%s") is undefined behaviour. Add a check:
String literals live in read-only memory, and strtok writes '\0' into the string. Declare a character array (char s[] = "...") to get a writable copy.
No. It uses internal static state. Use strtok_r on POSIX or strtok_s on Windows when threads or nested splitting are involved.
No. The second argument is a set of single characters, so "::" means "split on a colon". For a real substring delimiter, loop with strstr.
Copy it before calling strtok, for example with strdup and then free the copy when done.
strtok returns tokens one at a time, takes NULL after the first call, and overwrites delimiters in your buffer. It skips empty fields, needs a writable array, and is not safe across threads or nested loops. Reach for strtok_r or a hand-written strpbrk loop when those limits matter.