Code Example Demonstrating Implicit Declaration of strtok() Function

In C90, when a function is not explicitly declared (which is allowed in C99 for functions without prototypes), the default return value is used. To resolve the issue, it is recommended to declare the strcmp function before using it.


Solution:

The C Standard’s Annex K defines

strtok_s

, which are known as secure functions. While these functions are optional, they are mostly available on Microsoft systems, although their semantics differ from the standard functions.

Therefore, it is advisable to avoid using these functions as they are not portable.

It’s possible that

<string.h>

was not included, which could be another explanation. To ensure all possible explanations are considered, it’s recommended to post a complete program.

If your system supports it, you can use the POSIX alternative

strtok_r

instead of

strtok()

, which although has some shortcomings, is not problematic in your code.

Keep in mind that providing an empty string as the delimiter string serves no purpose. Instead, the second argument should consist of a string that contains feasible delimiters, such as

' '

,

' '

, and

'
'

, for whitespace or explicit delimiters like

','

.


The prototype specified in the Microsoft documentation is as follows:

char* strtok_s(
   char* str,
   const char* delimiters,
   char** context
);

While the C Standard includes a specific one.

#define __STDC_WANT_LIB_EXT1__ 1
#include 
char *strtok_s(char * restrict s1,
               rsize_t * restrict s1max,
               const char * restrict s2,
               char ** restrict ptr);

The prototypes exhibit dissimilarities and are not in harmony with each other. It is advised not to employ this particular function.

The prototype of

strtok_r

, which is a reentrant version of

strtok

, is defined in the POSIX function.

char *strtok_r(char *restrict str, const char *restrict delim,
               char **restrict saveptr);

Instead of relying on Microsoft’s specific version of

strtok_s

, a more versatile option is to utilize

strtok_r

and include the necessary lines for Windows compatibility.

#ifdef _MSC_VER
#define strtok_r(s,d,c)  strtok_s(s,d,c)
#endif

To obtain a comprehensive understanding of Annex K functions, refer to this inquiry.

Do you use the TR
24731 ‘safe’ functions
?

Frequently Asked Questions