![](/img/trans.png)
[英]C- implicit declaration of functions time, rand, printf, all of my function calls
[英]Implicit declaration of functions srand, rand and system
試圖解決一個練習,我必須每隔5秒在35°C和-10°C之間打印一個隨機溫度值,然后是日期和時間。 一切看起來都按預期工作,但是當我在測試腳本中輸入代碼時,我會得到以下錯誤。
這是我的代碼:
#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;
}
這些是我的結果:
這是測試腳本檢查的內容:
由於我無法看到自己的錯誤,任何幫助將不勝感激。
你未能#include <stdlib.h>
。 因此,您調用的函數被假定為接受未知數量的參數並返回int
類型的值。 這會導致未定義的行為。
將#include <stdlib.h>
放在文件的頂部。
我遇到了類似的問題。 我的代碼已編譯但我收到了警告信息。
// 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;
}
當我編譯它時,
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
添加<stdlib.h>
警告消息消失了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.