C Math

Beginner
11 min read
Updated: Nov 2026
5 examples
<math.h>

Definition and Usage

C has no built-in x^2 operator or Math class like some languages. For roots, powers, trigonometry, logs, and rounding you call library functions from <math.h>—for example sqrt(x), pow(x, y), and sin(angle).

Operators vs math.h

Everyday arithmetic uses operators: +, -, *, /, %. When you need square roots, angles, or careful decimal rounding, switch to math.h. Integer absolute value abs() lives in <stdlib.h> instead.

What should beginners learn first?

Include <math.h>, compile with -lm on GCC/Clang, then practice fabs(), sqrt(), pow(), and sin() with radian conversion. Those cover absolute value, roots, powers, and basic trigonometry.

This lesson vs the Functions index

This page is the C math lesson: setup, radians, examples, and pitfalls. The Math Functions index is only a catalog—search and open individual guides such as hypot(). Learn here first, then browse the index.

Link with -lm.

On GCC/Clang: gcc main.c -std=c11 -o main -lm. Without -lm you often see undefined reference to sqrt.

Trig uses radians.

sin, cos, and tan do not take degrees. Convert first: rad = deg * M_PI / 180.0.

Default type is double.

Most math.h functions take and return double. Use f / l suffixes for float / long double variants.

C Syntax

Include the header, call functions, and link the math library:

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double x = 16.0;
    double root = sqrt(x);
    double power = pow(2.0, 10.0);

    printf("sqrt(16) = %.0f\n", root);
    printf("2^10     = %.0f\n", power);

    return 0;
}
/* gcc math_basics.c -std=c11 -o math_basics -lm */

Core C Concepts

Ideas every C math tutorial builds on:

ConceptExampleNotes
Header#include <math.h>Declares floating-point math APIs
Linkgcc … -lmRequired on many GCC/Clang setups
Default typedoubleMost functions take/return double
Radiansdeg * M_PI / 180.0Before sin / cos / tan
Integer abs#include <stdlib.h>abs() is not in math.h
Variantssqrtf / sqrtlfloat / long double forms

Type variants

CallTypeUse when
sqrt(x)doubleDefault choice in most tutorials
sqrtf(x)floatYou store values as float
sqrtl(x)long doubleYou need extra precision

When to use which function

JobPrefer
Distance / radiussqrt() or hypot()
Powers / interestpow(), exp()
Anglessin(), cos() after degree→radian
Snap / gridsfloor(), ceil(), trunc()
Float compare tolerancefabs(a - b) < epsilon

How to Use Math in C

A practical workflow for this tutorial track:

  1. Add #include <math.h> (and <stdlib.h> if you need integer abs).
  2. Compile with -lm on GCC/Clang; confirm the program links cleanly.
  3. Store floating results in double unless you have a reason not to.
  4. Convert degrees to radians before calling circular trig functions.
  5. Print with printf format specifiers such as %.2f or %g.
  6. Open the functions index when you need a specific API guide.

Live Preview

Enter a non-negative number to preview sqrt(x)—the same idea as calling sqrt() after including <math.h>.

Negative inputs are invalid for real square roots (domain error in C).

Preview
sqrt(16.000000) = 4.000000

Compile tip: gcc file.c -std=c11 -o out -lm

C Examples

Five starter patterns from a C tutorial. Compile with gcc file.c -std=c11 -o out -lm.

Example 1 — Include math.h and call sqrt()

The smallest working program that uses the math library.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double area = 78.54;
    double radius = sqrt(area / M_PI);

    printf("Area: %.2f\n", area);
    printf("Radius: %.2f\n", radius);

    return 0;
}
sqrt() Tutorial

How It Works

  1. #include <math.h> declares sqrt and M_PI.
  2. Circle formula: from A = πr², solve r = sqrt(A / π).
  3. sqrt returns the positive root as a double.
  4. Link with -lm so the program builds on GCC/Clang.

Example 2 — abs() vs fabs()

Integer and floating-point absolute value use different headers.

C
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int n = -42;
    double x = -3.14159;

    printf("abs(%d)     = %d\n", n, abs(n));
    printf("fabs(%g) = %g\n", x, fabs(x));

    return 0;
}
fabs() Tutorial

How It Works

  1. abs is for integers (stdlib.h).
  2. fabs is for double (math.h).
  3. Both remove the sign; pick by type.
  4. Do not call abs on a floating value and expect fabs semantics.

Example 3 — Sine with radians (45 degrees)

Convert degrees to radians before calling sin().

C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>

int main(void) {
    double degrees = 45.0;
    double radians = degrees * M_PI / 180.0;
    double result = sin(radians);

    printf("sin(%.0f degrees) = %.6f\n", degrees, result);

    return 0;
}
sin() Tutorial

How It Works

  1. _USE_MATH_DEFINES helps expose M_PI on MSVC.
  2. Convert with deg * M_PI / 180.0.
  3. sin(radians) returns the sine as a double.
  4. sin(45) without conversion would treat 45 as radians—wrong for degrees.

Example 4 — pow() and sqrt() together

Compute hypotenuse length; prefer hypot() in production.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double a = 3.0;
    double b = 4.0;
    double c = sqrt(pow(a, 2.0) + pow(b, 2.0));

    printf("Sides: %.0f, %.0f\n", a, b);
    printf("Hypotenuse: %.0f\n", c);

    return 0;
}
pow() Tutorial

How It Works

  1. pow(a, 2.0) squares each side.
  2. sqrt(...) finishes the Pythagorean theorem.
  3. 3-4-5 is the classic right triangle check.
  4. hypot(a, b) is safer against overflow for large sides.

Example 5 — floor() vs trunc() on negatives

Rounding functions diverge on negative values—pick the rule you mean.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double x = -2.7;

    printf("x = %.1f\n", x);
    printf("floor(x) = %.1f  (toward -infinity)\n", floor(x));
    printf("trunc(x) = %.1f  (toward zero)\n", trunc(x));
    printf("round(x) = %.1f  (nearest integer)\n", round(x));

    return 0;
}
floor() Tutorial

How It Works

  1. floor moves toward −infinity.
  2. trunc chops toward zero.
  3. round picks the nearest whole number.
  4. Test negatives when choosing a rounding rule for your program.

Where to Go Next

This page teaches how math works in C (math.h, -lm, radians, examples). The full searchable catalog of every function tutorial lives on the Math Functions index—start with these six:

FunctionDescriptionTutorial
fabs()Floating absolute value.Try it
sqrt()Square root of x.Try it
pow()x raised to the power y.Try it
sin()Sine of angle in radians.Try it
floor()Round toward −infinity.Try it
hypot()Safe hypotenuse length.Try it

Need ceil, log, atan2, hyperbolic functions, or the rest? Open the complete Math Functions index.

Edge Cases & Pitfalls

Most C math bugs come from missing -lm, degrees vs radians, or the wrong absolute-value function.

-lm

undefined reference to sqrt

Add -lm at the end of your GCC/Clang link line. Including the header is not enough.

rad

Passing degrees to sin()

sin(45) is 45 radians. Convert with deg * M_PI / 180.0 first.

abs

abs vs fabs mix-up

Use abs for int and fabs for double. Wrong choice causes warnings or wrong results.

√

sqrt of a negative

Real sqrt has a domain error for negatives. Check the sign or use complex math intentionally.

rnd

floor vs trunc on negatives

For -2.7, floor gives -3 and trunc gives -2. Pick the rule your logic needs.

≈

Comparing doubles with ==

Prefer a tolerance: fabs(a - b) < 1e-9 (or your domain epsilon).

Platform Support

<math.h> is part of standard C. Linking details differ by toolchain.

ToolchainSupportNotes
GCC (Linux / MinGW)FullUsually needs -lm
Clang (macOS / Linux)FullUsually needs -lm
MSVCFullMath often links automatically; define _USE_MATH_DEFINES for M_PI
Embedded / freestandingVariesMay need a vendor math library or soft-float support

Core functions such as sqrt, pow, sin, fabs, and floor are portable C99/C11 APIs. Some constants (like M_PI) are common extensions—guard them or define your own PI when portability matters.

Key Takeaways

  • math.h provides floating-point math; link with -lm on GCC/Clang.
  • Start with fabs, sqrt, pow, and sin (radians).
  • Default type is double; use f/l variants when needed.

One line: C math is math.h + -lm—use double, convert degrees to radians, then open the functions index for details.

Frequently Asked Questions

math.h is the standard header for floating-point math: sqrt, pow, sin, cos, log, floor, fabs, and dozens more. Include it with #include <math.h> and link with -lm on GCC/Clang: gcc program.c -o program -lm.
On GCC and Clang, math functions live in libm (the math library), separate from libc. Without -lm you get linker errors like undefined reference to sqrt. MSVC often links math automatically.
Radians. Convert degrees first: double rad = degrees * (M_PI / 180.0); then sin(rad). On MSVC define _USE_MATH_DEFINES before math.h to get M_PI.
abs() works on integers and is declared in stdlib.h. fabs() works on double floating-point values and is in math.h. Use the one matching your variable type.
Most math.h functions take and return double. Float variants end in f (sqrtf, sinf); long double variants end in l (sqrtl, sinl). Match the variant to your variable type for clarity.
Learn math.h setup and -lm on this page, then open the Math Functions index for fabs(), sqrt(), pow(), and sin(). Those four cover absolute value, roots, powers, and basic trigonometry.

Explore every math function

Open the function index for searchable guides with syntax, examples, and FAQs.

Math Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful