1

Why some examples are using "endl" instead of "\n"?

I heard that processing time of "endl" is longer than "\n" because "endl" has some flushing process.

But, there are so many examples using "endl" on the internet.

I wanna know why they're using "endl" which thought to be inefficient.

Sorry for the awkward English...... Thanks

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
hi-jin
  • 13
  • 2
  • 2
    Using `std::endl` may be interested while debugging!. To see something written on the screen, before the programme clash – Damien Dec 11 '20 at 10:47
  • [Source](http://www.cplusplus.com/reference/ostream/endl/) It also calls flush() after sending \n – SelfishCrawler Dec 11 '20 at 10:48
  • 1
    Because 99.9% of the time, you want the flushing and the performance is satisfactory with it. A couple of years ago, this was the fashionable micro-optimization, even among people who consider premature optimization to be the root of all evil. – molbdnilo Dec 11 '20 at 10:48
  • 1
    It's also more verbose and possibly easier to understand for newbies? `endl` as the shortcut for *end line* is more obvious than some magic symbol `'\n'`. And of course you won't even notice any difference in toy programs with 20 lines of code.. – Yksisarvinen Dec 11 '20 at 10:51
  • `std::endl` flushes the output stream. If you use `\n` it will just get buffered, and you're may not to see new lines on the screen. The only problem with `std::endl` is performance. – jignatius Dec 11 '20 at 10:54
  • https://stackoverflow.com/questions/213907/stdendl-vs-n – WhozCraig Dec 11 '20 at 10:57
  • consider that there are lots of crappy tutorials online. Many of them have a `std::endl` in a "Hello World" example without properly explaining it. Some beginners are thaught `std::endl` from day one without actually knowing the difference to `'\n'`. Why authors of such online tutorials do that ony they can know ;) – 463035818_is_not_an_ai Dec 11 '20 at 10:59

2 Answers2

2

Oftentimes, direct usage of std::cout is something not meant for production code, where it's usually preferred to use some kind of logging facility (for writing to a file instead of standard stream etc.). Within such logging library, you would want to take care you only flush when desired, but for some ad hoc, debugging-like output of stuff, the performance implications of std::endl don't really matter and it's more important to immediately see the desired output.

lubgr
  • 37,368
  • 3
  • 66
  • 117
  • "it's more important to immediately see the desired output" - and to make sure that all output is printed before the pram crashes. – mrks Dec 11 '20 at 11:00
2
  1. Many think that << std::endl is different from << '\n' << std::flush, as it would insert the right end-line markers.
    That is obviously nonsense.

  2. Others do not understand that std::cin and std::cout are tied together, thus the latter is flushed when the former wants to read.

  3. Still others have never heard of std::cerr, and thus misdirect logging to std::cout.
    If you use the wrong tool, bludgeoning it into behaving as you want instead of taking a step back and recognizing you want a hammer and not a screwdriver is distressingly common.

  4. If you really want, maybe because your program is especially flaky and you are programming by trial and error (which makes it unlikely your code will actually be correct, instead of just seem to "work" at the end), you can easily make std::cout unbuffered too.
    No need to get into even more bad habits.

     std::cout << std::unitbuf;
    
Deduplicator
  • 44,692
  • 7
  • 66
  • 118