this post was submitted on 05 Apr 2025
146 points (95.6% liked)

Programmer Humor

34832 readers
484 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
top 50 comments
sorted by: hot top controversial new old
[–] [email protected] 15 points 11 hours ago

They aren't the same thing so the comparison is weird.

endl has a flush 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.

[–] [email protected] 15 points 15 hours ago

\n, because I ordered a newline, not a flush.

[–] [email protected] 2 points 11 hours ago

Endl is faster to type

[–] [email protected] 4 points 14 hours ago

Well, Java has System.lineSeparator so, maybe no?

[–] [email protected] 15 points 21 hours ago

#define endl "\n"

[–] [email protected] 2 points 17 hours ago (2 children)

Environment.NewLine might exist in C#

[–] [email protected] 1 points 4 hours ago

Microsoft really creating the problem and then forcing you to use their solution.

[–] [email protected] 2 points 16 hours ago

It might do. I encountered it last week as I needed it for a powershell script. So it exists in that at least

[–] [email protected] 44 points 1 day ago* (last edited 1 day ago) (1 children)

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, or I'm counting the cycles of every branch instruction.

[–] [email protected] 30 points 1 day ago (2 children)

std::endl provides zero portability benefits. C++ does have a portable newline abstraction, but it is called \n, not endl.

[–] [email protected] 45 points 1 day ago

Thank you two for demonstrating the image in the post so well.

[–] [email protected] 6 points 1 day ago (1 children)

No, there's no guarantee that in every context \n is translated portably.

[–] [email protected] 14 points 20 hours ago (2 children)

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.

[–] [email protected] 4 points 19 hours ago (1 children)

Ahhh, I see. Looks like the magic happens somewhere further down in iostream.

[–] [email protected] 3 points 15 hours ago

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.

[–] [email protected] 1 points 16 hours ago

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.

[–] [email protected] 12 points 1 day ago

os.linesep

Lol jk none of my stuff runs on Windows anyway

[–] [email protected] 2 points 19 hours ago
[–] [email protected] 17 points 1 day ago (5 children)
[–] [email protected] 38 points 1 day ago (3 children)

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 preferred

load more comments (3 replies)
[–] [email protected] 9 points 1 day ago* (last edited 1 day ago) (5 children)

Instead of this:

cout << "Hello world.\n";

You can do this:

cout << "Hello world." << endl;
[–] [email protected] 3 points 5 hours ago

The fact that you used the namespace for cout but not for endl inordinately bothers me

load more comments (4 replies)
[–] [email protected] 7 points 1 day ago (2 children)
[–] [email protected] 2 points 8 hours ago

Alternatively:

https://en.cppreference.com/w/cpp/io/manip/endl

p.s. The site isn't entirely mobile friendly

(I'm a cppref lover tbh)

[–] [email protected] 10 points 1 day ago (1 children)

Boy am I glad I don’t do C++ anymore. That string handling with the overloaded bitshift operator was wild.

[–] [email protected] 1 points 17 hours ago (1 children)

Ah, so you're a println! kinda guy?

[–] [email protected] 2 points 9 hours ago

🦀 🦀🦀🦀🦀🦀🦀🦀

[–] [email protected] 3 points 1 day ago

From memory it's a way to declare a line ending after your string.

[–] [email protected] 2 points 1 day ago

God bless your soul.

[–] [email protected] 11 points 1 day ago (1 children)
[–] [email protected] 8 points 1 day ago
/* I'm new to this language so just imagine there is a new line here when it prints: */
[–] [email protected] 10 points 1 day ago (1 children)
[–] [email protected] 20 points 1 day ago (1 children)
[–] [email protected] 3 points 1 day ago

Yeah \r gang4lyfe

[–] [email protected] 7 points 1 day ago

I'm on side \PHP_EOL

[–] [email protected] 4 points 1 day ago

Rebel side \0

[–] [email protected] 5 points 1 day ago
[–] [email protected] 5 points 1 day ago
load more comments
view more: next ›