3

Possible Duplicate:
What is useful about this C syntax?
C variable declarations after function heading in definition
What weird C syntax is this?

I'm 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 do they differ?

Community
  • 1
  • 1
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • @Paul R. You are correct, this is a duplicate of that question. Is is possible to edit the title of that question to be more descriptive? – mgilson Mar 23 '12 at 17:43

2 Answers2

9

Both functions are declared in the old-style (non-prototype) form. Old-style function declarations are obsolescent in the current C standard and their use is strongly discouraged by the C Standard.

In the second form there is no mention of the fget parameter type which is assumed to be an int. Then another object fget of type FILE * is declared and it shadows the parameter variable with the same name.

With gcc the -Wshadow warning option would get you a warning in your second example because of the shadowing of the parameter:

   -Wshadow
       Warn whenever a local variable shadows another local variable, 
       parameter or global variable or whenever a built-in function is shadowed.
ouah
  • 142,963
  • 15
  • 272
  • 331
  • So I assume the "encouraged" way to do it would be: `int getr(FILE* fget) { ...}` ? (preferably with a prototype at the top of the file) – mgilson Mar 23 '12 at 06:47
  • @mgilson: yes, you should define the function as: `int getr(FILE* fget) { ...}`. This is a function definition in the prototype syntax. Note that this function definition also serves as a prototype for later in the source code. – ouah Mar 23 '12 at 06:55
7

The first one is the K & R style of function definition, it is an obsolescent featureRef 1.

The second is popularly known as Implicit int feature prior to c99 standard.
Prior to c99 If a function returned no explicit type or didn't specify a type in declaration, then the type was assumed to be a int.

Both of the methods have been deprecated and find a mention in the c99 Standard.

References:
C99 Standard: Foreword Para 7:

Major changes in the second edition included:
— remove implicit int
— remove implicit function declaration

Ref 1
6.11.7 Function definitions

The use of function definitions with separate parameter identifier and declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.

Alok Save
  • 202,538
  • 53
  • 430
  • 533