When we run the code in C++ in the form of the output text, mostly we do not want that information to be displayed in one line. If it happens, the result is difficult to read. For users, it is tough to find breakpoints in the long block of the output. This is called the dreaded wall of text. A bulk of the text is also known as a string in C++. It is a sequence of the characters used to terminate the line with a special end of string characters. These special end of string characters are as follows.
The Stream Extract Operation
The stream extract operator is used to assemble the part of the output. It is also called the left shift operator used for bitwise processes. Technically, “<<” operator is an extraction operator that indicates text line as an outcome as well as commands that are executed as a result of cout statement. One cout statement should have more than one extraction operator and each of the operators represent a specific function in output. It is also used as a break of multiple types of output.
using namespace std;
int a = 3, b = 6;
cout << "The value of a is: " << a << ". The value of b is: " << b;
At the start of the program, we use a header file that is ‘#include <iostream.h>’ that means input-output stream. Then, we write the namespace. After that, we entered into the main body and we declared two variables with the name of “a” and “b”, respectively. With the support of the stream extraction operator, we print out variables during the initialization.
The endl Character
The endl character means the end of the line that is a part of the standard C++ functions libraries. Its purpose is to insert a new line in the display of the output sequence and move the output text that follows it into the next line of the output. To insert endl to the cout statement, programmer must add the stream extraction operator before the endl function. Here is the illustration of the endl operator.
using namespace std;
for (int i = 0; i < 10; i++)
cout << i << endl;
First of all, we use a header file which means this header file is an input-output stream. Then, we write the namespace quickly. after that, we start the main body. Here, we write for loop. In this loop, we initialized a variable with the name of “i” having a data type of int and the limit of 10 and increment it after getting the condition true. In the body of the loop, we print our variable and this loop is valid till the condition is true.
The \n Character
Here is the other way to break the line in C++ that is used as a newline character and its syntax is \n. Unlike endl, the \n must be in the double quotation where we are writing our text. Simply add \n in the middle of the string from where you want to break the line and start a new line.
using namespace std;
cout << "This is line one.\nThis is line two.";
After adding the header file, we enter the namespace. After that, we start the main body. We simply write a cout statement. In this statement, we write a string that contains two sentences and between these two sentences, we use \n character to break the line and start a new line.
Difference Between \n and endl
Although the endl and \n perform exactly the identical action. But still, there are some dissimilarities between both commands. First, both are having significantly changed syntax. The reason behind is the endl is a function on the other hand \n is a character. So that endl must be alone in the cout statement with stream extraction operator. You cannot use the endl function in the double quotation. This causes the program to output the endl as a string.
On the other side, \n must appear either in the double quotations or in the single quotation. You can easily add \n in anywhere of the cout statement without having any type of additional formatting. Failing to add \n in the single or double quotation will give us the compile error. Last but not least is the execution time of \n is less than the execution time of the endl statement as you can see in the above examples.
In this article, we defined how we can break the statement as well as string in the output. All the crucial components that a programmer must need to know to make its output as a formatted form. After placing these characters, you can see your output significantly changed. C++ standards offer these few ways to go to the new line by breaking the output up into a very easy-to-digest format.