I have to work with folks who confuse size_t with int, and as a result they have overflow bugs depending on whether O_LARGEFILE is in use. So yes, I care. Not because printf format strings are terribly important, but because understanding and adhering to the type system is essential to write bug-free software.
If they get overflow errors then your build process does not have the right warnings enabled/logged.
Suggestion: enable -Wall and treat warnings as errors.
Of course. You and I know they're doing it wrong, however sirclueless suggested above that type correctness is "arcane" and asks if "we would really care" when it comes to type issues in printf format strings.
Handily, gcc has -Werror, which turns all warnings into errors. So -Wall -Wextra -Werror should catch most things.
Except if you're using gcc (mingw) on Windows:
gcc -v shows that it's been configured with --disable-werror
If you're being sensible and using -Wall with gcc, it will pick up the %zu thing anyway:
sizeof-test.c: In function ‘main’: sizeof-test.c:5:3: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’
Compilers have warnings so that you don't have to remember archaic bits of C. :)
Learning arcane trivia is a side effect of learning the necessary stuff.
Re:Deep C
by numeromancer on Monday October 10, 2011 @03:23PM
(#3095350)
Yes, I would care. Someone who didn't know might notice the sizeof() and note that it returns size_t, but not know what to do about it, and end up saying "screw it" and just using "%d", which is not portable, and the program might crash when compiled for a different system. It might even run on his sytem today, but not tomorow, or run in the debug image but fail in production. I've seen this and similar things happen many-a-time.
Pet peeve alert: sizeof is not a function. It's a unary operator.
The parenthesis you usually see after it are part of the argument, which for type names looks like a cast to the type in question. Like any other cast, the syntax is the type name in parenthesis.
Apologies if you already knew this.
Understanding the memory model and execution model is a big plus, because it lets you write correct and efficient code (and if you are using C you care about correctness and efficiency). Recalling arcana about the C99 standard is just overkill and asking someone to pull stuff like that out of a hat in an interview is asking too much IMO.