3

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 snd "\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.

Community
  • 1
  • 1
Corey
  • 1,177
  • 4
  • 16
  • 24
  • You can learn more about how teletypes got us into this mess at http://www.oualline.com/practical.programmer/eol.html – Crashworks Aug 30 '09 at 23:18
  • Macs running OS X are "\n" systems as well since they are Unix under the hood. I would be very surprised if you ran into a OS 9 or older box. – stonemetal Aug 31 '09 at 00:48
  • @stonemetal, I've seen a few OS 9 boxes around. It's usually the same kind of crowd that won't upgrade from Windows 98 because their precious software (that they invested a lot of money in) won't run on anything newer. – dreamlax Aug 31 '09 at 01:27
  • 1
    Duplicate of http://stackoverflow.com/questions/1279779/what-is-the-difference-between-r-and-n – Andrew Grimm Mar 28 '10 at 06:59
  • This is NOT an exact duplicate. This question is asking about the reason why different platforms use different new line semantics. The linked question is about the meanings of `\r` and `\n`. It's a significant different (as is reflected by the accepted answers for both questions). – Adrian McCarthy Mar 03 '12 at 17:53

6 Answers6

31

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

dreamlax
  • 93,976
  • 29
  • 161
  • 209
16

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
  • 54,084
  • 6
  • 88
  • 105
  • 2
    You can never go wrong with 'just because'. Makes perfect sense now. – Corey Aug 30 '09 at 22:44
  • 4
    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 Aug 30 '09 at 23:35
  • +1 for the other comment – cletus Jan 20 '10 at 06:23
  • 1
    Actually, `\n` does mean new line. The fact that most implementations use line feed for `\n` is an implementation detail. – Adrian McCarthy Mar 03 '12 at 17:54
7

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
  • 28,584
  • 8
  • 61
  • 87
  • 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 Aug 30 '09 at 23:28
  • it's not a preference... it was dictated by the crude hardware of the time (i.e. printers) – JoelFan Aug 30 '09 at 23:36
  • @JoelFan: The difference between operating systems _was_ a matter of preference however. – Mooing Duck Jan 09 '12 at 22:28
  • Actually, `\n` does NOT necessarily mean line feed. It depends on the context. In C and C++, it's used to indicate a new line when doing I/O in text mode. Most implementations use line feed to represent it, but that's an implementation detail. A conforming implementation could use something like `U+0085 NEXT LINE` instead. – Adrian McCarthy Mar 03 '12 at 17:51
  • Without some specific contravening context, \n is U+000A, which is Line Feed. U+0085 doesn't have a single byte UTF-8 representation, making its use in source code dubious. – Jherico Mar 22 '12 at 17:25
4

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,289
  • 5
  • 38
  • 44
3

Wikipedia is your friend :)

Aziz
  • 20,065
  • 8
  • 63
  • 69
2

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.

  • +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 Aug 30 '09 at 22:44
  • If there was a book entitled "Neil Butterworth's Book of Facts" I would deeply consider purchasing it – dreamlax Aug 30 '09 at 22:57
  • 1
    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. –  Aug 30 '09 at 22:58