C++ std::string c_str() Function

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

Definition

In C++, std::string::c_str() returns a const char* to a null-terminated version of the string\'s text.

Use it when a C library function needs a const char*. 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 with empty brackets and pass the pointer to C APIs.

C++
const char* p = s.c_str();
std::puts(s.c_str());
CallWhat it does
s.c_str()Returns const char* to a null-terminated array owned by the string.

Do not modify through c_str(). Do not free it. Prefer keeping the std::string alive while the pointer is used.

Library declarations
C++
const char* c_str() const noexcept;

Parameters

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

Return Value

Return ValueDescription
const char*Pointer to a null-terminated character sequence.

C++ Examples

Example 1 — Basic c_str()

Print using a C function.

C++
#include <cstdio>
#include <string>

int main() {
    std::string s = "Hello C++";
    std::puts(s.c_str());
    return 0;
}

How It Works

  1. puts expects a C-string.
  2. c_str() supplies a null-terminated pointer.
  3. The text prints followed by a newline from puts.

Example 2 — Measure With strlen

Compare strlen(c_str()) with size().

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

int main() {
    std::string s = "abcd";
    std::cout << "size: " << s.size() << '\n';
    std::cout << "strlen: " << std::strlen(s.c_str()) << '\n';
    return 0;
}

How It Works

  1. size() is the std::string length.
  2. strlen(c_str()) walks until '\\0'.
  3. For ordinary text without embedded nulls, both counts match.

Example 3 — Pointer Lifetime

Use c_str() while the string still exists.

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

int main() {
    std::string s = "live";
    const char* p = s.c_str();
    std::cout << p << '\n';
    return 0;
}

How It Works

  1. We take the pointer only after s exists.
  2. We print while s is still in scope.
  3. Returning p after s is destroyed would be a bug—we avoid that.

Frequently Asked Questions

A const char* to a null-terminated character array with the same text as the string.
No. It is const. Use non-const data() (C++17) or indexed assignment instead.
No. The std::string owns the memory.
If the string is destroyed, moved, or reallocates its buffer.
Use c_str() when calling C APIs that want const char*. data() is useful when you need a writable pointer (C++17).

Next: capacity()

Keep going through the string functions. How many characters the current buffer can hold without reallocating.

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

14 people found this page helpful