Difference between \n and \r?

2018-12-31 03:00发布

What’s the difference between \n (newline) and \r (carriage return)?

In particular, are there any practical differences between \n and \r? Are there places where one should be used instead of the other?

9条回答
妖精总统
2楼-- · 2018-12-31 03:37

Two different characters.

\n is used as an end-of-line terminator in Unix text files

\r is used as an end-of-line terminator in Mac text files

\r\n (ie both) are used to terminate lines in Windows and DOS text files.

查看更多
梦该遗忘
3楼-- · 2018-12-31 03:39

Since nobody else mentioned it specifically (are they too young to know/remember?) - I suspect the use of \r\n originated for typewriters and similar devices.

When you wanted a new line while using a multi-line-capable typewriter, there were two physical actions it had to perform: slide the carriage back to the beginning (left, in US) of the page, and feed the paper up one notch.

Back in the days of line printers the only way to do bold text, for example, was to do a carriage return WITHOUT a newline and print the same characters over the old ones, thus adding more ink, thus making it appear darker (bolded). When the mechanical "newline" function failed in a typewriter, this was the annoying result: you could type over the previous line of text if you weren't paying attention.

查看更多
美炸的是我
4楼-- · 2018-12-31 03:45

Historically a \n was used to move the carriage down, while the \r was used to move the carriage back to the left side of the page.

查看更多
登录 后发表回答