To insert a new line, you can use the \n
character:
#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:
#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:
#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.
\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 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!