Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 215
Tom Dalling Avatar asked Jun 10 '10 16:06

Tom Dalling


2 Answers

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.

like image 168
sth Avatar answered Sep 21 '22 09:09

sth


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 )

like image 9
Fyodor Soikin Avatar answered Sep 19 '22 09:09

Fyodor Soikin