0

Problem:

When I try to compile the program I get 3 error statements, 2 of them are relating to using the pow math function to try and square the radius variable. The last error statement states that i'm missing a semi-colon on line 11 but I don't see where. I apologize for these newbie questions, hope you can help thanks.

What I've tried:

I've tried just doing "radius * radius" to get radius^2 rather than using pow(radius, 2,0); and it works but I still get the "line 11 expected ; " error. Also for my assignment i'm required to use the pow function so its a necessity that I get that working.

My Code

#include <stdio.h> //Including standard library

double calculatingFunction(diameter) {
    const double pi = 3.14159;      //Declaring pi as a constant
    double radius = diameter * 0.5;     //Calculating radius
    double circumference = 2 * pi * radius; //Calculating circumference
    double area = pi * (pow(radius, 2.0));  //Calculating area

    return (circumference, area);   //returning values of circumference of area
}

//Beggning of main function
int main(void) {

    //Prompting User for Input
    printf("Please enter a value for the diameter of a circle"); 

    double diameter; //Variable to store User's input
    scanf("%lf", &diameter); //Scanning for User's input

    //Final Output listing the calculated perimeter and area of circle
    printf("\nPerimeter of Circle: %lf Area of Circle: %lf", 
    calculatingFunction(diameter));


    return 0; //End of Program

}

Errors:

homework.c: In function ‘calculatingFunction’:
homework.c:8: warning: implicit declaration of function ‘pow’
homework.c:8: warning: incompatible implicit declaration of built-in 
function ‘pow’
homework.c:11: error: expected ‘;’ before ‘of’

Programs purpose:

I've left comments on my code so it should be pretty clear what output is expected. The program is basically taking a user's input for the diameter of a circle and calculating the area and perimeter from it.

dan1st
  • 12,568
  • 8
  • 34
  • 67
  • The return statement is incorrect – stark Oct 15 '19 at 15:12
  • Possible duplicate of [Using pow() function throws undefined reference error in C](https://stackoverflow.com/questions/4174080/using-pow-function-throws-undefined-reference-error-in-c) – gstukelj Oct 15 '19 at 15:12
  • What's that `area` after the `return` in `calculatingFunction`? Also you might want to use a `struct` to return two values. – gstukelj Oct 15 '19 at 15:15
  • I had a similar problem and you need to do `gcc file.c -lm -o file.out`. For some unknown reason, you need to include -lm when compiling programs with `math.h` –  Oct 15 '19 at 18:02

2 Answers2

1

Firstly the error on line 11 is the word area written without indicating that it is a comment. Secondly pow is defined inside <math.h>. Including this will most likely resolve the issue, otherwise see this SO post.

Lastly, the main problem is in the return statement. In C you have to create a struct to return multiple values, or use pointer arguments.

typedef struct {
    double circumference, area;
} data;

data calculatingFunction(double diameter) {
    ...
    data ret = {circumference, area};
    return ret; //returning values of circumference of area
}

Also declare diameter to double or else the compiler will usually assume that the parameter is int. double seems more appropriate for this parameter.

winidis
  • 140
  • 1
  • 7
  • 1
    In addition, the final `printf()` in `main()` is dodgy. It is passing the whole `data` type value returned by `calculatingFunction()` as a parameter to `printf()`, instead of passing the members individually! – Ian Abbott Oct 15 '19 at 17:07
0

pow() is declared in <math.h>. Did you include this in your headers?

(Also, your comment on line 9-10 is running onto a second line, escaping the commented-out part, and causing a compiler error.)

Nick Reed
  • 4,989
  • 4
  • 17
  • 37