函数的隐式声明与函数的原始版本之间的差异 [英] Difference between "implicit declaration of function" and the original version of the function
本文介绍了函数的隐式声明与函数的原始版本之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是gcc4.8。我写了这样的代码,使用睡眠。
int main(int argc, char *argv[])
{
/* I know it's worong to pass a floating number to sleep
* this is only for testing. */
sleep(0.001);
return 0;
}
我用"GCC-WALL A.C-O a"编译,得到警告"函数‘睡眠’的隐式声明[-W隐式函数-声明]"。然后我运行它,这个程序休眠大约1秒(似乎休眠间隔为0.001比1)。
然后我将代码更改为:
#include <unistd.h> /* add header file */
int main(int argc, char *argv[])
{
/* I know it's worong to pass a floating number to sleep
* this is only for testing. */
sleep(0.001);
return 0;
}
这一次它只休眠了0.001秒,看起来像是休眠楼层0.001到0。
这两个睡眠不应该相同吗?
推荐答案
在第一种(错误的)情况下,由于假定sleep
的原型采用浮点值(实际上是double
),所以将实际浮点值给予休眠。sleep
会将此double
的位表示解释为int
,并等待这么多秒。您很幸运,这只有1秒。在第二种情况下,浮点值被强制转换为取整为0的int
。
这篇关于函数的隐式声明与函数的原始版本之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文