Copyright

Math Functions in C Programming: Definition & Example

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Basic Input & Output in C Programming

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:02 Math in C
  • 1:26 Math.h
  • 3:51 Function Parameters
  • 4:31 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Yes, there is math in computer programming. But computer programs are powerful tools that let you carry out almost every math problem imaginable. In this lesson we will cover the math libraries that C provides, providing working code examples.

Math in C

Where's the math? Computers can do amazing things with huge numbers and complex calculations. Thankfully, we won't be trying to calculate the number of possible planets in the Andromeda galaxy, but we will be doing math in C.

Computer programming and math go hand in hand. In any program, you may be calculating a pay rate, increasing a value by 1, or doing long division. In order to use many of the math features in C, we will need to make use of a library.

You can think of a C library as a virtual library for your code. It is a set of pre-built functions that your program can use. These include input/output methods or math functions. If you want to use the library, you need to include its header file at the top of your code. The header file is the pointer to the library. Here are some available header files:

Library Header File Library
<stdio.h> Input/output
<stdlib.h> General utilities like random numbers, string functions, memory management
<math.h> Common math tools
<string.h> String methods
<time.h> Time/date methods

To use the file, it should be part of your first lines of code, before any main function:

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

The file extension .h indicates a header file, while the .c extension indicates your C program code. Next, we will dive right into the math.h library and some of its features. Note that there isn't a semi-colon at the end of the statements!

Math.h

The standard math library, math.h, contains a large number of mathematical tools. This includes trigonometry (sine, cosine, tangent, etc.), exponential functions, logarithmic functions, absolute values, square roots, etc. We've included a table for reference, but let's look at a few commonly used functions.

Floor/Ceiling

No, we haven't changed over to construction science! In some cases, you need to find the floor, which is the lowest value of a number without decimal places or ceiling, which is the next highest whole number of your values. Think of your age, expressed as a fairly exact value: It might be 28.74 as calculated by a computer program. However, the CEILING of your age is 29; the FLOOR is 28. It is often useful to return the floor or ceiling, especially if doing calculations based on your age at a point in time.

The following code will print out the floor and ceiling of an age, 34.38. Note that the math functions are floor and ceil, respectively. To use the function, enter the function name (e.g., floor), followed by the variable in parenthesis. In our code, we created a new variable to store the floor and ceiling:

#include <stdio.h>
#include <math.h>
int main(void) {
  double age = 34.38;
  int floor_age = floor(age);
  printf("FLOOR = %d", floor_age);
  printf("\n");
  int ceil_age = ceil(age);
  printf("CEILING = %d", ceil_age);
}

Go ahead and paste into your compiler. When compiled and run using Dev C++, this is the output:


C floor and ceiling output


Powers and Square Root

Most calculators have a power (e.g., 10 to the 5th power) or square root function. The math is still the same in C, but these are functions within the math library.

Let's take the age from the previous code and raise it up to the second power. The function is pow, and you must provide both the variable AND the power you are raising it to. In this case, we are raising to the second power, or 2.

double new_age = pow(age, 2);

Now, let's try the square root of the age. This function is sqrt and you only have to provide the variable:

double new_age = sqrt(age);

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support