\r和\n的区别是什么?

时间:2022-01-11 13:20:50

How are \r and \n different? I think it has something to do with Unix vs. Windows vs. Mac, but I'm not sure exactly how they're different, and which to search for/match in regexes.

你和你有什么不同?我认为它与Unix和Windows和Mac有一定的关系,但我不确定它们是如何不同的,以及在regex中搜索/匹配什么。

9 个解决方案

#1


314  

They're different characters. \r is carriage return, and \n is line feed.

它们是不同的字符。\r是回车,\n是换行。

On "old" printers, \r sent the print head back to the start of the line, and \n advanced the paper by one line. Both were therefore necessary to start printing on the next line.

在“旧”打印机上,\r将打印头发送回线的开始处,并将纸张按一行推进。因此,两者都必须在下一行开始印刷。

Obviously that's somewhat irrelevant now, although depending on the console you may still be able to use \r to move to the start of the line and overwrite the existing text.

显然,这有点不重要了,尽管取决于控制台,您仍然可以使用\r来移动到行的开头并覆盖现有的文本。

More importantly, Unix tends to use \n as a line separator; Windows tends to use \r\n as a line separator and Macs (up to OS 9) used to use \r as the line separator. (Mac OS X is Unix-y, so uses \n instead; there may be some compatibility situations where \r is used instead though.)

更重要的是,Unix倾向于使用\n作为行分隔符;Windows倾向于使用\r\n作为线分隔符,而mac(直到OS 9)使用\r作为行分隔符。(Mac OS X是Unix-y,所以用\n代替;可能会有一些兼容性的情况,但是可以使用\r。

For more information, see the Wikipedia newline article.

有关更多信息,请参见*新线文章。

EDIT: This is language-sensitive. In C# and Java, for example, \n always means Unicode U+000A, which is defined as line feed. In C and C++ the water is somewhat muddier, as the meaning is platform-specific. See comments for details.

编辑:这是语言敏感。例如,在c#和Java中,\n总是意味着Unicode U+000A,它被定义为行提要。在C和c++中,水有点泥泞,因为其含义是特定于平台的。有关详细信息,请参阅注释。

#2


79  

In C and C++, \n is a concept, \r is a character, and \r\n is (almost always) a portability bug.

在C和c++中,\n是一个概念,\r是一个字符,而\r\n是(几乎总是)一个可移植性缺陷。

Think of an old teletype. The print head is positioned on some line and in some column. When you send a printable character to the teletype, it prints the character at the current position and moves the head to the next column. (This is conceptually the same as a typewriter, except that typewriters typically moved the paper with respect to the print head.)

想想老式的电传打字机。打印头定位在某个行和某个列上。当您将一个可打印字符发送到电传打字机时,它会在当前位置打印字符,并将头移到下一列。(这在概念上和打字机是一样的,只是打字机通常会把纸张移到打印头上。)

When you wanted to finish the current line and start on the next line, you had to do two separate steps:

当您想要完成当前行并开始下一行时,您必须完成两个独立的步骤:

  1. move the print head back to the beginning of the line, then
  2. 然后将打印头移回线的开头。
  3. move it down to the next line.
  4. 把它移到下一行。

ASCII encodes these actions as two distinct control characters:

ASCII将这些动作编码为两个不同的控制字符:

  • \x0D (CR) moves the print head back to the beginning of the line. (Unicode encodes this as U+000D CARRIAGE RETURN.)
  • \x0D (CR)将打印头移回行开始处。(Unicode编码为U+000D回车。)
  • \x0A (LF) moves the print head down to the next line. (Unicode encodes this as U+000A LINE FEED.)
  • \x0A (LF)将打印头移到下一行。(Unicode编码为U+000A行提要。)

In the days of teletypes and early technology printers, people actually took advantage of the fact that these were two separate operations. By sending a CR without following it by a LF, you could print over the line you already printed. This allowed effects like accents, bold type, and underlining. Some systems overprinted several times to prevent passwords from being visible in hardcopy. On early serial CRT terminals, CR was one of the ways to control the cursor position in order to update text already on the screen.

在电传打字机和早期技术打印机的时代,人们实际上利用了这两种不同的操作。通过发送一个不跟随LF的CR,您可以打印您已经打印的行。这样的效果就像重音、粗体字和下划线一样。有些系统为了防止密码在硬拷贝中被显示而被多次打印出来。在早期的串行CRT终端上,CR是控制光标位置的一种方法,以更新屏幕上的文本。

But most of the time, you actually just wanted to go to the next line. Rather than requiring the pair of control characters, some systems allowed just one or the other. For example:

但大多数时候,你只是想去下一行。有些系统只允许一个或一个控制字符,而不需要两个控制字符。例如:

  • Unix variants (including modern versions of Mac) use just a LF character to indicate a newline.
  • Unix变体(包括现代版本的Mac)仅使用一个LF字符来指示换行符。
  • Old (pre-OSX) Macintosh files used just a CR character to indicate a newline.
  • 旧(pre-OSX) Macintosh文件仅使用一个CR字符来表示换行符。
  • VMS, CP/M, DOS, Windows, and many network protocols still expect both: CR LF.
  • vm、CP/M、DOS、Windows和许多网络协议仍然期望都是:CR LF。
  • Old IBM systems that used EBCDIC standardized on NL--a character that doesn't even exist in the ASCII character set. In Unicode, NL is U+0085 NEXT LINE, but the actual EBCDIC value is 0x15.
  • 旧的IBM系统在NL上使用EBCDIC标准,在ASCII字符集中甚至不存在这个字符。在Unicode中,NL是U+0085下一行,但实际的EBCDIC值是0x15。

Why did different systems choose different methods? Simply because there was no universal standard. Where your keyboard probably says "Enter", older keyboards used to say "Return", which was short for Carriage Return. In fact, on a serial terminal, pressing Return actually sends the CR character. If you were writing a text editor, it would be tempting to just use that character as it came in from the terminal. Perhaps that's why the older Macs used just CR.

为什么不同的系统会选择不同的方法?只是因为没有统一的标准。你的键盘可能会说“输入”,老式键盘会说“Return”,这是回车的缩写。实际上,在串行终端上,按Return实际上发送了CR字符。如果您正在编写一个文本编辑器,那么就很容易使用这个字符,因为它是从终端传入的。也许这就是为什么老mac电脑只使用CR。

Now that we have standards, there are more ways to represent line breaks. Although extremely rare in the wild, Unicode has new characters like:

现在我们有了标准,有更多的方法来表示换行。虽然在野外极为罕见,但Unicode却有这样的新角色:

  • U+2028 LINE SEPARATOR
  • U + 2028行分隔符
  • U+2029 PARAGRAPH SEPARATOR
  • U + 2029段分隔符

Even before Unicode came along, programmers wanted simple ways to represent some of the most useful control codes without worrying about the underlying character set. C has several escape sequences for representing control codes:

甚至在Unicode出现之前,程序员就需要简单的方法来表示一些最有用的控制代码,而不用担心底层字符集。

  • \a (for alert) which rings the teletype bell or makes the terminal beep
  • (用于警告)是哪一种铃声响起,或发出终端机嘟嘟声。
  • \f (for form feed) which moves to the beginning of the next page
  • \f(用于表单提要)移动到下一页的开头。
  • \t (for tab) which moves the print head to the next horizontal tab position
  • \t (for tab)将打印头移动到下一个水平标签位置。

(This list is intentionally incomplete.)

(此列表故意不完整。)

This mapping happens at compile-time--the compiler sees \a and puts whatever magic value is used to ring the bell.

这个映射发生在编译时——编译器会看到\a,并将使用任何魔法值来按铃。

Notice that most of these mnemonics have direct correlations to ASCII control codes. For example, \a would map to 0x07 BEL. A compiler could be written for a system that used something other than ASCII for the host character set (e.g., EBCDIC). Most of the control codes that had specific mnemonics could be mapped to control codes in other character sets.

请注意,这些助记符大多与ASCII码控制码有直接关联。例如,\a将映射到0x07 BEL。可以为一个系统编写一个编译器,该系统使用的是主机字符集(例如EBCDIC)以外的其他东西。大多数有特定助记符的控制码可以被映射到其他字符集的控制码。

Huzzah! Portability!

万岁!可移植性。

Well, almost. In C, I could write printf("\aHello, World!"); which rings the bell (or beeps) and outputs a message. But if I wanted to then print something on the next line, I'd still need to know what the host platform requires to move to the next line of output. CR LF? CR? LF? NL? Something else? So much for portability.

嗯,差不多。在C语言中,我可以写printf(“\aHello, World!”);它会按铃(或嘟嘟声)并输出一个信息。但是如果我想在下一行打印一些东西,我仍然需要知道主机平台需要移动到下一行的输出。CR低频?CR吗?低频?问吗?别的吗?可移植性。

C has two modes for I/O: binary and text. In binary mode, whatever data is sent gets transmitted as-is. But in text mode, there's a run-time translation that converts a special character to whatever the host platform needs for a new line (and vice versa).

C有两个I/O模式:二进制和文本。在二进制模式中,发送的任何数据都是按原样发送的。但是在文本模式中,有一个运行时转换,它可以将一个特殊字符转换为任何主机平台需要的新行(反之亦然)。

Great, so what's the special character?

很好,那么这个特殊的角色是什么?

Well, that's implementation dependent, too, but there's an implementation-independent way to specify it: \n. It's typically called the "newline character".

这也依赖于实现,但是有一种独立于实现的方式来指定它:\n。它通常被称为“换行符”。

This is a subtle but important point: \n is mapped at compile time to an implementation-defined character value which (in text mode) is then mapped again at run time to the actual character (or sequence of characters) required by the underlying platform to move to the next line.

这是一个很微妙但很重要的一点:在编译时将\n映射到实现定义的字符值(在文本模式中),然后在运行时再次映射到底层平台所需的实际字符(或字符序列),以移动到下一行。

\n is different than all the other backslash literals because there are two mappings involved. This two-step mapping makes \n significantly different than even \r, which is simply a compile-time mapping to CR (or the most similar control code in whatever the underlying character set is).

\n与所有其他反斜杠文字不同,因为有两个映射涉及。这两步映射使得\n明显不同于\r,它只是一个编译时映射到CR(或任何底层字符集的最相似的控制代码)。

This trips up many C and C++ programmers. If you were to poll 100 of them, at least 99 will tell you that \n means line feed. This is not entirely true. Most (perhaps all) C and C++ implementations use LF as the magic intermediate value for \n, but that's an implementation detail. It's feasible for a compiler to use a different value. In fact, if the host character set is not a superset of ASCII (e.g., if it's EBCDIC), then \n will almost certainly not be LF.

这让许多C和c++程序员都感到困惑。如果你要对其中的100个进行投票,至少99个会告诉你,\n意味着换行。这并不完全正确。大多数(也许全部)C和c++实现使用LF作为\n的魔法中间值,但这是实现细节。编译器使用不同的值是可行的。实际上,如果主机字符集不是ASCII的超集(例如,如果它是EBCDIC),那么\n几乎肯定不是LF。

So, in C and C++:

因此,在C和c++中:

  • \r is literally a carriage return.
  • \r字面上就是回车。
  • \n is a magic value that gets translated (in text mode) at run-time to/from the host platform's newline semantics.
  • \n是一个神奇的值,它在运行时从主机平台的换行语义转换为(在文本模式下)。
  • \r\n is almost always a portability bug. In text mode, this gets translated to CR followed by the platform's newline sequence--probably not what's intended. In binary mode, this gets translated to CR followed by some magic value that might not be LF--possibly not what's intended.
  • \r\n几乎总是一个可移植性错误。在文本模式中,这被转换为CR,然后是平台的换行序列——可能不是预期的。在二进制模式中,这被转换为CR,然后是一些可能不是LF的魔法值——可能不是故意的。
  • \x0A is the most portable way to indicate an ASCII LF, but you only want to do that in binary mode. Most text-mode implementations will treat that like \n.
  • \x0A是表示ASCII LF的最方便的方式,但您只希望以二进制模式进行。大多数文本模式实现都将处理类似\n。

#3


9  

  • "\r" => Return
  • “\ r”= >返回
  • "\n" => Newline or Linefeed (semantics)

    "\n" =>换行或换行符(语义)

  • Unix based systems use just a "\n" to end a line of text.

    基于Unix的系统只使用一个“\n”来结束一行文本。

  • Dos uses "\r\n" to end a line of text.
  • Dos使用“\r\n”来结束一行文本。
  • Some other machines used just a "\r". (Commodore, Apple II, Mac OS prior to OS X, etc..)
  • 其他一些机器只使用“\r”。(Commodore, Apple II, Mac OS在OS X之前,等等)

#4


4  

In short \r has ASCII value 13 (CR) and \n has ASCII value 10 (LF). Mac uses CR as line delimiter (at least, it did before, I am not sure for modern macs), *nix uses LF and Windows uses both (CRLF).

在短\r中有ASCII值13 (CR)和\n有ASCII值10 (LF)。Mac使用CR作为线分隔符(至少以前是这样的,我不确定现代的Mac), *nix使用LF和Windows同时使用两个(CRLF)。

#5


4  

\r is used to point to the start of a line and can replace the text from there, e.g.

\r用于指向一行的开始,并可以从那里替换文本。

main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
}

Produces this output:

产生该输出:

hai

\n is for new line.

\n是新线路。

#6


3  

In addition to @Jon Skeet's answer:

除了@Jon Skeet的回答:

Traditionally Windows has used \r\n, Unix \n and Mac \r, however newer Macs use \n as they're unix based.

传统上,Windows使用的是\r\n、Unix \n和Mac \r,不过更新的Mac电脑使用的是Unix。

#7


2  

in C# I found they use \r\n in a string.

在c#中,我发现它们在字符串中使用\r\n。

#8


2  

\r is Carriage Return; \n is New Line (Line Feed) ... depends on the OS as to what each means. Read this article for more on the difference between '\n' and '\r\n' ... in C.

\ r是回车;\n是新行(换行)……取决于操作系统对每种方法的理解。请阅读这篇文章,了解更多关于“\n”和“\r\n”的区别。在C。

#9


1  

\r used for carriage return. (ASCII value is 13) \n used for new line. (ASCII value is 10)

用于回车。(ASCII值为13)\n用于新行。(ASCII值是10)

#1


314  

They're different characters. \r is carriage return, and \n is line feed.

它们是不同的字符。\r是回车,\n是换行。

On "old" printers, \r sent the print head back to the start of the line, and \n advanced the paper by one line. Both were therefore necessary to start printing on the next line.

在“旧”打印机上,\r将打印头发送回线的开始处,并将纸张按一行推进。因此,两者都必须在下一行开始印刷。

Obviously that's somewhat irrelevant now, although depending on the console you may still be able to use \r to move to the start of the line and overwrite the existing text.

显然,这有点不重要了,尽管取决于控制台,您仍然可以使用\r来移动到行的开头并覆盖现有的文本。

More importantly, Unix tends to use \n as a line separator; Windows tends to use \r\n as a line separator and Macs (up to OS 9) used to use \r as the line separator. (Mac OS X is Unix-y, so uses \n instead; there may be some compatibility situations where \r is used instead though.)

更重要的是,Unix倾向于使用\n作为行分隔符;Windows倾向于使用\r\n作为线分隔符,而mac(直到OS 9)使用\r作为行分隔符。(Mac OS X是Unix-y,所以用\n代替;可能会有一些兼容性的情况,但是可以使用\r。

For more information, see the Wikipedia newline article.

有关更多信息,请参见*新线文章。

EDIT: This is language-sensitive. In C# and Java, for example, \n always means Unicode U+000A, which is defined as line feed. In C and C++ the water is somewhat muddier, as the meaning is platform-specific. See comments for details.

编辑:这是语言敏感。例如,在c#和Java中,\n总是意味着Unicode U+000A,它被定义为行提要。在C和c++中,水有点泥泞,因为其含义是特定于平台的。有关详细信息,请参阅注释。

#2


79  

In C and C++, \n is a concept, \r is a character, and \r\n is (almost always) a portability bug.

在C和c++中,\n是一个概念,\r是一个字符,而\r\n是(几乎总是)一个可移植性缺陷。

Think of an old teletype. The print head is positioned on some line and in some column. When you send a printable character to the teletype, it prints the character at the current position and moves the head to the next column. (This is conceptually the same as a typewriter, except that typewriters typically moved the paper with respect to the print head.)

想想老式的电传打字机。打印头定位在某个行和某个列上。当您将一个可打印字符发送到电传打字机时,它会在当前位置打印字符,并将头移到下一列。(这在概念上和打字机是一样的,只是打字机通常会把纸张移到打印头上。)

When you wanted to finish the current line and start on the next line, you had to do two separate steps:

当您想要完成当前行并开始下一行时,您必须完成两个独立的步骤:

  1. move the print head back to the beginning of the line, then
  2. 然后将打印头移回线的开头。
  3. move it down to the next line.
  4. 把它移到下一行。

ASCII encodes these actions as two distinct control characters:

ASCII将这些动作编码为两个不同的控制字符:

  • \x0D (CR) moves the print head back to the beginning of the line. (Unicode encodes this as U+000D CARRIAGE RETURN.)
  • \x0D (CR)将打印头移回行开始处。(Unicode编码为U+000D回车。)
  • \x0A (LF) moves the print head down to the next line. (Unicode encodes this as U+000A LINE FEED.)
  • \x0A (LF)将打印头移到下一行。(Unicode编码为U+000A行提要。)

In the days of teletypes and early technology printers, people actually took advantage of the fact that these were two separate operations. By sending a CR without following it by a LF, you could print over the line you already printed. This allowed effects like accents, bold type, and underlining. Some systems overprinted several times to prevent passwords from being visible in hardcopy. On early serial CRT terminals, CR was one of the ways to control the cursor position in order to update text already on the screen.

在电传打字机和早期技术打印机的时代,人们实际上利用了这两种不同的操作。通过发送一个不跟随LF的CR,您可以打印您已经打印的行。这样的效果就像重音、粗体字和下划线一样。有些系统为了防止密码在硬拷贝中被显示而被多次打印出来。在早期的串行CRT终端上,CR是控制光标位置的一种方法,以更新屏幕上的文本。

But most of the time, you actually just wanted to go to the next line. Rather than requiring the pair of control characters, some systems allowed just one or the other. For example:

但大多数时候,你只是想去下一行。有些系统只允许一个或一个控制字符,而不需要两个控制字符。例如:

  • Unix variants (including modern versions of Mac) use just a LF character to indicate a newline.
  • Unix变体(包括现代版本的Mac)仅使用一个LF字符来指示换行符。
  • Old (pre-OSX) Macintosh files used just a CR character to indicate a newline.
  • 旧(pre-OSX) Macintosh文件仅使用一个CR字符来表示换行符。
  • VMS, CP/M, DOS, Windows, and many network protocols still expect both: CR LF.
  • vm、CP/M、DOS、Windows和许多网络协议仍然期望都是:CR LF。
  • Old IBM systems that used EBCDIC standardized on NL--a character that doesn't even exist in the ASCII character set. In Unicode, NL is U+0085 NEXT LINE, but the actual EBCDIC value is 0x15.
  • 旧的IBM系统在NL上使用EBCDIC标准,在ASCII字符集中甚至不存在这个字符。在Unicode中,NL是U+0085下一行,但实际的EBCDIC值是0x15。

Why did different systems choose different methods? Simply because there was no universal standard. Where your keyboard probably says "Enter", older keyboards used to say "Return", which was short for Carriage Return. In fact, on a serial terminal, pressing Return actually sends the CR character. If you were writing a text editor, it would be tempting to just use that character as it came in from the terminal. Perhaps that's why the older Macs used just CR.

为什么不同的系统会选择不同的方法?只是因为没有统一的标准。你的键盘可能会说“输入”,老式键盘会说“Return”,这是回车的缩写。实际上,在串行终端上,按Return实际上发送了CR字符。如果您正在编写一个文本编辑器,那么就很容易使用这个字符,因为它是从终端传入的。也许这就是为什么老mac电脑只使用CR。

Now that we have standards, there are more ways to represent line breaks. Although extremely rare in the wild, Unicode has new characters like:

现在我们有了标准,有更多的方法来表示换行。虽然在野外极为罕见,但Unicode却有这样的新角色:

  • U+2028 LINE SEPARATOR
  • U + 2028行分隔符
  • U+2029 PARAGRAPH SEPARATOR
  • U + 2029段分隔符

Even before Unicode came along, programmers wanted simple ways to represent some of the most useful control codes without worrying about the underlying character set. C has several escape sequences for representing control codes:

甚至在Unicode出现之前,程序员就需要简单的方法来表示一些最有用的控制代码,而不用担心底层字符集。

  • \a (for alert) which rings the teletype bell or makes the terminal beep
  • (用于警告)是哪一种铃声响起,或发出终端机嘟嘟声。
  • \f (for form feed) which moves to the beginning of the next page
  • \f(用于表单提要)移动到下一页的开头。
  • \t (for tab) which moves the print head to the next horizontal tab position
  • \t (for tab)将打印头移动到下一个水平标签位置。

(This list is intentionally incomplete.)

(此列表故意不完整。)

This mapping happens at compile-time--the compiler sees \a and puts whatever magic value is used to ring the bell.

这个映射发生在编译时——编译器会看到\a,并将使用任何魔法值来按铃。

Notice that most of these mnemonics have direct correlations to ASCII control codes. For example, \a would map to 0x07 BEL. A compiler could be written for a system that used something other than ASCII for the host character set (e.g., EBCDIC). Most of the control codes that had specific mnemonics could be mapped to control codes in other character sets.

请注意,这些助记符大多与ASCII码控制码有直接关联。例如,\a将映射到0x07 BEL。可以为一个系统编写一个编译器,该系统使用的是主机字符集(例如EBCDIC)以外的其他东西。大多数有特定助记符的控制码可以被映射到其他字符集的控制码。

Huzzah! Portability!

万岁!可移植性。

Well, almost. In C, I could write printf("\aHello, World!"); which rings the bell (or beeps) and outputs a message. But if I wanted to then print something on the next line, I'd still need to know what the host platform requires to move to the next line of output. CR LF? CR? LF? NL? Something else? So much for portability.

嗯,差不多。在C语言中,我可以写printf(“\aHello, World!”);它会按铃(或嘟嘟声)并输出一个信息。但是如果我想在下一行打印一些东西,我仍然需要知道主机平台需要移动到下一行的输出。CR低频?CR吗?低频?问吗?别的吗?可移植性。

C has two modes for I/O: binary and text. In binary mode, whatever data is sent gets transmitted as-is. But in text mode, there's a run-time translation that converts a special character to whatever the host platform needs for a new line (and vice versa).

C有两个I/O模式:二进制和文本。在二进制模式中,发送的任何数据都是按原样发送的。但是在文本模式中,有一个运行时转换,它可以将一个特殊字符转换为任何主机平台需要的新行(反之亦然)。

Great, so what's the special character?

很好,那么这个特殊的角色是什么?

Well, that's implementation dependent, too, but there's an implementation-independent way to specify it: \n. It's typically called the "newline character".

这也依赖于实现,但是有一种独立于实现的方式来指定它:\n。它通常被称为“换行符”。

This is a subtle but important point: \n is mapped at compile time to an implementation-defined character value which (in text mode) is then mapped again at run time to the actual character (or sequence of characters) required by the underlying platform to move to the next line.

这是一个很微妙但很重要的一点:在编译时将\n映射到实现定义的字符值(在文本模式中),然后在运行时再次映射到底层平台所需的实际字符(或字符序列),以移动到下一行。

\n is different than all the other backslash literals because there are two mappings involved. This two-step mapping makes \n significantly different than even \r, which is simply a compile-time mapping to CR (or the most similar control code in whatever the underlying character set is).

\n与所有其他反斜杠文字不同,因为有两个映射涉及。这两步映射使得\n明显不同于\r,它只是一个编译时映射到CR(或任何底层字符集的最相似的控制代码)。

This trips up many C and C++ programmers. If you were to poll 100 of them, at least 99 will tell you that \n means line feed. This is not entirely true. Most (perhaps all) C and C++ implementations use LF as the magic intermediate value for \n, but that's an implementation detail. It's feasible for a compiler to use a different value. In fact, if the host character set is not a superset of ASCII (e.g., if it's EBCDIC), then \n will almost certainly not be LF.

这让许多C和c++程序员都感到困惑。如果你要对其中的100个进行投票,至少99个会告诉你,\n意味着换行。这并不完全正确。大多数(也许全部)C和c++实现使用LF作为\n的魔法中间值,但这是实现细节。编译器使用不同的值是可行的。实际上,如果主机字符集不是ASCII的超集(例如,如果它是EBCDIC),那么\n几乎肯定不是LF。

So, in C and C++:

因此,在C和c++中:

  • \r is literally a carriage return.
  • \r字面上就是回车。
  • \n is a magic value that gets translated (in text mode) at run-time to/from the host platform's newline semantics.
  • \n是一个神奇的值,它在运行时从主机平台的换行语义转换为(在文本模式下)。
  • \r\n is almost always a portability bug. In text mode, this gets translated to CR followed by the platform's newline sequence--probably not what's intended. In binary mode, this gets translated to CR followed by some magic value that might not be LF--possibly not what's intended.
  • \r\n几乎总是一个可移植性错误。在文本模式中,这被转换为CR,然后是平台的换行序列——可能不是预期的。在二进制模式中,这被转换为CR,然后是一些可能不是LF的魔法值——可能不是故意的。
  • \x0A is the most portable way to indicate an ASCII LF, but you only want to do that in binary mode. Most text-mode implementations will treat that like \n.
  • \x0A是表示ASCII LF的最方便的方式,但您只希望以二进制模式进行。大多数文本模式实现都将处理类似\n。

#3


9  

  • "\r" => Return
  • “\ r”= >返回
  • "\n" => Newline or Linefeed (semantics)

    "\n" =>换行或换行符(语义)

  • Unix based systems use just a "\n" to end a line of text.

    基于Unix的系统只使用一个“\n”来结束一行文本。

  • Dos uses "\r\n" to end a line of text.
  • Dos使用“\r\n”来结束一行文本。
  • Some other machines used just a "\r". (Commodore, Apple II, Mac OS prior to OS X, etc..)
  • 其他一些机器只使用“\r”。(Commodore, Apple II, Mac OS在OS X之前,等等)

#4


4  

In short \r has ASCII value 13 (CR) and \n has ASCII value 10 (LF). Mac uses CR as line delimiter (at least, it did before, I am not sure for modern macs), *nix uses LF and Windows uses both (CRLF).

在短\r中有ASCII值13 (CR)和\n有ASCII值10 (LF)。Mac使用CR作为线分隔符(至少以前是这样的,我不确定现代的Mac), *nix使用LF和Windows同时使用两个(CRLF)。

#5


4  

\r is used to point to the start of a line and can replace the text from there, e.g.

\r用于指向一行的开始,并可以从那里替换文本。

main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
}

Produces this output:

产生该输出:

hai

\n is for new line.

\n是新线路。

#6


3  

In addition to @Jon Skeet's answer:

除了@Jon Skeet的回答:

Traditionally Windows has used \r\n, Unix \n and Mac \r, however newer Macs use \n as they're unix based.

传统上,Windows使用的是\r\n、Unix \n和Mac \r,不过更新的Mac电脑使用的是Unix。

#7


2  

in C# I found they use \r\n in a string.

在c#中,我发现它们在字符串中使用\r\n。

#8


2  

\r is Carriage Return; \n is New Line (Line Feed) ... depends on the OS as to what each means. Read this article for more on the difference between '\n' and '\r\n' ... in C.

\ r是回车;\n是新行(换行)……取决于操作系统对每种方法的理解。请阅读这篇文章,了解更多关于“\n”和“\r\n”的区别。在C。

#9


1  

\r used for carriage return. (ASCII value is 13) \n used for new line. (ASCII value is 10)

用于回车。(ASCII值为13)\n用于新行。(ASCII值是10)