# 16 Math Functions That Every C Programmer Should Know

Updated:

Published:

Mathematics is at the heart of several programming tasks, and C programming offers a robust set of functions to tackle a range of complex calculations.

Whether you are a beginner or an experienced programmer, these math functions are essential for solving problems and developing efficient algorithms.

In this blog post, we’ll explore a list of math functions available in C programming. These functions, housed within the <math.h> header, equip C programmers with the tools they need to perform operations like trigonometry, exponential calculations, logarithms, rounding, and more.

## Math Functions for C Programmers

The following functions are used to calculate different values in C programming. Note that some functions on this list require the <math.h> header while others do not.

### 1. Add: sum = a + b

This function calculates the sum of two numbers. Note: This function does not require the <math.h> header.

Example:

#include <stdio.h>

int main() {

int num1 = 10;

int num2 = 5;

int sum = num1 + num2;

printf("Addition: %d + %d = %d\n", num1, num2, sum);

return 0;

}

Output:

Addition: 10 + 5 = 15

### 2. Subtract: subtraction = a - b

This function subtracts one number from another. Note: This function does not require the <math.h> header.

Example:

#include <stdio.h>

int main() {

int num1 = 10;

int num2 = 5;

int difference = num1 - num2;

printf("Subtraction: %d - %d = %d\n", num1, num2, difference);

return 0;

}

Output:

Subtraction: 10 - 5 = 5

### 3. Multiply: multiplication = a * b

This function multiplies two numbers. Note: This function does not require the <math.h> header.

Example:

#include <stdio.h>

int main() {

int num1 = 10;

int num2 = 5;

int product = num1 * num2;

printf("Multiplication: %d * %d = %d\n", num1, num2, product);

return 0;

}

Output:

Multiplication: 10 * 5 = 50

### 4. Divide: division = a / b

This function divides one number from another. Note: This function does not require the <math.h> header.

Example:

#include <stdio.h>

int main() {

int num1 = 10;

int num2 = 5;

int quotient = num1 / num2;

printf("Division: %d / %d = %d\n", num1, num2, quotient);

return 0;

}

Output:

Division: 10 / 5 = 2

### 5. Power: pow()

This function raises a number to a specified power.

Example:

#include <stdio.h>

#include <math.h>

int main() {

double base = 2.0;

double exponent = 3.0;

double result = pow(base, exponent);

printf("Power: %.2lf^%.2lf = %.2lf\n", base, exponent, result);

return 0;

}

Output:

Power: 2.00^3.00 = 8.00

### 6. Round Up: ceil()

This function rounds a floating-point number up to the nearest integer.

Example:

#include <stdio.h>

#include <math.h>

int main() {

double num = 4.3;

double ceilResult = ceil(num);

printf("Ceiling: %.2lf rounded up to the nearest integer is %.2lf\n", num, ceilResult);

return 0;

}

Output:

Ceiling: 4.30 rounded up to the nearest integer is 5.00

### 7. Round Down: floor()

This function rounds a floating-point number down to the nearest integer.

Example:

#include <stdio.h>

#include <math.h>

int main() {

double num = 4.7;

double floorResult = floor(num);

printf("Floor: %.2lf rounded down to the nearest integer is %.2lf\n", num, floorResult);

return 0;

}

Output:

Floor: 4.70 rounded down to the nearest integer is 4.00

### 8. Tangent: tan()

This function calculates the tangent of an angle.

Example:

#include <stdio.h>

#include <math.h>

int main() {

double angle = 45.0; // Angle in degrees

double radians = angle * (M_PI / 180.0); // Convert to radians

printf("Tangent: tan(%.2lf degrees) = %.2lf\n", angle, tangentResult);

return 0;

}

Output:

Tangent: tan(45.00 degrees) = 1.00

### 9. Cosine: cos()

This function calculates the cosine of an angle.

Example:

#include <stdio.h>

#include <math.h>

int main() {

double angle = 60.0; // Angle in degrees

double radians = angle * (M_PI / 180.0); // Convert to radians

printf("Cosine: cos(%.2lf degrees) = %.2lf\n", angle, cosineResult);

return 0;

}

Output:

Cosine: cos(60.00 degrees) = 0.50

### 10. Natural Logarithm: log()

This function calculates the natural logarithm of a number.

Example:

#include <stdio.h>

#include <math.h>

int main() {

double x = 2.0;

double result = log(x);

printf("Natural Logarithm: ln(%.2lf) = %.2lf\n", x, result);

return 0;

}

Output:

Natural Logarithm: ln(2.00) = 0.69

### 11. Logarithm: log10()

This function calculates the logarithm (base 10) of a number.

Example:

#include <stdio.h>

#include <math.h>

int main() {

double x = 100.0;

double result = log10(x);

printf("Base-10 Logarithm: log10(%.2lf) = %.2lf\n", x, result);

return 0;

}

Output:

Base-10 Logarithm: log10(100.00) = 2.00

### 12. Arc Tangent: atan()

This function calculates the inverse tangent of a number.

Example:

#include <stdio.h>

#include <math.h>

int main() {

double angle = 1.0;

double result = atan(angle);

printf("Arctangent: atan(%.2lf) = %.2lf radians\n", angle, result);

return 0;

}

Output:

### 13. Hypotenuse: hypot()

This function calculates the square root of the sum of squares of two numbers.

Example:

#include <stdio.h>

#include <math.h>

int main() {

double side1 = 3.0;

double side2 = 4.0;

double result = hypot(side1, side2);

printf("Hypotenuse Calculation: hypot(%.2lf, %.2lf) = %.2lf\n", side1, side2, result);

return 0;

}

Output:

Hypotenuse Calculation: hypot(3.00, 4.00) = 5.00

### 14. Cube Root: cbrt()

This function calculates the cube root of a number.

Example:

#include <stdio.h>

#include <math.h>

int main() {

double x = 27.0;

double result = cbrt(x);

printf("Cube Root: cbrt(%.2lf) = %.2lf\n", x, result);

return 0;

}

Output:

Cube Root: cbrt(27.00) = 3.00

### 15. Absolute Value: fabs()

This function calculates the absolute value of a floating-point number.

Example:

#include <stdio.h>

#include <math.h>

int main() {

double num = -7.5;

double absoluteValue = fabs(num);

printf("Absolute Value: fabs(%.2lf) = %.2lf\n", num, absoluteValue);

return 0;

}

Output:

Absolute Value: fabs(-7.50) = 7.50

### 16. Remainder: fmod()

This function calculates the remainder of a division between two floating-point numbers.

Example:

#include <stdio.h>

#include <math.h>

int main() {

double dividend = 10.5;

double divisor = 3.0;

double remainder = fmod(dividend, divisor);

printf("Floating-Point Modulo: fmod(%.2lf, %.2lf) = %.2lf\n", dividend, divisor, remainder);

return 0;

}

Output:

Floating-Point Modulo: fmod(10.50, 3.00) = 1.50

## Using Math Functions in C Programming

Math functions play a crucial role in many domains of programming, including graphics, simulation, finance, and scientific applications. With a deep understanding of these functions, you'll be well-equipped to tackle a wide range of development tasks efficiently and effectively.

Remember to bookmark this resource sheet so you can come back and keep adding new skills to your coding repertoire.

## Related Articles

A free suite of content management tools for marketers and developers.

CMS Hub is flexible for marketers, powerful for developers, and gives customers a personalized, secure experience