//a header can be included twice: directly and through
//another header. The guard lets its text in only once.
//--- point.h ---
#ifndef POINT_H
#define POINT_H
typedef struct {
int x;
int y;
} Point;
static inline int manhattan(Point p) {
return abs(p.x) + abs(p.y);
}
#endif //POINT_H
//--- the same point.h once more, as the second #include brings it:
//POINT_H is already defined, so the text is skipped.
//Without the guard manhattan would be defined twice - a build error
#ifndef POINT_H
#define POINT_H
typedef struct {
int x;
int y;
} Point;
static inline int manhattan(Point p) {
return abs(p.x) + abs(p.y);
}
#endif //POINT_H
//most compilers also know the shorter #pragma once,
//but the standard does not have it
Point p = {3, -4};
int distance = manhattan(p);
//distance is 7
printf("distance is %d\n", distance);