4

I've read the difference between std::endl and '\n' is that std::endl flushes the buffer and '\n' doesn't. However, as far as I know stdout on linux is line-buffered anyway, so does it mean that std::cout << ... << std::endl is the same as std::cout << ... << '\n'?

kadina
  • 5,042
  • 4
  • 42
  • 83
eri412
  • 57
  • 1
  • 3
  • 3
    _However, as far as I know stdout on linux is line-buffered anyway_ Only if `stdout` is writing to a device. If it's redirected to a file, then it's block-buffered. – Paul Sanders Oct 07 '20 at 23:55
  • and the buffering mode can be changed easily with [setvbuf()](https://en.cppreference.com/w/c/io/setvbuf) – phuclv Oct 08 '20 at 00:09
  • 4
    There is no need to say "basically". You are not presenting a summary of the difference; that *is* the difference. You can check [the documentation](https://en.cppreference.com/w/cpp/io/manip/endl); the cppreference documentaion starts by stating that `std::cout << std::endl` is the same as `std::cout << '\n';` followed by `std::cout.flush();`. – JaMiT Oct 08 '20 at 00:09

2 Answers2

0
std::ostream os;

os << std::endl; // more concise

os << '\n' << std::flush; // more explicit about flushing

Those two lines have the exact same effect.

The manual flushing is often a waste of time:

  • If the output stream is line-buffered, which should be the case for std::cout if it connects to an interactive terminal and the implementation can detect that, then printing \n already flushes.

  • If the output stream is paired with an input stream you read from directly afterwards (std::cout and std::cin are paired), then reading already flushes.

  • If you nobody waits for the data to arrive at its destination, flushing is again just going to waste.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
-2

This std::cout << ... << std::endl and this std::cout << ... << '\n' are not exactly the same. In the last one, the newline \n is streamed to the output. In most cases this will be interpreted by a console as a newline. But std::endl means (as you already mentioned) exactly flush and newline in the output console.

Assume you would write this in a document. In Linux the \n is sufficient. But Windows expects \r\n. So the new lines will not occur using \n and e.g. Notepad++ in Windows.

UweJ
  • 447
  • 3
  • 10
  • `os << std::endl;` is exactly the same as `os << '\n' << std::flush;`. It doesn't output some magical newline, but also \n. Whether the output stream is in text mode, and on a system that translates \n to \r\n or not is a different affair. – Deduplicator Oct 08 '20 at 00:21
  • 1
    "But Windows expects `\r\n`." -- I believe the console uses text mode by default, which means that on Windows, programs can ignore the fact that Windows internally uses `\r\n` as line breaks, so that programs can simply use `\n`. This is translated automatically to `\r\n` when appropriate (and vice versa). – Andreas Wenzel Oct 08 '20 at 00:22
  • So if I 1)use linux; 2) print to stdout(no redirection); 3)haven't changed buffering mode, they are equal? – eri412 Oct 08 '20 at 00:23
  • @Deduplicator Maybe I expressed it in a missleading way. My intention is to state, that `std::cout << ... << std::endl` is not the same as `std::cout << ... << "\n"`. As you already mentioned, there is no magical newline. I am not sure if it will always be `\n` or `\r\n` in Windows. So I wanted to state that the user can trust on it, that `std::endl` will create a newline and flush the output -tream in the output console of the operating system. – UweJ Oct 08 '20 at 09:51
  • `'\n'` tells the steam to do whatever is necessary to produce a new line. That hides system dependencies: it doesn't matter what the convention is on the OS that the code has been compiled for; the stream will get it right. – Pete Becker Oct 08 '20 at 13:53
  • @PeteBecker So it is a runtime-thing how the "\n" is interpreted and not a decision made at compiletime? – UweJ Oct 11 '20 at 10:50
  • The runtimes library handles `'\n'`; when you build an application you link to the runtime library. So in a sense, it's a compile-time decision, because that's when the application gets linked; in another sense, its a runtime decision, because the character `'\n'` just gets passed to the stream code, and the stream code does whatever is appropriate. – Pete Becker Oct 11 '20 at 15:49