The difference between carriage return (CR) and line feed (LF), '\r' and '\n'

One: the origin of "Carriage Return" and "Line Feed"

      First, clarify two concepts:

                    Enter CR - Moves the cursor to the beginning of the current line.
                    Newline LF - Moves the cursor "vertically" to the next line. (without moving to the beginning of the next line, i.e. without changing the cursor horizontal position)

     Before the advent of computers, there was a thing called a teletypewriter (Teletype Model 33, from which the concept of tty under Linux/Unix came from), which could type 10 characters per second. But it has a problem, that is, it takes 0.2 seconds to finish typing a line feed, which is just enough to type two characters. If in this 0.2 seconds, a new character is passed, then this character will be lost.
   Therefore, the developers thought of a way to solve this problem, which is to add two characters to indicate the end of each line. One, called "carriage return," told the typewriter to position the printhead at the left border; the other, called "line feed," told the typewriter to move the paper down one line. This is the origin of "line feed" and "carriage return", which can also be seen from their English names.
   Later, the computer was invented, and these two concepts were applied to the computer. At the time, memory was expensive, and some scientists thought adding two characters at the end of each line was too wasteful, adding one was fine. So, there was a disagreement.
   In Unix systems, each line ends with only "<newline>", that is, "\n"; in Windows systems, each line ends with "<newline><carriage return>", that is, "\n\r"; in Mac systems, Each line ends with "<carriage return>", that is, "\n";. A direct consequence is that if a file under Unix/Mac system is opened in Windows, all text will become one line; and if a file in Windows is opened under Unix/Mac, there may be an extra ^M at the end of each line. symbol.

Two: the difference

CR is represented by the symbol '\r', the decimal ASCII code is 13, and the hexadecimal code is 0x0D

LF is represented by the '\n' symbol, ASCII code is 10, hexadecimal is 0x0A

Dos and windows use carriage return + line feed CR/LF to indicate the next line,

And UNIX/Linux uses the newline character LF to indicate the next line,

The Macintosh (MAC OS system) uses the carriage return character CR to indicate the next line.

Therefore, the newline on the Windows platform is represented by two bytes 0d 0a in the text file, while the newline on UNIX and Apple platforms is represented by one byte 0a or 0d.

Note 1: Regarding the origin of the "Enter key", we have to start with the mechanical English typewriter. On the mechanical English typewriter, there is a part called "character car", and each time a word is typed, the "character car" advances by one frame. When a line of characters is filled, the typist has to push the "carriage" to the starting position. At this time, the typewriter has two actions to respond: one is that the "carriage" is returned to its position, and the other is that the drum rolls up a line to start Enter the next line, the action of pushing the "carriage" is called "enter".

Note 2: This difference requires the most attention when programming, transferring plain text between different OSes.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326915075&siteId=291194637