Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Windows use CR LF?

I understand the difference between the two so there's no need to go into that, but I'm just wondering what the reasoning is behind why Windows uses both CR and LF to indicate a line break. It seems like the Linux method (just using LF) makes a lot more sense, saves space, and is easier to parse.

like image 246
Kyle Avatar asked Jun 29 '11 13:06

Kyle


People also ask

Why did Windows use CRLF?

It comes from the teletype machines (and typewriters) from the days of yore. It used to be that when you were done typing a line, you had to move the typewriter's carriage (which held the paper and slid to the left as you typed) back to the start of the line (CR).

Is Windows CRLF or LF?

Whereas Windows follows the original convention of a carriage return plus a line feed ( CRLF ) for line endings, operating systems like Linux and Mac use only the line feed ( LF ) character.

Why does Windows use different line endings?

According to Wikipedia: in the beginning, the program had to put in extra CR characters before the LF to slow the program down so the printer had time to keep up - and CP/M and then later Windows used this method.

What is window CRLF?

The term CRLF refers to Carriage Return (ASCII 13, \r) Line Feed (ASCII 10, \n). For example: in Windows both a CR and LF are required to note the end of a line, whereas in Linux/UNIX an LF (\n) is only required. Files can be converted from one to another using the .


2 Answers

Historically when using dot-matrix printers teletypes CR would return the carriage to the first position of the line while LF would feed to the next line. Using CR+LF in the file themselves made it possible to send a file directly to the printer, without any kind of printer driver.

Thanks @zaph pointing out it was teletypes and not dot matrix printers

like image 95
Anders Abel Avatar answered Oct 16 '22 21:10

Anders Abel


@sshannin posted an URL from Raymond Chen's blog, but it doesn't work anymore. The blog has changed its internal software, so the URLs changed.

After crawling through the old posts in the new blog I've found it here.

Quote from the blog:

Why is the line terminator CR+LF?

This protocol dates back to the days of teletypewriters. CR stands for “carriage return” – the CR control character returned the print head (“carriage”) to column 0 without advancing the paper. LF stands for “linefeed” – the LF control character advanced the paper one line without moving the print head. So if you wanted to return the print head to column zero (ready to print the next line) and advance the paper (so it prints on fresh paper), you need both CR and LF.

If you go to the various internet protocol documents, such as RFC 0821 (SMTP), RFC 1939 (POP), RFC 2060 (IMAP), or RFC 2616 (HTTP), you’ll see that they all specify CR+LF as the line termination sequence. So the the real question is not “Why do CP/M, MS-DOS, and Win32 use CR+LF as the line terminator?” but rather “Why did other people choose to differ from these standards documents and use some other line terminator?”

Unix adopted plain LF as the line termination sequence. If you look at the stty options, you’ll see that the onlcr option specifies whether a LF should be changed into CR+LF. If you get this setting wrong, you get stairstep text, where

each     line         begins  

where the previous line left off. So even unix, when left in raw mode, requires CR+LF to terminate lines. The implicit CR before LF is a unix invention, probably as an economy, since it saves one byte per line.

The unix ancestry of the C language carried this convention into the C language standard, which requires only “\n” (which encodes LF) to terminate lines, putting the burden on the runtime libraries to convert raw file data into logical lines.

The C language also introduced the term “newline” to express the concept of “generic line terminator”. I’m told that the ASCII committee changed the name of character 0x0A to “newline” around 1996, so the confusion level has been raised even higher.

Here’s another discussion of the subject, from a unix perspective

I've changed this second link to a snapshot in The Wayback Machine, since the actual page is not available anymore.

I hope this answers your question.

like image 39
OMA Avatar answered Oct 16 '22 20:10

OMA