OS/161 Reference Manual


strtok - tokenize string


Standard C Library (libc, -lc)


#include <string.h>

char *
strtok(char *string, const char *separators);


strtok splits up the string string into fields using the characters found in separators as delimiters. The delimiters found are discarded. Multiple delimiter characters in a row are treated as a single delimiter.

When first called, strtok returns the first field of string. To retrieve successive fields of string, call strtok again repeatedly, passing NULL as the first argument. When no more fields are left, NULL is returned. If the string is empty or contains only delimiters, NULL will be returned on the first call.


Note that the state used to remember string across calls is global. Thus, strtok cannot be used from more than one thread at a time in a multithreaded program, nor can it be used in a subroutine called from within a loop that itself uses strtok. If these restrictions are problematic, use strtok_r.

The behavior if strtok is called again without passing a new string after it has returned NULL is undefined.

The behavior if strtok is called with the first argument NULL without having first passed a valid string is also undefined.

Return Values

strtok returns successive components of the passed-in string, and NULL when no more remain.