Skip to content
C++ Better Explained
Go back
C++ String Length: size() vs length() vs strlen()

C++ String Length: size() vs length() vs strlen()

Short answer: for a std::string, call s.length() or s.size() — they are the same function. For a C-style char*, use strlen(s).

std::string s = "hello";
std::cout << s.length();   // 5
std::cout << s.size();     // 5  — identical

size() and length() Are Identical

This trips people up because it looks like there must be a difference. There is not:

std::string s = "hello world";

std::cout << s.length() << '\n';   // 11
std::cout << s.size()   << '\n';   // 11

Both return the number of characters, both are O(1), both return size_t. The duplication is historical: length() reads naturally for text, while size() is the name every other STL container uses. Template code that might receive a vector or a string uses size() for consistency — that is the only real reason to prefer one.

strlen() Is for char Arrays

#include <cstring>

const char* c = "hello";
std::cout << std::strlen(c);      // 5

strlen counts bytes until it hits the null terminator '\0'. That has two consequences worth knowing:

std::string::size() has neither problem because the length is stored, not computed. This is one of several reasons to prefer std::string over raw char arrays — see string vs char array.

Learning C++ properly? The C++ Better Explained Ebook covers strings and the STL in plain English — 87 pages, just $19.

sizeof Is Not String Length

A very common mistake:

char arr[] = "hello";
std::cout << sizeof(arr);       // 6 — includes the '\0'
std::cout << std::strlen(arr);  // 5

const char* p = "hello";
std::cout << sizeof(p);         // 8 — the size of a POINTER

std::string s = "hello";
std::cout << sizeof(s);         // ~32 — the size of the object, not the text

sizeof is a compile-time question about types and storage. It never tells you how many characters a string holds.

The Unsigned Length Trap

length() returns size_t, which is unsigned. That makes this loop infinite on an empty string:

// BROKEN when s is empty
for (size_t i = 0; i <= s.length() - 1; ++i) {
    std::cout << s[i];
}

If s.length() is 0, then s.length() - 1 is not −1 — it wraps to roughly 18 quintillion, so the loop runs far past the end and reads memory it does not own.

Safe alternatives:

for (size_t i = 0; i < s.length(); ++i) { ... }     // < not <=

for (char c : s) { ... }                            // best

The same trap appears when comparing a length against a signed number:

int n = -1;
if (s.length() > n) { ... }    // n converts to a huge unsigned value

Compilers warn about this as a signed/unsigned comparison. It is worth fixing rather than silencing.

Checking for an Empty String

if (s.empty()) { ... }         // clear and O(1)
if (s.length() == 0) { ... }   // same thing, noisier

Prefer empty(). On other containers it can be cheaper than computing a size, and it states the intent directly.

Length of Unicode Text

length() returns bytes, not visible characters:

std::string s = "café";
std::cout << s.length();       // 5, not 4 — é takes two bytes in UTF-8

For ASCII these are the same. For anything international they are not, and counting user-perceived characters correctly needs a Unicode library. If you are validating a length limit on user input, be aware you are limiting bytes.

Quick Reference

You haveGet length withCost
std::strings.size() or s.length()O(1)
char* / char[]std::strlen(s)O(n)
Checking for emptys.empty()O(1)
Array storage sizesizeof(arr)compile time

Take Your C++ Further

If you want strings and the STL explained properly rather than looked up piecemeal, the C++ Better Explained Ebook covers the fundamentals in plain English. Just $19.

👉 Get the C++ Better Explained Ebook — $19


📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Share this post on:

Written by

Sahil Bora

Software Engineer. Author and creator of C++ Better Explained.


Previous Post
How to Check if a String Is a Number in C++
Next Post
How to Write to a File in C++ (ofstream Explained)

Keep Learning