'\" t .\" Copyright 2005-2013, Michael Kerrisk .\" Copyright, the authors of the Linux man-pages project .\" .\" SPDX-License-Identifier: Linux-man-pages-copyleft .\" .TH strtok 3 2026-02-25 "Linux man-pages 6.18" .SH NAME strtok \- extract tokens from strings .SH LIBRARY Standard C library .RI ( libc ,\~ \-lc ) .SH SYNOPSIS .nf .B #include .P .BI "char *strtok(char *_Nullable restrict " str \ ", const char *restrict " delim ); .fi .SH DESCRIPTION The .BR strtok () function breaks a string into a sequence of zero or more nonempty tokens. On the first call to .BR strtok (), the string to be parsed should be specified in .IR str . In each subsequent call that should parse the same string, .I str must be NULL. .P The .I delim argument specifies a set of bytes that delimit the tokens in the parsed string. The caller may specify different strings in .I delim in successive calls that parse the same string. .P Each call to .BR strtok () returns a pointer to a null-terminated string containing the next token. This string does not include the delimiting byte. If no more tokens are found, .BR strtok () returns NULL. .P A sequence of calls to .BR strtok () that operate on the same string maintains a pointer that determines the point from which to start searching for the next token. The first call to .BR strtok () sets this pointer to point to the first byte of the string. The start of the next token is determined by scanning forward for the next nondelimiter byte in .IR str . If such a byte is found, it is taken as the start of the next token. If no such byte is found, then there are no more tokens, and .BR strtok () returns NULL. (A string that is empty or that contains only delimiters will thus cause .BR strtok () to return NULL on the first call.) .P The end of each token is found by scanning forward until either the next delimiter byte is found or until the terminating null byte (\[aq]\[rs]0\[aq]) is encountered. If a delimiter byte is found, it is overwritten with a null byte to terminate the current token, and .BR strtok () saves a pointer to the following byte; that pointer will be used as the starting point when searching for the next token. In this case, .BR strtok () returns a pointer to the start of the found token. .P From the above description, it follows that a sequence of two or more contiguous delimiter bytes in the parsed string is considered to be a single delimiter, and that delimiter bytes at the start or end of the string are ignored. Put another way: the tokens returned by .BR strtok () are always nonempty strings. Thus, for example, given the string .RI \[dq] aaa;;bbb, \[dq], successive calls to .BR strtok () that specify the delimiter string .RI \[dq] ;, \[dq] would return the strings .RI \[dq] aaa \[dq] and .RI \[dq] bbb \[dq], and then a null pointer. .SH RETURN VALUE .BR strtok () returns a pointer to the next token, or NULL if there are no more tokens. .SH ATTRIBUTES For an explanation of the terms used in this section, see .BR attributes (7). .TS allbox; lbx lb lb l l l. Interface Attribute Value T{ .na .nh .BR strtok () T} Thread safety MT-Unsafe race:strtok .TE .SH STANDARDS C11, POSIX.1-2008. .SH HISTORY POSIX.1-2001, C89, SVr4, 4.3BSD. .SH CAVEATS Be cautious when using this function. If you do use it, note that: .IP \[bu] 3 The identity of the delimiting byte is lost. .IP \[bu] The .BR strtok () function uses a static buffer while parsing, so it's not thread safe. Use .BR strtok_r (3) if this matters to you. .SH EXAMPLES See .BR getaddrinfo_a (3). .SH SEE ALSO .BR memchr (3), .BR strchr (3), .BR string (3), .BR strpbrk (3), .BR strsep (3), .BR strspn (3), .BR strstr (3), .BR strtok_r (3), .BR wcstok (3)