What is the difference between \ R and \ n, how should I use it when coding?
Difference:
- \ r:
- Full Name: Carriage return (carriage is "word car" meaning, a part of the printer)
- Abbreviation: Return
- Abbreviation: R
- ASCII code: 13
- Action: Moves the cursor to the far left of the current line
- \ n:
- Alias: Line Feed
- Abbreviation: n
- ASCII code: 10
- Action: Move the cursor down one line
How different operating systems represent "carriage return + newline" (that is, the end of a line):
- Unix, linux:\n means carriage return + line break, \ r no meaning
- Windows, dos:\r\n means carriage return + newline, order cannot be changed
- mac:\r = carriage return + line break
History:
The English typewriter of the machine
: On the typewriter, there is a part called "Word Car" (carriage), each word entered, "word car" forward one. When a line is filled, the user pushes the "word cart" to the starting position, when the typewriter will have two actions: the "word cart" (roller) rolls up a line (equivalent to the "word car" moves down one line) to begin entering the next line. These two actions together are called "carriage returns", which is equivalent to the "enter" key in the keyboard now.
Telex Typewriter:
(The TTY concept under Teletype Model 33,linux/unix also comes from this) can be played 10 characters per second. But it has a problem, that is, when the line is finished, it will take 0.2 seconds to hit two characters. If there are new characters coming in this 0.2 seconds, then this character will be lost. So, the developers think of a way to solve this problem, is to add two after each line to end the character. One is called "return", which tells the typewriter to position the printhead at the left border, and the other is called "newline", which tells the typewriter to move the paper down one line. These two actions were then abbreviated as "\ r" and "\ n".
How to use in a programming language:
Because UNIX uses "\ n" to indicate the end of a line, "\ n" can be used directly in C (and other C-language inheritors, such as C + +, Java), and will be automatically converted to corresponding characters in different operating systems (for example, it will be turned into "\ r \ n" in Windows).
(Original article, reprint please specify the blog from Clement-xu)
What is the difference between \ R and \ n, how should I use it when coding?