The Preprocessor and math.h
Lines starting with # are handled before compiling. #include pulls in headers and #define creates constants or macros. math.h offers sqrt, pow, ceil, floor, fabs and more. Link it with -lm on some systems.
Example
#define SQUARE(x) ((x) * (x))
printf("%d\n", SQUARE(5));
printf("%.1f\n", sqrt(81.0));
printf("%.0f\n", pow(2, 10));
printf("%.0f %.0f\n", ceil(4.1), floor(4.9));Result
25 9.0 1024 5 4
Exercise
Create a constant with the preprocessor.
# PI 3.14
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.