\n Character
The new line character \n can be used as an alternative to end a line in C++ program. The backslash (\) is called an escape character and indicates a special character. Using a single cout statement with as many instances of \n as your program requires will print out multiple lines of text.
The simple program having a single break is shown below
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World! \n";
cout << "C++ is a beautiful language to clear basic concepts of
programming";
return 0;
}
If we use two \n\n characters together will create a blank line. Another way to insert a new line, is with the endl manipulator. Both \n and endl are used to break lines. However, \n is used more often and is the preferred way. A simple program below showing the use of creating single line and how to use endl.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World! \n\n";
cout << "C++ is a beautiful language to clear basic concepts of
programming";
return 0;
}
It will output as
Hello World
C++ is a beautiful language to clear basic concepts of programming
Use of endl in place of \n
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
cout << "I am a student";
return 0;
}
It will output as:
Hello World
I am a student