0
0
Cprogramming~20 mins

Include guards - Practice Problems & Coding Challenges

Choose your learning style9 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.