Including math.h
Link and use the math library.
Including math.h is a free C Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why math.h Exists
C keeps its core language small. Operations like square roots, powers, and trigonometry are not built into the language itself.
Instead they live in a standard library header called math.h. To use these functions you must include the header at the top of your program.
Including the Header
Add this line near the top of your file, alongside stdio.h:
#include <stdio.h>for input/output#include <math.h>for math functions
Angle brackets tell the compiler to look in the standard system folders.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("math.h is ready\n");
return 0;
}Your First math.h Call
Once math.h is included, you can call functions like sqrt (square root).
Most math functions take and return double values, so we print results with the %f format specifier.
#include <stdio.h>
#include <math.h>
int main(void) {
double r = sqrt(16.0);
printf("sqrt(16) = %f\n", r);
return 0;
}Linking the Math Library
On some systems (especially Linux with gcc), math functions live in a separate library file.
If you get an undefined reference error, add the -lm flag when compiling:
gcc program.c -lm
The -lm means "link the math library".
Everything Is double
By default, math.h functions work with the double type. They accept double arguments and return double results.
You can pass an int too. C automatically converts it to double before the call.
#include <stdio.h>
#include <math.h>
int main(void) {
int n = 25;
double root = sqrt(n);
printf("root = %f\n", root);
return 0;
}Printing math Results
Use %f to print a double. You can control the number of decimal places with %.2f (two decimals).
This keeps long decimal values tidy and readable.
#include <stdio.h>
#include <math.h>
int main(void) {
double r = sqrt(2.0);
printf("%f\n", r);
printf("%.2f\n", r);
return 0;
}Handy Constants
Many systems define useful constants in math.h, such as M_PI for pi.
These are not part of the strict standard, but they are very common. If M_PI is missing, you can define your own.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("pi is about %.5f\n", M_PI);
return 0;
}A Tour of What's Inside
The math.h header gives you many tools, grouped by purpose:
- Powers/roots:
pow,sqrt - Trig:
sin,cos,tan - Logs:
log,log10 - Rounding:
floor,ceil,round
We will explore each group in the next lessons.
Mixing math with Logic
Results from math.h are normal numbers you can compare and use in if statements.
Here we check whether a number is a perfect square by comparing its root against a whole number.
#include <stdio.h>
#include <math.h>
int main(void) {
double root = sqrt(49.0);
if (root == 7.0) {
printf("49 is a perfect square\n");
}
return 0;
}Common Mistakes
Watch out for these beginner errors:
- Forgetting
#include <math.h> - Forgetting
-lmwhen linking on Linux - Printing a
doublewith%dinstead of%f
Each of these produces a confusing error or wrong output.
Putting It Together
Here is a small complete program using two math functions together.
Notice the includes at the top and the double variables holding the results.
#include <stdio.h>
#include <math.h>
int main(void) {
double a = sqrt(81.0);
double b = pow(2.0, 5.0);
printf("sqrt=%.1f pow=%.1f\n", a, b);
return 0;
}Quick Check
Which header must you include to use functions like sqrt and pow?
Recap
You learned that math functions live in math.h, which you include with #include <math.h>.
These functions use the double type, are printed with %f, and on Linux may need the -lm link flag. Next we explore powers and roots in detail.
Frequently asked questions
Is the “Including math.h” lesson free?
Yes — the full text of “Including math.h” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.
What will I learn in “Including math.h”?
Link and use the math library. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Including math.h” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Including math.h
- Powers and Roots
- Trig and Logarithms
- Rounding and Absolute Values