C Math Functions


C programming provides a rich set of mathematical functions through the math.h library. These functions simplify complex computations, allowing programmers to perform operations like power, square root, absolute value, trigonometric calculations, and logarithms without writing custom code.

Understanding and using math functions effectively helps write efficient, readable, and accurate programs. These functions are widely used in scientific calculations, engineering programs, and game development.

Including the Math Library

To use math functions in C, you must include the math.h header:

#include <stdio.h>
#include <math.h>
  • math.h contains prototypes for all standard math functions.

  • When compiling, especially with gcc, link the math library using -lm:

gcc program.c -o program -lm
  • Omitting -lm may cause undefined reference errors during linking.

Commonly Used Math Functions

1. Power Function pow()

Calculates the value of a number raised to a power.

double pow(double base, double exponent);

Example:

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

int main() {
    double result = pow(2, 3); // 2^3
    printf("2 raised to 3 is %.0f\n", result);
    return 0;
}
  • Returns a double.

  • Can be used for fractional powers as well.

2. Square Root sqrt()

Calculates the square root of a number.

double sqrt(double x);

Example:

double num = 16;
printf("Square root of %.0f is %.2f\n", num, sqrt(num));
  • Returns the positive square root.

  • Input must be non-negative; negative inputs result in domain errors.

3. Absolute Value abs() and fabs()

  • abs(int x) for integers.

  • fabs(double x) for floating-point numbers.

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

int main() {
    int a = -10;
    double b = -5.75;
    printf("Absolute of %d is %d\n", a, abs(a));
    printf("Absolute of %.2f is %.2f\n", b, fabs(b));
    return 0;
}
  • Converts negative numbers to positive.

  • Useful in calculations where only magnitude matters.

4. Trigonometric Functions

C provides standard trigonometric functions using radians:

Function Description
sin(x) Sine of x
cos(x) Cosine of x
tan(x) Tangent of x

Example:

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

int main() {
    double angle = 3.14159 / 2; // 90 degrees in radians
    printf("sin(90°) = %.2f\n", sin(angle));
    printf("cos(90°) = %.2f\n", cos(angle));
    printf("tan(90°) = %.2f\n", tan(angle));
    return 0;
}
  • For degree values, convert to radians: radians = degrees * (PI / 180).

5. Logarithmic Functions

C supports natural logarithm (log()) and base-10 logarithm (log10()):

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

int main() {
    double num = 100.0;
    printf("Natural log of %.0f: %.2f\n", num, log(num));
    printf("Base-10 log of %.0f: %.2f\n", num, log10(num));
    return 0;
}
  • log() returns the natural logarithm (ln).

  • log10() returns logarithm base 10.

  • Input must be positive; zero or negative values cause errors.

6. Rounding Functions

  • ceil(x) – rounds up to the nearest integer.

  • floor(x) – rounds down to the nearest integer.

  • round(x) – rounds to the nearest integer.

double num = 4.7;
printf("Ceil: %.0f, Floor: %.0f, Round: %.0f\n", ceil(num), floor(num), round(num));
  • Useful for financial calculations, grids, and measurements.

7. Miscellaneous Functions

  • fmod(x, y) – remainder of x / y.

  • exp(x) – calculates e^x.

  • sqrt(x) – square root, already discussed.

Example using fmod():

printf("Remainder of 10/3: %.2f\n", fmod(10, 3));

Practical Example: Using Multiple Math Functions

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

int main() {
    double a = 5, b = 2;

    printf("Power: %.2f^%.2f = %.2f\n", a, b, pow(a, b));
    printf("Square root of %.2f = %.2f\n", a, sqrt(a));
    printf("Absolute of -%.2f = %.2f\n", b, fabs(-b));
    printf("Natural log of %.2f = %.2f\n", a, log(a));
    printf("Ceiling of %.2f = %.0f\n", 4.3, ceil(4.3));
    printf("Floor of %.2f = %.0f\n", 4.7, floor(4.7));
    printf("Remainder of %.2f / %.2f = %.2f\n", a, b, fmod(a, b));
    return 0;
}
  • Demonstrates power, square root, absolute, log, rounding, and modulus functions together.

  • Shows practical usage of math.h in real-world calculations.

Best Practices

  1. Always include math.h to access functions.

  2. Use -lm when compiling to link the math library.

  3. Ensure inputs are valid for domain-specific functions (e.g., positive for sqrt).

  4. Use radians for trigonometric functions.

  5. Combine math functions to simplify complex calculations.

Summary of the Tutorial

C provides a wide range of mathematical functions through math.h that make computations easier, more accurate, and efficient. Functions like pow(), sqrt(), abs(), sin(), cos(), log(), and rounding functions are essential tools for programmers working in scientific, financial, and engineering applications. Proper use of these functions ensures readable, maintainable, and error-free programs.


Practice Questions

  1. Write a program that uses pow() to calculate x raised to the power y for user input values.

  2. Create a program that uses sqrt() to find the square root of a number entered by the user.

  3. Write a program that demonstrates the use of abs() and fabs() for both integer and floating-point numbers.

  4. Develop a program to calculate the sine, cosine, and tangent of an angle in degrees (convert to radians).

  5. Write a program that uses log() and log10() to calculate natural and base-10 logarithms of a given number.

  6. Create a program to round a floating-point number using ceil(), floor(), and round().

  7. Write a program that calculates the remainder of two numbers using fmod().

  8. Develop a program that calculates e^x using exp() for a given value of x.

  9. Write a program that combines multiple math functions: calculate sqrt(pow(x, 2) + pow(y, 2)).

  10. Create a program to compute the area of a circle using M_PI from math.h and the pow() function for the radius squared.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top