tags:

views:

39

answers:

2

I came across this code in Stephen G Kochan's book, Programming in c. Is this possible?

float absolute_value(x)
float x;
{
   -----
   -----
}

So, as you can see, the argument x is declared after it's used it the method arguments. This throws an obvious compilation error in G++.

So, which C compiler supports this?

+1  A: 

That's the old style K&R format. It's not actually declaring the argument x, rather defining its type. By default, things were int unless otherwise specified.

Back when C was a much simpler language, not that far removed from my beloved BCPL, this was how you gave function arguments their types. None of these prototype stuff that you young whippersnappers take for granted.

Oh yeah, and get off my lawn :-)

paxdiablo
That means I have a very very old book, meant for oldies ;)
zengr
paxdiablo
I agree Sire :)
zengr
Back then, we had to do everything with 0's and 1's, and we couldn't afford any 1's so we had to pile up the 0's...
Steven Sudit
+1  A: 

This is the original way of declaring the types of function parameters in C. Any properly functioning C compiler is required to accept it. It is not allowed in C++, however, so every properly functioning C++ compiler must reject it (though in both cases, note that some specific combination of compiler flags might be required to achieve proper function). Once upon a time, C compilers only accepted this style, and would reject code like: float absolute_value(float x) {}. This was added (along with function prototypes) while C was being standardized.

Jerry Coffin
It's not allowed in C99, either.
dan04
@dan04: Not so. It is, according to §6.11.7 "an obsolescent feature", but it's still allowed (per §6.9.1/7): "The declarator in a function definition specifies the name of the function being defined and the identifiers of its parameters. If the declarator includes a parameter type list, the list also specifies the types of all the parameters; [...]"
Jerry Coffin