Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Windows carriage return \r\n consist of two characters or one character?

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

like image 788
Oh Chin Boon Avatar asked Aug 10 '11 15:08

Oh Chin Boon


People also ask

How many characters is a carriage return?

In ASCII and Unicode, the carriage return is defined as 13 (or hexadecimal 0D); it may also be seen as control+M or ^M.

What does \r and \n mean?

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

Is \r the same as \n?

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.

What's the difference between \N and R N?

\r\n is the standard line-termination for text formats on the Internet. \r\n is only used on Windows Notepad, the DOS command line, most of the Windows API, and in some (older) Windows apps. \n: UNIX systems use \n as their end of line character.


5 Answers

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.

like image 191
Alexey Ivanov Avatar answered Oct 19 '22 08:10

Alexey Ivanov


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
like image 41
Tomas Avatar answered Oct 19 '22 10:10

Tomas


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.

like image 21
Fred Foo Avatar answered Oct 19 '22 09:10

Fred Foo


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.

like image 45
ymett Avatar answered Oct 19 '22 09:10

ymett


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

like image 3
Peter Ruderman Avatar answered Oct 19 '22 08:10

Peter Ruderman