Updated June 8, 2023
Introduction to C++ endl
A predefined object of the class called iostream class is used to insert the new line characters while flushing the stream is called endl in C++. This endl is similar to \n which performs the functionality of inserting new line characters but it does not flush the stream whereas endl does the job of inserting the new line characters while flushing the stream. Hence the statement cout<<endl; will be equal to the statement cout<< ‘\n’ << flush; meaning the new line character used along with flush explicitly becomes equivalent to the endl statement in C++.
Syntax:
The syntax of C++ endl is as follows:
cout<< statement to be executed <<endl;
Working of C++ endl
- Whenever the program is writing the output data to the stream, all the data will not be written to the terminal at once. Instead, it will be written to the buffer until enough data is collected in the buffer to output to the terminal.
- But if are using flush in our program, the entire output data will be flushed to the terminal directly without storing anything in the buffer.
- Whenever there is a need to insert the new line character to display the output in the next line while flushing the stream, we can make use of endl in C++.
- Whenever there is a need to insert the new line character to display the output in the next line, we can make use of endl in ‘\n’ character but it does not do the job of flushing the stream. So if we want to insert a new line character along with flushing the stream, we make use of endl in C++.
- Whenever the program is writing the output data to the stream, all the data will not be written to the terminal at once. Instead, it will be written to the buffer until enough data is collected in the buffer to output to the terminal.
Examples of C++ endl
Following are the examples of c++ endl:
Example #1
C++ program to demonstrate endl in a program to print the given statements in a new line:
Code:
//The header file iostream is imported to enable us to use cout in the program
#include <iostream>
//a namespace called std is defined
using namespace std;
//main method is called
intmain( )
{
//cout is used to output the statement
cout<< "Welcome to ";
//cout is used to output the statement along with endl to start the next statement in the new line and flush the output stream
cout<< "C#"<<endl;
//cout is used to output the statement along with endl to start the next statement in the new line and flush the output stream
cout<< "Learning is fun"<<endl;
}
Output:
In the above program, the header file iostream is imported to enable us to use cout in the program. Then a namespace called std is defined. Then the main method is called. Then the cout is used to output the statement. Then again cout is used to output the statement along with endl to start the next statement in the new line and flush the output stream.
Example #2
C++ program to demonstrate endl in a program to prompt the user to enter his or her name while using endl to print each statement in the next new line while flushing the output stream:
Code:
//The header file iostream is imported to enable us to use cout in the program
#include <iostream>
//a namespace called std is defined
using namespace std;
//main method is called
intmain( )
{
//a string variable is used to store the name entered by the user
string name;
//cout is used to output the statement to prompt the user to enter his name with endl used in the statement so that the next statement is printed in the next new line
cout<< "Please enter your name: " <<endl;
//cin takes the name of the user entered by the user
cin>> name;
//cout is used to output the statement along with endl to start the next statement in the new line and flush the output stream
cout<< "My name is: "<< name <<endl;
}
Output:
In the above program, the header file iostream is imported to enable us to use cout in the program. Then a namespace called std is defined. Then the main method is called. Then a string variable is used to store the name entered by the user. Then cout is used to output the statement to prompt the user to enter his name withendl used in the statement so that the next statement is printed in the next new line. Then in takes, the name of the user entered by the user. Then cout is used to output the statement along with endl to start the next statement in the new line and flush the output stream.
Advantages
There are several advantages of using endl in C++. They are:
- Whenever the program is writing the output data to the stream, all the data will not be written to the terminal at once. Instead, it will be written to the buffer until enough data is collected in the buffer to output to the terminal.
- But if are using flush in our program, the entire output data will be flushed to the terminal directly without storing anything in the buffer.
- By making use of endl, we can insert the new line character to display the output in the next line while flushing the stream.
- There is no necessity to explicitly use flush if we are using endl in the program to flush the output stream.
Recommended Articles
This is a guide to C++ endl. Here we also discuss the introduction and working of c++ endl along with different examples and its code implementation. You may also have a look at the following articles to learn more –