C strtok() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
string.h

Definition

In C, strtok() splits a writable C-string into tokens. The first call passes the buffer. Later calls pass NULL to continue. Each delimiter is replaced with '\0' inside the same buffer.

You pass the buffer and the delimiter set. Both are required on the first call. The result is a pointer to the next token, or NULL when there are no more. Include <string.h>.

Syntax

Pass a writable buffer first, then NULL on later calls.

C
tok = strtok(buf, ",");
tok = strtok(NULL, ",");
CallWhat it does
strtok(buf, ",")Starts tokenization and returns the first token.
strtok(NULL, ",")Continues from the same buffer and returns the next token.

Include <string.h>. Never tokenize a string literal. Copy the text into a writable array first.

Library declarations
C
char *strtok(char *str, const char *delimiters);

Parameters

ParameterDescription
strRequired. A writable C-string on the first call. Pass NULL on later calls to continue.
delimitersRequired. The characters that separate tokens.

Return Value

Return ValueDescription
Token pointerPoints into the same buffer at the next token.
NULLReturned when there are no more tokens.

C Examples

Example 1 — Split a CSV Line

First call with the buffer; loop with NULL until strtok returns NULL.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char buf[] = "red,green,blue";
    char *tok = strtok(buf, ",");

    while (tok != NULL) {
        printf("%s\n", tok);
        tok = strtok(NULL, ",");
    }

    return 0;
}

How It Works

  1. Writable buffer. char buf[] can be modified by strtok.
  2. First call. strtok(buf, ",") returns "red" and cuts at the comma.
  3. Continue with NULL. Each later call resumes after the last cut.
  4. Stop on NULL. When tokens are gone, the loop ends.

Example 2 — Split Words on Spaces

Use a space as the delimiter to walk words in a sentence.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char buf[] = "learn C strings";
    char *tok;

    for (tok = strtok(buf, " "); tok != NULL; tok = strtok(NULL, " ")) {
        printf("[%s]\n", tok);
    }

    return 0;
}

How It Works

  1. Delimiter is space. Each word becomes one token.
  2. for-loop form. Init, test, and continue call sit in one place.
  3. Print each word. Brackets make empty results easy to spot (none here).
  4. Buffer is cut. After the loop, spaces in buf are '\0'.

Example 3 — Several Delimiters

The delimiter argument is a setβ€”any of those characters can separate tokens.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char buf[] = "1;2,3;4";
    char *tok = strtok(buf, ";,");

    while (tok != NULL) {
        printf("%s\n", tok);
        tok = strtok(NULL, ";,");
    }

    return 0;
}

How It Works

  1. Delimiter set. ";," means either ';' or ',' splits.
  2. Same loop pattern. First call with buf, then NULL.
  3. Four tokens. Mixed separators still produce 1, 2, 3, 4.
  4. Order in the set unused. ";," and ",;" behave the same.

Frequently Asked Questions

strtok() splits a writable C-string into tokens using delimiter characters.
str is the writable buffer on the first call, or NULL later. delimiters lists the separator characters.
A pointer to the next token, or NULL when there are no more tokens.
Include <string.h>.
No. Copy the text into a writable array first. strtok() writes '\0' into the buffer.

Next: strupr()

Convert characters in a string to uppercase (extension / portable loop).

strupr() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P πŸ”—

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful