Home   |   About   |   Terms   |   Contact    
Read & Learn
 

Different ways to create a new line in C++

C++

☯ All C++ Articles     ☯ All Articles


A new product.




Popular Google Pages:



◕ This article is regarding Different ways to create a new line in C++
Last updated on: .



Introduction

There are some characters in C++ that we can’t enter into a program directly from the keyboard.

For example:

■ 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.

These are:

■ 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. ";



Related articles:
How to display a value in Hexadecimal form in C++?
How C++ identify the base of a number?
Symbolic Constant in C++




Popular Google Pages:



Top of the page

Amazon & Flipkart Special Products

   


Top of the page