Difference Between strtok() and strtok_r()

Let us see the differences between strtok() and strtok_r() functions in a tabular form as shown below:

S.No.

strtok()

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.

 

 



strtok() and strtok_r() functions in C with examples

C provides two functions strtok() and strtok_r() for splitting a string by some delimiter. Splitting a string is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array.

Similar Reads

strtok() Function

The strtok() method splits str[] according to given delimiters and returns the next token. It needs to be called in a loop to get all tokens. It returns NULL when there are no more tokens....

strtok_r() Function

...

Difference Between strtok() and strtok_r()

...