C++ std::string length() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

Definition

In C++, std::string::length() returns the character count of the string. For std::string it is the same as size().

Beginners often prefer length() because it sounds like text. Library code often uses size() to match other containers. Include <string>.

This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.

Syntax

Call length with empty brackets.

C++
std::size_t n = s.length();
CallWhat it does
s.length()Returns the current character count. Same as s.size().

Do not mix this up with strlen from <cstring>. strlen walks a C-string until '\\0'. length() reads the stored size of a std::string.

Library declarations
C++
size_type length() const noexcept;

Parameters

ParameterDescription
Nonelength() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
size_typeNumber of characters in the string.

C++ Examples

Example 1 — Basic length()

Measure a short word.

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

int main() {
    std::string word = "fun";
    std::cout << word.length() << '\n';
    return 0;
}

How It Works

  1. The string stores three characters: f, u, n.
  2. length() returns 3.
  3. That matches what size() would return.

Example 2 — length() Equals size()

Print both for the same string.

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

int main() {
    std::string s = "Code";
    std::cout << "length: " << s.length() << '\n';
    std::cout << "size:   " << s.size() << '\n';
    std::cout << std::boolalpha
              << "equal:  " << (s.length() == s.size()) << '\n';
    return 0;
}

How It Works

  1. Both calls read the same stored count.
  2. Each prints 4 for "Code".
  3. The equality check confirms they match.

Example 3 — Loop Using length()

Print each character using a length-based index loop.

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

int main() {
    std::string s = "ABC";
    for (std::size_t i = 0; i < s.length(); ++i) {
        std::cout << s[i] << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The loop runs while i is less than length().
  2. Indices 0, 1, and 2 read A, B, and C.
  3. A range-based for loop is often clearer, but indexed loops are fine when you need the index.

Frequently Asked Questions

It returns how many characters are in the std::string.
Either. They return the same value. size() matches other STL containers; length() reads naturally for text.
length() is a std::string member and is O(1). strlen() scans a char* until '\0' and is O(n).
Yes. An empty string has length 0, and empty() returns true.
It counts char elements (bytes for char). Multi-byte UTF-8 characters may use more than one char.

Next: max_size()

Keep going through the string functions. Return the theoretical maximum number of characters this string type can hold.

max_size() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

11 people found this page helpful