views:

469

answers:

6

Whats the difference between \n (newline) and \r (carriage return)?

Edited to add: All the answers are fairly predictable, but I'd be interested to know if there are any PRACTICAL differences between \n and \r. Are there places where one should be used over the other?

A: 

one is new line ascii 10 and the other one is carriage return ascii 13

+2  A: 

\r is char code 13 and \n is char code 10

windows use \r\n, linux use \n and mac use \r as far as I know.

S.Mark
A: 

Two different characters.

\n is used as an end-of-line terminator in Unix text files

\r is used as an end-of-line terminator in Mac text files

\r\n (ie both) are used to terminate lines in Windows and DOS text files.

pavium
+5  A: 

historically a \n was used to move the carriage down, while the \r was used to move the carriage back to the left side of the page.

tster
Perhaps not a terribly practical answer to a computer question, but the historical tidbit gets an upvote from me anyway.
John Y
Er, I mean not practical on *modern* computers, of course.
John Y
The current generation will be asking 'what carriage?'
pavium
A horse carriage, naturally. Those were the only carriages in the olden days.
Goose Bumper
+1  A: 

Two different characters for different Operating Systems. Also this plays a role in data transmitted over TCP/IP which requires the use of \r\n

\n Unix

\r Mac

\r\n Windows and DOS.

a432511
+9  A: 

In terms of ascii code, it's 3 -- since they're 10 and 13 respectively;-).

But seriously, there are many:

  • in Unix and all Unix-like systems, \n is the code for end-of-line, \r means nothing special
  • as a consequence, in C and most languages that somehow copy it (even remotely), \n is the standard escape sequence for end of line (translated to/from OS-specific sequences as needed)
  • in old Mac systems (pre-OS X), \r was the code for end-of-line instead
  • in Windows (and many old OSs), the code for end of line is 2 characters, `\r\n', in this order
  • as a (surprising;-) consequence (harking back to OSs much older than Windows), \r\n is the standard line-termination for text formats on the Internet
  • for electromechanical teletype-like "terminals", \r commands the carriage to go back leftwards until it hits the leftmost stop (a slow operation), \n commands the roller to roll up one line (a much faster operation) -- that's the reason you always have \r before \n, so that the roller can move while the carriage is still going leftwards!-)
  • for character-mode terminals (typically emulating even-older printing ones as above), in raw mode, \r and \n act similarly (except both in terms of the cursor, as there is no carriage or roller;-)

In practice, in the modern context of writing to a text file, you should always use \n (the underlying runtime will translate that if you're on a weird OS, e.g., Windows;-). The only reason to use \r is if you're writing to a character terminal (or more likely a "console window" emulating it) and want the next line you write to overwrite the last one you just wrote (sometimes used for goofy "ascii animation" effects of e.g. progress bars) -- this is getting pretty obsolete in a world of GUIs, though;-).

Alex Martelli
Hahaha, funny! Nice one!
Seth Illgard
wow! \r thats more than an 'good explanation' :) \n
Nimbuz
brilliant, this is the kind of answer I was hoping for :)
Goose Bumper
Excellent and interesting..
Babu