C Program to Find the Area of a Circle Given the Radius

The area of a circle is A = π r² and the circumference is C = 2πr, where r is the radius. This program computes both from a user-supplied radius. Key points: use radius * radius instead of pow(radius, 2) (no need for the math library for a simple square), define π as a high-precision constant with #define, and validate the input (radius cannot be negative). The original post used conio.h, void main, clrscr(), and #define PI 3.142 (only 4 significant figures); this rewrite fixes all of those.

Radius Area (π r²) Circumference (2πr)
1 3.1416 6.2832
5 78.5398 31.4159
7 153.9380 43.9823
10 314.1593 62.8319

C Program: Area and Circumference of a Circle

/* Find the area and circumference of a circle given the radius
 * Compile: gcc -ansi -Wall -Wextra circle.c -o circle -lm */
#include <stdio.h>
#include <math.h>

#define PI 3.14159265358979323846

int main(void)
{
    double radius, area, circumference;

    printf("Enter the radius: ");
    if (scanf("%lf", &radius) != 1 || radius < 0.0) {
        printf("Enter a non-negative radius.\n");
        return 1;
    }

    area          = PI * radius * radius;
    circumference = 2.0 * PI * radius;

    printf("Radius        = %.4f\n", radius);
    printf("Area          = %.4f\n", area);
    printf("Circumference = %.4f\n", circumference);

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra circle.c -o circle -lm
./circle

Note: -lm is not strictly needed here since we do not call any math functions, but it is included for completeness when the header is present. If the linker complains, remove -lm.

Sample Output

Enter the radius: 5
Radius        = 5.0000
Area          = 78.5398
Circumference = 31.4159

Enter the radius: 7
Radius        = 7.0000
Area          = 153.9380
Circumference = 43.9823

Enter the radius: 0
Radius        = 0.0000
Area          = 0.0000
Circumference = 0.0000

Code Explanation

  • PI as a #define constant — using a named constant instead of the literal 3.14159 everywhere means you change it in one place if you want more or less precision. The value here has 20 digits — well beyond what double can represent (about 15–17 significant decimal digits), so this is the most precise value possible in C’s double.
  • radius * radius instead of pow(radius, 2)pow() is a general-purpose floating-point function that handles arbitrary real exponents. For a simple integer exponent like 2, multiplying directly is both faster and avoids linking the math library just for this purpose. Use pow() only when the exponent is non-integer or very large.
  • Input validation: radius < 0.0 — a negative radius has no geometric meaning. Checking after reading prevents computing a negative area, which would silently produce a wrong result. Zero radius is valid (degenerate circle) and outputs zeros correctly.
  • double instead of floatfloat has only about 6–7 significant digits of precision. For geometric calculations where rounding errors accumulate, double (15–17 digits) is the better default. The original post used float with %f; this rewrite uses double with %lf for scanf and %f/%.4f for printf.

What This Program Teaches

  • Named constants with #define — always name magic numbers. #define PI 3.14159265358979323846 is clearer than scattering 3.14 throughout the code and avoids subtle bugs where you type 3.1415 in one place and 3.14159 in another.
  • Choosing the right arithmetic — prefer simple multiplication over library functions when both are correct. r * r for squaring is idiomatic C. Reserve pow() for cases where the multiplication chain would be unwieldy (e.g., pow(x, 17)) or when the exponent is fractional (pow(x, 0.5) = √x).
  • double vs float — in modern C, double is the default floating-point type. float is used when memory is genuinely scarce (embedded systems, large arrays). For everyday scalar arithmetic, always use double.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>