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
Call
What it does
std::cout << std::endl
Writes a new line and flushes cout.
std::cout << "Hi" << std::endl
Prints 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
Parameter
Description
os
The output stream. It is passed in for you when you write cout << endl. You never write it yourself.
Return Value
Return Value
Description
ostream&
A reference to the same stream, so you can keep chaining <<.