"\n" or endl;

Hello, I am new to C++, and have seen some tutorials use "\n" to start a new line and some use endl. In general; which one should I use?
It really doesn't matter. endl clears the buffer in the cout object each time it is called (I think?), though this probably won't matter to you.
"\n" outputs a new line.
endl outputs a new line then flushes the output.

1
2
3
4
5
// this...
cout << endl;

// is the same as this:
cout << "\n" << flush;


flushing the output ensures that whatever your output actually becomes visible on screen. If you don't flush it's possible that the data will remain buffered.

Flushing has slightly more overhead.


All that said....

Unless you are really worried about performance and/or you're doing output between time consuming operations, it doesn't really matter which you use.

EDIT: ninja'd >=o!
Last edited on
Personally I just think "\n" looks so ugly and sloppy, endl just looks more professional to me. Also doesn't hurt that it clears the buffer, but as the other 2 replies said it doesn't really matter if you're just learning C++ and want to output a new line.
Last edited on
Well, if you're worried about performance, you shouldn't be doing any I/O.
I thought endl was also a platform-independent way of breaking a new line. For example: char 10 (LF) is the only one used in Linux, while a new line in Windows is composed by CR + LF (chars 13 and 10). It is my understanding that endl will provide the appropriate ones depending on the OS.

Or am I 100% mistaken? I don't want to test: I hate creating tiny projects in my HDD, which leaves me with no Windows machine to test on, and I don't have a Linux one, soooo.... please reply? hehe.
'\n' will work everywhere. When sending formatted output, the runtime will convert newlines to the system default. I think if you're putting text in a GUI element directly through the WinAPI, you do need to use "\r\n". GUI toolkits make the translation automatically.
Getting slightley off topic, don't dismiss what helios said about not doing any I/O operations if you want the best performance. He isn't just some purist trying to save a few Hertz here and there, check this out: http://en.wikipedia.org/wiki/CSRSS
(NOTE: In this article the phrase "system call" does not refer to using the "system(...)" function. It refers to a special type of interaction between the process and the kernal.)

If you want to verify this then just make an endless loop that outputs a single character to the screen. Watch how much of the CPU time you process takes up compared to the CSRSS service, I was shocked when I first realized this.
not doing any I/O operations

what if we have to?My research field is video processing
I/O operations is inevitable for me
Thanks
closed account (zb0S216C)
If input takes too long, the CPU will fire an interrupt, allowing another process to continue. It's true that I/O is slow, that's why the South Bridge handles it. However, with the new Quick-Path Interconnect, and serialized buses, I/O performance is getting faster. Still, I/O devices have a higher priority over the CPU ( thanks to bus arbiters ) because they cannot be stopped; not physically, but because it would result in data loss.

Wazzak
Last edited on
what if we have to?My research field is video processing
I/O operations is inevitable for me
My statement applies to superfluous I/O. Specially console I/O.
Topic archived. No new replies allowed.