C++ ends Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, ends writes one null character, the character with code 0, to an output stream. A null character marks the end of a C string.

Do not mix it up with endl. endl ends a line. ends ends a string. It is rarely needed today. Include <iostream> to use it.

This is a manipulator. You do not call it on its own. You send it into a stream with << or >>, and it changes how that stream behaves from then on (or does one small job, like ending a line).

Syntax

Put std::ends into an output stream.

C++
std::ostringstream out;
out << "ab" << std::ends;       // the text is a, b, and a zero character
CallWhat it does
out << std::endsWrites one character with the code 0.
out << "ab" << std::endsWrites three characters: a, b, and the null character.
out << std::endlDifferent: writes a new line and flushes.

With std::string and std::ostringstream you do not need ends. str() already gives a safe string.

Library declarations
C++
template<class CharT, class Traits>
basic_ostream<CharT, Traits>& ends(basic_ostream<CharT, Traits>& os);

Parameters

ParameterDescription
osThe output stream. It is passed in for you when you write out << ends. You never write it yourself.

Return Value

Return ValueDescription
ostream&A reference to the same stream, so you can keep chaining <<.

C++ Examples

Example 1 — See That ends Adds a Character

Compare the length of the text with and without ends.

C++
#include <iostream>
#include <sstream>

int main() {
    std::ostringstream a;
    a << "ab";

    std::ostringstream b;
    b << "ab" << std::ends;

    std::cout << "without ends: " << a.str().size() << '\n';
    std::cout << "with ends:    " << b.str().size() << '\n';
    return 0;
}

How It Works

  1. The first stream gets the two letters ab, so its text has 2 characters.
  2. The second stream gets the same letters plus ends, which adds one null character.
  3. The text of the second stream has 3 characters. The last one is invisible but real.

Example 2 — size() and strlen() Disagree

strlen() stops at the first null character, so it counts one character fewer.

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

int main() {
    std::ostringstream out;
    out << "hi" << std::ends;

    std::string s = out.str();

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

How It Works

  1. The text holds h, i, and the null character from ends.
  2. s.size() counts all three characters, because a std::string can hold zero characters.
  3. strlen() reads a C string and stops at the first null character, so it counts only hi.

Example 3 — A Null Character in the Middle of a String

ends can be used between pieces, but the text keeps all of them.

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

int main() {
    std::ostringstream out;
    out << "a" << std::ends << "b";

    std::string s = out.str();

    std::cout << "size: " << s.size() << '\n';
    std::cout << "second char is null: " << (s[1] == '\0' ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. out << "a" << std::ends << "b" writes a, a null character, and b.
  2. The string holds all three characters, so the size is 3.
  3. The character at index 1 is the null character. A std::string keeps it, but a C function like printf would stop there.

Frequently Asked Questions

It writes a null character (code 0) to the output stream.
endl writes a new line and flushes. ends writes a zero character, which marks the end of a C string, and does not flush.
No. Use str() to get a std::string, which knows its own length. ends is mostly for old code.
No. On screen it usually looks like nothing, but it is part of the text and counts in size().
With the old strstream classes, where the text had to end with a null before you could use it as a C string. Those classes are deprecated.

Next: fixed

Print floating-point numbers with a fixed number of decimals.

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

13 people found this page helpful