2

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

Yktula
  • 14,179
  • 14
  • 48
  • 71

3 Answers3

8

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
  • 713
  • 4
  • 10
  • 4
    C99 supports K&R-style function definitions (they are deprecated, but supported nonetheless). – James McNellis Apr 13 '10 at 23:03
  • 1
    K&R style declarations are valid in C99, except that in C99 all parameters have to explicitly declared, while in K&R (and C89/90) `int` parameters could be left undeclared. – AnT stands with Russia Apr 13 '10 at 23:14
  • @James McNellis: interesting - I had thought that K&R-style function definition support was removed from C99, too. I'm not sure why... – Michael Burr Apr 13 '10 at 23:16
  • 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 Apr 14 '10 at 14:19
7

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
  • 81,409
  • 55
  • 245
  • 302
0

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
  • 169,610
  • 28
  • 168
  • 175