What is the difference between "\ n" and "\ r \ n" (special characters for line break)?

6

For line breaks, I usually use \n . But I realize that in some cases, such as in the Sublime Text editor, some texts that I need to capture the linebreak are only captured when I use the regex \r\n .

I have read some things on the internet about \r\n and the only thing I understand is that it has something related to the Windows operating system .

In some cases, I had trouble using \n on a download system for txt files. This system is written in PHP. I needed to replace the line breaks \n to \r\n so that the text is interpreted correctly. But that left me in doubt whether the problem was related to the operating system or the PHP programming language.

Thus:

  • What is the difference between \n and \r\n ?

  • When should I use one or the other?

  • Is this related to the operating system I use, or the programming language (as in the PHP example)?

asked by anonymous 08.08.2016 / 16:20

2 answers

11

\n means "" "or" line-feed ", that is," new line ". \r means " carriage return ", meaning "carriage return". When the ASCII table was standardized, \n received code 10 and \r received code 13.

The idea originally, when the character encoding tables as bit sequences were designed, is that \n was interpreted as the command to cause the cursor to move down, and \r the command to it move back to the beginning of the line. This distinction was important for digital typewriters that preceded computers, for digital telegraphs, for teletypes, and for the programming of the first printers that emerged. In fact, this is surprisingly older than you think, already appearing in the year 1901 along with some of the first of these tables character encoding.

Thus, in a text for which a line break was inserted, it was necessary to use \r\n . First the cursor should move to the beginning of the line and then down. And that was the line-breaking pattern adopted much later by Microsoft.

Multics (and later Unix) followed a different path, and decided to implement \n as a line break, which already included a carriage return. After all, it does not make much sense to have one thing without having the other together, and by using them as one thing, it is guaranteed that they will never be separated. This also has the advantage of saving space by using a single byte to encode the line break rather than two, and in those years where memory was small and processing low power, each saved byte counted heavily. >

Other companies, such as Apple and Commodore, also followed a similar path to Unix, but instead of adopting \n for line breaks, they adopted \r .

Other smaller companies have adopted other codes for line breaks. For example, QNX has adopted character 30 of the ASCII table. Atari adopted the 155. Acorn and RISC OS adopted \n\r instead of \r\n . Sinclair adopted the 118.

Sources:

08.08.2016 / 16:53
5

According to the same English topic :

\ r = CR (Carriage Return) // Used as a line break in Mac OS prior to version X

\ n = LF (Line Feed) // Used as a Unix / Mac OS line break above version X

\ r \ n = CR + LF // Used as line break in Windows

When should I use each?

File Generation

In a log file write, for example, that will be run / processed by another application, it is important that the environment pattern is followed, as the target application will do as well. Doing so is only recommended if you are writing code for a specific environment. PHP has a reserved constant PHP_EOL (strongly recommend), which will resolve the line break for you according to the environment, so you will do your cross-platform code with regard to breaking lines.

HTML

Sending the content (text) of a file to be rendered by the browser, it is interesting to use the nl2br function that will convert all line breaks to <br /> .

    
08.08.2016 / 16:24