这是可疑使用非原型函数声明有效吗? [英] Is this dubious use of a non-prototype function declaration valid?

查看:260
本文介绍了这是可疑使用非原型函数声明有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有效的C(C99)code?

  INT F();
INT克(INT X)
{
    如果(X℃,)返回F(X);
    否则返回F(X,X);
}

如果先按g 是不断所谓带负参数和˚F显然,该方案已未定义行为不是一个函数这需要一个 INT 参数,或者如果先按g 是不断调用一个非负的参数和˚F不是一个函数,它接受两个 INT 参数。但在其他方面?

作为示例考虑它调用先按g 从上面的,并提供了这个单独的源文件˚F

  INT克();
#IFDEF FOO
INT F(int类型的,INT B){返回A + B; }
INT主(){返回克(1); }
#其他
INT F(int类型的){返回; }
INT的main(){返回克(-1); }
#万一


解决方案

让我们问周围的其他方法:?为什么它的不可以有效的。我真的找不到,禁止上述code任何参数或规则。在各自的另一分支的函数调用从未执行过(尽管在评论中讨论表明它不是那么容易!)。

Is this valid C (C99) code?

int f();
int g(int x)
{
    if (x<0) return f(x);
    else return f(x,x);
}

Obviously the program has undefined behavior if g is ever called with a negative argument and f is not a function that takes a single int argument, or if g is ever called with a non-negative argument and f is not a function that takes two int arguments. But otherwise?

Consider as an example this separate source file which calls g from the above and provides f:

int g();
#ifdef FOO
int f(int a, int b) { return a+b; }
int main() { return g(1); }
#else
int f(int a) { return a; }
int main() { return g(-1); }
#endif

解决方案

Let's ask the other way around: Why would it not be valid?. I really can't find any argument or rule that forbids the above code. The function call in the respective other branch is never executed (although discussion in the comments indicate it's not so easy!).

这篇关于这是可疑使用非原型函数声明有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆