On this page
  1. strtok
  2. Include
  3. Signature & Overview
  4. Notes & Pitfalls
  5. Example

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

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