\n(换行符)和\r(回车符)有什么区别?
特别地,\n和\r之间有什么实际的区别吗?是否有应该使用其中一种而不是另一种的地方?
\n(换行符)和\r(回车符)有什么区别?
特别地,\n和\r之间有什么实际的区别吗?是否有应该使用其中一种而不是另一种的地方?
当前回答
\n(换行符)和\r(回车符)有什么区别? 特别地,\n和\r之间有什么实际的区别吗?是否有应该使用其中一种而不是另一种的地方?
我想用各自的转义序列\n(换行符)和\r(回车符)做一个简短的实验,以说明它们之间的明显区别。
我知道,这个问题是独立于语言的。尽管如此,我们至少需要一种语言来完成这个实验。在我的例子中,我选择了c++,但这个实验通常适用于任何编程语言。
该程序只是通过for循环迭代将一个句子打印到控制台。
换行符计划:
#include <iostream>
int main(void)
{
for(int i = 0; i < 7; i++)
{
std::cout << i + 1 <<".Walkthrough of the for-loop \n"; // Notice `\n` at the end.
}
return 0;
}
输出:
1.Walkthrough of the for-loop
2.Walkthrough of the for-loop
3.Walkthrough of the for-loop
4.Walkthrough of the for-loop
5.Walkthrough of the for-loop
6.Walkthrough of the for-loop
7.Walkthrough of the for-loop
注意,此结果不会在任何系统上提供,您正在执行此c++代码。但它适用于最现代的系统。阅读下文了解更多细节。
现在,同样的程序,但不同的是,在打印序列的末尾,\n被\r取代。
车厢返回程序:
#include <iostream>
int main(void)
{
for(int i = 0; i < 7; i++)
{
std::cout << i + 1 <<".Walkthrough of the for-loop \r"; // Notice `\r` at the end.
}
return 0;
}
输出:
7.Walkthrough of the for-loop
注意到区别在哪了吗?不同之处在于,当您在每个打印序列的末尾使用回车返回转义序列\r时,此序列的下一次迭代不会进入下面的文本行-在每个打印序列的末尾,光标不会跳转到下一行的*开头。
相反,在使用\r字符之前,光标跳回到该行的开头,他一直在该行的末尾。-结果是,打印序列的每个后续迭代都将取代前一个。
*注:A \n不一定跳转到下一行文本的开头。在一些较老的操作系统上,\n换行符的结果可能是,它跳转到下一行的任何位置,而不仅仅跳转到开头。这就是为什么,他们需要使用\r \n来获得在下一个文本行的开始。
这个实验向我们展示了在打印序列的迭代输出中换行符和回车符的区别。
在讨论程序中的输入时,一些终端/控制台可能会隐式地将回车符转换为换行符,以获得更好的可移植性、兼容性和完整性。
但是,如果您可以选择为另一个选择,或者希望或需要显式地只使用特定的一个,那么您应该始终使用符合其目的并严格区分的那个操作。
其他回答
In windows, the \n moves to the beginning of the next line. The \r moves to the beginning of the current line, without moving to the next line. I have used \r in my own console apps where I am testing out some code and I don't want to see text scrolling up my screen, so rather than use \n after printing out some text, of say, a frame rate (FPS), I will printf("%-10d\r", fps); This will return the cursor to the beginning of the line without moving down to the next line and allow me to have other information on the screen that doesn't get scrolled off while the framerate constantly updates on the same line (the %-10 makes certain the output is at least 10 characters, left justified so it ends up padded by spaces, overwriting any old values for that line). It's quite handy for stuff like this, usually when I have debugging stuff output to my console screen.
一点历史
/r代表返回或回车,它的历史归功于打字机。一个回车符将你的回车符一直向右移动,这样你就在行首输入了。
/n代表新行,同样,从打字的日子开始,你移动到新的行。但不一定要在开头,这就是为什么一些操作系统同时需要/r返回和/n换行符,因为打字机的换行顺序是这样的。这也解释了旧的8位计算机使用Return而不是Enter,从回车中返回,这是很熟悉的。
因为没有人特别提到它(他们是不是太小了,不知道/不记得?)-我怀疑\r\n的使用起源于打字机和类似的设备。
当你在使用多行打字机时,想要打出新的一行,它必须执行两个物理操作:将滑架滑回页面的开头(在美国是左),并将纸张上一个缺口。
在行式打印机的时代,打印粗体文本的唯一方法,例如,是做一个没有换行符的回车,并在旧的字符上打印相同的字符,从而添加更多的墨水,从而使它看起来更暗(粗体)。当打字机的机械式“换行”功能失效时,令人讨厌的结果是:如果你不注意,你可能会把上一行文本敲过。
#include <stdio.h>
void main()
{
int countch=0;
int countwd=1;
printf("Enter your sentence in lowercase: ");
char ch='a';
while(ch!='\r')
{
ch=getche();
if(ch==' ')
countwd++;
else
countch++;
}
printf("\n Words = ",countwd);
printf("Characters = ",countch-1);
getch();
}
让我们以这个例子为例,试着把\n放在\r的地方,它不会工作,并试着猜测为什么?
\n(换行符)和\r(回车符)有什么区别? 特别地,\n和\r之间有什么实际的区别吗?是否有应该使用其中一种而不是另一种的地方?
我想用各自的转义序列\n(换行符)和\r(回车符)做一个简短的实验,以说明它们之间的明显区别。
我知道,这个问题是独立于语言的。尽管如此,我们至少需要一种语言来完成这个实验。在我的例子中,我选择了c++,但这个实验通常适用于任何编程语言。
该程序只是通过for循环迭代将一个句子打印到控制台。
换行符计划:
#include <iostream>
int main(void)
{
for(int i = 0; i < 7; i++)
{
std::cout << i + 1 <<".Walkthrough of the for-loop \n"; // Notice `\n` at the end.
}
return 0;
}
输出:
1.Walkthrough of the for-loop
2.Walkthrough of the for-loop
3.Walkthrough of the for-loop
4.Walkthrough of the for-loop
5.Walkthrough of the for-loop
6.Walkthrough of the for-loop
7.Walkthrough of the for-loop
注意,此结果不会在任何系统上提供,您正在执行此c++代码。但它适用于最现代的系统。阅读下文了解更多细节。
现在,同样的程序,但不同的是,在打印序列的末尾,\n被\r取代。
车厢返回程序:
#include <iostream>
int main(void)
{
for(int i = 0; i < 7; i++)
{
std::cout << i + 1 <<".Walkthrough of the for-loop \r"; // Notice `\r` at the end.
}
return 0;
}
输出:
7.Walkthrough of the for-loop
注意到区别在哪了吗?不同之处在于,当您在每个打印序列的末尾使用回车返回转义序列\r时,此序列的下一次迭代不会进入下面的文本行-在每个打印序列的末尾,光标不会跳转到下一行的*开头。
相反,在使用\r字符之前,光标跳回到该行的开头,他一直在该行的末尾。-结果是,打印序列的每个后续迭代都将取代前一个。
*注:A \n不一定跳转到下一行文本的开头。在一些较老的操作系统上,\n换行符的结果可能是,它跳转到下一行的任何位置,而不仅仅跳转到开头。这就是为什么,他们需要使用\r \n来获得在下一个文本行的开始。
这个实验向我们展示了在打印序列的迭代输出中换行符和回车符的区别。
在讨论程序中的输入时,一些终端/控制台可能会隐式地将回车符转换为换行符,以获得更好的可移植性、兼容性和完整性。
但是,如果您可以选择为另一个选择,或者希望或需要显式地只使用特定的一个,那么您应该始终使用符合其目的并严格区分的那个操作。
历史上,a \n用于向下移动车厢,而\r用于将车厢移回页面的左侧。