The difference between \n and \t: Newline character (\n): Adds a new line and moves the cursor to the next line. Tab (\t): Adds a horizontal tab, moving the cursor to the next tab position (default interval is 8 characters).
The difference between \n and \t in c
\n
and \t
are two escape sequences in C that represent newlines and tabs respectively.
Newline character (\n)
<code class="cpp">cout << "行 1" << endl; cout << "行 2" << endl;</code>
Tab character (\t)
<code class="cpp">cout << "姓名" << "\t" << "电话号码" << endl; cout << "约翰" << "\t" << "555-1234" << endl;</code>
Difference summary
Features | Line break (\n) | Tab (\t) |
---|---|---|
New line | Tab character | |
Vertical | Horizontal | |
1 character line | 8 character space | |
Not applicable | Allow alignment |
Example
<code class="cpp">// 换行符示例 cout << "这是一个多行字符串\n"; cout << "这是下一行"; // 制表符示例 cout << "姓名" << "\t" << "电话号码" << endl; cout << "约翰\t" << "555-1234" << endl;</code>
<code>这是一个多行字符串 这是下一行 姓名 电话号码 约翰 555-1234</code>
The above is the detailed content of What is the difference between n and t in c++. For more information, please follow other related articles on the PHP Chinese website!