0

I'm trying to make my program return a random number [-5,5] using rand() function in C. The program is working but I dont like the fact its giving 2 warning everytime i compile the code. Here is the code:

#include <time.h>
#include<stdio.h>
int main(int argc, char *argv[]){

    int randomNumber = 0;
    for (int index = 1; index < argc; index++) {
        printf("The %d is %s\n", index, argv[index]);
    }
    getchar();

    srand(time(NULL));
    randomNumber = (rand()%11)-5;
    return randomNumber;

}

And these are the warnings:

warning C4013: 'srand' undefined; assuming extern returning int
warning C4013: 'rand' undefined; assuming extern returning int

Why are these warnings showing up?I'm afraid they might cause some problem later so I would like to eliminate those warnings.

Edits:

Added another library:

#include <stdlib.h>

2 warnings gone, 1 new one:

warning C4244: 'function' : conversion from 'time_t' to 'unsigned int', possible loss of data

New warning solved by change in the srand call to:

srand((unsigned int)time(NULL));

This is the final and solved program:

#include <time.h>
#include<stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){

    int randomNumber = 0;
    for (int index = 1; index < argc; index++) {
        printf("The %d is %s\n", index, argv[index]);
    }
    getchar();


    srand((unsigned int)time(NULL));
    randomNumber = (rand()%11)-5;
    return randomNumber;
}

SOLVED,showed the edits step by step for future reference to other users. Thanks

Carlos Siestrup
  • 1,031
  • 2
  • 13
  • 33
  • 1
    You forget to `#include ` – David Ranieri Oct 13 '15 at 18:39
  • How about `#include ` or ``#include `? – lit Oct 13 '15 at 18:40
  • Included,the 2 previous ones are gone but now there is another one: `warning C4244: 'function' : conversion from 'time_t' to 'unsigned int', possible loss of data` – Carlos Siestrup Oct 13 '15 at 18:48
  • `return randomNumber;` from `main` doesn't seems a good idea, take a look to [What should main() return in C and C++?](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) – David Ranieri Oct 13 '15 at 19:04
  • @AlterMann My program is supposed to run another program that will return different numbers depending of erro kind. This code is only meant to simulate erro occurence of the program i'm supposed to run in the future(which is not done), that why a random number. – Carlos Siestrup Oct 13 '15 at 19:50

2 Answers2

1

You need to include stdlib.h. That is where those functions are defined.

RyGuyinCA
  • 226
  • 1
  • 6
0

You're getting this warning because the functions rand and srand haven't been defined.

From the man page:

NAME rand, rand_r, srand - pseudo-random number generator

SYNOPSIS

   #include <stdlib.h>

   int rand(void);

   int rand_r(unsigned int *seedp);

   void srand(unsigned int seed);

By including stdlib.h, you get the prototypes for these functions, and the compiler can ensure you're calling them correctly.

dbush
  • 205,898
  • 23
  • 218
  • 273