math.h C Library Functions

The math.h header file includes C standard library functions to perform common mathematical operations and transformations. It contains Trigonometric, Exponential, logarithmic, Rounding off functions etc.

All the functions math.h library take double as input argument and return a double. Even if we pass a float value, it gets promoted(type casted) to a double value.

List of math.h Library Functions

Click on function names below to see detailed description of functions.

Function Description
acos It returns the arc cosine in radians.
asin() It returns the arc sine in radians.
atan It returns the arc tangent in radians.
atan2 It returns the arc tangent with two parameters.
ceil It returns the smallest integer value greater than or equal to x.
cos It returns the cosine of an angle in radians.
cosh It returns the hyperbolic cosine.
exp It returns the value of e raised to the power of x(ex).
fabs It returns the absolute value of x.
floor It returns the largest integral value less than or equal to x.
fmod It returns the floating point remainder of x divided by y.
frexp It breaks the floating point number x into its mantisa and an integer exponent of 2.
idexp It returns the result of multiplying x with 2 raised to the power of exponent.
log It returns the natural logarithm(base-e logarithm) of x.
log10 It returns the common logarithm(base 10 logarithm) of x.
modf It breaks a floating point number into an integral and a fractional part.
pow It returns base raised to the power of exponent(xy).
sin It returns the sine of a radian angle.
sinh It returns the hyperbolic sine of a radian angle.
sqrt It returns the square root of a number.
tan It returns the tangent of a radian angle.
tanh It returns the hyperbolic tangent of a radian angle.

Macros Defined in math.h Library

Macro Description
HUGE_VAL Some math.h functions returns this MACRO to represent very large value(greater than the range of floating point number) of the calculated result. If magnitude of the result is large enough to be represented as floating point number, then it sets errno to ERANGE and returns HUGE_VAL or its negation -HUGE_VAL.

C Program to Draw a Hut on Screen Using C Graphics

Write a program in C to draw a hut and color it using graphics.h header file
In this program, we will draw a hut on screen using line and rectangle function and then fill it with different patterns and colors. We will use below mentioned graphics functions in this program.

Function Argument Description
initgraph It initializes the graphics system by loading the passed graphics driver then changing the system into graphics mode.
setcolor It changes the current drawing colour. Default colour is white. Each color is assigned a number, like BLACK is 0 and RED is 4. Here we are using colour constants defined inside graphics.h header file.
setfillstyle It sets the current fill pattern and fill color.
rectangle It draws a rectangle on screen. It takes the coordinates of top left and bottom right corners.
line It draws a straight line between two points on screen.
floodfill It is used to fill a closed area with current fill pattern and fill color. It takes any point inside closed area and color of the boundary as input.
closegraph It unloads the graphics drivers and sets the screen back to text mode.

C program to draw a hut and color it using graphics

C Program to Draw a Hut on Screen Using C Graphics

#include<graphics.h>
#include<conio.h>
 
int main(){
 int gd = DETECT,gm;
    initgraph(&gd, &gm, "X:\\TC\\BGI");
    /* Draw Hut */
    setcolor(WHITE);
    rectangle(150,180,250,300);
    rectangle(250,180,420,300);
    rectangle(180,250,220,300);
 
    line(200,100,150,180);
    line(200,100,250,180);
    line(200,100,370,100);
    line(370,100,420,180);
 
    /* Fill colours */
    setfillstyle(SOLID_FILL, BROWN);
    floodfill(152, 182, WHITE);
    floodfill(252, 182, WHITE);
    setfillstyle(SLASH_FILL, BLUE);
    floodfill(182, 252, WHITE);
    setfillstyle(HATCH_FILL, GREEN);
    floodfill(200, 105, WHITE);
    floodfill(210, 105, WHITE);
     
    getch();
    closegraph();
    return 0;
}

Program Output

HUT

atan C Library Function

atan C Library Function

The function double atan(double x); returns the arc tangent of x, expressed in radians. Arc tangent is the inverse operation of tangent.

Function prototype of atan

double atan(double x);

  • x : A floating point value whose arc tangent(inverse tangent) to be computed, in the interval [-INFINITY, INFINITY].

Return value of atan

Function atan returns the principal arc tangent of x, in the interval [-Pi/2, +Pi/2] radians.

C program using atan function

The following program shows the use of atan function to calculate inverse of tangent.

atan C Library Function

#include <stdio.h>
#include <math.h>
 
#define PI 3.14159
 
int main(){
    double input, radian, degree;
    /* Range of tan function is -INFINITY to +INFINITY */
    printf("Enter a number between -INFINITY to +INFINITY\n");
    scanf("%lf", &input);
     
    radian = atan(input);
    /* 
     *  Radian to degree conversion
     *  One radian is equal to 180/PI degrees.
     */
    degree = radian * (180.0/PI);
     
    printf("The arc tan of %0.4lf is %0.4lf in radian\n",
        input, radian);
    printf("The arc tan of %0.4lf is %0.4lf in degree\n",
        input, degree);
         
    return 0;
}

Output

Enter a number between -INFINITY to +INFINITY
1.0
The arc tan of 1.0000 is 0.7854 in radian
The arc tan of 1.0000 is 45.0000 in degree
Enter a number between -INFINITY to +INFINITY
9999.0
The arc tan of 9999.0000 is 1.5707 in radian
The arc tan of 9999.0000 is 89.9943 in degree

strcspn C Library Function

strcspn C Library Function

The function size_t strcspn(const char *str1, const char *str2); scans str1 to find the first occurrence of any of the characters that are part of str2. It returns the length of the prefix of str1 which consists entirely of characters not in str2. If none of the characters of str2 matches with any character of str1, the function will return the length of str1.

Function prototype of strcspn

size_t strcspn(const char *str1, const char *str2);
  • str1 : This is the string to be scanned.
  • str2 : This is the string containing a list of characters to match in str1.

Return value of strcspn

This function returns the length of the initial part of string str1 not containing any of the characters of string str2. If none of the characters of str2 matches with any character of str1, then it return length of string str1.

C program using strcspn function

The following program shows the use of strcspn function.

strcspn C Library Function

#include <stdio.h>
#include <string.h>
#include <conio.h>
 
int main()
{
   char firstString[100], secondString[100];
   int position;
    
   printf("Enter first string\n");
   scanf("%s", &firstString);
   printf("Enter second string\n");
   scanf("%s", &secondString);
    
   position = strcspn(firstString, secondString);
 
   printf("Any character of second string matched at %d\n",
        position+1);
    
   getch();
   return(0);
}

Output

Enter first string
asdfgAqwE
Enter second string
MNJA
Any character of second string matched at 6

cos C Library Function

cos C Library Function

The function double cos(double x); returns the cosine of x, expressed in radians.

Function prototype of cos

double cos(double x);

  • x : A floating point value representing an angle in radians.

Return value of cos

It returns the cosine of x.

C program using cos function

The following program shows the use of cos function to calculate cosine of an angle.

cos C Library Function

#include <stdio.h>
#include <math.h>
 
#define PI 3.14159
 
int main(){
    double value, radian, degree;
    printf("Enter an angle in degree\n");
    scanf("%lf", &degree);
     
    /* 
     *  Degree to radian conversion
     *  One radian is equal to 180/PI degrees.
     */
    radian = degree * (PI/180.0);
    value = cos(radian);
     
    printf("The cosine of %lf is %0.4lf\n", degree, value);
         
    return 0;
}

Output

Enter an angle in degree
45
The cosine of 45.0000 is 0.7071
Enter an angle in degree
180
The cosine of 180.0000 is -1.0000

strlen C Library Function

The function size_t strlen(const char *str);calculates the length of a string. The length of a C string is the number of character before terminating null character(‘\0’).

Function prototype of strlen

size_t strlen(const char *str);
  • str : This is the string whose length is to be found.

Return value of strlen

This function returns the length of string.

C program using strlen function

The following program shows the use of strlen function to find the length of string.

strlen C Library Function

#include <stdio.h>
#include <string.h>
#include <conio.h>
 
int main()
{
   char string[100];
   int length;
    
   printf("Enter a string\n");
   scanf("%s", &string);
    
   length = strlen(string);
 
   printf("Length of %s is %d", string, length);
    
   getch();
   return(0);
}

Output

Enter a string
TechCrashCourse
Length of TechCrashCourse is 15

cosh C Library Function

The function double cosh(double x); returns the hyperbolic cosine of x radians.

Function prototype of cosh

double cosh(double x);
  • x : A floating point value representing an angle in radians.

Return value of cosh

It returns the hyperbolic cosine of x.

C program using cosh function

The following program shows the use of cosh function to calculate hyperbolic cosine of a number.

cosh C Library Function

#include <stdio.h>
#include <math.h>
 
int main(){
    double value, radian;
    printf("Enter a number\n");
    scanf("%lf", &radian);
     
    value = cosh(radian);
    printf("The hyperbolic cosine of %lf is %0.4lf\n", radian, value);
         
    return 0;
}

Output

Enter a number
2.5
The hyperbolic cosine of 2.500000 is 6.1323
Enter a number
1.0
The hyperbolic cosine of 2.500000 is 1.5431

fmod C Library Function

fmod C Library Function

The function double fmod(double x, double y); returns the floating point remainder of x divided by y.

Function prototype of fmod

double fmod(double x, double y);

  • x : A floating point value of the numerator.
  • y : A floating point value of the denominator.

Return value of fmod

It returns remainder of dividing x by y.

C program using fmod function

The following program shows the use of fmod function to find the floating point remainder.

fmod C Library Function

#include <stdio.h>
#include <math.h>
 
int main ()
{
  double num, denom, remainder;
  printf("Enter numerator and denominator\n");
  scanf("%lf %lf", &num, &denom);
   
  remainder = fmod(num, denom);
  printf("Remainder of %lf/%lf is %lf\n", num, denom, remainder);
   
  return 0;
}

Output

Enter numerator and denominator
12.5 3
Remainder of 12.500000/3.000000 is 0.500000

tan C Library Function

tan C Library Function

The function double tan(double x); returns the tangent of x, expressed in radians.

Function prototype of tan

double tan(double x);
  • x : A floating point value representing an angle in radians.

Return value of tan

It returns the tangent of x.

C program using tan function

The following program shows the use of tan function to calculate tangent of a radian.

tan C Library Function

#include <stdio.h>
#include <math.h>
 
#define PI 3.14159265
 
int main(){
    double value, radian, degree;
    printf("Enter an angle in degree\n");
    scanf("%lf", &degree);
     
    /* 
     *  Degree to radian conversion
     *  One degree is equal to PI/180 radians.
     *  Where PI = 22/7 = 3.14159(approx)
     */
    radian = degree * (PI/180.0);
    value = tan(radian);
     
    printf("The tangent of %lf is %lf\n", degree, value);
    return 0;
}

Output

Enter an angle in degree
45
The tangent of 45.000000 is 1.000000
Enter an angle in degree
60
The tangent of 60.000000 is 1.732051

sinh C Library Function

sinh C Library Function

The function double sinh(double x); returns the hyperbolic sine of x radians.

Function prototype of sinh

double sinh(double x);

  • x : A floating point value representing an angle in radians.

Return value of sinh

It returns the hyperbolic sine of x.

C program using sinh function

The following program shows the use of sinh function to calculate hyperbolic sine of an angle in radian.

sinh C Library Function

#include <stdio.h>
#include <math.h>
 
int main(){
    double value, radian;
    printf("Enter a number\n");
    scanf("%lf", &radian);
     
    value = sinh(radian);
    printf("The hyperbolic sine of %lf is %lf\n", radian, value);
         
    return 0;
}

Output

Enter a number
0.25
The hyperbolic sine of 2.500000 is 0.252512