strtok in C: In-Place Delimiter Tokenization & strtok_r Safety

strtok in c can split a delimited string into tokens, and it is fast and requires no allocation. In c, the first call to strtok passes a modifiable char array and a delimiter string, strtok overwrites the first delimiter byte following a token with '\0', and returns a pointer to the beginning of the token; subsequent calls pass a delimiter string, but not a char array, to continue scanning the same buffer. Use the reentrant POSIX strtok_r(), or manually scan with strcspn/strspn in multi-threaded programs, since strtok was mutating the buffer (string literals caused a segmentation fault), and had hidden internal static state between calls.

strtok in C: Splitting Strings into Tokens & Reentrant Parsing — Technical Overview

strtok in c can split a delimited string into tokens, and it is fast and requires no allocation. In c, the first call to strtok passes a modifiable char array and a delimiter string, strtok overwrites the first delimiter byte following a token with '\0', and returns a pointer to the beginning of the token; subsequent calls pass a delimiter string, but not a char array, to continue scanning the same buffer. Use the reentrant POSIX strtok_r(), or manually scan with strcspn/strspn in multi-threaded programs, since strtok was mutating the buffer (string literals caused a segmentation fault), and had hidden internal static state between calls.

#include <stdio.h>
#include <string.h>

int main(void) {
    char csv[] = "gcc,clang,c17";
    for (char *tok = strtok(csv, ","); tok != NULL; tok = strtok(NULL, ",")) {
        printf("Token: %s\n", tok);
    }
    return 0;
}

Runnable C example

#include <stdio.h>
#include <string.h>

int main(void) {
    char csv[] = "gcc,clang,c17";
    for (char *tok = strtok(csv, ","); tok != NULL; tok = strtok(NULL, ",")) {
        printf("Token: %s\n", tok);
    }
    return 0;
}

Open example in compiler

Expected Output

Token: gcc
Token: clang
Token: c17