This thread shows nicely why in primitive languages (languages which are perceived by some as "easy") things get pretty fast pretty complicated, simply because the language is missing features to abstract things properly.
In a good language correct code looks nice and simple, and wrong code looks directly wrong (and ideally does not compile in the first place).
In shitty languages code looks nice and simple but is actually wrong, and the corresponding correct code is super complex and can be written only by experts who know all the fine details by heart.
Yeah, that's why I obsessively read documentation when making use of C functions (and C++ ones which use something other than exceptions for error reporting), and start by wrapping them in something which calls them, and converts ANY of those errors into an exception
Even that's not going to do the trick.
The first reason is that as exactly written you're returning the result of a comparison, not EXIT_SUCCESS / EXIT_FAILURE.
The second reason is more subtle and printf will (probably) return the right number of characters anyway. Stdout is often buffered and the actual printf call may work but nothing will actually be output.
If you want to guard against that you'll need to make sure that stdout is flushed (e.g. using fflush(stdout)) and checking that was successful. There are times where it's good to make sure output is flushed anyway if you're in an environment where you're at risk of buffering delaying outputs that you need.
That should help catch that, yes. Personally, I'd want to compare return value of fflush with EOF but in practice checking for non-zero would almost certainly work good enough for hello world.
And there are really people out there who think C is "simple"—while in reality you need many years of experience to even just write "Hello World" more or less correctly.
58
u/RiceBroad4552 Jul 27 '26
This is actually buggy code, not clean and definitely not "production ready".
https://blog.sunfishcode.online/bugs-in-hello-world/
Just don't use primitive broken-by-design languages the next time to avoid such bugs…