C++ endl Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, endl ends the current line. It does two things: it writes a new line character, and then it flushes the output stream, which sends all waiting text to the screen or file.

It is the most common way for beginners to start a new line: cout << "Hello" << endl;. 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::endl into an output stream. It needs no brackets.

C++
std::cout << "Hello" << std::endl;     // Hello and a new line
std::cout << "Hello" << '\n';          // new line, no flush
CallWhat it does
std::cout << std::endlWrites a new line and flushes cout.
std::cout << "Hi" << std::endlPrints Hi and moves to the next line.
std::cout << '\n'Writes only a new line. It is faster because there is no flush.

For most output, '\n' is enough. Use endl when the text must appear at once.

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

Parameters

ParameterDescription
osThe output stream. It is passed in for you when you write cout << endl. 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 — Print Two Lines with endl

Each endl ends a line.

C++
#include <iostream>

int main() {
    std::cout << "Hello" << std::endl;
    std::cout << "World" << std::endl;
    return 0;
}

How It Works

  1. std::cout << "Hello" prints the word. The cursor stays on the same line.
  2. std::endl writes a new line, so the cursor moves down. It also flushes the output.
  3. The same happens for World, so the two words are on separate lines.

Example 2 — endl and '\n' Look the Same

Both end a line. The difference is only the flush, which you cannot see here.

C++
#include <iostream>

int main() {
    std::cout << "first" << std::endl;
    std::cout << "second" << '\n';
    std::cout << "third" << "\n";
    return 0;
}

How It Works

  1. std::endl writes a new line and flushes.
  2. '\n' (a character) and "\n" (a string) write only the new line. On screen the result is the same.
  3. The flush matters for speed and for logs that must be saved at once, not for how the output looks.

Example 3 — endl Writes One Character

In a string stream the new line is one character in the text.

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

int main() {
    std::ostringstream out;

    out << "x" << std::endl;

    std::cout << "length: " << out.str().size() << '\n';
    std::cout << "last is newline: " << (out.str().back() == '\n' ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. out << "x" adds one character, so the text is x.
  2. endl adds the new line character and flushes. Flushing a string stream changes nothing, because it has no screen or file behind it.
  3. The text now has two characters, and the last one is the new line.

Frequently Asked Questions

It writes a new line to the output stream and then flushes the stream.
Both start a new line. endl also flushes the buffer, which is slower. '\n' only writes the new line.
It is a function that takes a stream. That is why it needs no brackets: << std::endl passes the function to the stream, which calls it.
When the output must appear immediately, for example before a long calculation or for a log. For normal output '\n' is fine.
Yes. ofstream << endl writes a new line and flushes the file buffer.

Next: ends

Write a null character to the output stream.

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

12 people found this page helpful