函数rand,rand和system的隐式声明 [英] Implicit declaration of functions srand, rand and system

查看:221
本文介绍了函数rand,rand和system的隐式声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试解决一项运动,我必须打印35°C和70°C之间的随机温度值.每5秒-10°C,然后加上日期和时间.一切看起来都按预期进行,但是,当我在测试脚本中输入代码时,出现以下错误.

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. Everything looks to be working as intended, however when I enter the code in a test script I get following errors.

这是我的代码:

#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;
}

这些是我的结果:

这是测试脚本检查的内容:

This is what the test script checks for:

由于我看不到自己的错误,因此将不胜感激.

As I am unable to see my own mistakes, any help would be greatly appreciated.

推荐答案

您未能完成#include <stdlib.h>.结果,假定您调用的函数接受未知数量的参数并返回类型为int的值.这会导致不确定的行为.

You failed to #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. This causes undefined behavior.

#include <stdlib.h>放在文件顶部.

这篇关于函数rand,rand和system的隐式声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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