No debate, std::endl can be a disaster on some platforms due to flushing crap all the time.
Just because the box says something is flushable doesn’t mean you should flush it.
It’s a very C++ thing that the language developers saw the clusterfuck that is stream flushing on the kernel and decided that the right course of action was to create another fucking layer of hidden inconsistent flushing.
I hear C++ was greatly inspired by the fifth circle of hell.
programmers manage to do stupid shit in every language. i was wondering if there was a way to stop them, and golang comes close but maybe proves it can’t be done. idk!
Considering std::cout should only directly be used when you are too lazy to place breakpoints, I totally get the decision to auto-flush.
Simple.
\n
when you just want a newline.
endl
when you need to flush at the moment.Useful in case you are printing a debug output right before some function that might do bed stuff to buffers.
Edit: I wrote
println
instead ofendl
somehow. Guess I need more downtimeI only program in C. I was under the assumption that \n also flushes
I remember having to
fflush
a couple of times.It depends on whether you are printing to a terminal or to a file (and yes the terminal is also a file), and even then you can control the flushing behaviour using something like
unbuffer
printf
is superior and more concise, andsnprintf
is practically the only C string manipulation function that is not painful to use.Try to print a 32-bit unsigned int as hexadecimal number of exactly 8 digits, using
cout
. You can dostd::hex
andstd::setw(8)
andstd::setfill('0')
and don’t forget to usestd::dec
afterwards, or you can just, you know,printf("%08x")
like a sane person.Just don’t forget to use
-Werror=format
but that is the default option on many compilers today.C++23 now includes
std::print
which is exactly likeprintf
but better, so the whole argument is over.I went digging in cppref at the format library bc I thought c++20 or c++23 added something cool.
Found
std::print
and was about to reply to this comment to share it bc I thought it was interesting. Then I read the last sentence.Darn you and your predicting my every move /j
std::cout << "\nwhy not both" << std::endl;
I prefer \n for 0.001% better performance
I prefer \n for less typing.
same
I prefer endl for more typing because it lets me pretend to work more than I am
^ least deranged coder
Fuck endl, all my homies hate endl
Cout << “\n”; is dumb and you should feel bad
Feel this is relevant https://www.youtube.com/watch?v=IkpZggx4yNI
You’re right, that is incredibly dumb. Just not for the reasons you think it is. Imagine using iostream rather than stdio and unironically trying to clown on \n
Is this not a debate for freshman students and other assorted opinionated know-nothings? Or just people shitposting.
My mistake. You think it’s srs.
(And your opinion is still bad)
Oh no! Did I hurt your feelings by clapping back when you insulted me on a shitpost comment chain? Your lack of self awareness is astounding
🙄
bloods 4 lyfe
I am very sorry to remind everyone about the existence of Visual Basic, but it has:
- VbCrLf
- VbNewLine
- ControlChars.CrLf
- ControlChars.NewLine
- Environment.NewLine
- Chr(13) & Chr(10)
And I know what you’re asking: Yes, of course all of them have subtly different behavior, and some of them only work in VB.NET and not in classic VB or VBA.
The only thing you can rely on is that “\r\n” doesn’t work.
Apology not accepted, fuck you for reminding me!
great reminder to avoid microsoft products as much as i can
deleted by creator
\n is fun until you’re an a system that needs an additional \r
Bedeviled NXP/ARM SDK stdlib. Hate it, we need \n\r there. Why!!! What a PITA.
Unix needed only \n because it had complex drivers that could replace \n with whatever sequence of special characters the printer needed. Also, while carriage return is useful, they saw little use for line feed
On dos (which was intended for less powerful hardware than unix) you had to actually use the correct sequence which often but not always was \r\n (because teleprinters used that and because it’s the “most correct” one).
Now that teleprinters don’t exist, and complex drivers are not an issue for windows, and everyone prefers to have a single \n, windows still uses \r\n, for backward compatibility.
Kinda in Java, you can call System.out.println or you can call System.out.print and explicitly write the newline.
I haven’t looked at the code but I always assumed that
println
was a call toprint
with a new line added to the original input.
Something like this:void print(String text) { ... } void println(String text) { this.print(text + '\n'); }
That is pretty much what it does except it doesn’t hardcode
\n
but instead uses the proper line ending for the platform it’s running on.I haven’t worked with java for a couple of months now, currently working in Delphi, so could not remember the how else to do new line except backslash n on top of my head. :-)
C++ style text streams are bad and a dead-end design and
'\n'
.fprintf(stdout, "%c", '\012');
Wasn’t this {fmt} library merged into STL now? Does this solve this issue?
Anyways, there was also a constant that is the OS line ending without a flush, right?
As long as it doesn’t end in ;
print('string\n', end='')
I just learned that in Python, it’s fucking terrible. Python is a fucking mess and my next script will be in a different language.
As a python lover, I have to ask, what don’t you like about it and what languages do you generally prefer?
I prefer strongly typed languages. Using bytes isn’t intuitive.
Transforming certain data types into other data types is often not straightforward.
The identation is the worst though. Let me format the code however I want.
python is a bad joke that never ends
Perhaps TS is not a terrible language for shell scripts after all
Never tried it, but I will probably be more at home than python.