The difference between \r\n and \n. The difference between carriage return/line feed in different systems.

1 The difference between \r\n and \n

\r\n and \n are two common control character symbols. They have different functions and uses in the computer field.
\r\n is widely used in Windows systems, while \n is more common in Unix and Linux systems .

  • In Windows operating systems, each line in a text file ends with \r\n, while in Unix and Linux systems, each line in a text file ends with \n as the end.

  • \r\n, often called carriage return and line feed, is a control character used to indicate the position of a line break in a text file. Therefore, \r\n is considered the standard line break character in Windows systems and DOS (Disk Operating System).

  • \nIt is also a control character that can represent a newline character. In Unix and Linux systems, \n is considered a standard newline character and is used to indicate the position of a newline in a file.

  • The difference between and \r\n is that \n only represents a line break, but does not add a carriage return character at the beginning of the new line. Therefore, the main difference between \r\n and \n is the way they are used to represent newlines in different operating systems.

2 What is carriage return/line feed

\n is a line break, which in English is New line.

\r means carriage return, which is Carriage return in English.

The difference between line feed and carriage return:

  • 回车 (\r) 本义是光标重新回到本行开头, the English return of r, the control character can be written as CR, that is, Carriage Return;
  • 换行 (\n) 本义是光标往下一行(不一定到下一行行首), the English newline of n, the control character can be written as LF, which is Line Feed.

These characters behave differently in different operating systems:

  • Under the WIN system, these two characters are the original meaning of expression.
  • In UNIX-like systems, a newline \n appears as the cursor moves to the next line and returns to the beginning of the line.
  • On MAC, \r behaves like returning to the beginning of the line and going to the next line.

In different languages, the code also differs:

  • The newline character in C language is "\n", which means that after entering a line of content, the cursor moves to the starting position of the next line.

3 \r\nand\n Story

\r is a carriage return character, \n is a line feed character

Before computers existed, there was something called a Teletype Model 33, which could type 10 characters per second. But there is a problem with it, that is, it takes 0.2 seconds to type a line and break it, which is enough to type two characters. If a new character is transmitted during these 0.2 seconds, this character will be lost.

Therefore, the developers thought of a way to solve this problem, which was to add two characters to indicate the end of each line. One is called "carriage return" and tells the typewriter to position the print head at the left border; the other is called "line feed" and tells the typewriter to move the paper down one line.

This is the origin of "line feed" and "carriage return", which can be seen from their English names.

Later, the computer was invented, and these two concepts were transferred to the computer. At that time, memory was expensive, and some scientists thought it was too wasteful to add two characters at the end of each line. Just add one. So, there were differences:
Unix 系统里,每行结尾只有“<换行>”,即“\n”;
Windows系统里面,每行结尾是“<回车><换行>”,即“ \r\n”;
Mac系统里,每行结尾是“<回车>”.

A direct consequence is

  • If a file under Unix/Mac system is opened in Windows, all text will become one line;
  • If the file in Windows is opened under Unix/Mac, there may be an extra ^M symbol at the end of each line.

Guess you like

Origin blog.csdn.net/m0_51233386/article/details/134153294