C strlen()

The strlen() function is defined in the string.h header file. It helps to calculate the length of a given string without including the terminating null character.


size_t strlen(const char *str); #where str should be a string

 

strlen() Parameters: 

The strlen() function takes a single parameter. While computing it doesn't count the null character \0.

Parameter Description Required / Optional
str  the string whose length is to be found Required

strlen() Return Value

The function returns the length of the string in a type size_t (the unsigned integer type).

Input Return Value
string length of string(integer)

Examples of strlen() 

Example 1: Working of strlen() function in C?


#include <stdio.h>
#include <string.h>

int main()
{
   char string[30];
   int length;

   strcpy(string, "learnetutorials.com");

   length = strlen(string);
   printf("The length of |%s| is |%d|\n", string, length);
   
   return(0);
}

Output:


The length of tutorialspoint.com| is |18|

Example 2: How strlen() works in C?


#include <stdio.h>
#include <string.h>

int main (){

    char x[30]="Tutorials";
    char y[30]={'T','u','t','o','r','i','a','l','s','\0'};

    // using the %zu format specifier to print size_t
    printf("Length of string x = %zu \n",strlen(x));
    printf("Length of string y = %zu \n",strlen(y));

    return 0;
}

Output:


Length of string x = 9
Length of string y = 9