views:

129

answers:

6

Possible Duplicate:
What is the difference between \r and \n?

Hi,

What is difference between \n (newline) and \r (carriage return)? They both move current cursor to the next line. Are they same?

+8  A: 

\r returns the cursor to the beginning of the line, NOT to the next line. When you use \nin Linux, \r is implied, in windows, it is not.

Using \r in UNIX may result in overwriting the same line.

The Guy Of Doom
+6  A: 

I suggest you read this.

In short, a newline in Windows is "\r\n", while a newline in Unix is just "\n" (and, just to make life difficult, a newline in older Macs is "\r")

BlueRaja - Danny Pflughoeft
+2  A: 

Actually, a carriage return is supposed to move the cursor to the beginning of the current line. Then, newline moves the cursor exactly down one.

Nowadays, compilers will often automatically convert one or the other to \r\n on Windows or \n on Linux. Mac used to use \r but they have changed to the \n convention.

(edit: removed false/untested statements)

Ricket
*if you `putchar('\r')` and compile in Linux, the `\r` will be replaced by `\n`.* Absolutely wrong. Printing `\r` will move the cursor back to the start of the line in the console, and subsequent prints will overwrite the characters next to the cursor.
KennyTM
Hmm.. Maybe it was windows... I just tried it and you are correct though. I wish I could remember, I just ran into a problem like that a couple weeks ago...
Ricket
Ah well, for the lack of correct memory, I just removed that entire paragraph. I know there was some quirk but I can't figure it out at the moment.
Ricket
A: 

No they're not. Modern text editors often treat them the same however because their old uses don't make much sense for digital word processors.

For example \r literally means "return to the beginning of the line". While this might have been useful for a typewriter if you just wanted to overwrite everything on that line this sort of functionality doesn't make much sense for digital type.

\n on the other hand would simply move down a line without returning to the beginning. This was also useful on a typewriter for indentation or bulleting. Again, not something that makes much sense for digital type.

Telnet is one example where both characters are still used in this manner.

Both characters were included in ascii language simply because when it was being spec'd they hadn't realized that functionality that was useful on a typewriter didn't make much sense on a computer.

Spencer Ruport
I don't know if "they hadn't realised" -- I suspect it was more because at the time people were still widely using output devices such as teletypes where the distinction *was* meaningful. (For example, carriage return without newline was how we used to do strikeout (and even bold) on ye olde typewriters, or I guess daisy wheel printers, before we had bitmapped printers.)
itowlson
Yeah that's what I was trying to say and I did so poorly. :D I rephrased it a bit. Perhaps it's better now?
Spencer Ruport
+1  A: 

Read The Great Newline Schism it explains everything in deep detail with great humor.

Rickard von Essen
A: 

Ah the old days of the typewriter...

The difference between the two stems from days of yonder when typing was done directly to paper. It required two actions to go to the next line:

  1. pushing the 'carriage' (big cilinder on the top) back to the left (this is where the character would end up).
  2. shifting the paper one line up. (thus going down one line)

Splitting these two actions facilitated going back to a precise character position to correct it (there was no way to go up one line, or left one character!). Holding paper whiteout on the erroneous character and hitting that key would neatly whiteout exactly that erroneous character, then you could go back again and hit the correct key (there was a key for not moving the carriage though).

In the young computer age these actions were translated 1 to 1 into \r for carriage return and \n for shifting the 'paper'.

Nowadays the major operating systems apparently have differing opinions on whether this is still necessary for computer technology where going back to previous position is much easier. However, in modern programming languages you'll generally see that \n is assumed to mean \r\n.

NomeN