The difference between carriage return (CR) and line feed (LF), '\ R' and' \ N'

Source: Internet
Author: User

1. The origin of carriage return and line feed

First, clarify two concepts:

Press ENTER Cr-move the cursor to the beginning of the current row.
Wrap lf-move the cursor vertical to the next line. (Instead of moving to the beginning of the next line, that is, do not change the horizontal position of the cursor)

Before the computer appeared, there was a kind of thing called teletype model 33, which also came from the TTY concept in Linux/Unix. It can contain 10 characters per second. But there is a problem, that is, when a line breaks a line, it takes 0.2 seconds, just two characters. If a new character is passed in the 0.2 s, the character will be lost.
As a result, the developers thought of a way to solve this problem, that is, adding two end characters after each line. One is "enter", which tells the typewriter to position the print head on the left boundary, and the other is "line feed", which tells the typewriter to move the paper down one line. This is the source of "line feed" and "Carriage Return". They can also be seen in their English names.
Later, computers were invented, and these two concepts were invented on computers. At that time, memory was very expensive. Some scientists thought it would be too waste to add two characters at the end of each line. Just add one character. As a result, there were differences.
In Unix systems, each line ends with "<line feed>", that is, "\ n". In Windows systems, each line ends with "<line feed> <press enter> ", that is, "\ n \ r"; in MAC systems, the end of each line is "<press enter>", that is, "\ n ";. One direct consequence is that if a file in UNIX/MAC is opened in windows, all the text will be changed to a line; if a file in Windows is opened in UNIX/MAC, A ^ m symbol may be added at the end of each line.

Ii. Differences

Cr is represented by the '\ R' symbol, in ASCII notation.CodeYes. The hexadecimal code is 0x0d.

Lf uses the '\ n' symbol. the ASCII code is 10 and the hexadecimal value is 0x0a.

DOS and Windows use carriage return + line feed Cr/LF to represent the next line,

In Unix/Linux, the line break lf indicates the next line,

The Mac OS system uses the carriage return (CR) to indicate the next line.

Therefore, on Windows, line breaks are represented by 0d 0a in text files, while on UNIX and Apple platforms, line breaks are represented by 0a or 0d in one byte.

NOTE 1: The History of the "Enter key" must start with a mechanical English typewriter. On a mechanical English typewriter, There is a component called a car. Every time a word is entered, a car moves forward. When a line of characters is filled, the typo has to push the "car" to the starting position. At this time, the typewriter will have two actions to respond: first, the "car" is reset, the second is to roll a row on the drum to start entering the next line. The press to push the "car" is "enter ".

NOTE 2: This difference requires the most attention when transmitting plain text between programming and different operating systems.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.