Jump to content

C++ Lessons : 4.1. Output (New Lines)


quix
 Share

Recommended Posts

  • Manager

New Lines

To insert a new line, you can use the \n character:

Example

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World! \n";
  cout << "I am learning C++";
  return 0;
}

Tip: Two \n characters after each other will create a blank line:

Example

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World! \n\n";
  cout << "I am learning C++";
  return 0;
}

Another way to insert a new line, is with the endl manipulator:

Example

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!" << endl;
  cout << "I am learning C++";
  return 0;
}

Both \n and endl are used to break lines. However, \n is most used.

But what is \n exactly?

The newline character (\n) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.

1.png.56210348f845fad9f2d8f843d77e3c16.png

Click! : adelinrzo

Click! quiixx.

Click! : raizo

 

Guardian of Leaks!

 

image.png.3ca1bf60c26a3523b93df92ea8521802.png (24.01.2023)

image.png.990bdf55e8ebaad7e92cc11967598b8d.png (12.03.2023)

image.png.25e5d76f6f7de46f49423812d3d500a3.png (30.03.2023)

Link to comment
Share on other sites

  • quix locked this topic
Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...