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 ofx:sqrt(16)is4.0.pow(x, y)returnsxto the power ofy:pow(2, 10)is1024.0.round(x)roundsxto the nearest whole number:round(3.7)is4.0. A half is rounded away from zero, soround(2.5)is3.0.fabs(x)returns the absolute value ofx, the number without its minus sign:fabs(-3.5)is3.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.50Printing 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
7Use 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
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.
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.
Start programming for free
11/12