strlen() function in c

The strlen() function calculates the length of a given string.The strlen() function is defined in string.h header file. It doesn’t count null character ‘\0’.

Syntax:

int strlen(const char *str);

Parameter:



  • str: It represents the string variable whose length we have to find.

Return: This function returns the length of string passed.

Below programs illustrate the strlen() function in C:

Example 1:-

filter_none

edit
close

play_arrow

link
brightness_4
code

// c program to demonstrate
// example of strlen() function.
  
#include<stdio.h>
#include <string.h>
  
int main()
{
   char ch[]={'g', 'e', 'e', 'k', 's', '\0'};
  
   printf("Length of string is: %d", strlen(ch));
  
 return 0;
}

chevron_right


Output :

Length of string is: 5

Example 2:-

filter_none

edit
close

play_arrow

link
brightness_4
code

// c program to demonstrate
// example of strlen() function.
  
#include<stdio.h>
#include <string.h>
  
int main()
{
   char str[]= "geeks";
  
   printf("Length of string is: %d", strlen(str));
  
 return 0;
}

chevron_right


Output:

Length of string is: 5

Example 3:-

filter_none

edit
close

play_arrow

link
brightness_4
code

// c program to demonstrate
// example of strlen() function.
#include<stdio.h>
#include <string.h>
  
int main()
{
   char *str = "geeks";
  
   printf("Length of string is: %d", strlen(str));
  
 return 0;
}

chevron_right


Output :

Length of string is: 5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.