0
0
CHow-ToBeginner · 3 min read

How to Use strtok in C: Syntax, Example, and Common Pitfalls

In C, strtok is used to split a string into smaller parts called tokens based on specified delimiters. You call strtok first with the string to tokenize and delimiters, then repeatedly with NULL to get the next tokens until no tokens remain.
📐

Syntax

The strtok function is declared as char *strtok(char *str, const char *delim);. The first call should pass the string to split as str, and the delimiters as delim. Subsequent calls should pass NULL for str to continue tokenizing the same string.

  • str: The string to tokenize on the first call, then NULL for next tokens.
  • delim: A string containing delimiter characters to split the string.
  • Returns a pointer to the next token or NULL if no more tokens are found.
c
char *strtok(char *str, const char *delim);
💻

Example

This example splits a sentence into words using space and comma as delimiters. It shows how to call strtok first with the string, then repeatedly with NULL to get all tokens.

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

int main() {
    char text[] = "Hello, world! Welcome to C programming.";
    const char delimiters[] = " ,!";

    char *token = strtok(text, delimiters);
    while (token != NULL) {
        printf("%s\n", token);
        token = strtok(NULL, delimiters);
    }

    return 0;
}
Output
Hello world Welcome to C programming.
⚠️

Common Pitfalls

One common mistake is calling strtok with the original string every time, which resets tokenization and causes an infinite loop or repeated tokens. Another is using strtok on string literals, which are read-only and cause undefined behavior. Always use a modifiable array.

Also, strtok modifies the original string by inserting null characters to separate tokens, so keep a copy if you need the original string intact.

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

int main() {
    // Wrong: calling strtok with original string every time
    char text[] = "apple,banana,orange";
    char *token = strtok(text, ",");
    while (token != NULL) {
        printf("%s\n", token);
        // Wrong: passing text again resets tokenization
        token = strtok(text, ",");
    }

    // Right way:
    // token = strtok(NULL, ",");

    return 0;
}
Output
apple apple apple ... (infinite loop)
📊

Quick Reference

FunctionDescription
strtok(str, delim)Start tokenizing string str using delimiters delim
strtok(NULL, delim)Get next token from previous string
ReturnsPointer to token or NULL if no more tokens
ModifiesOriginal string by inserting '\0' at delimiters
NoteUse modifiable string, not string literals

Key Takeaways

Call strtok first with the string, then with NULL to get all tokens.
strtok modifies the original string by inserting null characters.
Never pass string literals to strtok; use a modifiable array instead.
Passing the original string repeatedly resets tokenization and causes errors.
Use delimiters string to specify all characters that separate tokens.