I am trying to generate some numbers with the rand() function like below:
int main()
{
int i=0
for(i=0;i<20;i++)
{
printf("%f\n",rand_md());
}
return 0;
}
float rand_md()
{
return (float)rand()/(RAND_MAX+1.0);
}
But when I run gcc rand.c, I get the error like below:
rand.c: In function ‘main’:
rand.c:21: warning: format ‘%f’ expects type ‘double’, but argument 2 has type ‘int’
rand.c: At top level:
rand.c:36: error: conflicting types for ‘rand_md’
rand.c:21: error: previous implicit declaration of ‘rand_md’ was here
What’s wrong with my code?
Best Regards,
When you use a function without declaring it first, C will assume it returns an
int. To fix this, you have to declare the method signature forrand_mdbefore you call it inmain().Add something like this before
mainor in a header file: