What are Line Feed and Carriage Return characters?

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 do 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 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 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(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 mean 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 traditional we encoded above two operations in to two invisible character encoding namely LF(10) and CR(13) in ASCII. When ever a text contain 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 mean “move to the beginning of the current line and then move one row down”!

Hope below gif will helps us understand what I mean(15sec).

https://www.youtube.com/watch?v=oxN1C2QQUIE

 

Leave a comment

Leave a Reply