LINE FEED AND CARRIAGE RETURN
Line Feed and Carriage Return Explained
We all know what a line feed is \n and what a carriage return character is \r. But most of us do not know why we have these two different characters to represent a new line. What does each of them really mean?
To start with, these are an emulation of how line ending is handled in a traditional typewriter (hey, I actually worked on one of these :)).
Line Feed is the act of moving the cursor exactly one row down, staying at the current column.
In a typewriter, this is rolling the page one row up without moving the platen horizontally.
Carriage Return is the act of moving the cursor to the beginning of the line, staying on the current row.
In a typewriter, this is moving the platen to the right horizontally without rolling the page.
The above operation, when done in succession, will make the typing head point to the beginning of the next line (the below gif shows this). Unfortunately, the exact same operation cannot be directly translated to a modern-day computer because the mechanics of both are completely different. Nevertheless, the act of creating a new line remained the same. When it comes to a computer/terminal, newline always means bringing the cursor to the beginning of the next line on a display. No moving mechanical parts, no platen, no mechanical levers pulling left-right.
Now to achieve this, traditionally we encoded the above two operations into two invisible character encodings, namely LF (10) and CR (13) in ASCII. Whenever a text contains these characters, it is an instruction to the output device to handle the newline. But this very basic operation is handled very differently in various operating systems. For example, all Unix derivatives encode just LF (\n) itself as a newline, and on the other hand, Windows more closely emulates the typewriter by using CR (\r) + LF (\n). This literally means “move to the beginning of the current line and then move one row down”!
Comments
Post a Comment