This content originally appeared on DEV Community and was authored by Pieter D
This is how the three big operating systems encode newlines.
OS | Escape sequence | Name | Full name |
---|---|---|---|
Windows | \r\n | CRLF | Carriage Return, Line Feed |
Mac | \n | LF | Line Feed |
Linux | \n | LF | Line Feed |
It begs the question: why does Windows use two characters to signal the start of a new line? Isn't one character more efficient?
To understand why Windows does what it does, we have to travel back in time to typewriters. Think of them as printers that have a keyboard. Whenever you typed a letter, a hammer would smash ink against paper. In the early 20th century - before the internet - people figured out how to connect typewriters over a wired telephone network so that companies could send each other direct messages across the country. One of these connected typewriter brands was Teletype.
In the 1960s, a new Teletype model came out that could be controlled from a computer. Problem was, if you let your computer send a \n
character to the Teletype, the typewriter didn't always return to the start of the next line fast enough for the next character to be printed. That could lead to smudged letters being printed halfway your previous line.
The workaround was to always send a \r
character before sending a \n
character. The \r
would return to the start of the current line while the \n
pushed the paper up one line. This sequence of two characters gave the typewriter enough time to catch up before the first character of the next line had to be processed.
Our second player in this story is CP/M, a popular operating system from around that time. CP/M wanted to make it easy to interact with Teletype machines, and device drivers weren't a thing yet, so its engineers adopted Teletype's newline sequence as CP/M native newline sequence for compatibility reasons. Microsoft's competitor MS-DOS, in its turn, wanted to be compatible with CP/M. Therefore, MS-DOS also sided with the \r\n
. MS-DOS would later evolve into Windows over the 80s and 90s, and the rest is history.
Microsoft could choose to migrate Windows files to \n
. Mac actually did the same thing around in 2001, when it successfully retired its \r
newline sequence in favor of \n
with the launch of Mac OS X 10.0. Nowadays, Teletype is out of the picture and device drivers are mature. There doesn't seem to be much reason to continue using two newline characters – other than wasting disk space. Then again... if it ain't broke, does it need to be fixed?
This content originally appeared on DEV Community and was authored by Pieter D
Pieter D | Sciencx (2021-11-14T13:44:57+00:00) Why Windows uses \r\n newlines instead of \n. Retrieved from https://www.scien.cx/2021/11/14/why-windows-uses-rn-newlines-instead-of-n/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.