这是什么c函数?

What kind of c function is this?

我正在读 "Compiler Design in C" 本书。在基础知识部分,我找到了一个词法分析器的 c 代码片段,类似这样 -

static int Lookahead = -1;
int match(token)
int token;
{
 if(Lookahead == -1)
    Lookahead = lex();

 return token == Lookahead;
}

void advance(){
   Lookahead = lex();
}

我对这个匹配函数如何在 gnu gcc 上编译感到困惑。所以我写了一个看起来像

的函数
int a(token)
int token;
{
 printf("Value of token is %d", token);
}
int main()
{
 printf("Hello world!\n");
 a(1);
 return 0;
}

我得到以下输出-

世界,您好! token 的值为 1

但我不明白该函数声明背后的原因。以这种方式声明函数有什么好处? token 的值如何为 1?为什么它不是编译错误?它是 C 中的某种函数声明吗?

感谢您检查我的问题。任何形式的帮助都会很棒。

这是一种陈旧且已弃用的 K&R 函数声明风格。

int match(token)
int token;
{
    // Function body
}  

等同于

int match(int token)
{
    // Function body
}    

除此之外,编译器不会检查是否使用正确数量的参数调用函数,也不会检查参数类型。它依赖于默认参数提升。
C89和C99也支持这种风格。

这是 Kernighan 和 Ritchie 最初开发 C 编程语言时声明 C 函数的原始方式。它被称为 'K&R C'

C 经历了标准化过程,这种形式的函数声明已更改为您习惯的形式。

在页面上 http://en.wikipedia.org/wiki/The_C_Programming_Language 这本书的第一版会有旧式函数声明,我认为它在第二版中被删除了。

原始 K&R C 中的函数定义更简单。在您的示例中,函数参数令牌的类型声明实际上是多余的。代码可以这样简化:

static int Lookahead = -1;
int match(token) {
    if (Lookahead == -1)
        Lookahead = lex();
    return token == Lookahead;
}
void advance(){
   Lookahead = lex();
}

确实函数return类型和变量类型也默认为int,void还没有发明,return语句是可选的...导致进一步简化:

static Lookahead=-1;
match(token){
  if(Lookahead==-1) Lookahead=lex();
  return token==Lookahead;
}
advance(){Lookahead=lex();}

这种原始风格更紧凑,但很容易出错,现代符合规范的编译器不再支持。您示例中的函数定义语法仍然受支持,但被认为已过时并且可能在某个时候消失。