简体   繁体   中英

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. 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> . 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.

Put #include <stdlib.h> at the top of your file.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM