-2
float pie = 3.14;


int main()
{   
 float r,res;
 printf("Enter radius value\n");
 scanf("%f", &r);

  res=area( r );
  printf("Area = %f\n",res);
 //printf("Circumference =  %f\n", circum( r ));
}

float area( float r )
{
 return pie * r * r;
}

I'm getting error as conflicting types for 'area' and previous implicit declaration of 'area' was here,But this code works fine if i changed to integer

In IDE it works fine if i changed that code to integer float pie = 3.14;

    int main() {
    int r,res; 
    printf("Enter radius value\n"); 
    scanf("%d", &r); res=area( r ); 
    printf("Area = %d\n",res); 
     } 
    int area( int r )
     { 

return pie * r * r;
 }

without declaration it works fine for integer but for floating point numbers i am getting error why?

Poornima
  • 1
  • 3
  • 3
    In the absence of a forward declaration, the return type of a function is implicitly taken to be `int`. But it's bad practice to rely on that. Just add a proper forward declaration *before* the function that calls it: `float area(float r);` – Tom Karzes Jan 18 '18 at 13:08
  • See the linked question. The problem is that you have defined your function `area()` after main and provided no other declaration before main. Just move the whole function before main and it should work. – Candy Gumdrop Jan 18 '18 at 13:10
  • 2
    BTW: it's `PI` not `PIE`. A pie is something the serve you in English pubs. – Jabberwocky Jan 18 '18 at 13:12
  • @MichaelWalz but PI != 3.14 ;) – Alfie J. Palmer Jan 18 '18 at 13:16
  • @AlfieJ.Palmer yep, but it's still better than `22./7.` which I've seen in some code recently ;-) – Jabberwocky Jan 18 '18 at 13:18
  • In addition, you shouldn't be using a dinosaur compiler. Use a standard C compiler instead. – Lundin Jan 18 '18 at 13:48

3 Answers3

1

C allows implicit function declaration, so because you didn't give an explicit function declaration prior to calling it for the first time, it's implicitly declared with this prototype:

int area(); // Implicit arguments

And later when you give a real declaration, they're conflict.

If you want the function to return an actual floating point number, give a forward declaration:

float area(float);

Add the above line before int main() and the problam is solved.

By the way, implicit function declaration is removed as of C11 standard (ISO/IEC 9899:2011).

iBug
  • 35,554
  • 7
  • 89
  • 134
  • In IDE it works fine if i changed that code to integer float pie = 3.14; int main() { int r,res; printf("Enter radius value\n"); scanf("%d", &r); res=area( r ); printf("Area = %d\n",res); //printf("Circumference = %f\n", circum( r )); } int area( int r ) { return pie * r * r; } without declaration it works fine for integer but for floating point numbers i am getting error why? – Poornima Jan 18 '18 at 13:24
  • @Poornima Solution added. – iBug Jan 18 '18 at 13:25
0

Whenever you are using a common term or a variable all over the program like you did here

float pie = 3.14

write this instead

#define pie 3.14 //this is a macro

and coming to your error , whenever you are using a function, you have to provide a explicit declaration of it or in other words prototype of it. Type this on top of your C file

float area(float r) 

or

float area(float)

And if you feel lazy then use your function before your main function instead using it after you main function.

-1

You should do function call after function defination.Otherwise declar function in header file and use it.

Rohit
  • 142
  • 8