gpt4 book ai didi

c - 这是什么c函数?

转载 作者:太空狗 更新时间:2023-10-29 15:48:42 24 4
gpt4 key购买 nike

我正在阅读“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 也支持这种风格。

关于c - 这是什么c函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28655753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com