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