83

Windows carriage return is \r\n while it is \n in Unix, is \r\n treated as two characters?

shingara
  • 46,608
  • 11
  • 99
  • 105
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215

6 Answers6

125

These are two characters:

  • \r is carriage return;
  • \n is line feed.

Two characters combined represent a new line on Windows. Whereas on Linux, \n represents new line. It moves cursor to the start of new line on Linux. On Windows, the cursor will stay at the same column in the console but on the next line.

\r on Linux has the same effect as on Windows: moves cursor to the start of the line. It is possible to print different information on the same line where \r is used instead of \n.

Alexey Ivanov
  • 11,541
  • 4
  • 39
  • 68
  • 5
    +1. As an additional info for the OP: [Newline](http://en.wikipedia.org/wiki/Newline). – informatik01 Dec 14 '13 at 23:01
  • 1
    In C, `\n` is not required to be line feed (LF). The standard requires that '\n' be a single `char` value that text mode i/o will translate to and from the platform's preferred sequence for beginning a new line. That said, all C implementations I know of choose to use LF for `\n`. Then again, I don't have hands-on experience with any C compilers that target EBCDIC. – Adrian McCarthy Feb 27 '23 at 18:26
10

Actually \r is 0x0D (^M) and \n is 0x0A (^J), but..... on windows:

\n will write 0x0D 0x0A

on unix:

\r will write 0x0D
\n will write 0x0A
Tomas
  • 57,621
  • 49
  • 238
  • 373
  • Thus on Windows we should print \x0A explicitly if we want to move 1 line down, while staying in the same column? – Viet Nov 15 '12 at 04:16
  • 1
    If I'm right, this is due to character conversion happening when opening files as text streams. Treating `stdout` as a binary stream rather than a text stream would result in `\n` writing sole `0x0A` rather than `0x0D 0x0A` on Windows. See @FredFoo 's anwer. – loxaxs May 03 '17 at 09:21
  • This is the correct way to look at it because it deals with the root cause: original Term design and HEX values. – eco Jul 28 '21 at 23:46
8

Depends on the setting. \r\n is two bytes wide (in ASCII, UTF-8, etc.), but I/O libraries such C's stdio library and others, when operating in text mode, may translate between \n and \r\n quasi-transparently.

I.e., on a Windows platform, a C program reading a text-mode stream txt_in with

while ((c = getc(txt_in)) != EOF)
    printf("%02x\n", c);

will not report the ASCII code for \r. Conversely, putc('\n', txt_out) will actually write \r\n to the text-mode stream txt_out.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
5

Windows doesn't distinguish between \r\n and any other two characters. However, there is one situation where it is treated as one character: if you use the C runtime and open a file as text, \r\n in the file will be read as \n, and \n will be written to the file as \r\n.

ymett
  • 2,425
  • 14
  • 22
3

Yes, it's two characters: carriage return '\r' followed by linefeed '\n'.

Peter Ruderman
  • 12,241
  • 1
  • 36
  • 58
0

I conducted some tests (nothing fancy) with Notepad++ and Sublime Text 2 on Windows, and it turns out that \r and \n are indeed 2 distinct characters, but...

Different text editors might insert or they might not insert \r when Return key is pressed.

Try the following in your text editor:

1
2
3
4
5

Then press Ctrl+F, enable regular expressions and search for \r. Depending on your text editor and its settings, you might 'hit' or 'not hit' a character at the end of each line. If the above text is copy-pasted from another editor, behavior might also differ...

Some text editors allow for customizations in their settings, where you can specify if you prefer \r\n or unix-style \n to be inserted when you press Return. On top of that, they might allow you to choose to enforce a consistent style by stripping or inserting the \r character before saving a file.

Asotos
  • 995
  • 11
  • 14
  • 4
    You should not rely on a text editor, because some of them could be doing some magic while loading the file. The right way to verify would be to open the file in a hex editor. – Telemat Feb 09 '15 at 07:25