C statement between function declaration and curly brace [duplicate]

梦想的初衷 提交于 2019-12-10 18:14:12

问题


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?


回答1:


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.



回答2:


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.



来源:https://stackoverflow.com/questions/9834916/c-statement-between-function-declaration-and-curly-brace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!