header-guard
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| header-guard [May 14, 2026 at 12:50] – created yanevskiv | header-guard [May 14, 2026 at 13:01] (current) – yanevskiv | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| # Header guard | # Header guard | ||
| - | **Header guards** are used | + | **Header guards** are a pattern |
| + | |||
| + | Header guards follow the same general pattern: | ||
| + | ```c | ||
| + | #ifndef __HEADER_GUARD_H__ | ||
| + | #define __HEADER_GUARD_H__ | ||
| + | |||
| + | // ... | ||
| + | |||
| + | #endif /* __HEADER_GUARD_H__ (This is just a comment) */ | ||
| ``` | ``` | ||
| - | // my_header.h | ||
| - | #ifndef __MY_HEADER_H__ | ||
| - | #define __MY_HEADER_H__ | ||
| - | // ... your code goes here ... | + | In C and C++ programming languages, `#include` is nothing but text substitution. If you include something multiple times |
| - | #endif /* __MY_HEADER_H__ | + | ## Example |
| + | A prototypical example of a header guard is the following. | ||
| + | ```c | ||
| + | // hello_world.h | ||
| + | #ifndef __HELLO_WORLD_H__ | ||
| + | #define __HELLO_WORLD_H__ | ||
| + | |||
| + | struct Hello { | ||
| + | const char *text = " | ||
| + | }; | ||
| + | |||
| + | #endif /* __HELLO_WORLD_H__ | ||
| ``` | ``` | ||
| + | |||
| + | This allows multiple inclusion of " | ||
| + | ```c | ||
| + | // main.c | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | int main() { | ||
| + | Hello world; | ||
| + | return 0; | ||
| + | } | ||
| + | ``` | ||
| + | |||
header-guard.1778763053.txt.gz · Last modified: by yanevskiv
