- 1 year ago
What newline to use when writing to stdout on windows
When writing to stdout (standard output) on Windows, you typically use the newline sequence \r\n
(carriage return + line feed) to represent a new line. This is the standard newline convention in Windows.
Here's an example of writing to stdout with the newline sequence in a batch script:
batch@echo off
echo Line 1
echo Line 2
In this example, each echo
statement represents a new line in the output. The newline sequence \r\n
is automatically added by the echo
command when writing to stdout.
Similarly, when writing to stdout in other programming languages, such as C++, you can use std::endl
or \r\n
to insert a newline.
For example, in C++:
cpp#include <iostream>
int main()
{
std::cout << "Line 1" << std::endl;
std::cout << "Line 2" << std::endl;
return 0;
}
In this C++ example, the std::endl
manipulator or \r\n
is used to output a newline after each line.
By using the appropriate newline sequence (\r\n
) when writing to stdout on Windows, you ensure that the output is displayed correctly with the expected line breaks in Windows-based environments.