272

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.

edddd
  • 379
  • 1
  • 12
pheromix
  • 16,775
  • 24
  • 79
  • 150
  • 1
    Have a look at Wikipedia: http://en.wikipedia.org/wiki/Carriage_Return#Computers – exic Oct 05 '12 at 13:54
  • 3
    Possible duplicate of [What are carriage return, linefeed, and form feed?](http://stackoverflow.com/questions/3091524/what-are-carriage-return-linefeed-and-form-feed) – jtbandes Aug 09 '16 at 06:05
  • 2
    Have a look at this article, It clears out everything impeccably http://digital.ni.com/public.nsf/allkb/5A5A050A3019A573862575F30061D49B – Rohit Saluja Oct 02 '16 at 08:08

4 Answers4

413

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.

Tsunamis
  • 5,282
  • 1
  • 18
  • 21
  • 26
    you'd think even old typewriters should have thought about making \n represent two steps. – ColacX Jul 30 '14 at 18:47
  • 27
    @ColacX It is often useful to perform a carriage return without a line feed when overwriting the text on the current line is desired. This applies to both typewriters and terminals. – Dan Bechard Dec 29 '14 at 20:06
  • 3
    So, in Windows, the proper sequence for the end of a line would look like `\n\r`? – Delfino Apr 07 '15 at 03:25
  • 29
    @Delfino not really. On mechanical printers, it made sense to initiate a carriage return earlier, since it's slower, and feed the line while the carriage is still moving. – Maciej Stachowski Apr 15 '15 at 10:31
  • @Tsunamis By historical reasons, the right combination is new line and return: you push the typewriter's lever and the line will feed, pushing all way will perform carriage return. See: http://www.machinesoflovinggrace.com/manuals/Manual-RemingtonNoiseless8-9.jpg – Gustavo Apr 24 '17 at 23:03
  • also, Line Feed = `U+000A` while Carriage Return = `U+000D` – chharvey Nov 27 '17 at 03:15
  • Then why is that that in Notepad++ I can delete all \n -s, yet the CR signs remain at the ends of lines, but the whole text is not in one line, of course? Why to have two distinct characters, when you can do everything with one? – GregT Mar 23 '18 at 10:50
  • 2
    If you would send `foo` `\r` `bar` to a terminal, you would see only _bar_ on your screen, because _foo_ will be overwritten with _bar_ after moving the cursor to the beginning of the line. The text editor obivously can't overwrite the text in the same line via control characters. So in Notepad++ a single `\r` will be interpreted similar to `\n` or `\r\n`. – Tsunamis Mar 23 '18 at 15:45
  • 5
    Do not forget that older Macs used only \r – Envite Jun 11 '18 at 14:16
  • It's easy to remember the most commonly used order from the abbreviation CRLF - carriage return, then line feed. You'll rarely, if ever, see the order LFCR. – Mottie Sep 08 '18 at 12:17
  • Explains why the supposedly sorted list of characters I'm looking at has alternating ASCII values of 10 and 13... Was probably sorted on Windows as \r\n and is displayed on my Unix system as two separate characters. – H Froedge Nov 21 '19 at 18:15
  • 1
    I remember the most commonly used order as **R**etur**N** – hyperspasm Jul 22 '20 at 11:56
46

Since I can not comment because of not having enough reward points I have to answer to correct answer given by @Burhan Khalid.
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 horizontly and Line feed shifts the cursor to the next line vertically.Combination of both gives you new line(\n) effect.
Reference - https://en.wikipedia.org/wiki/Carriage_return#Computers

Monalisa Das
  • 577
  • 4
  • 3
  • Also, it become the difference between breaking line and breaking paragraph when computers replaced typewriters - text processing. – Gustavo Apr 24 '17 at 22:58
  • This seems to be talking about Windows only. It's *sort of* correct for Unix-like systems, but text files only contain the newline character, `\n`, which appears as a newline + carriage return when displaying it on the screen. – wjandrea Dec 31 '20 at 03:34
9

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.

Burhan Khalid
  • 161,711
  • 18
  • 231
  • 272
  • 14
    I believe the carriage return refers to moving to the beginning of the same line, rather than moving to the next line. The typewriter analogy refers to both moving down to the next line vertically (line feed) and returning to the beginning of the line horizontally (carriage return). http://en.wikipedia.org/wiki/Carriage_return – Feckmore Jan 17 '15 at 15:53
0

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 examle, 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