函数声明与返回类型不同会带来什么影响?
本帖最后由 shirui8653719 于 2013-03-10 00:09:24 编辑
#include "stdafx.h"
int abc(float a);
int _tmain(int argc, _TCHAR* argv[])
{
float n,m=7;
n = abc(m);
printf("%d\n",n);
return 0;
}
int abc(float a)
{
float g;
g = a;
printf("%f\n",g);
return g;
}
输出的结果g=7.000000,n=0。g=7.000000这个很好理解,但是为什么n=0呢?为什么不是7?
[解决办法]
跟“函数声明与返回类型不同”没什么关系。
int转float,float转int(截断),这都没问题的。
问题在于printf("%d\n",n);这句,n是float,但%d期待int,这里就不是int和float互转的问题了。
[解决办法]
函数声明不正确会出现令人惊讶的错误,最关键的是,错误只有在特定条件下才出现,比较跟踪。
看看这个例子吧!
http://bbs.csdn.net/topics/390383557