了解C语言中的函数原型 [英] Understanding Function Prototype in C

查看:135
本文介绍了了解C语言中的函数原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下面的程序工作正常?

  int main()
{
int x ;
x = foo();
printf(%d,x);
getchar();
返回0;
}

int foo()
{
return 2;
}

而不是这个程序?

  // double function(void); 

int main(){
double val;
val = function();
printf(%ul\\\
,val);
}

双函数(void){
double num;
num = DBL_MAX;
printf(%ul\\\
,num);
返回num;





$ b根据我的理解,在这两种情况下函数的定义在 main()的。那么为什么在第一种情况下函数会被调用,即使编译器在 main()之前没有定义它,而不是第二种情况?

解决方案

由于隐式函数声明,编译器假定未指定的类型默认为 int



在第一种情况下巧合的是,但在第二种情况下没有。


Why does the following program works fine?

int main()
{
    int x;
    x = foo();
    printf("%d",x);
    getchar();
    return 0;
}

int foo()
{
    return 2;
}

and not this program?

//double function(void);

int main(){
    double val;
    val = function();
    printf("%ul\n",val);
}

double function(void){
    double num;
    num = DBL_MAX;
    printf("%ul\n",num);
    return num;
}

In my understanding, definition of functions in both these cases are absent before main(). So why is it that in the first case the function is called anyway, even when compiler has no definition of it before main() and not in second case?

解决方案

Because of implicit function declaration, the compiler assumes that unspecified types are int by default.

In the first case that is coincidentally true, but not in the second case.

这篇关于了解C语言中的函数原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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