Difference between \n and end1

Im confused about the differences between placing a \n in a string (or char) literal to be output and inserting an endl into the stream. Someone told me that there are two differences — one syntactic and one temporal relative to insertion, but I am still unsure. Halp!


closed account (E0p9LyTq)
endl flushes the buffer, where \n does not. Otherwise, they are the same.

endl is good, because if the program crashes, your stuff gets printed. BUT, there is a performance hit with endl.


http://stackoverflow.com/questions/4026394/c-difference-between-endl-and-n
Note: It's "endl" with an L and not "end1" with the number one.

Putting '\n' in a string inserts a line break.
Outputting 'endl' to a stream inserts a line break and flushes the stream.

1
2
3
4
// these 2 lines have the same effect:
cout << endl;

cout << '\n' << flush;



Output streams are buffered, meaning when you output something, it might not be actually output immediately, but instead might be stored internally until the buffer is emptied (flushed). Doing a flush makes sure that everything you have sent to cout gets flushed and thus ensures that it will be visible on screen.

Flushing is computationally more expensive, so code that does a whole lot of unnecessary endls might be slower than if it just used line breaks. But it would have to do a WHOLE LOT for it to really make a noticable difference in speed.
Ok. What about the timing relative to insertion? Would endl output slower than the \n because it is buffering? Why use endl over \n?
Last edited on
no its not, output will be buffered when using cout or its kind
endl slower because its put a newline then flushing, for example cout<<endl<<endl<<endl it'll flush 3 times that's why its slower than cout<<'\n'<<'\n'<<endl

About how buffer work and why use endl nstead of \n Disch already explain it
Hmm ok. Thanks for the explanation. Sry if my questions sound kinda dumb/weird. I'm very new to programming.
I also questioning the same thing when i started programing lol,
Note that reason "if the program crashes, your stuff gets printed" is bad. You should use endl because you need it, not because you afraid that your program might crash. Valid reason to flushing buffer manually is that you expect no further output been done in some time (for example a lengthy computation is ahead) and you want user to see your text before that wait time.

If you want to see output for debugging purposes, there are several solutions:
1) Preferable, but hard for some people: learn to use the debugger. You will never need the debug output again.
2) If you want to stick some output lines to see variable value and will remove those later, use std::cerr instead of std::cout: std::cerr is unbuffered (everything is printed immideately), and later you could locate and remove debug output easier (by using search).
3) If you want your persistent ouput routines to give you information before crash, turn off std::cout buffering in the beginning of your program. You can easily turn it off later when you will fid and fix your bug.
Good information.
Topic archived. No new replies allowed.