In C, strtok() splits a writable C-string into tokens. The first call passes the buffer. Later calls pass NULL to continue. Each delimiter is replaced with '\0' inside the same buffer.
You pass the buffer and the delimiter set. Both are required on the first call. The result is a pointer to the next token, or NULL when there are no more. Include <string.h>.
Syntax
Pass a writable buffer first, then NULL on later calls.
C
tok = strtok(buf, ",");
tok = strtok(NULL, ",");
Call
What it does
strtok(buf, ",")
Starts tokenization and returns the first token.
strtok(NULL, ",")
Continues from the same buffer and returns the next token.
Include <string.h>. Never tokenize a string literal. Copy the text into a writable array first.
Library declarations
C
char *strtok(char *str, const char *delimiters);
Parameters
Parameter
Description
str
Required. A writable C-string on the first call. Pass NULL on later calls to continue.
delimiters
Required. The characters that separate tokens.
Return Value
Return Value
Description
Token pointer
Points into the same buffer at the next token.
NULL
Returned when there are no more tokens.
C Examples
Example 1 — Split a CSV Line
First call with the buffer; loop with NULL until strtok returns NULL.