Saturday, March 15, 2008

Carriage Return (CR) & Line Feed (LF)

Today while playing with `od` - a command-line tool to dump file data in octal and other formats - and looking at 012 (octal code of \n) it just came to my mind to find out the difference between carriage return (\r) and line feed (\n) characters and how do they date back in history. I got my answer from the very first attempt in Google which directly led me to Yahoo answers.

The two characters emulate the two main actions in typewriters. Carriage-Return which moves the cursor back to the beginning of the line corresponds to the moving of typewriter’s carriage to the initial leftmost position, whereas Line-Feed which move the cursor to the next line corresponds to the rolling up of paper by one line. The combination of both i.e. CR-LF were initially used in Teletypewriters to move the print head back to the beginning of new line.

Even though the two still performs the same action in standard output and printers, not all operating systems use both of them. *nix based systems use \n, Mac uses \r, whereas Windows uses both \r\n. This is a reasons why *nix text documents look weird in windows.

1 comment:

ipsita said...

Simple but interesting.