
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What is strtok_r() function in C language?
This function is similar to the strtok() function. The only key difference is that the _r, which is called as re-entrant function.
A re-entrant function is a function which can be interrupted during its execution. This type of function can be used to resume execution.
Because of this fact, re-entrant functions are thread-safe, means they can safely be interrupted by threads without any harm.
strtok_r() function has an extra parameter called the context. so that function can resume at the right place.
The syntax for strtok_r() function is as follows:
#include <string.h> char *strtok_r(char *string, const char *limiter, char **context);
Example
Following is the C program for the use of strtok_r() function −
#include <stdio.h> #include <string.h> int main(){ char input_string[] = "Hello Tutorials Point"; char token_list[20][20]; char* context = NULL; char* token = strtok_r(input_string, " ", &context); int num_tokens = 0; // Index to token list. We will append to the list while (token != NULL){ strcpy(token_list[num_tokens], token); // Copy to token list num_tokens++; token = strtok_r(NULL, " ", &context); } // Print the list of tokens printf("Token List:
"); for (int i=0; i < num_tokens; i++) { printf("%s
", token_list[i]); } return 0; }
Output
When the above program is executed, it produces the following result −
Token List: Hello Tutorials Point
- Related Articles
- What is strlen function in C language?
- What is strcoll() Function in C language?
- What is strspn() Function in C language?
- What is strcpy() Function in C language?
- What is strcat() Function in C language?
- What is strncat() Function in C language?
- What is strcmp() Function in C language?
- What is strncmp() Function in C language?
- What is strstr() Function in C language?
- What is strncpy() Function in C language?
- What is strrev() Function in C language?
- What is function prototype in C language
- What is exit() function in C language?
- What is strtok() function in C language?
- What is a malloc function in C language?

Advertisements