JDK
JDK

Reputation: 45

Computing surface area and volume of a cylinder - C

I'm writing a function that doesn't return any data. The program computes area and volume of a cylinder. The formulas that i'm trying to use are surfacearea = 2π ∗ (radius)^2 + height ∗ (2π ∗ radius) and volume = π ∗ (radius)^2 ∗ height I'm having trouble setting up the equations. I have this:

surface_area_calc = (2 * PI) ∗ (pow (radius,2)) + height ∗ ((2 * PI) ∗ 
radius);
volume_calc = PI ∗ (pow (radius,2)) ∗ height;

but i'm sure it's wrong because i'm getting errors when i try to compile the program. the error i'm getting is saying that 'non-ASCII characters are not allowed outside of literals and identifiers'.

Upvotes: 0

Views: 753

Answers (3)

alk
alk

Reputation: 70971

You have two choices instead of the wrong

#define PI = 3.14159265358979323846

Either stick to using a pre-processor macro and do

#define PI 3.14159265358979323846

or use a const qualified C variable by doing

const double PI = 3.14159265358979323846;

As the code uses the library function pow() the code ought to provide a prototype for this function to the compiler.

This can simply be achieved by including the appropriate library header. In this case do:

#include <math.h>

To when compilation is done tell the linker to actually link the library implementing the stuff from math.h (which often resides in libm.*) use (for GCC) the option

-lm

(more here)

Note that when steering the linker via the compiler the libraries need to be specified after the source files making use of what the libraries provide.

gcc main.c -o main -lm

The following most likely will fail

gcc -lm main.c -o main

Upvotes: 2

pmg
pmg

Reputation: 108978

Don't put the = in the macro

#define PI 3.14159...

With the = it gets copied to the code

volume_calc = PI ∗ (pow (radius,2)) ∗ height;

becomes

volume_calc = = 3.14159 ∗ (pow (radius,2)) ∗ height;
//            ^^^^^^^^^

which is a syntax error.

Upvotes: 3

gsamaras
gsamaras

Reputation: 73384

Are you in Linux? Is so, do:

#include <math.h>

at the top of your code and compile with -lm, like this:

gcc test.c -o test -lm

Upvotes: 1

Related Questions