Home >Backend Development >C++ >How to use /n in c++
The newline character in C is \n, which is used to move the cursor to the beginning of the next line. You can: Output the newline character: Use std::cout to output \n to the console or file. To include a newline character in a string: use the escape sequence \n. Reading newline characters: Use std::getline() to read a line of text from the input.
Using newlines in C
In C, \n
is a newline character, used to move the cursor to the beginning of the next line. It can be generated by the escape sequence '\\n'
.
Usage:
std::cout
to \n
Output to console or file. For example: <code class="cpp">std::cout << "Hello" << std::endl; // 输出 "Hello" 并换行</code>
'\\n'
Newline character. For example: <code class="cpp">std::string str = "Hello\nWorld"; // 字符串包含一个换行符</code>
std::getline()
to read a line of text from the input, including newlines symbol. For example: <code class="cpp">std::string line; std::getline(std::cin, line); // 从标准输入读取一行文本</code>
Other line breaks:
In addition to \n
, C has the following line breaks:
Note:
std::getline()
, line breaks are usually automatically removed. std::endl
macro, which can automatically handle the differences in newline characters in different operating systems. The above is the detailed content of How to use /n in c++. For more information, please follow other related articles on the PHP Chinese website!