C strlen() library function

The function size_t strlen(const char *str);calculates the length of a string. The length of a C string is the number of character before terminating null character('\0').

Function prototype of strlen

size_t strlen(const char *str);
  • str : This is the string whose length is to be found.

Return value of strlen

This function returns the length of string.

C program using strlen function

The following program shows the use of strlen function to find the length of string.

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

int main()
   char string[100];
   int length;
   printf("Enter a string\n");
   scanf("%s", &string);
   length = strlen(string);

   printf("Length of %s is %d", string, length);

Enter a string
Length of TechCrashCourse is 15