The escape character "\n" functions as a newline character in C, which is used to force a newline in text output so that the text output wraps to the next line. It is also used to create multiline strings, control output formats, and perform file writing operations.
The role of \n in C
In C, the escape character "\n" represents a newline character, used to force line breaks in text output.
Uses
"\n" has the following main uses:
<code class="cpp">cout << "行 1" << "\n" << "行 2";</code>
Output:
<code>行 1 行 2</code>
<code class="cpp">string paragraph = "第 1 段\n" "第 2 段\n" "第 3 段";</code>
<code class="cpp">cout << left << setw(20) << "姓名" << "\n" << left << setw(20) << "邮箱";</code>
Output:
<code> 姓名 邮箱</code>
<code class="cpp">ofstream file("output.txt"); file << "行 1" << "\n" << "行 2";</code>
File content:
<code>行 1 行 2</code>
The above is the detailed content of What is the use of \n in c++. For more information, please follow other related articles on the PHP Chinese website!