Latest web development tutorials

C library functions - strlen ()

C standard library - <string.h> C standard library - <string.h>

description

C library functionsize_t strlen (const char * str) calculate the length of thestringstr,until the end of the null character, but not including the null terminator character.

statement

Here is the strlen () function's declaration.

size_t strlen(const char *str)

parameter

  • str - the string length to be calculated.

return value

This function returns the length of the string.

Examples

The following example demonstrates the strlen () function is used.

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

int main ()
{
   char str[50];
   int len;

   strcpy(str, "This is w3cschool.cc");

   len = strlen(str);
   printf("|%s| 的长度是 |%d|\n", str, len);
   
   return(0);
}

Let's compile and run the above program, which will result in the following:

|This is w3cschool.cc| 的长度是 |20|

C standard library - <string.h> C standard library - <string.h>