gpt4 book ai didi

gcc - gcc 在 c99 模式下是否错误地执行隐式函数声明?

转载 作者:行者123 更新时间:2023-12-04 15:43:16 25 4
gpt4 key购买 nike

考虑以下代码:

int main (void) {
int i = xyzzy();
return i;
}
int xyzzy (void) {
return 42;
}

现在,虽然 xyyzy 的原型(prototype)在使用时未知,这适用于 c89 模式,因为没有原型(prototype)的函数的默认返回类型是 int所以隐函数原型(prototype)和实际函数是兼容的。

而且,实际上,如果将函数的返回类型更改为 float ,你得到(如预期的那样):
testprog.c:6: error: conflicting types for 'xyzzy'
testprog.c:2: error: previous implicit declaration of 'xyzzy' was here

因为隐式原型(prototype)和实际函数不再匹配。

使用 gcc --std=c89 --pedantic -Wall -Wextra 编译的原始代码只给我警告:
testprog.c: In function 'main':
testprog.c:2: warning: implicit declaration of function 'xyzzy'

这是意料之中的,因为 c89 在 3.7.1 Function definitions 中有这样的说法:

extern int max(int a, int b) { ... }: Here extern is the storage-class specifier and int is the type specifier (each of which may be omitted as those are the defaults).



3.3.2.2 Function calls :

If the expression that precedes the parenthesized argument list in a function call consists solely of an identifier, and if no declaration is visible for this identifier, the identifier is implicitly declared exactly as if, in the innermost block containing the function call, the declaration extern int identifier(); appeared.



所以在声明它之前使用一个函数肯定会导致创建默认原型(prototype)。

然而,这两个短语在 c99 中都被删除了,我们在 6.5.2.2 Function calls 中找到了。 (我的粗体):

If the expression that denotes the called function has type pointer to function returning an object type, the function call expression has the same type as that object type, and has the value determined as specified in 6.8.6.4. Otherwise, the function call has type void.



我理解它的意思是,如果在您尝试调用函数时没有看到声明,它会用 void 隐式声明。返回类型。

然而,当使用 gcc --std=c99 --pedantic -Wall -Wextra 编译时,我得到关于隐式声明的相同警告。

c99 不应该将该函数隐式声明为返回 void ?如果有,我会期待 previous implicit declaration类似于我尝试将其重新声明为返回 float 时遇到的错误.

gcc在这里坏了,还是我在标准中遗漏了什么?

最佳答案

您正在错误地阅读标准。 C 中没有隐式函数声明之类的东西。它已被 C99 从语言中删除。

当 GCC 看到一个看起来像隐式函数声明的错误构造时,它会发出警告。就标准而言,这是可以的。标准在这里需要诊断,警告就是诊断。您可以使用 -Werror=implicit-function-declaration向 GCC 标记以将其变为错误。

关于gcc - gcc 在 c99 模式下是否错误地执行隐式函数声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13986301/

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