64

They both mean "new line" but when is one used over the other?

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
Emanuil Rusev
  • 34,563
  • 55
  • 137
  • 201
  • 1
    http://stackoverflow.com/questions/1279779/what-is-the-difference-between-r-and-n – Josh Lee Sep 29 '10 at 13:27
  • 1
    possible duplicate of [Difference between CR LF, LF and CR line break types?](http://stackoverflow.com/questions/1552749/difference-between-cr-lf-lf-and-cr-line-break-types) – Josh Lee Sep 29 '10 at 13:28

7 Answers7

89

\r\n is a Windows Style

\n is a POSIX Style

\r is a old pre-OS X Macs Style, Modern Mac's using POSIX Style.


\r is a carriage return and \n is a line feed, in old computers where it not have monitor, have only printer to get out programs result to user, if you want get printing from staring of new line from left, you must get \n for Line Feed, and \r for get Carriage return to the most left position, this is from old computers syntax saved to this time on Windows platform.

blacktide
  • 10,654
  • 8
  • 33
  • 53
Svisstack
  • 16,203
  • 6
  • 66
  • 100
  • 9
    CR+LF is commonly used in many networking protocols (like HTTP), regardless of the OS used to transmit the data. `\r` is commonly mapped to CR whenever the target system uses ASCII or Unicode as its character system. (If the target system uses EBCDIC, then `\r` may not have a mapping.) `\n` is trickier, as it depends on context. It's often mapped to an ASCII/Unicode LINE FEED, but it's not always that simple. Answers to the "Possible Duplicate" question has details. – Adrian McCarthy Oct 08 '12 at 20:13
  • You can see the HTTP spec from here: https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html CRLF (carriage return line feed) – mfaani Mar 14 '21 at 20:43
15

\n means new line. It means that the cursor must go to the next line.

\r means carriage return. It means that the cursor should go back to the beginning of the line.

Unix programs usually only needs a new line (\n).

Windows programs usually need both.

Philippe Carriere
  • 3,712
  • 4
  • 25
  • 46
  • 2
    Coming in 9 years later, you wrote Windows and Windows programs, instead of MacOS and Windows – martinomburajr Mar 23 '19 at 14:08
  • 1
    I'm pretty sure you're wrong and that Windows and Mac behave differently. But since I'm not Mac expert, I'll remove the line about mac entirely and leave it to more knowledgeable people. – Philippe Carriere Mar 04 '21 at 18:00
  • Prior to Mac OS X, Macs used `\r` alone as its line terminator. As Mac OS X / OS X / macOS is a Unix, the old convention was dropped in favor of the standard Unix convention of a single linefeed. – chepner Jul 18 '21 at 18:49
8

\n is a newline only, \r\n is a newline and a carriage return (i.e. move the cursor to the left).

Jackson Pope
  • 14,520
  • 6
  • 56
  • 80
4

Different Operating Systems handle newlines in a different way. Here is a short list of the most common ones: DOS and Windows

They expect a newline to be the combination of two characters, namely '\r\n' (or 13 followed by 10).

Unix (and hence Linux as well)

Unix uses a single '\n' to indicate a new line.

Mac

Macs use a single '\r'.

so this causes problems when u port your app from windows to mac when u're using folder path's and alike.

Emerion
  • 820
  • 6
  • 13
3

'\n' is the default in Unix, '\r\n' is the default in Windows.

Thomas Mueller
  • 48,905
  • 14
  • 116
  • 132
2

This is how new line is represented in operating systems Windows (\r\n)and linux (\n)

On Unix the \r is a Carriage Return (CR) and the \n a Line Feed (LF) which together happen to be the be Windows newline identifier and you are replacing them with a Unix newline identifier.

On Windows the \r is a CR, too, but the \n is a combination of CR and LF. So effectively you are trying to replace CR+CR+LF with CR+LF. Doesn't make much sense, does it.

From "perldoc perlop": All systems use the virtual ""\n"" to represent a line terminator, called a "newline". There is no such thing as an unvarying, physical newline character. It is only an illusion that the operating system, device drivers, C libraries, and Perl all conspire to preserve. Not all systems read ""\r"" as ASCII CR and ""\n"" as ASCII LF. For example, on a Mac, these are reversed, and on systems without line terminator, printing ""\n"" may emit no actual data. In general, use ""\n"" when you mean a "newline" for your system, but use the literal ASCII when you need an exact character. For example, most networking protocols expect and prefer a CR+LF (""\015\012"" or ""\cM\cJ"") for line terminators, and although they often accept just ""\012"", they seldom tolerate just ""\015"". If you get in the habit of using ""\n"" for networking, you may be burned some day.

Andriy Sholokh
  • 852
  • 2
  • 6
  • 15
1

The difference is between different operating systems, on Windows, the newline character is \r\n while on Linux it is just \n Mac OSX has just \r its really just a matter of what the designers of the OS made it to be

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201