Why Does a New Line Need Two Characters?


Ever wondered why Windows text files stubbornly use two characters CR and LF to represent a new line, while your Linux friends smugly get by with just one? Let’s unravel this quirky bit of computing history and see where CRLF still rules the roost.
A Tale of Two Characters: CR and LF
First, some quick definitions:
CR (Carriage Return, \r, ASCII 13): Moves the cursor to the start of the line, just like yanking the carriage back to the left on a typewriter.
LF (Line Feed, \n, ASCII 10): Moves the paper up one line, but leaves the carriage exactly where it is... otherwise, imagine typing on the edge of the page forever!
Back in the days of mechanical typewriters and teletypes, these were two distinct physical actions. If you wanted to start a fresh line, you had to both return the carriage (CR) and feed the paper (LF). So, to reliably start a new line, you needed both actions. Hence, CR+LF.
How Did We Get Here?
Teletype Era: Early computers talked directly to teletype machines. These machines needed both CR and LF to properly start a new line, so computer systems just sent both characters. If you skipped one, your printouts would look like a staircase or a jumbled mess.
Standards and Inheritance: CP/M, MS-DOS, and eventually Windows inherited this CRLF convention. Internet protocols like SMTP, HTTP, and POP also specified CRLF as the official line ending, cementing its place in tech history.
Unix Rebels: Unix designers, always looking to save a byte (and maybe a little rebellious), decided just LF was enough. Their device drivers handled the translation, and the C language followed suit, using just "\n" for new lines.
Why CR before LF?
Early printers and teletypes were slow. The carriage (print head) physically needed time to zip back to the start of the line. Sending LF before CR would move the paper up, but the print head might still be halfway across the page. That’s why CR always comes first—give the carriage a head start!
Where Is CRLF Still Used Today?
Windows and DOS: CRLF (\r\n) is still the standard line ending in Windows environments and text files.
Internet Protocols: Many network protocols (SMTP for email, HTTP for the web, POP/IMAP for mail retrieval) require CRLF to mark the end of a line or command.
Some Editors and Tools: Many modern text editors can handle all three conventions (CRLF, LF, CR), but if you’re writing code or text files on Windows, CRLF is what you’ll get by default.
Other Line Break Oddities
Old Mac OS (pre-OS X): Used just CR (\r).
Unix, Linux, macOS (post-OS X): Use just LF (\n).
Cross-Platform Headaches: If you’ve ever seen weird spacing or “^M” symbols in your code, you’ve run into mismatched line endings!
TL;DR
CRLF is a living fossil from the days of typewriters and teletypes, still thriving in Windows and internet protocols. It survives because of history, compatibility, and a bit of stubbornness. So next time you see "\r\n", tip your hat to a century of mechanical and digital tradition... and maybe run a line ending converter before sharing files with your Unix pals!
Happy coding, may your line endings always match your platform!
Subscribe to my newsletter
Read articles from Sunny Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
