strlen() function in C Programming Language

Rumman Ansari   Software Engineer   2019-03-31   8425 Share
☰ Table of Contents

Table of Content:


strlen( ) function in C gives the length of the given string. Syntax for strlen( ) function is given below.

Syntax

size_t strlen(const char *str);

Returns

The strlen( ) function returns the length of the null-terminated string pointed to by str. The null terminator is not counted.

Program

In below example program, length of the string "ILoveCProgrammingLanguage" is determined by strlen( ) function as below. Length of this string 25 is displayed as output.

#include 
#include 
 
int main( )
{
    int length;
    char array[50]="ILoveCProgrammingLanguage" ;
 
    length = strlen(array) ;
 
    printf ( "\string length  = %d \n" , length ) ;
    return 0;
}

Output

string length  = 25
Press any key to continue . . .
#include 
#include  
 
int main( )
{ 
    printf("%d \n", strlen("ILoveCProgrammingLanguage"));
    return 0;
}

Output

25
Press any key to continue . . .

Points to be Noted

  1. strlen( ) function counts the number of characters in a given string and returns the integer value.
  2. It stops counting the character when a null character is found. Because null character indicates the end of the string in C.