3 ms·
Well, printf is a vararg function, so even what type checking C has is out of play (except for the first parameter). C doesn't just let you reinterpret bits at
by AnimalMuppet 12y ago
Well, printf is a vararg function, so even what type checking C has is out of play (except for the first parameter).
C doesn't just let you reinterpret bits at will. You have to jump through one of a few very specific hoops. You have to cast, you have to use a union, or you have to use a vararg function. (Granted, those are some pretty big hoops, but my point is, you have to use one in order to reinterpret bits.)
I don't think C's type promotion rules are in the same category. You can promote a short to a double when adding it to a double, but that's not exactly "reinterpreting bits at will".
- mikeash 12y agoOr you can call a function without a prototype (no longer allowed in C99, thankfully), call a function whose prototype's type signature doesn't match the implementation, access a global variable whose declaration doesn't match the definition, access a struct using a different declaration than the one used by whoever put the data there in the first place, or just do an accidental type pun through a void *, which requires no casting. It's not at all hard to do by accident. Much of this is why binary compatibility for libraries written in C is so hard to achieve over the long term. I agree that type promotion isn't the same, but I didn't mention that anyway....
- AnimalMuppet 12y agoArgh. I forgot that C doesn't do name mangling. Most of those problems go away in C++, though (he said hopefully, but without complete confidence...)
- mikeash 12y agoThe problem of mismatched prototypes goes away (except for extern "C" functions, of course) but then you get fun stuff like fragile base classes. If you have a base class in a library and a subclass in an application that links against that library, you can never add a virtual function to the base class again without breaking backwards compatibility (and causing all sorts of accidental type-punning chaos when you try to call methods).