Instead of hard coding the magic value 3.14 use a constant. On my host math.h defines M_PI
if __USE_OPEN
, __USE_MISC
or __USE_GNU
are defined. If your math.h doesn't you can define it like this:
#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif
The entry point for a hosted c program is the function int main()
(or int main(int, char *[])
. This is your main problem.
area()
takes a float r argument, but you populate that variable in in the function itself. Either leave out the argument or as shown here let caller do the I/O and pass in the argument and return the result.
Always check the return value of scanf()
otherwise you may be operating on uninitialized values.
Prefer trailing to leading \n
as the output stream might be line buffered.
#define __USE_XOPEN
#include <math.h>
#include <stdio.h>
float area(float r) {
return M_PI * r * r;
}
int main() {
printf("Enter the radius of Circle : ");
float r;
if(scanf("%f", &r) != 1) {
printf("scanf failed\n");
return 1;
}
printf("Area of Circle : %f\n", area(r));
}