BEST SITE FOR WEB DEVELOPERS

C++ Tutorial

C++ HOME C++ Intro C++ Get Started C++ Syntax C++ Output C++ Comments C++ Variables C++ User Input C++ Data Types C++ Operators C++ Strings C++ Math C++ Booleans C++ Conditions C++ Switch C++ While Loop C++ For Loop C++ Break/Continue C++ Arrays C++ Structures C++ Enums C++ References C++ Pointers

C++ Functions

C++ Functions C++ Function Parameters C++ Function Overloading C++ Scope C++ Recursion

C++ Classes

C++ OOP C++ Classes/Objects C++ Class Methods C++ Constructors C++ Access Specifiers C++ Encapsulation C++ Inheritance C++ Polymorphism C++ Files C++ Exceptions C++ Date

C++ How To

Add Two Numbers Random Numbers

C++ Reference

C++ Reference C++ Keywords C++ <iostream> C++ <fstream> C++ <cmath> C++ <string> C++ <cstring> C++ <ctime>

C++ Examples

C++ Examples C++ Compiler C++ Exercises C++ Quiz C++ Certificate

C++ Language. W3Schools in English. Lessons for beginners

Ua Es De

C++ New Lines


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;
}
Try it Yourself »

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;
}
Try it Yourself »

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;
}
Try it Yourself »

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.

Examples of other valid escape sequences are:

Escape Sequence Description Try it
\t Creates a horizontal tab Try it
\\ Inserts a backslash character (\) Try it
\" Inserts a double quote character Try it


Comments