Popular Google Pages:
◕ Send a story and get ₹ 200/- More..
◕ Bengali Story writing competition. More..
◕ This article is regarding Different ways to create a new line in C++
Last updated on: 17th December 2016.
There are some characters in C++ that we can’t enter into a program directly from the keyboard.
■ In C++ we can’t make a newline by pressing the Enter key.
Instead we have to use a code in our source code file for the new line.
■ We can’t put Double Quotation Mark in the middle of a string literal like the following example:
cout << "We are from "India" " << endl;
To solve this problem we have to use Escape Sequence.
The right example is:
cout << "We are from \"India\" " << endl;
◕ What are the codes for a New line?
- We can use three types of code for a new line.
■ cout << endl; // using the endl manipulator
■ cout << '\n'; // using the '\n' character constant
■ cout << "\n"; // using the "\n" string
All three of the above codes move the cursor to the beginning of the next line.
◕ Where we should use endl or '\n'?
- When we use number, then endl is easier to use for new line.
- When we use a longer string, then '\n' is often more convenient than using endl.
Examples are given bellow:
cout << "Enter a number:" << endl;
cout << "\n\n We are Indian. \n We are proud to be an Indian. ";
How to display a value in Hexadecimal form in C++?
How C++ identify the base of a number?
Symbolic Constant in C++
Top of the page