Why declaring function arguments this way: void func(x) int x; {} [duplicate] - c

This question already has answers here:
Alternative (K&R) C syntax for function declaration versus prototypes
(5 answers)
Closed 3 years ago.
I stumbled upon these lines when looking into putchar.c
I'm wondering about why the arguments ptr and c are declared outside the arguments body ?
Is this some kind of "good old way" or does it have some actual use ?
int
_putchar_r (ptr, c)
struct _reent *ptr;
int c;
{
return __sputc (c, _stdout_r (ptr));
}

Indeed it is the "olden" way of declaring a function's parameters.
I kind of like it, because it serves as a constant reminder that all a function's parameters are local variables that exist only in the scope of the function and that any argument passed into any function ALWAYS is a value copy.

Its a K&R C style introduced in classic C Programming Book

It is a function definition with an identifier list. Each identifier in the identifier list is declared before the compound statement of the function.
So a function can be defined either with a parameter type list or using the old style with an identifier list.

Related

Uncommon function definition in the 'less' program source [duplicate]

This question already has answers here:
C function syntax, parameter types declared after parameter list
(7 answers)
Closed 3 years ago.
My friend asked me could I add "frozen top line" feature to the standard less *NIX program. I started to view the source and was very surprised in function definition the filename.c:
public char *
last_component(name)
char *name;
{
....
}
I wrote some code at the C and usually I met:
return_type function_name arguments_list; if it is function prototype
return_type function_name arguments_list code-block if it is function definition.
But here I found something new: return_type function_name arguments list something_mysterious; code block.
What does this syntax means?
This is old C syntax (also known as K&R style), where a function definition could be written as where the argument are declared on the subsequent line:
int func(a,b)
int a;
char b;
{

what does variable declaration after function declaration mean? [duplicate]

This question already has answers here:
Alternative (K&R) C syntax for function declaration versus prototypes
(5 answers)
Closed 8 years ago.
I've seen a few function definitions like this recently while playing with GNU Bison:
static VALUE
ripper_pos(self)
VALUE self;
{
//code here
}
Why is the type of self outside of the parenthesis? Is this valid C?
Those are old K&R style function parameter declarations, declaring the types of the parameters separately:
int func(a, b, c)
int a;
int b;
int c;
{
return a + b + c;
}
This is the same as the more modern way to declare function parameters:
int func(int a, int b, int c)
{
return a + b + c;
}
The "new style" declarations are basically universally preferred.
This is the so-called "old" variant of declaring function arguments. In ye olden days, you couldn't just write argument types inside the parentheses, but you had to define it for each argument right after the closing parenthesis.
In other words, it is equivalent to ripper_pos( VALUE self )
Yes, it uses an older style of function definition in which the parameters, sans type, are listed in parentheses, followed by the declaration of those variables with their types before the opening brace of the function body. So self is of type VALUE.
This is old c. K&R C used this convention, before ANSI C enforced typed parameters.
static VALUE // A static function that returns 'VALUE' type.
ripper_pos(self) // Function 'ripper_pos' takes a parameter named 'self'.
VALUE self; // The 'self' parameter is of type 'VALUE'.
That's the old-style C function declaration syntax.
It is really old C code, where you first specify the argument names, and then their types. See for example here.

C variable declarations after function heading in definition [duplicate]

This question already has answers here:
What is this strange function definition syntax in C? [duplicate]
(6 answers)
Closed 8 years ago.
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) {
That looks like K&R (pre-ANSI) style. I don't think it's valid C99, but are they using C99? Joel
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.
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`

What is this strange function definition syntax in C? [duplicate]

This question already has answers here:
Alternative (K&R) C syntax for function declaration versus prototypes
(5 answers)
Closed 8 years ago.
I've seen a few function definitions like this recently while playing with GNU Bison:
static VALUE
ripper_pos(self)
VALUE self;
{
//code here
}
Why is the type of self outside of the parenthesis? Is this valid C?
Those are old K&R style function parameter declarations, declaring the types of the parameters separately:
int func(a, b, c)
int a;
int b;
int c;
{
return a + b + c;
}
This is the same as the more modern way to declare function parameters:
int func(int a, int b, int c)
{
return a + b + c;
}
The "new style" declarations are basically universally preferred.
This is the so-called "old" variant of declaring function arguments. In ye olden days, you couldn't just write argument types inside the parentheses, but you had to define it for each argument right after the closing parenthesis.
In other words, it is equivalent to ripper_pos( VALUE self )
Yes, it uses an older style of function definition in which the parameters, sans type, are listed in parentheses, followed by the declaration of those variables with their types before the opening brace of the function body. So self is of type VALUE.
This is old c. K&R C used this convention, before ANSI C enforced typed parameters.
static VALUE // A static function that returns 'VALUE' type.
ripper_pos(self) // Function 'ripper_pos' takes a parameter named 'self'.
VALUE self; // The 'self' parameter is of type 'VALUE'.
That's the old-style C function declaration syntax.
It is really old C code, where you first specify the argument names, and then their types. See for example here.

in c: func(void) vs. func() [duplicate]

This question already has answers here:
Is it better to use C void arguments "void foo(void)" or not "void foo()"? [duplicate]
(6 answers)
Closed 5 years ago.
When a C function does not accept any arguments, does it have to be declared/defined with a "void" parameter by the language rules?
PC-Lint seems to have problems when there's nothing at all in the argument-list, and I was wondering if it's something in the language syntax that I don't know about.
Edit: I just found a duplicate (back-dupe? it came first) question, C void arguments, which has more answers and explanations.
void means the function does not take any parameters. For example,
int init (void)
{
return 1;
}
This is not the same as defining
int init ()
{
return 1;
}
because in the second case the compiler will not check whether the function is really called with no arguments at all; instead, a function call with arbitrary number of arguments will be accepted without any warnings (this is implemented only for the compatibility with the old-style function definition syntax, pre-ANSI).
IIRC func(void) in C will declare a function that takes no parameters whereas func() declares a function that will take any number of parameters. I believe the latter is an artifact coming from pre-ANSI C.
According to Wikipedia here, the declaration func() does basically declare the function "without information about the parameters".

Resources