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

断了今生、忘了曾经 提交于 2019-12-17 02:50:13

问题


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?


回答1:


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.




回答2:


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 )




回答3:


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.




回答4:


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'.



回答5:


That's the old-style C function declaration syntax.




回答6:


It is really old C code, where you first specify the argument names, and then their types. See for example here.



来源:https://stackoverflow.com/questions/3016213/what-is-this-strange-function-definition-syntax-in-c

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