Windows CMD.exe inserting new line after program runs

1

Does CMD.exe automatically put a new line on the terminal window after a program has run? My program is outputing one line of text, via .NET Console.WriteLine("TEXT"); However, I'm also seeing a blank line when when I run it in CMD.exe. Why is that?

It would look like this:

TEXT

C:\>

Notice the blank line in the above output. My program only writes 1 line though.

Ryan M

Posted 2016-01-29T23:21:23.170

Reputation: 113

1I think so. It separates it from the text that shows about Microsoft when you open cmd. They might of put it before C :> rather than after their copyright info. – ss4566654768 – 2016-01-29T23:33:09.457

Answers

4

The CMD prompt such as C:\> is always displayed at the beginning of a line. In order for CMD to be able to guarantee that, it must insert a newline after a console program ends, since the program itself might have left a line not terminated with a newline at the end.

Hypothetically, if CMD did not automatically insert a newline after the program ended, and if your code used Console.Write("TEXT") instead of WriteLine, then the prompt would have ended up being displayed in the middle of the line like TEXTC:\>.

dxiv

Posted 2016-01-29T23:21:23.170

Reputation: 1 784

Just a small correction - it inserts 2 newlines. With a bit of trickery you can suppress 1 of them, try <nul (set/p _any_variable=string to emit) for example and you will see the blank line is not emitted. – DavidPostill – 2016-01-30T11:10:35.280

@DavidPostill As far as I can tell, CMD itself inserts just 1 newline. Most programs end the output with a newline of their own, which makes an extra empty line appear, but that newline is written by the program itself, not CMD. For example, running a console app which does just a WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"test", ...) with no terminating newline will display test on one line then the prompt will appear on the very next line. – dxiv – 2016-01-30T18:36:36.900

OK, fair enough ... – DavidPostill – 2016-01-30T18:38:10.730