views:

152

answers:

3

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) {

A: 

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`
Michael Mrozek
+7  A: 

That looks like K&R (pre-ANSI) style. I don't think it's valid C99, but are they using C99? Joel

Joel J. Adamson
James McNellis
AndreyT
Michael Burr
C in a Nutshell says: "This notation...is deprecated, although compilers still support it. In new C source code, use only prototype notation for function definitions..." (pg 98, First Edition, 2005)If it is "Berkeley style," then they are probably not going to change their compiler to contradict with their style.
Joel J. Adamson
+2  A: 

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.

Kristopher Johnson