C Math

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
<math.h>

What You’ll Learn

C programs use the standard math library for roots, powers, trigonometry, logarithms, and rounding. This guide teaches how to include <math.h>, link correctly, work with double values, and call essential functions before diving into the full tutorial series.

01

math.h

Standard header.

02

Link -lm

GCC/Clang tip.

03

double

Default type.

04

Radians

Trig angles.

05

Roots & Pow

sqrt, pow.

06

30 Functions

Full index.

Definition and Usage

C does not have a built-in x^2 operator or a Math class like Java. Instead, you call library functions declared in <math.h>sqrt(x), pow(x, y), sin(angle), and many more. Most take and return double for floating-point precision.

Integer math uses normal operators (+, -, *, /, %). When you need square roots, trigonometry, or careful rounding of decimals, switch to math.h functions. Integer absolute value abs() is in <stdlib.h> instead.

💡
Beginner Tip

Always compile with -lm on GCC/Clang when using math functions: gcc main.c -std=c11 -o main -lm. Circular trig (sin, cos, tan) expects radians—multiply degrees by M_PI / 180.0 first.

📝 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 main.c -std=c11 -o main -lm */

Common Headers

  • <math.h> — floating-point math: sqrt, sin, floor, fabs, etc.
  • <stdlib.h> — integer abs(), labs(), llabs().
  • _USE_MATH_DEFINES — define before <math.h> on MSVC for M_PI.

Type Variants

sqrt(x)    /* double in, double out */
sqrtf(x)   /* float in, float out */
sqrtl(x)   /* long double in, long double out */

⚡ Quick Reference

Square root
sqrt(25.0)

Returns 5.0

Power
pow(2.0, 8.0)

Returns 256.0

Degrees → rad
deg * M_PI / 180.0

Before sin/cos

Compile
gcc -std=c11 -lm

Link math lib

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. These five programs cover setup, absolute value, trigonometry, powers, and rounding.

📚 Getting Started

Include math.h and call your first function.

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;
}

How It Works

From circle area A = πr², solve r = sqrt(A / π). sqrt returns the positive square root as a double.

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;
}

How It Works

Both remove the sign. abs is for integers (stdlib.h); fabs is for double (math.h). Do not mix them on the wrong type.

📈 Practical Patterns

Trigonometry, powers, and rounding in real programs.

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;
}

How It Works

sin(45) without conversion would be wrong—that is 45 radians, not degrees. Always convert: rad = deg * M_PI / 180.0.

Example 4 — pow() and sqrt() Together

Compute hypotenuse length with pow and sqrt, or use 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;
}

How It Works

Classic 3-4-5 triangle. For real code prefer hypot(a, b)—it avoids overflow when sides are huge. See the hypot() tutorial in the function index.

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

Rounding functions behave differently—pick the one that matches your rule.

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;
}

How It Works

On negatives, floor and trunc diverge. round picks the nearest whole number (halfway cases tie away from zero). Choose based on your business logic.

🚀 Common Use Cases

  • Graphics and games — angles with sin/cos, distances with sqrt or hypot.
  • Science and engineering — exponentials with exp, logarithms with log/log10.
  • Finance — compound interest with pow, rounding with floor/round.
  • Signal processing — hyperbolic functions sinh, cosh, tanh.
  • Grid and UI layout — snap coordinates with floor, ceil, or trunc.
  • Error checking — compare floats with tolerance using fabs(a - b) < epsilon.

🧠 How C Math Works

1

Include math.h

Add #include <math.h> and link with -lm on GCC/Clang.

Setup
2

Pass double values

Most functions take double; integers promote automatically in calls.

Input
3

Library computes result

Platform math library handles precision, edge cases, and special values.

Compute
=

Use the return value

Assign to a variable, print with printf, or pass to the next calculation.

📝 Notes

  • Link with -lm on GCC/Clang when using math.h functions.
  • Trig functions use radians, not degrees—convert explicitly.
  • abs() is for integers; fabs() is for double.
  • Floating-point results may have tiny rounding errors—avoid == for exact equality.
  • pow(0, 0) and domain errors may return NaN or trigger errno—check docs per function.
  • Hyperbolic functions (sinh, tanh) are not the same as circular trig despite similar names.

Conclusion

C math through <math.h> gives you roots, powers, trigonometry, logarithms, rounding, and more. Remember -lm, radians for trig, and the difference between integer abs and floating fabs.

Practice the examples on this page, then explore the full Math Functions index for in-depth tutorials on all 30 functions in our series.

💡 Best Practices

✅ Do

  • Compile with gcc ... -lm on GCC/Clang
  • Convert degrees to radians before sin/cos/tan
  • Use fabs(a - b) < epsilon to compare floats
  • Pick floor, ceil, round, or trunc intentionally
  • Use hypot(x, y) instead of sqrt(x*x + y*y) for long sides

❌ Don’t

  • Pass degrees directly to sin() without conversion
  • Use abs() on double values (use fabs)
  • Compare floats with == after math operations
  • Forget -lm and wonder why linking fails
  • Confuse tan() with tanh()

Key Takeaways

Knowledge Unlocked

Five things to remember about C math

Use these points as you write your first C programs.

5
Core concepts
📝 02

Link -lm

GCC/Clang.

Compile
📈 03

Radians

Not degrees.

Trig
🛠 04

fabs

Not abs.

Types
📚 05

30 Functions

Full index.

Next step

❓ 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.
Did you know?

C has no exponentiation operator—you cannot write 2^10 in standard C. Use pow(2.0, 10.0) from <math.h> instead, or bit shifts for powers of two: 1 << 10 equals 1024 for integers.

Explore All Math Function Tutorials

Browse 30 beginner-friendly guides with syntax, five examples each, 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.

6 people found this page helpful