代码之家  ›  专栏  ›  技术社区  ›  Tom Dalling

C语言中这种奇怪的函数定义语法是什么[[副本]

  •  34
  • Tom Dalling  · 技术社区  · 16 年前

    我最近在玩GNU Bison时看到过一些这样的函数定义:

    static VALUE
    ripper_pos(self)
        VALUE self;
    {
        //code here
    }
    

    self

    6 回复  |  直到 16 年前
        1
  •  39
  •   pat    12 年前

    那些是老K&R样式的函数参数声明,分别声明参数的类型:

    int func(a, b, c)
       int a;
       int b;
       int c;
    {
      return a + b + c;
    }
    

    int func(int a, int b, int c)
    {
      return a + b + c;
    }
    

    “新风格”的宣言基本上是普遍赞成的。

        2
  •  9
  •   Fyodor Soikin    16 年前

    这是声明函数参数的所谓“旧”变体。在过去,你不能只在括号里写参数类型,但是你必须在右括号之后为每个参数定义它。

    换句话说,它相当于 ripper_pos( VALUE self )

        3
  •  4
  •   mipadi    16 年前

    是的,它使用了一种老式的函数定义,其中参数sans type列在括号中,后面是这些变量的声明 它们的类型在函数体的左大括号之前。所以呢 self VALUE .

        4
  •  3
  •   Stephen    16 年前

    这是 古老的 c。K&在ansic强制使用类型化参数之前,rc使用了这个约定。

    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'.
    
        6
  •  2
  •   axel_c    16 年前

    这是一个非常古老的C代码,首先指定参数名,然后指定它们的类型。参见示例 here