Bird
Raised Fist0
Cprogramming~20 mins

Include guards - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Include Guards Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this code with include guards?

Consider two header files a.h and b.h included in main.c. Both headers define the same macro with include guards. What will be the output when main.c is compiled and run?

C
#include "a.h"
#include "b.h"
#include <stdio.h>

int main() {
    printf("%d\n", VALUE);
    return 0;
}

// a.h
#ifndef A_H
#define A_H
#define VALUE 10
#endif

// b.h
#ifndef B_H
#define B_H
#undef VALUE
#define VALUE 20
#endif
A0
B10
CCompilation error due to redefinition of VALUE
D20
Attempts:
2 left
💡 Hint

Include guards prevent multiple inclusions of the same header file. Different headers can define conflicting macros; the last #define wins.

🧠 Conceptual
intermediate
1:30remaining
What is the main purpose of include guards?

Why do programmers use include guards in header files?

ATo speed up the compilation by skipping unused code
BTo prevent multiple inclusions of the same header file causing redefinition errors
CTo allow conditional compilation based on platform
DTo automatically include dependent libraries
Attempts:
2 left
💡 Hint

Think about what happens if a header file is included more than once.

🔧 Debug
advanced
2:30remaining
Why does this header cause a compilation error?

Look at this header file example.h. It is supposed to have include guards but causes a compilation error when included multiple times. What is the problem?

C
#ifndef EXAMPLE_H
#define EXAMPLE_H

int add(int a, int b);

#endif // EXAMPLE_H

#ifndef EXAMPLE_H
#define EXAMPLE_H

int subtract(int a, int b);

#endif // EXAMPLE_H
AThe include guard macro is repeated twice, so the second part is ignored causing missing declarations
BThe header file is missing #pragma once directive
CThe functions are declared inside the include guard, which is incorrect
DThe header file should use #ifdef instead of #ifndef
Attempts:
2 left
💡 Hint

Check how many times the macro EXAMPLE_H is defined and what happens after the first definition.

📝 Syntax
advanced
1:30remaining
Which option correctly implements include guards?

Choose the correct syntax for include guards in a header file named myheader.h.

A
#ifndef MYHEADER
#define MYHEADER_H
// code
#endif
B
#ifdef MYHEADER_H
#define MYHEADER_H
// code
endif
C
#ifndef MYHEADER_H
#define MYHEADER_H
// code
#endif
D
#ifndef MYHEADER_H
#define MYHEADER
// code
#endif
Attempts:
2 left
💡 Hint

The macro name in #ifndef and #define must match exactly.

🚀 Application
expert
3:00remaining
How many times is the function declared after these includes?

Given these files, how many times is the function foo() declared in the final compiled code?

// foo.h
#ifndef FOO_H
#define FOO_H
void foo();
#endif

// bar.h
#ifndef BAR_H
#define BAR_H
#include "foo.h"
#endif

// main.c
#include "foo.h"
#include "bar.h"
A1
B3
C2
DCompilation error due to multiple declarations
Attempts:
2 left
💡 Hint

Consider how include guards prevent multiple inclusions of foo.h.

Practice

(1/5)
1. What is the main purpose of include guards in C header files?
easy
A. To define global variables
B. To speed up the compilation process
C. To prevent multiple inclusion of the same header file
D. To include standard libraries automatically

Solution

  1. Step 1: Understand what include guards do

    Include guards use preprocessor directives to ensure a header file is included only once during compilation.
  2. Step 2: Identify the main purpose

    This prevents errors caused by multiple definitions from including the same file multiple times.
  3. Final Answer:

    To prevent multiple inclusion of the same header file -> Option C
  4. Quick Check:

    Include guards prevent multiple inclusion = D [OK]
Hint: Include guards stop repeated header file inclusion [OK]
Common Mistakes:
  • Thinking include guards speed up compilation
  • Confusing include guards with automatic library inclusion
  • Believing include guards define variables
2. Which of the following is the correct way to start an include guard in a header file named myheader.h?
easy
A. #include MYHEADER_H
B. #ifndef MYHEADER_H #define MYHEADER_H
C. #define MYHEADER_H #ifndef MYHEADER_H
D. #ifdef MYHEADER_H #define MYHEADER_H

Solution

  1. Step 1: Recall the include guard pattern

    The standard pattern starts with #ifndef followed by a unique macro, then #define for the same macro.
  2. Step 2: Match the correct syntax

    #ifndef MYHEADER_H #define MYHEADER_H correctly uses #ifndef MYHEADER_H and #define MYHEADER_H in the right order.
  3. Final Answer:

    #ifndef MYHEADER_H #define MYHEADER_H -> Option B
  4. Quick Check:

    Include guard starts with #ifndef + #define = B [OK]
Hint: Include guards start with #ifndef then #define [OK]
Common Mistakes:
  • Reversing #define and #ifndef order
  • Using #ifdef instead of #ifndef
  • Using #include instead of #ifndef
3. Given the following header file example.h with include guards:
#ifndef EXAMPLE_H
#define EXAMPLE_H
int value = 10;
#endif

What happens if example.h is included twice in a C source file?
medium
A. Runtime error when accessing 'value'
B. No error; 'value' is defined only once
C. Compilation will fail due to macro redefinition
D. Compilation error due to multiple definitions of 'value'

Solution

  1. Step 1: Understand include guards behavior

    Include guards use preprocessor directives to ensure a header file is included only once during compilation, preventing multiple definitions of the same code.
  2. Step 2: Analyze multiple inclusion effect

    The first inclusion processes the content and defines the macro; the second skips it due to the macro being defined. However, defining a variable like int value = 10; in a header file causes multiple definition errors if the header is included in multiple source files or included multiple times in the same translation unit without proper extern usage.
  3. Final Answer:

    Compilation error due to multiple definitions of 'value' -> Option D
  4. Quick Check:

    Defining variables in headers causes multiple definition errors = C [OK]
Hint: Avoid defining variables in headers; use extern declarations instead [OK]
Common Mistakes:
  • Thinking include guards prevent multiple variable definitions
  • Confusing runtime errors with compile errors
  • Assuming variables defined in headers are safe
4. Consider this header file config.h without include guards:
int config_value = 5;

When included multiple times, it causes errors. How can you fix it by adding include guards?
medium
A. #ifndef CONFIG_H #define CONFIG_H int config_value = 5; #endif
B. #ifdef CONFIG_H #define CONFIG_H int config_value = 5; #endif
C. #ifndef CONFIG_H int config_value = 5; #endif
D. #define CONFIG_H #ifndef CONFIG_H int config_value = 5; #endif

Solution

  1. Step 1: Identify correct include guard structure

    The guard must start with #ifndef CONFIG_H, then #define CONFIG_H, and end with #endif.
  2. Step 2: Check each option

    #ifndef CONFIG_H #define CONFIG_H int config_value = 5; #endif correctly wraps the variable definition inside the guard with proper order and syntax.
  3. Final Answer:

    #ifndef CONFIG_H #define CONFIG_H int config_value = 5; #endif -> Option A
  4. Quick Check:

    Proper include guard syntax fixes multiple inclusion = A [OK]
Hint: Use #ifndef + #define + #endif to wrap header content [OK]
Common Mistakes:
  • Using #ifdef instead of #ifndef at start
  • Missing #define after #ifndef
  • Placing #define before #ifndef
5. You have two header files, a.h and b.h. Both define the same include guard macro HEADER_H. What problem can this cause when both headers are included in a source file?
hard
A. One header's content may be skipped, causing missing declarations
B. Both headers will be included twice, causing errors
C. No problem; include guards work independently
D. Compilation will fail due to macro redefinition

Solution

  1. Step 1: Understand include guard macro uniqueness

    Include guards rely on unique macro names to prevent multiple inclusions of the same file.
  2. Step 2: Analyze effect of duplicate guard macros

    If both headers use the same macro, the second header's content is skipped because the macro is already defined, causing missing declarations.
  3. Final Answer:

    One header's content may be skipped, causing missing declarations -> Option A
  4. Quick Check:

    Duplicate guard macros cause skipped header content = A [OK]
Hint: Use unique guard macros per header file [OK]
Common Mistakes:
  • Assuming include guards are independent even with same macro
  • Expecting compilation failure due to macro redefinition
  • Thinking both headers get included twice
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard