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`
Related
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.
This question already has answers here:
Alternative (K&R) C syntax for function declaration versus prototypes
(5 answers)
Closed 5 years ago.
I was going through the make program's source code and I came across the following function declaration:
struct dep *
read_all_makefiles (makefiles)
char **makefiles;
{ ... followed by function code ...
How do we decipher this declaration?
It's the old K&R style of function parameter declaration, prior to the ANSI/ISO standard C. This style is outdated now but can still be found in some very old codes. Although it's still in standard, it's recommended not to write like this anymore.
To decipher, simply move the parameter declaration list back to the function prototype, one-by-one, with the identifiers matching.
Quoting draft N1570, §6.9.1/13:
EXAMPLE 1
extern int max(int a, int b)
{
return a > b ? a : b;
}
EXAMPLE 2
extern int max(a, b)
int a, b;
{
return a > b ? a : b;
}
See Alternative (K&R) C syntax for function declaration versus prototypes
This question already has answers here:
What is the difference between a definition and a declaration?
(27 answers)
Closed 8 years ago.
#include <stdio.h>
int main()
{
int a;
printf("%d\n",a);
}
The statement in the above example: int a; — is it a declaration or definition? If it is a declaration, the printf() statement should give an error, but it prints a garbage value. So we cannot call it a declaration. But as per the syntax it is a declaration. So what is the difference in this case?
There's no way around the fact that this is a declaration. Every definition in C language is a declaration at the same time. In the reverse direction: some declarations are definitions and some are not.
int a; in your code is a declaration that happens to be a definition as well.
When you declare a local variable in c, space on the stack is created for the variable. When you declare int a without giving a a specific value, it's value will be whatever is already in memory in the location set aside for it on the stack. That is most likely a garbage value, which is why you're seeing strange numbers when printing the value of a. To avoid this, declare int a = 0, or some other number.
As for how to declare rather than define a varaible, you should use the extern keyword as explained in the other answer.
It's a definition.
For a variable in C, you'd have to use the 'extern' keyword if you wanted to only declare it, not define it. A good article can be found here
Declaration means we are just creating a variable or method. Defination means we are assigning some value for a variable & doing some functions in method
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.
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.