In my current project, I am coding according to the C11 standard (building with gcc -std=c11
) and needed something like strnlen
(a "safe" version of strlen
which returns the length of a 0-terminated string, but only up to a given maximum). So I looked it up (e.g. https://en.cppreference.com/w/c/string/byte/strlen) and it seems the C11 standard mentions such a function, but with the name strnlen_s
.
Hence I went with strnlen_s
, but this turned out to be undefined when including string.h
. On the other hand, strnlen
is defined, so my current solution is to use strnlen
with a remark that the standard name seems to be strnlen_s
but that this is not defined by GCC.
The question is: am I correct to assume that strnlen
is the most portable name to use or what could I do for the code to be most portable/standard?
Note: Microsoft (https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strnlen-strnlen-s) implements both functions with the distinction that strnlen_s
checks if the string pointer is NULL
and returns 0
in that case while strnlen
has no such check.