How to Use strtok in C: Syntax, Example, and Common Pitfalls
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
NULLfor next tokens. - delim: A string containing delimiter characters to split the string.
- Returns a pointer to the next token or
NULLif no more tokens are found.
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.
#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; }
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.
#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; }
Quick Reference
| Function | Description |
|---|---|
| strtok(str, delim) | Start tokenizing string str using delimiters delim |
| strtok(NULL, delim) | Get next token from previous string |
| Returns | Pointer to token or NULL if no more tokens |
| Modifies | Original string by inserting '\0' at delimiters |
| Note | Use modifiable string, not string literals |