pheromix
pheromix

Reputation: 19317

What is the difference between a "line feed" and a "carriage return"?

If there are two keywords then they must have their own meanings. So I want to know what makes them different and what their code is.

Upvotes: 303

Views: 400066

Answers (5)

Philippe Cloutier
Philippe Cloutier

Reputation: 599

Each of these designates a control character used to trigger a manual line break (newline). The fact that there are 2 is historical, from the old days of typewriters and then teletype printers, back when pages were printed character-by-character, each line from left to right.

A carriage return (CR) represents moving the sheet of paper to the right; in other words returning the cursor to the beginning of the line. In ASCII (and Unicode), CR has code point 13 (0x0D).

A line feed (LF) represents moving the sheet of paper up; in other words moving the cursor down by 1 line. In ASCII (and Unicode), CR has code point 10 (0x0A).

Historically (due to print timings reasons), various protocols and encodings represented newlines using more than 1 control character. To represent a newline in a raw text file (.txt, .html, .php for example), Microsoft Windows (influenced by CP/M) uses a sequence which contains both (CR+LF), which is most correct historically, but suboptimal storage-wise. Other operating systems adopted more optimal conventions, but unfortunately different ones, most importantly just LF on Unix, and just CR on Macintosh. These differences have persisted to this day.

To summarize, nowadays, both can represent a newline, but it depends on context (mostly on the operating system).

Upvotes: 1

Monalisa Das
Monalisa Das

Reputation: 757

In very layman language Enter key press is combination of carriage return and line feed.

Carriage return points the cursor to the beginning of the line horizontally and Line feed shifts the cursor to the next line vertically. Combination of both gives you the new line (\n) effect.

Reference: https://en.wikipedia.org/wiki/Carriage_return#Computers

Upvotes: 56

Michael Otterbine
Michael Otterbine

Reputation: 23

Both "line feed' (0x0A or 10) and 'carriage return' (0x0D or 13) are single-byte values. These values are the accepted standard for LF/CR. Most languages will type these as 'characters.' You can find these values on any standard ASCII table.

For example, in C# a string such as:

String str = "\n\r";

is two characters long (ignoring the hidden end null character '0x00' required in string types). However, you could make an equivalent array of type character such as:

char[] c = new char[](){0x0A,0x0D}; // LF, CR

Upvotes: 1

Burhan Khalid
Burhan Khalid

Reputation: 174624

Both of these are primary from the old printing days.

Carriage return is from the days of the teletype printers/old typewriters, where literally the carriage would return to the next line, and push the paper up. This is what we now call \r.

Line feed LF signals the end of the line, it signals that the line has ended - but doesn't move the cursor to the next line. In other words, it doesn't "return" the cursor/printer head to the next line.

For more sundry details, the mighty Wikipedia to the rescue.

Upvotes: 11

Tsunamis
Tsunamis

Reputation: 6230

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. The code is \r.

Windows editors often still use the combination of both as \r\n in text files. Unix uses mostly only the \n.

The separation comes from typewriter times, when you turned the wheel to move the paper to change the line and moved the carriage to restart typing on the beginning of a line. This was two steps.

Upvotes: 483

Related Questions