Difference between "endl" and "\n"
22,488
Yes, they're different.
"\n"
is just a string of length 1 that gets appended to stdout.
std::endl
, instead, is an object that will cause to append the newline character ("\n"
) AND to flush stdout buffer. For this reason it will take more processing.
Author by
Nawaz
Following Rust and Haskell isocpp.org/wiki/faq Contact me on LinkedIn. Religion of C Correcting Grammar for Microsoft Products and Technology
Updated on December 23, 2020Comments
-
Nawaz over 3 years
Possible Duplicate:
C++: “std::endl” vs “\n”I'm wondering if there is any significant difference between these two ways to print newline :
cout << endl; //approach1 cout << "\n"; //approach2
Is there any practical difference?
-
Billy ONeal over 13 yearsActually, it can be different, but it doesn't have to be. Most consoles are line buffered which means they're going to get flushed on the newline whether or not you explicit flush on your own,
-
Johannes Schaub - litb over 13 yearsostream has its own buffer too, so the console being line buffered isn't the only factor here, I think. If ostream doesn't flush after it placed the '\n' into its buffer, the console won't ever see the newline.
-
watson1180 over 13 years[Nitpicking Mode On] '\n' is a char and "\n" is a string of length 1. Writing a char to the buffer could be faster in some cases. [Nitpicking Off]
-
Caleb Huitt - cjhuitt over 13 yearsIn my last job, an unnamed number of years ago, we had an occasion (writing a large text file) in which changing from endl for each line to "\n" for them made a very noticeable difference -- from a ~2s pause when saving down to no user-detectable pause when saving.
-
Lightness Races in Orbit about 13 years@BillyOneal: The console is not relevant. The iostream is flushed with
endl
and not with"\n"
, and that's all there is to it as far as C++ is concerned. -
Billy ONeal about 13 years@Tomalak: Not true. As far as C++ is concerned, the underlying stream is allowed to flush on every call for all it cares, including on newlines. (This isn't common unless
unitbuf
is set of course)endl
forces the flush, but just because you're using"\n"
instead ofendl
doesn't mean you've avoided flushing the buffer. -
Lightness Races in Orbit about 13 years@BillyOneal: No. As far as C++ is concerned the underlying stream does not exist.
-
Billy ONeal about 13 years@Tomalak: That doesn't make any sense. Of course the underlying stream exists, How else could one use
operator<<
on it? (Note: When I say "underlying stream" I mean the particular instance ofstd::ostream
-- i.e. a filestream or stringstream. Perhaps I was not clear?) My point is that the stream is allowed to flush whenever it wants. You can force it to flush, but you cannot prevent it from flushing. In most common implementations, for the console streams, a newline just happens to trigger a flush -- the C++ standard doesn't say this has to happen, but it doesn't disallow it either. -
Lightness Races in Orbit about 13 yearsI said "as far as C++ is concerned." ostream could be writing into a river of water for all the language cares. There is a separation between the language abstraction and any specifics of an underlying console stream; indeed, that's half the point of any programming language existing in the first place (along with making programming machines a little more human-friendly).
-
Lightness Races in Orbit about 13 years@Billy: >>(Note: When I say "underlying stream" I mean the particular instance of std::ostream -- i.e. a filestream or stringstream. Perhaps I was not clear?)<< Ah! Well that's different then. What strange terminology!
-
Billy ONeal about 13 years@Tomalak: Sorry -- I was thinking in terms of the actual
operator<<
functions, which are defined in terms of the genericostream
-- not the particular instance you're using.