

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Welcome to Memory Management
incomplete
2: C Program Structure
incomplete
3: Interpreted Quiz
incomplete
4: C Is Compiled
incomplete
5: Comments
incomplete
6: Basic Types
incomplete
7: Strings
incomplete
8: Printing Variables
incomplete
9: Compilation: Types
incomplete
10: Variables
incomplete
11: Constants
incomplete
12: Functions
incomplete
13: Void
incomplete
14: Unit Tests
incomplete
15: Math Operators
incomplete
16: If Statements
incomplete
17: Logical Operators
incomplete
18: Ternary
incomplete
19: Type Sizes
incomplete
20: Sizeof
incomplete
21: For Loop
incomplete
22: While Loop
incomplete
23: Do While Loop
incomplete
24: Pragma Once and Header Guards
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
We saw how .h header files are used in a previous lesson, but before we go further let's talk about a potential issue you might run into: multiple inclusions. If the same header file gets included more than once, you can end up with some nasty errors caused by redefining things like functions or structs.
One simple solution (and the one we'll use for the rest of this course) is #pragma once. Adding this line to the top of a header file tells the compiler to include the file only once, even if it's referenced multiple times across your program.
// my_header.h
#pragma once
struct Point {
int x;
int y;
};
Another common way to avoid multiple inclusions is with include guards, which use preprocessor directives like this:
#ifndef MY_HEADER_H
#define MY_HEADER_H
// some cool code
#endif
This method works by defining a unique macro for the header file. If it's already been included, the guard prevents it from being processed again.
Throughout this course, you'll see #pragma once in our header files. It's quicker and less error-prone than traditional include guards, and it works well with most modern compilers.