views:

277

answers:

6

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

I understand that it's different for each Operating System, for example, "\r\n" may be expected in Windows, "\n" may be expected in Unix and "\r" may be expected in Macs.

What are the differences between these two (or three, if you want to include "\r\n") escape sequences? Why isn't it consistent across all systems?

I'm curious about this.

+4  A: 

Wikipedia is your friend :)

Aziz
Then how come it never remembers my birthday?
John Lockwood
It does ... http://en.wikipedia.org/wiki/February_31 :P
Aziz
+3  A: 

it isn't consistent due to the historical decisions made by each operating system maker. as for the differences, see characters 12 and 15 here: http://www.asciitable.com/

dusoft
+11  A: 

Because it just isn't.

\r means "carriage return". \n means "new line" line feed

Some systems are wise enough to suggest that all they need to get the cursor to a new line is \n. Others think that it's more appropriate to go \r\n, because, they may claim that the column position needs to be reset as well (i.e. the \r).

Sometimes in computing things are 'just this way'. This is one of those cases :)

Noon Silk
You can never go wrong with 'just because'. Makes perfect sense now.
Corey
the 2 characters are for historical hardware reasons... old printers needed to get the 2 characters to move to the next line... the \r moved the carriage back and the \n advanced the paper to the next line
JoelFan
+1 for the other comment
cletus
+2  A: 

Some systems don't even have the concept of an end of line. VAX/VMS and the IBM VM operating systems all support record-based files where no end of line marker is needed.

anon
+1, that's one of those interesting facts that I'll never be able to bring up in a conversation but is worth knowing nonetheless.
dreamlax
If there was a book entitled "Neil Butterworth's Book of Facts" I would deeply consider purchasing it
dreamlax
At one job I was on we invented the concept of "Boring, but true." This can be usefully be applied to any number of computing concepts and technologies.
anon
+13  A: 

The difference between '\r' and '\n' is 3.

dreamlax
A wise guy, eh?
bobbymcr
+1 because this made me smile.
Daniel Pryden
are you referring to the ascii codes or just being random? :D
CrazyJugglerDrummer
+7  A: 

The accepted answer isn't exactly accurate. In point of fact, \n means 'linefeed', not 'new line'. The distinction is important because both symbols hearken back to the days of teletypes as the primary output for a computer instead of a CRT or LCD. For a teletype, the act of moving the print head back to column 1 and the act of feeding the paper up 1 line were two distinct actions. As to the origin of the differing conventions for end of line characters in files, the other answers have correctly pointed out that this is just a historical curiosity.

Jherico
While this is technically correct, the differing sequences between operating systems was simply a matter of preference of their creators (as noted in comments and answers). +1 for an actual explanation, though, I appreciate it.
Corey
it's not a preference... it was dictated by the crude hardware of the time (i.e. printers)
JoelFan