October 09, 2003

Why I hate C, reason #4,348

Varargs. Or, these days, stdargs. It's how C lets you define functions that take a variable number of arguments.

Why do I hate it? Because, even though you're forced to view the argument list opaquely, there's no argument count! That is, the standard says the variable length argument list lives in a magic va_arg thingie, you must use the Magic Functions to get the individual arguments out, and you must have the magic ... signature to note varargs in the function declaration, but... the standard can't be bothered to mandate that an argument count is passed. This seems utterly mad.

Yes, I know, it's an extra parameter to be constructed and passed, but the caller knows how many parameters its passing in at compile time and that it's passing them in as part of a variable length parameter list so calculation's a compile-time cost. The callee has to figure out how many parameters are being passed anyway, either with an explicit count or something like a format string, so it's not like this information's not available or passed. Not just throwing a count in as something that must be there and instead force people building or using vararg functions to do it anyway, but by hand which is far more error-prone.

Yeah, I'm sure there were Very Good Reasons to not mandate this in the standard. I have no idea what they are, but I'm sure they all suck.

C. Bleah.

Posted by Dan at October 9, 2003 10:41 AM | TrackBack (0)
Comments

The reason is because C didn't use to have prototypes (think "K&R"). Without prototypes, the caller function does not know that they're calling a function with varargs. Therefore the caller would not have pushed a count. I assume that after the introduction of prototypes, the reason to not push a count was to preserve compatibility with the old language (can't force everybody to upgrade at once, you know). Besides, prototypes were still only optional.

Now, as for why C++ doesn't require a count to be pushed, that's a better question. I assume it's because Bjarne wanted C++ to be a strict superset of C, and therefore he held onto a lot of ugly design decisions that had already been enshrined in history.

Posted by: Kimberley Burchett at October 9, 2003 12:32 PM

Okay, you've managed to come up with the one reason I can't actually complain about.

Well, OK, I can complain--prototypes shoud've been mandatory. :)

Posted by: Dan at October 13, 2003 02:45 PM