- cross-posted to:
- programmer_humor@programming.dev
- cross-posted to:
- programmer_humor@programming.dev
They aren’t the same thing so the comparison is weird.
endl
has aflush
which is important when doing something like embedded work or RTOS development. If i was doing multiple lines they all were\n
until the last line when i actually want to push the buffer.Obviously depending on the tuning of the compiler’s optimization multiple flushes could be reduced but the goal should always be to write as optimal as possible.
Endl is faster to type
\n, because I ordered a newline, not a flush.
Well, Java has System.lineSeparator so, maybe no?
#define endl “\n”
Environment.NewLine might exist in C#
It might do. I encountered it last week as I needed it for a powershell script. So it exists in that at least
vbCrLf
If I’m writing C++, I’m usually optimizing for portability over performance, in which case I would prefer std::endl as it would yield the best results regardless of platform; it also keeps the end-of-line character out of other strings, making code just a little cleaner.
\n is for when I’m done pretending that anything that isn’t Unix-like is OK, and I’m counting the cycles of every branch instruction.
std::endl provides zero portability benefits. C++ does have a portable newline abstraction, but it is called
\n
, not endl.Thank you two for demonstrating the image in the post so well.
No, there’s no guarantee that in every context \n is translated portably.
The same is true of std::endl. std::endl is simply defined as
<< '\n' << std::flush
; nothing more, nothing less. In all cases where endl gives you a “properly translated” newline, so does\n
.Yeah it’s an artificial dichotomy based on a popular misconception of what std::endl is and how \n is interpreted.
Ultimately it does not ask about line endings, but about flushing, which is a completely orthogonal question.
Ahhh, I see. Looks like the magic happens somewhere further down in iostream.
It’s controlled by whether the stream’s opened in text mode or binary mode. On Unix, they’re the same, but on Windows, text mode has line ending conversion.
os.linesep
Lol jk none of my stuff runs on Windows anyway
Rebel side
\0
What the heck is endl???
std::endl
is used in output streams in C++ to end the line, using the os specific line termination sequence, and flush the buffer.The later one is a performance issue in many cases, why the use of
"\n"
is considered preferredDon’t most terminals flush the buffer on newline anyway?
It is the stream itself that is buffered, so the terminal does not handle the contents until the stream is flushed.
Maybe, but there is the internal buffer. Also, most I/O happens in files not consoles
Instead of this:
cout << "Hello world.\n";
You can do this:
cout << "Hello world." << endl;
something has replaced your lessthan signs with their HTML counterparts. such a silly thing to do in a code block
I think that’s client side. Doesn’t happen for me.
same here. AP isn’t standardized enough, apparently
Boy am I glad I don’t do C++ anymore. That string handling with the overloaded bitshift operator was wild.
Ah, so you’re a println! kinda guy?
From memory it’s a way to declare a line ending after your string.
God bless your soul.
\r\n gang
Get out
Yeah
\r
gang4lyfe
Environment.NewLine()
/* I'm new to this language so just imagine there is a new line here when it prints: */
I’m on side
\PHP_EOL
\r\n