Is there a reason to use endl with cout when I can just use \n? My C++ book says to use endl, but I don’t see why. Is \n not supported as widely as endl, or am I missing something?
Is there a reason to use endl with cout when I can just use
Share
endlappends'\n'to the stream and callsflush()on the stream. Sois equivalent to
A stream may use an internal buffer which gets actually streamed when the stream is flushed. In case of
coutyou may not notice the difference since it’s somehow synchronized (tied) withcin, but for an arbitrary stream, such as file stream, you’ll notice a difference in a multithreaded program, for example.Here‘s an interesting discussion on why flushing may be necessary.