‘\n’ 换行,光标移到下一行的开头;
'\r' 回车,光标移到当前行的开头,不会换到下一行,如果接着输出的话,本行以前的内容会被逐一覆盖;
#include <iostream>
using namespace std;
int main()
{
cout << "this is the first line\n";
cout << "this is the second line\r";
cout << "this is the third line\n";
cout << "this is the fouth line\r";
cout << "this is the fifth line\n";
cout<<"First"<<"\n"<<"Second"<<endl;
cout<<"First123"<<"\r"<<"Second"<<endl;
return 0;
}

本文通过一个简单的C++程序示例,详细解释了在C++中使用'
'和'
'字符的效果差异。'
'用于换行,将光标移动到下一行的开头;而'
'则用于回车,将光标移回到当前行的开头,若随后继续输出,则会覆盖原有内容。通过实际代码运行结果,读者可以直观地理解这两种转义字符的作用。
4383

被折叠的 条评论
为什么被折叠?



