Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of carriage return and new line feed

Tags:

text

newline

Is it important to have the correct order of carriage return then new line feed? For text editors does it matter in what order they appear?

For example instead of

\r\n 

this

\n\r 

Seems like Jeff has allready writen up a very nice Blog Post on the subject.

like image 685
Chad Avatar asked Dec 11 '09 05:12

Chad


People also ask

Is line feed the same as carriage return?

A line feed means moving one line forward. The code is \n . A carriage return means moving the cursor to the beginning of the line.

What is carriage return and new line?

The /r stands for return or carriage return which owes it's history to the typewriter. A carriage return moved your carriage all the way to the right so you were typing at the start of the line. The /n stands for new line, again, from typewriter days you moved down to a new line.

What is form feed and carriage return?

Form feed is a page-breaking ASCII control character. It forces the printer to eject the current page and to continue printing at the top of another. Often, it will also cause a carriage return. The form feed character code is defined as 12 (0xC in hexadecimal), and may be represented as control+L or ^L .

What is a new line feed?

Adding Newline Characters in a String. Operating systems have special characters denoting the start of a new line. For example, in Linux a new line is denoted by “\n”, also called a Line Feed. In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF.


2 Answers

The traditional order, when both control characters are used, is Carriage Return, then Line Feed.

The reason for this goes back to the old ASR-33 Teletype.

When a Carriage Return is issued to an ASR-33, the print head, if it is near the right margin, takes over a tenth of a second to return to the left margin, plus there is a bit of "bounce" when the left margin is hit.

If the order were Line Feed, then Carriage Return, the first printed character might occur a tenth of a second after the Carriage Return, and thus might end up printing (as a smear) halfway across the page. But if Line Feed comes after Carriage Return then the time taken by the Line Feed provides extra time for the print head to complete it's trip.

Some systems (I'm thinking the old Xerox Sigma 7 OS, eg) do Line Feed then Carriage Return, but they inject, eg, NULL characters into the data stream to allow the print head to do it's thing.

And, of course, when you get to faster devices (some early 30 CPS teleprinters, eg), the problem gets worse and a more complex strategy is required.

(There is also the point that, for user input, the Carriage Return is provided by the user pressing the Return key, while the Line Feed must be provided by the computer. For this reason it was often the "style" to have normal print lines begin with Line Feed and end with Carriage Return. A prompt for user input, then, consisted only of a Line Feed, while the user input ended with the Carriage Return. This scheme worked well when used consistently, but of course that didn't always happen.)

like image 61
Hot Licks Avatar answered Sep 19 '22 15:09

Hot Licks


There are three common linefeed formats:

  • \r\n for the DOS\Windows world
  • \r for the pre-OSX Mac world
  • \n for the Unix and Unix-like world

\n\r is not a standard anywhere that I'm aware of and will likely result in your editor thinking that it has a Unix-format text file, and then it'll display the weird \r character as text.

Historically, \r translates to carriage return (CR, ASCII code 13) which refers to old school typewriter, where you would push the carriage back to the left to return the cursor back to the start of the line. \n translates to line feed (LF, ASCII code 10) which moves the character down the page one character. Although potentially interesting, it generally doesn't matter — just use the appropriate linefeed format for your current platform.

like image 45
Conspicuous Compiler Avatar answered Sep 20 '22 15:09

Conspicuous Compiler