1
#define RAND_MAX 10
#include<stdio.h>
int main()
{
  double x;
  x=randRange(-1.0,1.0);
  printf("x value is %f::",x);
  return 0;
}

double randRange(double min, double max)
{
  return rand() * (max - min) / RAND_MAX + min;
}

Error::The snippet below is the error being generated--

$gcc main.c -o demo -lm -pthread -lgmp -lreadline 2>&1
main.c:11:8: error: conflicting types for 'randRange'
double randRange(double min, double max) {
^
main.c:6:5: note: previous implicit declaration of 'randRange' was here
x=randRange(-1.0,1.0);
^

Error in conflicting types??I have checked return types.

4 Answers4

5

You need to declare the function above the point where it is called.

double randRange(double min, double max);

int main()
{
    double x;
    x=randRange(-1.0,1.0);
    ...

double randRange(double min, double max)
{

If you don't, ansi C gives you an implicit declaration for a function returning int

simonc
  • 41,632
  • 12
  • 85
  • 103
1

It's because you use randRange before it's defined. Either move the definition to be above where you use it, or create a prototype (declaration) before.

Functions which are not declared when you call them default to return int. This default declaration is what is meant by the "implicit declaration" in the error message.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Your implicit declaration of randRange declares it as int (*)(float, float).

Always pre-declare functions to avoid this kind of errors. Just put

double randRange(double min, double max);

above your main.

Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • I'm not so sure about `int (*)(float, float)`. I rather think it is `int (*)()`, having no specified argument types. – glglgl Oct 07 '13 at 16:01
  • @glglgl Nah, I tend to get compiler warnings when I do implicit declarations with wrongly typed pointers. e.g. passing a `struct a *` where a `void *` is expected in an implicit declaration. – Sergey L. Oct 07 '13 at 16:14
0

You must be using an "old" C compiler (or running a modern compiler in "old" C89/90 mode). In a compliant modern C compiler (C99 and later) your code would typically fail to compile even earlier, at the point of the first call to randRange, since you are trying to use randRange function before it is declared. This is illegal in modern C.

In old C (C89/90) calling an undeclared function is legal. An attempt to call randRange as randRange(-1.0,1.0) makes the compile to implicitly conclude that it must be a function that has two double parameters and returns int. If your function is actually defined differently, then the behavior of your program is undefined.

This is exactly what happened in your case. Except that your compiler decided to "go an extra mile" and reported an error for a function with this sort of implicit declaration conflict (instead of quietly generating a program whose behavior is undefined).

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765