What’s the difference between \n
(newline) and \r
(carriage return)?
In particular, are there any practical differences between \n
and \r
? Are there places where one should be used instead of the other?
What’s the difference between \n
(newline) and \r
(carriage return)?
In particular, are there any practical differences between \n
and \r
? Are there places where one should be used instead of the other?
lets take this example try putting \n in place of \r it will not work and try to guess why?
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.To complete,
In a shell (bash) script, you can use
\r
to send cursor, in front on line and, of course\n
to put cursor on a new line.For example, try :
AA--AA
AA--AABB
BB--AABB
But don't forget to use
-en
as parameters.In windows, the \n moves to the beginning of the next line. The \r moves to the beginning of the current line, without moving to the next line. I have used \r in my own console apps where I am testing out some code and I don't want to see text scrolling up my screen, so rather than use \n after printing out some text, of say, a frame rate (FPS), I will printf("%-10d\r", fps); This will return the cursor to the beginning of the line without moving down to the next line and allow me to have other information on the screen that doesn't get scrolled off while the framerate constantly updates on the same line (the %-10 makes certain the output is at least 10 characters, left justified so it ends up padded by spaces, overwriting any old values for that line). It's quite handy for stuff like this, usually when I have debugging stuff output to my console screen.
A little history
The /r stands for "return" or "carriage return" which owes it's history to the typewriter. A carriage return moved your carriage all the way to the right so you were typing at the start of the line.
The /n stands for "new line", again, from typewriter days you moved down to a new line. Not nessecarily to the start of it though, which is why some OSes adopted the need for both a /r return followed by a /n newline, as that was the order a typewriter did it in. It also explains the old 8bit computers that used to have "Return" rather than "Enter", from "carriage return", which was familiar.
In terms of ascii code, it's 3 -- since they're 10 and 13 respectively;-).
But seriously, there are many:
\n
is the code for end-of-line,\r
means nothing special\n
is the standard escape sequence for end of line (translated to/from OS-specific sequences as needed)\r
was the code for end-of-line instead\r\n
, in this order\r\n
is the standard line-termination for text formats on the Internet\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!-) Wikipedia has a more detailed explanation.\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;-).Just to add to the confusion, I've been working on a simple text editor using a TextArea element in an HTML page in a browser. In anticipation of compatibility woes with respect to CR/LF, I wrote the code to check the platform, and use whichever newline convention was applicable to the platform.
However, I discovered something interesting when checking the actual characters contained in the TextArea, via a small JavaScript function that generates the hex data corresponding to the characters.
For the test, I typed in the following text:
Hello, World[enter]
Goodbye, Cruel World[enter]
When I examined the text data, the byte sequence I obtained was this:
48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 0a 47 6f 6f 64 62 79 65 2c 20 43 72 75 65 6c 20 57 6f 72 6c 64 0a
Now, most people looking at this, and seeing 0a but no 0d bytes, would think that this output was obtained on a Unix/Linux platform. But, here's the rub: this sequence I obtained in Google Chrome on Windows 7 64-bit.
So, if you're using a TextArea element and examining the text, CHECK the output as I've done above, to make sure what actual character bytes are returned from your TextArea. I've yet to see if this differs on other platforms or other browsers, but it's worth bearing in mind if you're performing text processing via JavaScript, and you need to make that text processing platform independent.
The conventions covered in above posts apply to console output, but HTML elements, it appears, adhere to the UNIX/Linux convention. Unless someone discovers otherwise on a different platform/browser.