On this page
strtok
Prev: strncpy | Up: string.h | Next: stdint.h
Rel: strchr(), string.h, stdlib.h & string.h Guide
Ref: cppreference — strtok
Tags: #public #prog #c #stdlib #string #tier/legacy
Include
#include <string.h>
Signature & Overview
char *strtok(char *restrict str, const char *restrict delim);
Splits a string in place by mutating delimiters to '\0' using hidden static state (not thread-safe or reentrant).
Legacy / Avoid: Prefer strchr() in all new code.
Notes & Pitfalls
- Mutates the input string in place AND is NOT thread-safe/reentrant: Never pass a string literal to
strtok(); usestrtok_r()(POSIX) orstrchr()/strcspn().
Example
#include <assert.h>
#include <string.h>
int main(void) {
char buf[] = "a,b,c"; // must be mutable!
char *tok = strtok(buf, ",");
assert(tok != NULL && strcmp(tok, "a") == 0);
}
Linked from
Related notes
- tuts/c/stdlib/string/strncpy Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string
- tuts/c/stdlib/string/strchr Linked here; shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/stdint Linked here; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/stdlib_and_string_h Linked here; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/string/memchr shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memcmp shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memcpy shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memmove shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memset shared tags: #c, #prog, #stdlib, #string, #tier