Implicit declaration of functions srand, rand and system

39,950

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.

Share:
39,950
TheAlPaca02
Author by

TheAlPaca02

Updated on July 05, 2022

Comments

  • TheAlPaca02
    TheAlPaca02 almost 2 years

    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.

    implicit declaration of functions 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:

    These are my results

    This is what the test script checks for:

    This is what the test script checks for

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

  • TheAlPaca02
    TheAlPaca02 about 8 years
    Is it normal that my script is able to run while I forgot this include? I always assumed that the script wouldn't even have ran if this was the problem..
  • dbush
    dbush about 8 years
    @TheAlPaca02 The program will still compile with the mismatched signature. If the parameters are not of the correct type however, the compiler won't be able to detect it.
  • TheAlPaca02
    TheAlPaca02 about 8 years
    Ok understood, thanks! Will run the testscript again tomorrow morning.
  • M.M
    M.M almost 6 years
    So basically you are saying that the other answer solved your problem? You don't need to post another answer to say that the other answer is good -- you can simply upvote the other answer