strnlen()

Compute the length of a string, to a maximum number of bytes

Synopsis:

#include <string.h>

size_t strnlen( const char * s,
                size_t maxlen );

Arguments:

s
The string whose length you want to calculate.
maxlen
The maximum length to check.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

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.

Returns:

The minimum of maxlen and the number of characters that precede the terminating null character.

Examples:

#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

Classification:

POSIX 1003.1-2008 CX

Safety:  
Cancellation point No
Interrupt handler Yes
Signal handler Yes
Thread Yes

Last modified: 2013-12-23

comments powered by Disqus