6.20.15 strlen Function

Finds the length of a string.

Include

<string.h>

Prototype

size_t strlen(const char *s);

Argument

s
the string

Return Value

Returns the length of a string.

Remarks

This function determines the length of the string, not including the terminating null character.

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

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

int main(void)
{
  char str1[20] = "We are here";
  char str2[20] = "";
  char str3[20] = "Why me?";

  printf("str1 : %s\n", str1);
  printf("\t(string length = %d characters)\n\n",
         strlen(str1));
  printf("str2 : %s\n", str2);
  printf("\t(string length = %d characters)\n\n",
         strlen(str2));
  printf("str3 : %s\n", str3);
  printf("\t(string length = %d characters)\n\n\n",
         strlen(str3));

}

Example Output

str1 : We are here
        (string length = 11 characters)

str2 :
        (string length = 0 characters)

str3 : Why me?
        (string length = 7 characters)