# Thread: C programming 8 decimal places

1. ## C programming 8 decimal places

Hello all, this is my first ever forum post so apologies in advance for any mistakes I make.

I was asked to create a program which would calculate a circle's circumference and area given the user enters the radius. One specification is to declare pi to 8 decimal places. I understand this cannot be achieved using the float data type but I cannot get double to work, so have used float as a temporary solution. My code is:

Code:
```#include <stdio.h>

int main()
{
int r;
float pi = 3.14159265;
printf("\nEnter the cirle's radius to calculate its area and circumfrence\n\n");
scanf("%i", &r);
printf("\nThe circle with a radius of %i has an area of %f and a circumference of %f\n\n", r, pi*r*r, 2*pi*r);
return 0;
}```

2. You didn't really make any mistakes, but your post is lacking information for us to help you.

Do you have an actual question? What specifically didn't work about the double? Did it compile? If not, tell us what compiler you're using, and copy-paste the exact error messages with line number. Did it not run or crash? How far did it get before it stopped? Did it run but produce incorrect output? What input did you give, what output did you expect, and what output did you actually get?

Also, why is the radius an int and not a float or double?

3. All you need to do in that code is change the keyword "float"
Code:
`float pi = 3.14159265;`
to "double", which will turn that line into
Code:
`double pi = 3.14159265;`

Lemme guess, you tried something like
Code:
`    double float pi = 3.14159265;`
If you feel silly, learn from it.

4. Originally Posted by anduril462
You didn't really make any mistakes, but your post is lacking information for us to help you.

Do you have an actual question? What specifically didn't work about the double? Did it compile? If not, tell us what compiler you're using, and copy-paste the exact error messages with line number. Did it not run or crash? How far did it get before it stopped? Did it run but produce incorrect output? What input did you give, what output did you expect, and what output did you actually get?

Also, why is the radius an int and not a float or double?
To clarify - when I use the double function instead, the value for pi is still printed at 6 decimal places. The area and circumference values change however. Does this mean when I use double, the calculation uses pi to 8 decimal places but the printf function limits the visual representation of pi to 6 decimal places?

(From the brief I assumed the radius value entered would just be an integer - I know I could easily change it to float to enable input of decimal numbers.)

5. ## Compiler

I use the gcc compile command in the Ubuntu terminal.

6. Try
Code:
`printf("%.9f", pi);`

7. You should note that floats have only 6 digits of precision anyway, so printing 9 digits won't make it any more accurate.

A double can hold about 15 digits of precision, so you can print something more meaningful with a larger format precision.

8. Originally Posted by Salem
You should note that floats have only 6 digits of precision anyway, so printing 9 digits won't make it any more accurate.

A double can hold about 15 digits of precision, so you can print something more meaningful with a larger format precision.
I assumed (never a good idea) that he'd switched to double, since grumpy already mentioned that. But now that I look at the picture of his code, I see he's still using a float.