Possible Duplicate:
What is the difference between \r and \n?
Hi,
What is difference between \n
(newline) and \r
(carriage return)? They both move current cursor to the next line. Are they same?
Possible Duplicate:
What is the difference between \r and \n?
Hi,
What is difference between \n
(newline) and \r
(carriage return)? They both move current cursor to the next line. Are they same?
\r
returns the cursor to the beginning of the line, NOT to the next line. When you use \n
in Linux, \r is implied, in windows, it is not.
Using \r
in UNIX may result in overwriting the same line.
I suggest you read this.
In short, a newline in Windows is "\r\n"
, while a newline in Unix is just "\n"
(and, just to make life difficult, a newline in older Macs is "\r"
)
Actually, a carriage return is supposed to move the cursor to the beginning of the current line. Then, newline moves the cursor exactly down one.
Nowadays, compilers will often automatically convert one or the other to \r\n
on Windows or \n
on Linux. Mac used to use \r but they have changed to the \n convention.
(edit: removed false/untested statements)
No they're not. Modern text editors often treat them the same however because their old uses don't make much sense for digital word processors.
For example \r
literally means "return to the beginning of the line". While this might have been useful for a typewriter if you just wanted to overwrite everything on that line this sort of functionality doesn't make much sense for digital type.
\n
on the other hand would simply move down a line without returning to the beginning. This was also useful on a typewriter for indentation or bulleting. Again, not something that makes much sense for digital type.
Telnet is one example where both characters are still used in this manner.
Both characters were included in ascii language simply because when it was being spec'd they hadn't realized that functionality that was useful on a typewriter didn't make much sense on a computer.
Read The Great Newline Schism it explains everything in deep detail with great humor.
Ah the old days of the typewriter...
The difference between the two stems from days of yonder when typing was done directly to paper. It required two actions to go to the next line:
Splitting these two actions facilitated going back to a precise character position to correct it (there was no way to go up one line, or left one character!). Holding paper whiteout on the erroneous character and hitting that key would neatly whiteout exactly that erroneous character, then you could go back again and hit the correct key (there was a key for not moving the carriage though).
In the young computer age these actions were translated 1 to 1 into \r
for carriage return and \n
for shifting the 'paper'.
Nowadays the major operating systems apparently have differing opinions on whether this is still necessary for computer technology where going back to previous position is much easier. However, in modern programming languages you'll generally see that \n
is assumed to mean \r\n
.