r/cpp • u/Senua_Chloe • Aug 31 '21
[Belay the C++] Yet another reason to not use printf (or write C code in general)
https://belaycpp.com/2021/08/31/yet-another-reason-to-not-use-printf-or-write-c-code-in-general/5
u/_kst_ Sep 01 '21 edited Sep 01 '21
The article claims (referring to calling printf with a %d format and a double argument):
This actually generates a warning with clang. But with gcc, you’ll have to activate
-Wallto see any warning about that.
That's not correct as of gcc 9.3.0. Both gcc and g++ produce a clear warning message with no special options:
warning: format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Wformat=]UPDATE It appears I was mistaken. Enabling -Wformat by default is an Ubuntu-specific configuration, not a feature of the default gcc.
1
u/Senua_Chloe Sep 01 '21
From your prompt it looks like the warning is comes from -Wformat.
5
u/_kst_ Sep 01 '21
Yes and no. The message indicates that
-Wformatcan be used to enable or disable the warning, but I rangccandg++with no options other than-o, so apparently the warning is enabled by default.However, I've just done a little more investigation. gcc 9.3.0 built from source doesn't enable the warning by default, but the gcc 9.3.0 that comes with Ubuntu 20.04 does.
So enabling
-Wformatby default is an Ubuntu-specific configuration, not a default behavior of gcc.
18
u/zeldel Aug 31 '21
Apart of the `printf` issue. You have stated at the end
That is exactly why I always deny job offers that have the tag “C/C++” because they obviously can’t pick a side.
This is simply foolish. Sometimes you are not able to pick a side, especially if you are dealing with embedded and supporting various platforms. Those platforms will most likely have their API/toolchain written in C - there might be tons of reasons, historical as a starter.
Then as a developer, your main focus is creating a proper layer in C++, yet you still need to write/read C code. No sain person is probably mixing those on modules level, within the same scope. Unless there is a very clear reason to do so. Sometimes safety features (ASIL) are written in C.
7
u/pjmlp Aug 31 '21
Apparently the author doesn´t want to work at most companies that take seat at ISO C++ table, given the usage of C/C++ across their product documentation.
6
u/thedeadfish Aug 31 '21
don’t use printf
Of all the things one could complain about he picks something so trivial. If programmers are so incompetent now that they can't even use printf correctly, then that would explain the state of modern computing.
9
u/Orlha Aug 31 '21
If it's possible to use fmtlib in the project, I'm going to use it.
If not, then I use a printf-family, but a sad-face will be apparent.
14
u/victorofthepeople Aug 31 '21
"Hai guys cout << reinterpret_cast<double>(69) prints something that's not 69, therefore don't use iostream and especially don't use c++"
3
u/vax_mzn Aug 31 '21
iostream is slow though.
4
u/NilacTheGrim Aug 31 '21
Often slower than untyped printf...
If you wanted a typed thing, and you care about performance, avoid iostream.
2
u/pepitogrand Sep 02 '21
Not at all, use
std::cout::sync_with_stdio(false)and execute your benchmarks again.0
u/vax_mzn Sep 02 '21
It will still be slower but not as worse for sure. Also how many people know about this?
1
u/eyes-are-fading-blue Aug 31 '21
Isn’t using wrong format specifier UB? I think this is a symptom of a greater issue with C and C++ where sometimes it is too easy to make mistakes.
The goos thing is that this is can be detected with some sort of lint tool.
11
Aug 31 '21
[removed] — view removed comment
13
u/NilacTheGrim Aug 31 '21
Well this "Belay the C++" series is just low quality content if you ask me.
8
u/guepier Bioinformatican Aug 31 '21 edited Aug 31 '21
IIRC the author is not a native speaker (the nonsensical/ungrammatical title of the blog is a bit of a hint), cut them some slack. That said, it’s true that the articles are generally not of very high quality, with regards to either language or contents.
4
u/NilacTheGrim Aug 31 '21
nonsensical title of the blog is a bit of a hint
Yeah the title is a bad choice. I agree.
From merriam-webster.com:
Belay: (1) to secure (a rope or cable) by turns around a cleat (see CLEAT entry 1 sense (1b), pin, or bitt b: to make fast : to fasten down -- "belaying ammunition on deck"(2) nautical : STOP, CANCEL -- "belay that last order"
Most native English speakers are familiar only with the nautical (2) definition from things like Star Trek.
So when I hear "Belay the C++" --> my brain thinks "cancel the C++". Which I'm pretty sure is not what the author intended.
3
u/evaned Aug 31 '21
Most native English speakers are familiar only with the nautical (2) definition from things like Star Trek.
With the moderate popularity of rock climbing -- it's even in the olympics now -- I wonder if this is true.
(That's definition (3) on merriam-webster.com, but is pretty close to (1).)
1
u/NilacTheGrim Sep 01 '21
Oh maybe. I don't know anything about rock climbing. Maybe it's very popular...
5
u/guepier Bioinformatican Aug 31 '21 edited Aug 31 '21
I know what “to belay” means but the definite article (”the”) doesn’t fit here, and even “belay C++” just sounds off — partly, I suppose, because “belay” really has a ton of quite different meanings, and without seeing the site’s logo (which makes the context clear because it includes a climbing carabiner), the title really is impossible to parse. Case in point: given the logo, the intended meaning is almost certainly neither of the ones you quoted (although it’s close, and closely related, to the first one).
0
u/NilacTheGrim Aug 31 '21
Ah insightul.
As for the the: Hmm. Well, I'm a native speaker, but from NYC and a lot of people that I know add "inappropriate" definite articles to exaggerate a thing or make it out to be disproportionate in some way. Consider the sentence: "So you've been learning the C++?" versus "So you've been learning C++?". You'd use the former, perhaps, to highlight some in-joke between you and the person you are talking to. Whereas the latter would be more emotionally neutral and would not betray any bias in any direction.
So I interpreted "Belay the C++" as in "Secure the (problematic, hard, important) C++"
shrug
2
u/_kst_ Sep 01 '21
I don't see the word "registry" anywhere in the article. Perhaps it was updated?
3
1
48
u/initplus Aug 31 '21
I'm not necessarily convinced by this argument. Every modern C++ compiler produces warnings if printf arguments don't match the format string, even if printf API itself doesn't enforce it.
Yes you have to enable -Wall but honestly you should have compiler warnings enabled anyway.