Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Math Functions

You don't have to write every function yourself. C comes with a library of ready-made functions, and you have already used some of them: printf, strlen, toupper. The header file math.h holds functions for math:

  • sqrt(x) returns the square root of x: sqrt(16) is 4.0.
  • pow(x, y) returns x to the power of y: pow(2, 10) is 1024.0.
  • round(x) rounds x to the nearest whole number: round(3.7) is 4.0. A half is rounded away from zero, so round(2.5) is 3.0.
  • fabs(x) returns the absolute value of x, the number without its minus sign: fabs(-3.5) is 3.5.

They are functions like the ones you write: they take arguments and return a value. All four return a double, so print them with %f or %.2f. To use them, add #include <math.h> at the top of the program, next to #include <stdio.h>:

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

int main(void) {
    printf("%.2f\n", sqrt(16));
    printf("%.2f\n", pow(2, 10));
    printf("%.2f\n", round(3.7));
    printf("%.2f\n", fabs(-3.5));
    return 0;
}

This program prints:

4.00
1024.00
4.00
3.50

Printing them with %d is a compile error here: format specifies type 'int' but the argument has type 'double'. And if you forget the #include, the program doesn't compile either:

#include <stdio.h>

int main(void) {
    printf("%.2f\n", sqrt(16));
    return 0;
}
main.c:4:22: error: call to undeclared library function 'sqrt' with type 'double (double)'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    4 |     printf("%.2f\n", sqrt(16));
      |                      ^
main.c:4:22: note: include the header <math.h> or explicitly provide a declaration for 'sqrt'
1 error generated.

The first line is one you know: a call to a function that the compiler doesn't know yet, this time a library function. The note says what to do: include the header <math.h>, or declare sqrt yourself. Now you can see what the #include is for: math.h holds the prototypes of these functions.

The absolute value of a whole number has its own function, abs, in another header file, stdlib.h (the standard library). It takes and returns an int, so you print it with %d:

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

int main(void) {
    int morning = -5;
    int noon = 3;
    printf("Change: %d degrees\n", abs(noon - morning));
    printf("%d\n", abs(-7));
    return 0;
}

This program prints:

Change: 8 degrees
7

Use abs for whole numbers and fabs for decimal ones: abs(-2.5) quietly returns 2. And you can call library functions inside your own functions, which is what you will do now.

Instructions

  1. 1

    A ladder leans against a wall. Its top is height meters up the wall, and its foot stands distance meters from the wall. The length of the ladder is the square root of height * height + distance * distance (that's the Pythagorean theorem). Add #include <math.h> at the top and write a function ladder_length with two parameters of type double, height and distance, that returns the length of the ladder. The program then prints Ladder: 5.00 and Ladder: 3.61.

  2. 2

    Add #include <stdlib.h> and write a function difference with two parameters of type int, a and b, that returns how far apart the two numbers are, always without a minus sign: abs(a - b). At the end of main, before return 0;, print difference(-5, 3) and then difference(3, -5), each with the label Difference:. Both lines are Difference: 8.

Back

11/12

  1. 1. Defining Functions
  2. 2. Calling Functions
  3. 3. Function Prototypes
  4. 4. Parameters and Arguments
  5. 5. Return Values
  6. 6. Print or Return?
  7. 7. return Ends the Function
  8. 8. Local Variables
  9. 9. Arrays and Functions
  10. 10. Strings and Functions
  11. 11. Math Functions
  12. 12. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.