[英]Implicit declaration of functions srand, rand and system
Trying to solve an exercise where I have to print a random temperature value between 35°C & -10°C every 5 seconds followed by the date and time. 试图解决一个练习,我必须每隔5秒在35°C和-10°C之间打印一个随机温度值,然后是日期和时间。 Everything looks to be working as intended, however when I enter the code in a test script I get following errors.
一切看起来都按预期工作,但是当我在测试脚本中输入代码时,我会得到以下错误。
This is my code:
这是我的代码:
#include<stdio.h>
#include<unistd.h>
#include<time.h>
#define temp_max 35
#define temp_min -10
#define FREQUENCY 5
int main(void)
{
srand(time(NULL));
while(1)
{
int number = rand() % (temp_max*100 - temp_min*100) + temp_min*100;
double temperature = (double) number;
temperature /= 100;
printf("Temperature=%1.2f @ ",temperature);
fflush(stdout);
system("date");
sleep(FREQUENCY);
}
return 0;
}
These are my results: 这些是我的结果:
This is what the test script checks for: 这是测试脚本检查的内容:
As I am unable to see my own mistakes, any help would be greatly appreciated. 由于我无法看到自己的错误,任何帮助将不胜感激。
You failed to #include <stdlib.h>
. 你未能
#include <stdlib.h>
。 As a result, the functions you called were assumed to accept an unknown number of arguments and return a value of type int
. 因此,您调用的函数被假定为接受未知数量的参数并返回
int
类型的值。 This causes undefined behavior. 这会导致未定义的行为。
Put #include <stdlib.h>
at the top of your file. 将
#include <stdlib.h>
放在文件的顶部。
I had a similar problem. 我遇到了类似的问题。 My code was compiled but I got warning message.
我的代码已编译但我收到了警告信息。
// Selection sort
#include <stdio.h>
#include <math.h>
int main()
{
int list[100], i;
for(i = 0 ; i < 100; i++) {
list[i] = ( rand()%99 + 1 );
printf("%d ", list[i]);
} printf("\n");
return 0;
}
When I compile it, 当我编译它时,
gcc -o selection_sort selection_sort.c -lm
selection_sort.c: In function ‘main’:
selection_sort.c:11:15: warning: implicit declaration of function ‘rand’; did you mean ‘nanl’? [-Wimplicit-function-declaration]
list[i] = ( rand()%99 + 1 );
^~~~
nanl
After adding <stdlib.h>
warning message was gone. 添加
<stdlib.h>
警告消息消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.