Why does "\n" give a new line on Windows?

Go To StackoverFlow.com

17

The line-break marker on Windows should be CR+LF whereas on Unix, it's just LF.

So when I use something like Console.Write("line1\nline2");, why would it work "properly" and give me two lines? I expect this \n not to work, and only a combo of \r\n would work.

2012-04-05 19:12
by user1032613
What are you using to view such a file - Oded 2012-04-05 19:14
Chances are C# is implicitly doing the conversion for you - Brad Christie 2012-04-05 19:14
I'm printing this line to the command-line console, using Console.Write() - user1032613 2012-04-05 19:15
\n has produced a newline on Windows since the debut of Visual Studio. I personally remember using it as far back as VS6 in the early/mid 90s - Escobar Ceaser 2012-04-05 19:16
I personally remember in VB6 on XP, using Chr(13) alone did not give me a new line. I had to use MsgBox("ln1" + Chr(13) + Chr(10) + "ln2"). - user1032613 2012-04-05 19:18
This is just the default behavior of the console handler, the one that's assigned to Console.Out by default. You'll run out of luck when you redirect the output of the console process to a file and open that file with an editor - Hans Passant 2012-04-05 19:26
@HansPassant Depends on your editor whether or not you are out of luck. My editor is fine. And thanks for pointing out that I was talking rubbish - David Heffernan 2012-04-05 19:35
'\n' means LINEFEED so that is the reason you get two lines. http://en.wikipedia.org/wiki/ASCI - H2ONaCl 2014-06-12 11:10
still in C (don't ask me why i'm mentioning it) \n in functions like fprintf are expanded to CR+LF or just LF, depending on the OS of the compiler - i.e. they are environment-independent. i wondered if the same thing happens in C# (most probably not) http://stackoverflow.com/questions/6891252/c-newline-character-under-windows-command-line-redirectio - hello_earth 2014-06-25 23:47


16

'\n' is the Line Feed character. Traditionally, it caused the printer to roll the paper up one line. '\r' is the Carriage Return character, which traditionally caused the printer head to move to the far left edge of the paper.

On printers and consoles that interpret the characters in this manner, the output of line1\nline2 would be

line1
     line2

Many consoles (and editors) will interpret '\n' to mean that you want to start a new line and position the cursor at the beginning of that new line. That is what you see here.

You should use Environment.NewLine rather than hard-coding any particular constants.

2012-04-05 19:15
by Eric J.
Console is a viewer I guess - NoName 2012-04-05 19:31
@OAOD: Sure, type myFileWithSlashNDelimiters will display the text, either as separate lines or one long line. Not sure which, but either way it's a viewer. I answered before his clarifying comment that he is using Console.Write() - Eric J. 2012-04-05 19:35
\n is the Line Feed character in the ASCII table, so of course, you get a new line. The unexpected thing is that the Console.Write method seems to also imply \r, which the asker is not supplying, and which is the Carriage Return - H2ONaCl 2014-06-29 10:44


9

This is just the standard behaviour of the underlying Windows console. A native C app will do exactly the same if you output 0x0A to the console.

Of course, you should be using Environment.NewLine for your new lines. Environment.NewLine resolves to \r\n on Windows and \n on Unix like systems.

2012-04-05 19:55
by David Heffernan


1

File encodings != Console interpretation.

In other words, while the "Windows Standard" of CR + LF exists for files, just the LF, or \n has resulted in the appropriate carriage return and new line interpretation in console windows.

2012-04-05 19:15
by Jesse C. Slicer


1

In my experience, when you output to the Console with WriteLine() it accepts the \n escape character. When you are using a StreamWriter and call WriteLine() it makes you type \r\n to move to a new line. I assume that the Console has been programmed to accept the \n escape character without the carriage return \r.

2012-04-05 19:23
by codeblooded


0

\n is the line feed character. On both *nix and Windows systems it should create 2 lines. The \r is the carriage return, it moves the writing instrument to the beginning of the line.

Most modern consoles/editors are resilient enough to interpret \n as \r\n

2012-04-05 19:23
by Colin D
Ads