问题
When reading some FreeBSD source code (See: radix.h lines 158-173), I found variable declarations that followed the \"function heading\" in the definition.
Is this valid in ISO C (C99)? when should this be done in production code instead of just declaring the variables within the \"function heading?\" Why is it being done here?
I refer to the function heading the string that looks like this: int someFunction(int i, int b) {
回答1:
That looks like K&R (pre-ANSI) style. I don't think it's valid C99, but are they using C99? Joel
回答2:
I think you are referring to the "old-fashioned" pre-ANSI way of declaring parameters in C. It looked something like this:
int foo(a, b)
int a,
int b
{
/* ... */
}
That may still be valid in C99, and will be accepted by compilers for backward-compatibility reasons, but it should be considered deprecated/archaic.
回答3:
Er. Maybe I'm misunderstanding your question, but i
and b
in that snippet are parameters to the function. It's not some compact way of declaring variables within the function, like:
int someFunction() {
int i, b;
When you call someFunction
, you pass it those arguments:
someFunction(1, 2); // `i` will be `1` and `b` `2` within `someFunction`
来源:https://stackoverflow.com/questions/2633776/c-variable-declarations-after-function-heading-in-definition