In C, strlen() counts the characters in a C-string. It starts at the first character and stops when it reaches the ending '\0'. That ending mark is not included in the count.
You pass one string. "Hello" returns 5, and an empty string "" returns 0. The string must already end with '\0', or the count can run past the array. Include <string.h>.
Syntax
Pass the C-string you want to measure.
C
strlen("Hello");
Call
What it does
strlen("Hello")
Counts the letters in Hello. The result is 5.
Include <string.h>. Print the result with %zu.
Library declarations
C
size_t strlen(const char *s);
Parameters
Parameter
Description
str
Required. The C-string to measure. It must end with '\0'.
Return Value
Return Value
Description
Length
How many characters come before '\0'.
0
The string is empty, like "".
C Examples
Example 1 — Basic String Length
Call strlen on a literal and print the result with %zu (the portable format for size_t).