- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Math Functions
![]() Share with a Friend |
C Programming - C Math Functions
Math Functions in C
C provides a variety of built-in mathematical functions in the math.h library. These functions cover a wide range of mathematical operations, including basic arithmetic, trigonometry, logarithmic functions, and more.
Common Mathematical Functions in C:
To use these functions, include the header file math.h:
C
#include <math.h>
- Basic Arithmetic Functions:
- abs()
Returns the absolute value of an integer.
C
int abs(int x);
Example:
C
#include <stdio.h>
#include <stdlib.h>
int main() {
int x = -5;
printf("Absolute value of %d is %d\n", x, abs(x));
return 0;
}
- fabs()
Returns the absolute value of a floating-point number.
C
double fabs(double x);
Example:
C
#include <stdio.h>
#include <math.h>
int main() {
double x = -5.6;
printf("Absolute value of %.2f is %.2f\n", x, fabs(x));
return 0;
}
- floor()
Returns the largest integer less than or equal to a given number.
C
double floor(double x);
Example:
C
#include <stdio.h>
#include <math.h>
int main() {
double x = 3.7;
printf("Floor of %.2f is %.2f\n", x, floor(x));
return 0;
}
- ceil()
Returns the smallest integer greater than or equal to a given number.
C
double ceil(double x);
Example:
C
#include <stdio.h>
#include <math.h>
int main() {
double x = 3.2;
printf("Ceiling of %.2f is %.2f\n", x, ceil(x));
return 0;
}
- fmod()
Returns the remainder of dividing x by y.
C
double fmod(double x, double y);
Example:
C
#include <stdio.h>
#include <math.h>
int main() {
double x = 5.8, y = 2.3;
printf("Remainder of %.2f / %.2f is %.2f\n", x, y, fmod(x, y));
return 0;
}
- Trigonometric Functions:
- sin()
Returns the sine of an angle (in radians).
C
double sin(double x);
- cos()
Returns the cosine of an angle (in radians).
C
double cos(double x);
- tan()
Returns the tangent of an angle (in radians).
C
double tan(double x);
- asin()
Returns the arcsine (inverse sine) of a value, in radians.
C
double asin(double x);
- acos()
Returns the arccosine (inverse cosine) of a value, in radians.
C
double acos(double x);
- atan()
Returns the arctangent (inverse tangent) of a value, in radians.
C
double atan(double x);
- atan2()
Returns the arctangent of y/x using the signs of both arguments to determine the correct quadrant.
C
double atan2(double y, double x);
- Exponential and Logarithmic Functions:
- exp()
Returns the exponential function of a given number (e^x).
C
double exp(double x);
- log()
Returns the natural logarithm (base e) of a number.
C
double log(double x);
- log10()
Returns the logarithm of a number with base 10.
C
double log10(double x);
- pow()
Returns x raised to the power of y (i.e., x^y).
C
double pow(double x, double y);
- sqrt()
Returns the square root of a number.
C
double sqrt(double x);
- Hyperbolic Functions:
- sinh()
Returns the hyperbolic sine of a value.
C
double sinh(double x);
- cosh()
Returns the hyperbolic cosine of a value.
C
double cosh(double x);
- tanh()
Returns the hyperbolic tangent of a value.
C
double tanh(double x);
- Miscellaneous Functions:
- gcd()
Returns the greatest common divisor of two integers.
C
int gcd(int x, int y);
- lcm()
Returns the least common multiple of two integers.
C
int lcm(int x, int y);
- modf()
Breaks a floating-point number into its integer and fractional parts.
C
double modf(double x, double *intpart);
Example Program Using Math Functions:
C
#include <stdio.h>
#include <math.h>
int main() {
double x = 2.5;
double y = 3.5;
// Basic Math Operations
printf("Absolute value of %.2f is %.2f\n", x, fabs(x));
printf("Floor of %.2f is %.2f\n", x, floor(x));
printf("Ceiling of %.2f is %.2f\n", y, ceil(y));
// Exponential and Logarithmic
printf("Exponent of %.2f is %.2f\n", x, exp(x));
printf("Logarithm of %.2f is %.2f\n", x, log(x));
// Trigonometric
printf("Sine of %.2f is %.2f\n", x, sin(x));
printf("Cosine of %.2f is %.2f\n", y, cos(y));
// Power
printf("%.2f raised to the power %.2f is %.2f\n", x, y, pow(x, y));
return 0;
}
Conclusion:
The math.h library in C provides a wide range of functions for performing mathematical operations. Whether you are working with basic arithmetic, trigonometry, logarithms, or advanced hyperbolic functions, this library makes it easy to perform complex mathematical calculations in your programs.
