10

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?

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 3
    Possible duplicate: http://stackoverflow.com/questions/213907/c-stdendl-vs-n – Bill Cheatham Dec 22 '10 at 18:57
  • There's rarely any practical difference. Except that `endl` will flush the stream. Unless you absolutely need to flush the stream you can use either of them. – Daniel Lidström Dec 22 '10 at 19:03
  • 4
    Use std::endl if this has any interaction with the user. But prefer '\n' if you are just building an offline file or something. – Martin York Dec 22 '10 at 19:07

1 Answers1

24

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.

peoro
  • 25,562
  • 20
  • 98
  • 150
  • 1
    Actually, 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, – Billy ONeal Dec 22 '10 at 19:04
  • 3
    ostream 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. – Johannes Schaub - litb Dec 22 '10 at 19:21
  • 1
    [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] – watson1180 Dec 22 '10 at 19:45
  • 3
    In 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. – Caleb Huitt - cjhuitt Dec 22 '10 at 20:47
  • @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. – Lightness Races in Orbit Feb 10 '11 at 13:07
  • @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 of `endl` doesn't mean you've avoided flushing the buffer. – Billy ONeal Feb 10 '11 at 16:04
  • 1
    @BillyOneal: No. As far as C++ is concerned the underlying stream does not exist. – Lightness Races in Orbit Feb 11 '11 at 09:55
  • @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 of `std::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. – Billy ONeal Feb 11 '11 at 14:53
  • I 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 Feb 14 '11 at 10:20
  • @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! – Lightness Races in Orbit Feb 14 '11 at 10:21
  • @Tomalak: Sorry -- I was thinking in terms of the actual `operator<<` functions, which are defined in terms of the generic `ostream` -- not the particular instance you're using. – Billy ONeal Feb 14 '11 at 18:57