Home >Backend Development >C++ >What is \n in c++
In
C, \n is a newline character, which moves the output position to the next line. Purpose: 1. Create a newline in a string and move the text to the next line. 2. Used for output, file operations and string operations. Compared with endl, endl not only wraps new lines, but also refreshes the output stream to ensure data is written. \n Only works on output streams, such as cout and ofstream. Note the newline conventions of different platforms, Windows uses "\r\n".
\n
In C, \n is an escape character, representing a newline character . When the output stream encounters \n, it moves the current output position to the next line.
Usage:
\n Mainly used to create line breaks in strings to move text to the next line. It can be used for output, file operations, or string operations.
Example:
<code class="cpp">// 在 cout 中使用 \n cout << "这是第一行" << endl; cout << "这是第二行" << endl;</code>
Output:
<code>这是第一行 这是第二行</code>
Difference from endl:
endl is also a An escape character used for newlines, but it is more comprehensive than \n. In addition to newlines, endl also flushes the output stream, ensuring that data is written to the device immediately.
Note:
The above is the detailed content of What is \n in c++. For more information, please follow other related articles on the PHP Chinese website!