I don't have enough reputation to comment on other answers, so I'll have to provide my own.
1) To address this statement:
"strtok_s is a buffer overrun safe version of strtok on Windows. The
standard strtok on windows is thread safe..."
This is not true. strtok_s is the thread safe version for the MSVC compiler. strtok is not thread safe!
2) To address this statement:
"This would probably break if compiling on Cygwin which reports itself
as windows but has POSIX interfaces like strtok_r
already defined."
Again, not true. The difference is which compiler you use. When using Microsoft's Visual C++ compiler, MSVC, the function is strtok_s.
Another compiler, such as the GNU Compiler Collection, GCC, may use a different standard library implementation such as strtok_r
. Think compiler, not target platform, when identifying which function to use.
In my opinion, Joachim Pileborg's answer is the best one on this page. However, it needs a small edit:
#if defined(_WIN32) /* || defined(_WIN64) */
#define strtok_r strtok_s
#endif
Both _WIN32 and _WIN64 are predefined macros provided by the MSVC compiler. _WIN64 is defined when compiling a 64 bit target. _WIN32 is defined for both 32 and 64 bit targets. This is a compromise that Microsoft made for backwards compatibility. _WIN32 was created to specify the Win32 API. Now you should consider _WIN32 to specify Windows API -- it is not specific to a 32 bit target.
strtok_r()
is POSIX;strtok_s()
is Windows. Use plainstrtok()
which is available in all implementations (supporting C89). – Gabrielegabriellstrtok
is such a horrible interface. It is stateful and non-reentrant. – Aviculture