回车符 \r
和换行符 \n
在IT技术中有什么区别?
具体而言,\r
和\n
之间是否存在任何实际差异?是否有应使用其中一个而不是另一个的情况?
回车符 \r
和换行符 \n
在IT技术中有什么区别?
具体而言,\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();
}
让我们以这个例子为例,尝试把 \r 替换成 \n,它不会起作用,请猜测原因?
\r\n
而不是\r?\n
来分割行时,这些错误/有问题的代码仍然可能存在。这种情况可以在Windows代码库中找到,特别是在没有提供虚拟换行符的语言运行时中。 - user2864740我根据 Ian Boyd的回答 写了一个回车符示例。
这个 Python 脚本将通过在返回到行首来打印字符来打印同一行的字符。
\r
import time
for i in "I'm", "N", "Ne", "Neo":
time.sleep(1)
print(i, end='\r')
\n
import time
for i in "I'm", "N", "Ne", "Neo":
time.sleep(1)
print(i, end='\n')