Compute the length of a string, to a maximum number of bytes
#include <string.h> size_t strnlen( const char * s, size_t maxlen );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The strnlen() function computes the length of the string pointed to by s, not including the terminating null character, up to a maximum of maxlen bytes. The function doesn't check any more than the first maxlen bytes.
The minimum of maxlen and the number of characters that precede the terminating null character.
#include <stdio.h> #include <stdlib.h> #include <string.h> int main( void ) { printf( "%d\n", strnlen( "Howdy", 10 ) ); printf( "%d\n", strnlen( "Hello world\n", 5 ) ); printf( "%d\n", strnlen( "", 10 ) ); return EXIT_SUCCESS; }
produces the output:
5 5 0
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | Yes |
Signal handler | Yes |
Thread | Yes |