C Standard Library

C <string.h> - strlen() Function



The C <string.h> strlen() function returns the length of a byte string, which is, the number of characters in the byte string whose first element is pointed to by str (without including the terminating null character).

The behavior is undefined if there is no null character in the character array pointed to by str.

Syntax

size_t strlen ( const char * str );

Parameters

str Specify pointer to the character array.

Return Value

Returns the length of a byte string.

Example:

The example below shows the usage of strlen() function.

#include <stdio.h>
#include <string.h>
 
int main (){
  char str1[50] = "Hello World!";
  char str2[50] = "Programming is easy.";

  printf("str1 contains: %ld characters.\n", strlen(str1));
  printf("str2 contains: %ld characters.\n", strlen(str2));   
  return 0;
}

The output of the above code will be:

str1 contains: 12 characters.
str2 contains: 20 characters.

❮ C <string.h> Library

5