Example 1 — Including a guarded header twice
The second include expands to nothing, so the struct is defined once.
/* point.h */
#ifndef POINT_H
#define POINT_H
struct Point { int x, y; };
#endif
/* main.c */
#include <stdio.h>
#include "point.h"
#include "point.h" /* skipped by the guard */
int main(void) {
struct Point p = {3, 4};
printf("p = (%d, %d)\n", p.x, p.y);
return 0;
} How It Works
- The first include defines
POINT_Hand the struct. - The second include sees
POINT_His defined and skips the contents. - Without the guard,
struct Pointwould be redefined and the compiler would report an error.
