Challenge - 5 Problems
Header Hero
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of program with standard and user header includes
What is the output of this C program?
C
#include <stdio.h> #include "myheader.h" int main() { printMessage(); return 0; } // Contents of myheader.h // void printMessage() { printf("Hello from header!\n"); }
Attempts:
2 left
💡 Hint
Remember that function definitions should be in source files, not headers.
✗ Incorrect
Including a header with only a function definition inside a comment does not provide the function implementation to the linker. The program compiles but fails linking with undefined reference error.
❓ Predict Output
intermediate2:00remaining
Effect of include guards in header files
What will be the output of this program?
C
#include <stdio.h> #include "guarded.h" #include "guarded.h" int main() { printf("%d\n", VALUE); return 0; } // guarded.h content: // #ifndef GUARDED_H // #define GUARDED_H // #define VALUE 10 // #endif
Attempts:
2 left
💡 Hint
Include guards prevent multiple definitions.
✗ Incorrect
The include guard ensures the header content is included only once, so VALUE is defined once as 10.
🔧 Debug
advanced2:00remaining
Identify the error with include directive
Why does this code fail to compile?
C
#include <stdio.h> #include <myheader.h> int main() { printHello(); return 0; } // myheader.h content: // void printHello() { printf("Hello!\n"); }
Attempts:
2 left
💡 Hint
Angle brackets are for system headers, quotes for user headers.
✗ Incorrect
Using angle brackets tells the compiler to look in system directories, so it cannot find user header myheader.h and compilation fails.
❓ Predict Output
advanced2:00remaining
Output when including header with macro and function
What is the output of this program?
C
#include <stdio.h> #include "macrofunc.h" int main() { printf("%d\n", SQUARE(5)); printSquare(5); return 0; } // macrofunc.h content: // #define SQUARE(x) ((x)*(x)) // void printSquare(int x) { printf("%d\n", x*x); }
Attempts:
2 left
💡 Hint
Macros are replaced by the preprocessor, functions need to be linked properly.
✗ Incorrect
The macro SQUARE(5) expands to (5*5) = 25. The function printSquare prints 25. Both print 25.
🧠 Conceptual
expert2:00remaining
Why use include guards or #pragma once?
What is the main reason to use include guards or #pragma once in header files?
Attempts:
2 left
💡 Hint
Think about what happens if a header is included multiple times.
✗ Incorrect
Include guards or #pragma once prevent the compiler from processing the same header multiple times, avoiding errors from repeated definitions.