WeCodeAcademy

C Preprocessor and Math

Lines starting with # are handled by the preprocessor before compiling. #define makes a constant or macro. The <math.h> library has math functions (link with -lm).

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

#define PI 3.14159
#define SQUARE(x) ((x) * (x))

int main(void) {
  printf("%.2f\n", PI * SQUARE(2));
  printf("%.1f\n", sqrt(81.0));
  printf("%.0f\n", pow(2, 10));
  printf("%.0f %.0f\n", floor(2.7), ceil(2.1));
  return 0;
}
Output
12.57
9.0
1024
2 3
Quick check

Which line makes a named constant with the preprocessor?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.