C++ strlen() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cstring>

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 <cstring>.

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.
Library declarations
C++
size_t strlen(const char* str);

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 strlen()

Count the characters in a greeting.

C++
#include <iostream>
#include <cstring>

int main() {
    const char* text = "Hello";

    std::cout << "Length: " << std::strlen(text) << '\n';
    return 0;
}

How It Works

  1. "Hello" has five letters.
  2. The hidden '\0' ends the string but is not counted.
  3. Result type is size_t.

Example 2 — strlen vs sizeof

Array storage includes the null terminator; strlen does not.

C++
#include <iostream>
#include <cstring>

int main() {
    char text[] = "Hello";

    std::cout << "strlen: " << std::strlen(text) << '\n';
    std::cout << "sizeof: " << sizeof text << '\n';
    return 0;
}

How It Works

  1. strlen counts visible characters: 5.
  2. sizeof text is the array size: 5 letters + '\0' = 6.
  3. Do not confuse array bytes with string length.

Example 3 — Prefer std::string::size()

Same idea with modern C++ strings.

C++
#include <iostream>
#include <string>

int main() {
    std::string text = "Hello";

    std::cout << "size: " << text.size() << '\n';
    return 0;
}

How It Works

  1. size() returns the stored length.
  2. No need to scan for '\0' yourself.
  3. Learn strlen for C-strings; prefer size() for std::string.

Frequently Asked Questions

strlen() counts the characters in a C-string and stops before the ending null character. That null is not included in the count.
str is the C-string to measure. It must end with a null character.
The number of characters before the null. An empty string returns 0.
No. The ending null marks where the count stops, and it is not included.
Use strlen() for a char* C-string. For a std::string, use size() or length().

Next: strncat()

Append at most n characters from one C-string onto another.

strncat() 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.

8 people found this page helpful