C Tutorial

C Math

math.h gives sqrt, pow, round, and fabs. Link the math library when you compile locally.

Include math.h

Arithmetic operators handle plus, minus, times, and divide. Square roots, powers, and rounding live in the math library. Bring it in with #include <math.h> at the top of the file, next to#include <stdio.h>.

The functions take and return floating-point values in most cases. Store the result in a doubleunless you have a reason not to.

Compile the examples in /c/try. That is the C editor. Do not paste them into Python at/try, HTML at /html/try, or C++ at /cpp/try.

A complete program

This file uses four functions you will see often: sqrt, pow, fabs, andround. Copy it, change the numbers, and compile again.

Example

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

int main(void) {
  double side = 9.0;
  printf("sqrt(9) = %f\n", sqrt(side));
  printf("pow(2, 8) = %f\n", pow(2.0, 8.0));
  printf("fabs(-4.5) = %f\n", fabs(-4.5));
  printf("round(2.6) = %f\n", round(2.6));
  return 0;
}

Locally, gcc often needs the math library on the link line. Add -lm at the end:gcc -std=c17 main.c -o main -lm. On StudyGrid the C editor already links it for you.

sqrt

sqrt(x) returns the square root of x. Pass a non-negative number. A negative argument is a domain error; many compilers print nan (not a number) instead of a useful result.

Distance formulas use square roots. The length of a right-triangle hypotenuse with legs 3 and 4 issqrt(3 * 3 + 4 * 4), which is 5.

Example

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

int main(void) {
  double a = 3.0;
  double b = 4.0;
  double c = sqrt(a * a + b * b);
  printf("%f\n", c);
  return 0;
}

pow

pow(base, exp) raises base to exp. Both arguments are treated as floating-point, so the result is a double even when the inputs look like integers.

For a small integer power you can still write n * n. Use pow when the exponent is not a tiny constant, or when you are already working in doubles.

Prefer pow(2.0, 8.0) over integer literals so the types match the library. Integerpow is not a built-in operator in C.

fabs and round

fabs(x) returns the absolute value of a double: negative numbers become positive. Use it when a difference might go either way and you only care about size. Integer absolute value isabs in #include <stdlib.h>; this chapter stays with fabs.

round(x) returns the nearest integer as a double. Halfway cases (such as 2.5) round away from zero, so 2.5 becomes 3.0 and -2.5 becomes -3.0.

Example

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

int main(void) {
  printf("%f\n", fabs(-12.0));
  printf("%f\n", fabs(7.25));
  printf("%f\n", round(3.2));
  printf("%f\n", round(3.8));
  return 0;
}

The usual toolkit

CallMeaning
sqrt(x)Square root of x
pow(x, y)x to the power y
fabs(x)Absolute value of a double
round(x)Nearest integer (as double)
floor(x)Largest integer not greater than x
ceil(x)Smallest integer not less than x

Most of these return double. Assigning to int truncates toward zero. Do not takesqrt of a negative number if you expect a real result. Next: booleans, which comparisons produce.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.

Physics

Hypotenuse, then displacement

Pythagoras is also how you get the magnitude of a 2D vector. A displacement 3 m east and 4 m north has size 5 m. The same numbers appear in a 3-4-5 triangle because the geometry is the same.

sqrt lives in math.h. Locally gcc often needs -lm. The StudyGrid C editor already links it.

|r| = √(x² + y²)

Example

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

int main(void) {
  double a = 3.0;
  double b = 4.0;
  double c = sqrt(a * a + b * b);
  printf("c = %.1f m\n", c);
  return 0;
}

Maths

Roots of a quadratic

x² − 5x + 6 = 0 factors as (x − 2)(x − 3) = 0, so the roots are 2 and 3. The quadratic formula does the same job when factoring is ugly: discriminant b² − 4ac, then the two signs on the square root.

If the discriminant is negative, sqrt returns NaN on many libm implementations. This listing assumes a positive discriminant.

x = (−b ± √(b² − 4ac)) / (2a)

Example

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

int main(void) {
  double a = 1.0, b = -5.0, c = 6.0;
  double d = b * b - 4.0 * a * c;
  double x1 = (-b + sqrt(d)) / (2.0 * a);
  double x2 = (-b - sqrt(d)) / (2.0 * a);
  printf("x = %.1f and %.1f\n", x1, x2);
  return 0;
}

Physics

Distance in free fall from rest

From rest, s = ½ g t². After 2 s that is about 19.6 m. The object has also picked up speed v = g t = 19.6 m/s. Neither figure includes air resistance, so a real dropped ball is slower.

pow(t, 2.0) is t squared. t * t would do, and avoids a library call. Both are fine at this scale.

s = ½ g t²

g = 9.81 is a local standard, not a universal constant. On the Moon you would swap in 1.62.

Example

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

int main(void) {
  const double g = 9.81;
  double t = 2.0;
  double s = 0.5 * g * pow(t, 2.0);
  printf("s = %.2f m\n", s);
  return 0;
}

FAQ: C Math

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c math examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c math in this C C lesson (C Math).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.