C strlen() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
string.h

Definition

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");
CallWhat 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

ParameterDescription
strRequired. The C-string to measure. It must end with '\0'.

Return Value

Return ValueDescription
LengthHow many characters come before '\0'.
0The 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).

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

int main(void) {
    const char *s = "Hello";
    size_t len = strlen(s);

    printf("length = %zu\n", len);
    return 0;
}

How It Works

  1. Point at the string. s refers to "Hello" ending with '\0'.
  2. Count characters. strlen walks H e l l o and stops at the terminator.
  3. Store the result. len is 5 ('\0' is not included).
  4. Print with %zu. That format matches size_t on modern compilers.

Example 2 — Empty String

An empty C-string is just '\0' at index 0. Its length is zero.

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

int main(void) {
    const char *s = "";

    printf("length = %zu\n", strlen(s));
    return 0;
}

How It Works

  1. Empty means terminator only. s[0] is already '\0'.
  2. Loop never runs. The count stays at 0 and returns immediately.
  3. Still a valid string. "" is a legal C-string of length 0.
  4. Useful check. if (strlen(s) == 0) detects empty input (or compare s[0] == '\0').

Example 3 — sizeof vs strlen

sizeof is the array’s capacity. strlen is how many characters are stored before '\0'.

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

int main(void) {
    char buf[16] = "Hi";

    printf("sizeof  = %zu\n", sizeof buf);
    printf("strlen  = %zu\n", strlen(buf));
    return 0;
}

How It Works

  1. Array size. buf has 16 bytes of storage → sizeof buf is 16.
  2. Stored text. Only "Hi" is written (plus '\0'), so length is 2.
  3. Different questions. Capacity vs content length—do not mix them up.
  4. Copy tip. To duplicate the string you need strlen(buf) + 1 bytes, not sizeof buf unless you want the whole array.

Frequently Asked Questions

strlen() counts characters before the ending '\0'. It does not count the terminator.
str is required. It must be a valid null-terminated C-string.
A size_t length. An empty string returns 0.
Include <string.h>.
No. strlen() counts at runtime until '\0'. sizeof on an array returns the array size in bytes.

Next: strcmp()

Compare two null-terminated C-strings lexicographically.

strcmp() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful