endl Function in C++
C++'s “std::endl” function is a powerful tool for manipulating and formatting output streams. Despite its apparent simplicity, this function has a big impact on the program's operation and efficiency. In this tutorial, we'll explore the details of “std::endl," including its usage, alternatives, and effect on C++ programs.
What Does a New Line in C++ Mean?
To know about the endl function in C++, we first need to know about the new Line in C++.
Not every time we print text in C++; we want every piece of information to appear on a single line. This may produce difficult-to-read results. Your program's users will struggle to locate particular topics within a lengthy block of text—also referred to as the feared "wall of text."
A text segment, commonly known as a "string" by C++ programmers, is a sequential string of characters that ends with the unique "\0" end-of-string character. Numerous other special characters exist, including tab {\t} and newline {\n} (the subject of this blog entry).
A newline in C++ is a unique character that indicates that a line in a text stream has ended. The escape sequence is used to indicate it. The newline character tells the output device to advance the cursor to the start of the following line when it is sent to a console, terminal, or file. In C++, "\n" is the character used for newline.
This is a simple example showing how to utilize the newline character in C++:
#include <iostream>
int main()
{
std::cout << “Hi!\n”;// Output "Hi!" followed by a newline
std::cout << “New Line\n”; // Output " New Line." followed by a newline
std::cout<<”Bye!!\n”; //Output “Bye!!” followed by newline.
return 0;
}
Output:
In this program's version, line breaks within the strings given to std::cout are indicated by the notation "\n." The output device is told to advance to the beginning of the next line each time "\n" is encountered, producing the same output as before but without explicitly utilizing std::endl.
The endl Function:
The endl function, is part of the standard function library in C++, adds a newline character to your output sequence and moves the following text to the next output line.
Endl must be added after an extraction operator in a cout statement. Std::endl is a manipulator defined in the header in C++, not just a function. It has two functions: it flushes the buffer of the stream and appends a newline character ('\n') to the output stream. This combination is appropriate for interactive or real-time applications where timely feedback is crucial since it guarantees that any buffered output is instantly written to the output device.
In the above example program, we used "\n" to get newline. Now, we use the endl function.
Example program using endl function:
Example 1:
#include <iostream>
int main()
{
std::cout << “Hi!” << std::endl; //Output "Hi!" followed by a newline
std::cout<<”New Line”<< std::endl; //Output "New Line" followed by a newline
std::cout<<”Bye!!”<< std::endl; //Output "Bye!!" followed by a newline
return 0;
}
Output:
The code uses C++'s std::endl to print "Hello!", "New Line" and "Bye!" each after a new line.
Example 2:
#include <iostream>
int main()
{
for (int num= 0; num < 10; num++)
{
std::cout << i << std::endl; // Output: i value followed by next line
}
return 0;
}
Output:
This code prints the value of “num” and a newline after each iteration using std::endl in a for loop that iterates from 0 to 9. The program returns 0 to indicate a successful termination after printing all values.
Difference Between \n and endl:
There are a few significant distinctions between the two instructions, even though “\n” and “endl” accomplish nearly the same thing.
Syntax:
endl: "endl" needs to be used on its own in a cout statement after an extraction operator (\<). The software would output endl as a string if you used quote marks around endl. Therefore, you should avoid doing that.
std::cout << “Text” << std::endl;
Example:
#include <iostream>
int main()
{
std::cout << "Hello!" <<std:: endl
std::cout<< "Good Morning" <<std:: endl;
return 0;
}
Output:
\n: "\n" needs to be enclosed in either single or double quotation marks ("or "). No extra formatting is necessary when placing "n" during a cout statement. If " n" is not enclosed in quotation marks, an error during compilation will occur.
std::cout << “Text\n”;
Example:
#include <iostream>
int main()
{
std::cout << "Hello!\n" ;
std::cout<< "Good Morning\n" ;
return 0;
}
Output:
Flushing the Output Stream:
endl: A program flushes the output buffer every time it uses the "endl" function. Writing an output each time "endl" is called causes the program to operate much slower since pulling from the output buffer takes time.
\n: "\n" does not flush the buffer for output. Because cout flushes automatically, the program writes all the output at once, cutting down on run time.