| 1 |
'\" t |
| 2 |
.\" Copyright 2005-2013, Michael Kerrisk <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 6 |
.\" |
| 7 |
.TH strtok 3 2026-02-25 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
strtok \- extract tokens from strings |
| 10 |
.SH LIBRARY |
| 11 |
Standard C library |
| 12 |
.RI ( libc ,\~ \-lc ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.B #include <string.h> |
| 16 |
.P |
| 17 |
.BI "char *strtok(char *_Nullable restrict " str \ |
| 18 |
", const char *restrict " delim ); |
| 19 |
.fi |
| 20 |
.SH DESCRIPTION |
| 21 |
The |
| 22 |
.BR strtok () |
| 23 |
function breaks a string into a sequence of zero or more nonempty tokens. |
| 24 |
On the first call to |
| 25 |
.BR strtok (), |
| 26 |
the string to be parsed should be |
| 27 |
specified in |
| 28 |
.IR str . |
| 29 |
In each subsequent call that should parse the same string, |
| 30 |
.I str |
| 31 |
must be NULL. |
| 32 |
.P |
| 33 |
The |
| 34 |
.I delim |
| 35 |
argument specifies a set of bytes that |
| 36 |
delimit the tokens in the parsed string. |
| 37 |
The caller may specify different strings in |
| 38 |
.I delim |
| 39 |
in successive |
| 40 |
calls that parse the same string. |
| 41 |
.P |
| 42 |
Each call to |
| 43 |
.BR strtok () |
| 44 |
returns a pointer to a |
| 45 |
null-terminated string containing the next token. |
| 46 |
This string does not include the delimiting byte. |
| 47 |
If no more tokens are found, |
| 48 |
.BR strtok () |
| 49 |
returns NULL. |
| 50 |
.P |
| 51 |
A sequence of calls to |
| 52 |
.BR strtok () |
| 53 |
that operate on the same string maintains a pointer |
| 54 |
that determines the point from which to start searching for the next token. |
| 55 |
The first call to |
| 56 |
.BR strtok () |
| 57 |
sets this pointer to point to the first byte of the string. |
| 58 |
The start of the next token is determined by scanning forward |
| 59 |
for the next nondelimiter byte in |
| 60 |
.IR str . |
| 61 |
If such a byte is found, it is taken as the start of the next token. |
| 62 |
If no such byte is found, |
| 63 |
then there are no more tokens, and |
| 64 |
.BR strtok () |
| 65 |
returns NULL. |
| 66 |
(A string that is empty or that contains only delimiters |
| 67 |
will thus cause |
| 68 |
.BR strtok () |
| 69 |
to return NULL on the first call.) |
| 70 |
.P |
| 71 |
The end of each token is found by scanning forward until either |
| 72 |
the next delimiter byte is found or until the |
| 73 |
terminating null byte (\[aq]\[rs]0\[aq]) is encountered. |
| 74 |
If a delimiter byte is found, it is overwritten with |
| 75 |
a null byte to terminate the current token, and |
| 76 |
.BR strtok () |
| 77 |
saves a pointer to the following byte; |
| 78 |
that pointer will be used as the starting point |
| 79 |
when searching for the next token. |
| 80 |
In this case, |
| 81 |
.BR strtok () |
| 82 |
returns a pointer to the start of the found token. |
| 83 |
.P |
| 84 |
From the above description, |
| 85 |
it follows that a sequence of two or more contiguous delimiter bytes in |
| 86 |
the parsed string is considered to be a single delimiter, and that |
| 87 |
delimiter bytes at the start or end of the string are ignored. |
| 88 |
Put another way: the tokens returned by |
| 89 |
.BR strtok () |
| 90 |
are always nonempty strings. |
| 91 |
Thus, for example, given the string |
| 92 |
.RI \[dq] aaa;;bbb, \[dq], |
| 93 |
successive calls to |
| 94 |
.BR strtok () |
| 95 |
that specify the delimiter string |
| 96 |
.RI \[dq] ;, \[dq] |
| 97 |
would return the strings |
| 98 |
.RI \[dq] aaa \[dq] |
| 99 |
and |
| 100 |
.RI \[dq] bbb \[dq], |
| 101 |
and then a null pointer. |
| 102 |
.SH RETURN VALUE |
| 103 |
.BR strtok () |
| 104 |
returns a pointer to the next token, |
| 105 |
or NULL if there are no more tokens. |
| 106 |
.SH ATTRIBUTES |
| 107 |
For an explanation of the terms used in this section, see |
| 108 |
.BR attributes (7). |
| 109 |
.TS |
| 110 |
allbox; |
| 111 |
lbx lb lb |
| 112 |
l l l. |
| 113 |
Interface Attribute Value |
| 114 |
T{ |
| 115 |
.na |
| 116 |
.nh |
| 117 |
.BR strtok () |
| 118 |
T} Thread safety MT-Unsafe race:strtok |
| 119 |
.TE |
| 120 |
.SH STANDARDS |
| 121 |
C11, POSIX.1-2008. |
| 122 |
.SH HISTORY |
| 123 |
POSIX.1-2001, C89, SVr4, 4.3BSD. |
| 124 |
.SH CAVEATS |
| 125 |
Be cautious when using this function. |
| 126 |
If you do use it, note that: |
| 127 |
.IP \[bu] 3 |
| 128 |
The identity of the delimiting byte is lost. |
| 129 |
.IP \[bu] |
| 130 |
The |
| 131 |
.BR strtok () |
| 132 |
function uses a static buffer while parsing, so it's not thread safe. |
| 133 |
Use |
| 134 |
.BR strtok_r (3) |
| 135 |
if this matters to you. |
| 136 |
.SH EXAMPLES |
| 137 |
See |
| 138 |
.BR getaddrinfo_a (3). |
| 139 |
.SH SEE ALSO |
| 140 |
.BR memchr (3), |
| 141 |
.BR strchr (3), |
| 142 |
.BR string (3), |
| 143 |
.BR strpbrk (3), |
| 144 |
.BR strsep (3), |
| 145 |
.BR strspn (3), |
| 146 |
.BR strstr (3), |
| 147 |
.BR strtok_r (3), |
| 148 |
.BR wcstok (3) |