What is the difference between \ R and \ n, how should I use it when coding?

Source: Internet
Author: User

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:
    • Full Name: New line
    • 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?

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.