{"id":40193,"date":"2004-03-18T06:59:00","date_gmt":"2004-03-18T06:59:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/03\/18\/why-is-the-line-terminator-crlf\/"},"modified":"2004-03-18T06:59:00","modified_gmt":"2004-03-18T06:59:00","slug":"why-is-the-line-terminator-crlf","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040318-00\/?p=40193","title":{"rendered":"Why is the line terminator CR+LF?"},"content":{"rendered":"<p>This protocol dates back to the days of teletypewriters.\nCR stands for &#8220;carriage return&#8221; &#8211; the CR control character\nreturned the print head (&#8220;carriage&#8221;) to column 0 without\nadvancing the paper. LF stands for &#8220;linefeed&#8221; &#8211; the LF\ncontrol character advanced the paper one line without\nmoving the print head.\nSo if you wanted to return the print head to column zero\n(ready to print the next line) and advance the paper (so\nit prints on fresh paper), you need both CR and LF.\n<\/p>\n<p>\nIf you go to the various internet protocol documents, such as\n<a HREF=\"http:\/\/www.ietf.org\/rfc\/rfc0821.txt\">RFC 0821 (SMTP)<\/a>,\n<a HREF=\"http:\/\/www.ietf.org\/rfc\/rfc1939.txt\">RFC 1939 (POP)<\/a>,\n<a HREF=\"http:\/\/www.ietf.org\/rfc\/rfc2060.txt\">RFC 2060 (IMAP)<\/a>,\nor <a HREF=\"http:\/\/www.ietf.org\/rfc\/rfc2616.txt\">RFC 2616 (HTTP)<\/a>,\nyou&#8217;ll see that they all specify CR+LF as the line termination\nsequence.\nSo the the real question is not &#8220;Why do CP\/M, MS-DOS, and Win32 use CR+LF\nas the line terminator?&#8221; but rather &#8220;Why did other people\nchoose to differ from these standards documents and use some other\nline terminator?&#8221;<\/p>\n<p>\nUnix adopted plain LF as the line termination sequence.\nIf you look at\n<a HREF=\"http:\/\/www.chemie.fu-berlin.de\/cgi-bin\/man\/sgi_irix?stty+1\">\nthe stty options<\/a>,\nyou&#8217;ll see that the onlcr option specifies whether\na LF should be changed into CR+LF.\nIf you get this setting wrong, you get stairstep text,\nwhere<\/p>\n<pre>\neach\n    line\n        begins\n<\/pre>\n<p>where the previous line left off.\nSo even unix, when left in raw mode, requires CR+LF to terminate\nlines.  The implicit CR before LF is a unix invention,\nprobably as an economy, since it saves one byte per line.\n<\/p>\n<p>\nThe unix ancestry of the C language carried this convention\ninto the C language standard, which requires only &#8220;\\n&#8221; (which\nencodes LF) to\nterminate lines, putting the burden on the runtime libraries\nto convert raw file data into logical lines.\n<\/p>\n<p>\nThe C language also introduced the term &#8220;newline&#8221; to express\nthe concept of &#8220;generic line terminator&#8221;.  I&#8217;m told that the ASCII\ncommittee changed the name of character 0x0A to &#8220;newline&#8221; around 1996,\nso the confusion level has been raised even higher.\n<\/p>\n<p>\n<a HREF=\"http:\/\/www.essenz.com\/support\/comp.unix.misc\/Sep\/20\/40645.html\">\nHere&#8217;s another discussion of the subject,\nfrom a unix perspective<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This protocol dates back to the days of teletypewriters. CR stands for &#8220;carriage return&#8221; &#8211; the CR control character returned the print head (&#8220;carriage&#8221;) to column 0 without advancing the paper. LF stands for &#8220;linefeed&#8221; &#8211; the LF control character advanced the paper one line without moving the print head. So if you wanted to [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-40193","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>This protocol dates back to the days of teletypewriters. CR stands for &#8220;carriage return&#8221; &#8211; the CR control character returned the print head (&#8220;carriage&#8221;) to column 0 without advancing the paper. LF stands for &#8220;linefeed&#8221; &#8211; the LF control character advanced the paper one line without moving the print head. So if you wanted to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/40193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=40193"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/40193\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=40193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=40193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=40193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}