Example 1 — A math module used by main
Build with gcc main.c mathops.c -o app.
/* mathops.h */
#ifndef MATHOPS_H
#define MATHOPS_H
int gcd(int a, int b);
int lcm(int a, int b);
#endif
/* mathops.c */
#include "mathops.h"
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
/* main.c */
#include <stdio.h>
#include "mathops.h"
int main(void) {
printf("gcd(48, 18) = %d\n", gcd(48, 18));
printf("lcm(4, 6) = %d\n", lcm(4, 6));
return 0;
} How It Works
main.cknows only the declarations inmathops.h.- The linker connects
main.oandmathops.o. - The guard keeps the header safe to include repeatedly.
