gpt4 book ai didi

c - 如果没有头文件,如何调用像 time() 这样的 C 函数?

转载 作者:行者123 更新时间:2023-12-01 23:36:41 26 4
gpt4 key购买 nike

根据我检查过的文档,time_t time(time_t *seconds)<time.h> 下声明头文件。然而,当我运行这段代码时:

#include<stdio.h>
#include<stdlib.h>
int main()
{
srand(time(NULL));
printf("%d",rand());
return 0;
}

不包括<time.h> ,它工作正常,或者至少工作。这是怎么发生的?

最佳答案

如果启用编译器警告,它不会正常工作。如果没有所需的 header ,它会发出警告,例如

1347694184/source.c: In function ‘main’:
1347694184/source.c:7:11: warning: implicit declaration of function ‘time’ [-Wimplicit-function-declaration]
srand(time(NULL));
^~~~

故事的寓意:始终使用正确的编译器设置来发出警告(如果可能,将警告视为错误)。注意警告,您会更加安全。

关于c - 如果没有头文件,如何调用像 time() 这样的 C 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65488894/

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