C expression between function declaration and curly brace
Possible duplicate:
What's useful in this C syntax? Declaring a C variable after a function header in a definition.
What strange C syntax is this?
I am trying to understand some code and it has something like the following:
int getr(fget)
FILE *fget;
{
/* More declarations and statements here */
return (1);
}
Is there any difference between the above and:
int getr(fget)
{
FILE *fget;
/* More declarations and statements here */
return (1);
}
If so, how are they different?
source to share
Both functions are declared in old fashioned (non-prototype) form. Old-style function declarations are deprecated in the current C standard, and their use is strongly discouraged by the C standard.
The second form does not mention the type of parameter fget
that is considered int
. Then another object of the fget
type is declared FILE *
and it modifies the parameter variable with the same name.
With gcc
a warning parameter, the warning -Wshadow
in the second example will be displayed due to parameter shading:
-Wshadow
Warn whenever a local variable shadows another local variable,
parameter or global variable or whenever a built-in function is shadowed.
source to share
The first is the K and R style defining style , this is the deprecated Ref 1 function .
The second is known as the Implicit int function prior to the c99 standard.
Before c99 If the function did not return an explicit type or did not specify the type in the declaration, then the type was considered int
.
Both methods are deprecated and mentioned in the c99 standard.
Literature:
Standard C99: Preface Pair 7:
Major changes in 2nd edition:
- remove implicit int
- remove implicit function declaration
Reference 1
6.11.7 Function definitions
The use of function definitions with separate parameter identifiers and declaration lists (not format prototype type and identifier declarators) is a deprecated feature.
source to share