Site Tools


header-guard

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
header-guard [May 14, 2026 at 12:52] yanevskivheader-guard [May 14, 2026 at 13:01] (current) yanevskiv
Line 1: Line 1:
 # Header guard # Header guard
-**Header guards** are a pattern used in C and C++ programming languages when writing header files. +**Header guards** are a pattern used in C and C++ programming languages when writing header files. Header files (.h files) are files that are included in source files (.c and .cpp files) near the top (hence the name) using the `#include <...>` directive. They are used to allow multiple inclusions of the same file but prevent multiple definition errors. 
-Header files are files that +
  
-A prototypical example of a header guard is the following+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) */ 
 +``` 
 + 
 +In C and C++ programming languages, `#include` is nothing but text substitution. If you include something multiple times 
 + 
 +## Example 
 +A prototypical example of a header guard is the following.
 ```c ```c
 // hello_world.h // hello_world.h
Line 10: Line 22:
  
 struct Hello { struct Hello {
-    const char *world;+    const char *text = "world";
 }; };
  
 #endif /* __HELLO_WORLD_H__ */ #endif /* __HELLO_WORLD_H__ */
 ``` ```
 +
 +This allows multiple inclusion of "hello\_world.h"
 +```c
 +// main.c
 +#include <hello_world.h>
 +#include <hello_world.h>
 +
 +int main() {
 +    Hello world;
 +    return 0;
 +}
 +```
 +
header-guard.1778763157.txt.gz · Last modified: by yanevskiv