Question Details

No question body available.

Tags

c powershell

Answers (1)

Accepted Answer Available
Accepted Answer
January 11, 2026 Score: 5 Rep: 72 Quality: High Completeness: 80%

This behavior is not caused by printf or the C runtime, but by how PowerShell and the Windows console host handle program termination and output.

1. Why the prompt appears on a new line

On Windows, the shell controls where the next prompt is printed, not the program.
If a program exits while the cursor is not at the start of a fresh line, PowerShell always moves the cursor to a new line before printing the next prompt.

This is intentional behavior to keep the prompt readable and is independent of whether your program printed \n or not.

So even though your program outputs:

hellohello

(with no newline), PowerShell ensures the next prompt starts on a new line.

This is shell behavior, not C behavior.


2. Why output.txt contains 0D 00 0A 00 (CRLF) and a BOM

This part is 100% PowerShell, not your program.

When you do:

./r.exe > output.txt

PowerShell:

  • Captures the program’s output as text

  • Writes it to the file using UTF-16 LE

  • Adds a BOM (FF FE)

  • Appends a CRLF (\r\n) at the end

That’s why Format-Hex shows:

FF FE ... 0D 00 0A 00

Your program never emitted those bytes — PowerShell added them during redirection.


3. How to verify your program does NOT print a newline

Use a raw redirection that bypasses PowerShell’s text processing, for example:

cmd /c r.exe > output.txt

or dump stdout from inside the program using a debugger.

You’ll see that:

  • No newline is printed by printf

  • The extra newline only appears due to PowerShell


4. Summary

  • printf does not auto-add \n

  • PowerShell prints the prompt on a new line by design

  • PowerShell redirection writes text as UTF-16 with CRLF

  • This behavior is Windows shell–specific, not a C runtime feature

Your C program is behaving exactly as specified by the C standard.