DEV Community

Cover image for Why Windows uses \r\n newlines instead of \n
Pieter D
Pieter D

Posted on • Edited on

Why Windows uses \r\n newlines instead of \n

You may have noticed that Windows has a different way of storing newlines than Mac and Linux. A newline on Windows is encoded as \r\n, which symbolizes a Carriage Return (CR) followed by a Line Feed (LF). By contrast, Mac and Linux simply use a \n.

Why does Windows do its own thing, and why does it use two characters to signal the start of a new line?

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?

Top comments (0)