Difference between "endl" and "\n" [duplicate]
Asked Answered
F

1

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?

Freedman answered 22/12, 2010 at 18:55 Comment(3)
Possible duplicate: https://mcmap.net/q/63456/-quot-std-endl-quot-vs-quot-n-quotGlanders
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.Salaam
Use std::endl if this has any interaction with the user. But prefer '\n' if you are just building an offline file or something.Peag
M
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.

Mia answered 22/12, 2010 at 18:57 Comment(11)
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,Candlewick
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.Nonsectarian
[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]Flaw
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.Lamellar
@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.Meyerhof
@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.Candlewick
@BillyOneal: No. As far as C++ is concerned the underlying stream does not exist.Meyerhof
@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.Candlewick
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).Meyerhof
@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!Meyerhof
@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.Candlewick

© 2022 - 2024 — McMap. All rights reserved.