Challenge - 5 Problems
Header and Source File Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of program with separated header and source files
Given the following files, what is the output when the program is compiled and run?
C
// file: math_ops.h #ifndef MATH_OPS_H #define MATH_OPS_H int add(int a, int b); int multiply(int a, int b); #endif // file: math_ops.c #include "math_ops.h" int add(int a, int b) { return a + b; } int multiply(int a, int b) { return a * b; } // file: main.c #include <stdio.h> #include "math_ops.h" int main() { int x = 3, y = 4; printf("Sum: %d\n", add(x, y)); printf("Product: %d\n", multiply(x, y)); return 0; }
Attempts:
2 left
💡 Hint
Remember that the header file declares functions and the source file defines them.
✗ Incorrect
The header file math_ops.h declares the functions add and multiply. The source file math_ops.c defines these functions. The main.c file includes the header and calls these functions. The output is the sum and product of 3 and 4, which are 7 and 12 respectively.
❓ Predict Output
intermediate2:00remaining
Result of including header file multiple times
What will happen if a header file without include guards is included multiple times in a source file?
C
// file: utils.h int square(int x) { return x * x; } // file: main.c #include "utils.h" #include "utils.h" int main() { int val = 5; return square(val); }
Attempts:
2 left
💡 Hint
Think about what happens when the same function is defined twice in one translation unit.
✗ Incorrect
Including the header file twice without include guards causes the function square to be defined twice in the same source file, leading to a compilation error due to multiple definitions.
🔧 Debug
advanced2:00remaining
Identify the cause of linker error in multi-file project
You have these files. Why does the linker give an 'undefined reference' error for function greet() when compiling main.c?
C
// file: greet.h #ifndef GREET_H #define GREET_H void greet(void); #endif // file: main.c #include "greet.h" int main() { greet(); return 0; }
Attempts:
2 left
💡 Hint
Check if the function greet() has a definition linked during compilation.
✗ Incorrect
The header greet.h declares greet(), but no source file defines it. The linker cannot find the function implementation, causing an undefined reference error.
📝 Syntax
advanced2:00remaining
Correct syntax for include guards in header files
Which option shows the correct way to write include guards in a header file named config.h?
Attempts:
2 left
💡 Hint
Include guards start with #ifndef and then #define the same macro.
✗ Incorrect
Option C correctly uses #ifndef to check if CONFIG_H is not defined, then defines it, and ends with #endif. This prevents multiple inclusion.
🧠 Conceptual
expert2:00remaining
Best practice for function definitions in header files
Which statement about placing function definitions in header files is correct?
Attempts:
2 left
💡 Hint
Think about what happens if a function is defined in a header included by multiple source files.
✗ Incorrect
Defining functions in header files included by multiple source files causes multiple definitions at link time. Best practice is to put only declarations in headers and definitions in source files.