\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: