Strtok_r in C

In this article, we will discuss the strtok_r() function in C with their syntax and examples.

The two functions strtok() and strtok_r() in C allow us to divide a string by a delimiter. The task of splitting a string is quite frequent. For instance, we might desire individual things in an array from a comma-separated list of objects in a file.

What is the strtok() Function?

Str[] is divided using the specified delimiters by the strtok() method, which returns the subsequent token. It must be called repeatedly to obtain all tokens. When there are no more tokens, it returns NULL.

Syntax of strtok():

It has the following syntax:

Parameters:

str: It is the pointer to the string to be tokenized.

delims: It is a string containing all delimiters.

Return Value:

It returns the pointer to the first token encountered in the string.

It returns NULL if there are no more tokens found.

Program:

Let's take an example to split a string using the strtok() function in C.

Output:

Strtok_r in C

What is strtok_r() Function?

The Strtok_r() function performs the same task of parsing a string into a sequence of tokens as the strtok() function in C. As a reentrant variant of strtok(), strtok_r() is thread safe.

Syntax of strtok_r():

It has the following syntax:

Parameters:

str: It is the pointer to the string to be tokenized.

delims: It is a string containing all delimiters.

saveptr: It is a pointer to a char * variable that is used internally by strtok_r() to maintain context between successive calls that parse the same string.

Return Value:

It returns the pointer to the first token encountered in the string.

It returns NULL if there are no more tokens found.

Program:

Let's take an example to split a string using the strtok_r() function in C.

Output:

Strtok_r in C

Difference Between strtok() and strtok_r()

There are several differences between the strtok() and strtok_r() in C. Some main difference between the strtok() and strtok_r() in C are as follows:

S.nostrtok()strtok_r()
1.It is used to break string str into a series of tokens.It is used to decode a string into a pattern for tokens.
2.The syntax is as follows: char *strtok(char *str, const char *delim)Its syntax is as follows: char *strtok_r(char *string, const char *limiter, char **context);
3.It uses the delimiter to proceed.It is a re-entered variant of strtok().
4.It takes two parameters.It takes three parameters.
5.It returns a pointer to the first token found in the string.It returns a pointer to the first token found in the string.
6.It is not thread-safe.It is thread-safe.

Benefits of strtok_r()

A string can be tokenized (divided) into smaller substrings using the strtok_r() method, which is a version of the strtok() function in C and C++. Strtok_r() is thread-safe and reentrant, making it a safer option in multithreaded or reentrant applications. The main distinction between strtok() and strtok_r() is that strtok_r() is thread-safe and reentrant. The advantages of using strtok_r() are as follows:

  1. Thread Safety: Strtok_r() needs you to give a pointer to a saveptr parameter in contrast to strtok(), which uses a static internal pointer to keep track of the current parsing position. As a result, strtok_r() can be called concurrently without interfering with other threads because each thread can have its saveptr, making strtok_r() thread-safe.
  2. Reentrancy: Because strtok_r() is reentrant, it can be called without risk from inside signal handlers or nested function calls without affecting how other strings are parsed. It increases its adaptability to complex program frameworks.
  3. Predictable Behavior: Compared to strtok() function, which depends on a hidden static pointer, strtok_r()'s behaviour is simpler to comprehend and forecast because the saveptr pointer is clearly provided. It can make it easier to debug and maintain code that utilizes strtok_r() function.
  4. Portability: Strtok_r() is a standardized function specified in the POSIX standard (IEEE Std 1003.1), making it more portable across multiple platforms and systems. Strtok() is widely available and supported in most C and C++ libraries.
  5. Custom Parsing: By repeatedly invoking strtok_r() with various delimiters and the same saveptr, you can tokenize a text with numerous delimiters. When working with complex input data, this flexibility can come in helpful.'

Conclusion:

In conclusion, strtok_r() is a more secure and flexible substitute for strtok() for tokenizing strings in a multithreaded or reentrant context. In these situations, its explicit saveptr parameter and thread safety make it the best option.