"std::endl" vs "\n"
c++, c++-faq, iostream
Solution
The varying line-ending characters don't matter, assuming the file is open in text mode, which is what you get unless you ask for binary. The compiled program will write out the correct thing for the system compiled for.
The only difference is that `std::endl` flushes the output buffer, and `'\n'` doesn't. If you don't want the buffer flushed frequently, use `'\n'`. If you do (for example, if you want to get all the output, and the program is unstable), use `std::endl`.
Problem
Many C++ books contain example code like this... ``` std::cout << "Test line" << std::endl; ``` ...so I've always done that too. But I've seen a lot of code from working developers like this instead: ``` std::cout << "Test line\n"; ``` Is there a technical reason to prefer one over the other, or is it just a matter of coding style?
Related problems
- "\n" or '\n' or std::endl to std::cout?
- What's the problem with "using namespace std;"?
- mixing cout and printf for faster output
- Using scanf() in C++ programs is faster than using cin?
- Does the C++ standard mandate poor performance for iostreams, or am I just dealing with a poor implementation?
- Does printf always flush the buffer on encountering a newline?
- How can I print a newline without flushing the buffer?