Bird
Raised Fist0
Cprogramming~20 mins

Header files and include directive - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Header Hero
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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"); }
ALinker error: undefined reference to printMessage
BCompilation error: function printMessage not declared
CHello from header!\nHello from header!
DHello from header!
Attempts:
2 left
💡 Hint
Remember that function definitions should be in source files, not headers.
❓ Predict Output
intermediate
2: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
ACompilation error: redefinition of VALUE
B0
CRuntime error
D10
Attempts:
2 left
💡 Hint
Include guards prevent multiple definitions.
🔧 Debug
advanced
2: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"); }
ABecause function printHello is not declared before use
BBecause angle brackets <> are used for user headers instead of quotes ""
CBecause function printHello is defined in header, causing multiple definition error
DBecause header file content is commented out
Attempts:
2 left
💡 Hint
Angle brackets are for system headers, quotes for user headers.
❓ Predict Output
advanced
2: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); }
A25\n25
BCompilation error: multiple definitions of printSquare
CRuntime error
D25\n10
Attempts:
2 left
💡 Hint
Macros are replaced by the preprocessor, functions need to be linked properly.
🧠 Conceptual
expert
2:00remaining
Why use include guards or #pragma once?
What is the main reason to use include guards or #pragma once in header files?
ATo allow headers to be included only in main.c file
BTo speed up compilation by skipping system headers
CTo prevent multiple inclusion of the same header causing redefinition errors
DTo automatically link libraries during compilation
Attempts:
2 left
💡 Hint
Think about what happens if a header is included multiple times.

Practice

(1/5)
1. What is the main purpose of a header file in C programming?
easy
A. To store declarations and share them between source files
B. To execute the main program logic
C. To compile the program faster
D. To store variable values during runtime

Solution

  1. Step 1: Understand what header files contain

    Header files usually contain declarations like function prototypes and macros, not executable code.
  2. Step 2: Understand their role in sharing code

    They allow multiple source files to share these declarations, so the compiler knows about functions and variables.
  3. Final Answer:

    To store declarations and share them between source files -> Option A
  4. Quick Check:

    Header files = declarations shared [OK]
Hint: Header files hold declarations, not code [OK]
Common Mistakes:
  • Thinking header files contain executable code
  • Confusing header files with source files
  • Believing header files speed up compilation
  • Assuming header files store variable values
2. Which of the following is the correct syntax to include a system header file named stdio.h?
easy
A. #include "stdio.h"
B. #include
C. include
D. #include stdio.h

Solution

  1. Step 1: Recall syntax for system headers

    System headers use angle brackets <> to tell the compiler to look in system directories.
  2. Step 2: Check each option

    #include uses #include with <stdio.h>, which is correct syntax. #include "stdio.h" uses quotes which are for user headers. Options C and D miss the # or angle brackets.
  3. Final Answer:

    #include <stdio.h> -> Option B
  4. Quick Check:

    System headers use <> [OK]
Hint: Use < > for system headers, " " for your own [OK]
Common Mistakes:
  • Using quotes for system headers
  • Omitting the # in #include
  • Not using angle brackets or quotes
  • Writing include without #
3. What will be the output of this C program?
#include <stdio.h>
#include "myheader.h"

int main() {
    printMessage();
    return 0;
}

Assuming myheader.h contains:
void printMessage() {
    printf("Hello from header!\n");
}
medium
A. No output
B. Compilation error: function not declared
C. Runtime error
D. Hello from header!

Solution

  1. Step 1: Understand #include "myheader.h" effect

    The content of myheader.h is inserted, so printMessage() is defined before main.
  2. Step 2: Check function call and output

    printMessage() prints "Hello from header!\n", so the program outputs that line.
  3. Final Answer:

    Hello from header! -> Option D
  4. Quick Check:

    Included function runs and prints message [OK]
Hint: Including header inserts code, so functions are available [OK]
Common Mistakes:
  • Assuming function is undeclared without prototype
  • Thinking header files can't contain function definitions
  • Confusing compile and runtime errors
  • Ignoring the newline in output
4. Identify the error in this code snippet:
#include 
#include 

int main() {
    printHello();
    return 0;
}

Assuming myheader.h is a user-defined header file in the project folder.
medium
A. Function printHello() is not defined
B. Missing semicolon after #include directives
C. Using angle brackets for a user-defined header file
D. Missing main function return type

Solution

  1. Step 1: Check #include syntax for user headers

    User headers should be included with quotes "myheader.h" to search local directories first.
  2. Step 2: Understand effect of wrong include syntax

    Using angle brackets makes compiler look in system directories, so it may not find myheader.h causing an error.
  3. Final Answer:

    Using angle brackets for a user-defined header file -> Option C
  4. Quick Check:

    User headers use "" not <> [OK]
Hint: Use "" for your headers, < > for system headers [OK]
Common Mistakes:
  • Using < > for user headers
  • Thinking #include needs semicolons
  • Assuming printHello() is always undefined
  • Ignoring main return type
5. You want to create a header file mathutils.h that declares a function int add(int a, int b); and use it in two source files main.c and calc.c. Which is the best practice to avoid multiple definition errors?
hard
A. Put only the function declaration in mathutils.h and define the function in one source file
B. Define the function in both source files and include mathutils.h everywhere
C. Put the function definition in mathutils.h and include it in both source files
D. Include mathutils.h only in main.c and define function in calc.c

Solution

  1. Step 1: Understand declarations vs definitions in headers

    Headers should contain declarations (function prototypes), not definitions (function bodies), to avoid multiple definitions.
  2. Step 2: Apply to multiple source files

    Define the function in one source file (e.g., calc.c) and declare it in mathutils.h included by both files.
  3. Final Answer:

    Put only the function declaration in mathutils.h and define the function in one source file -> Option A
  4. Quick Check:

    Declarations in headers, definitions in one source file [OK]
Hint: Declare in header, define once in source file [OK]
Common Mistakes:
  • Defining functions in headers causing multiple definitions
  • Not including header in all source files needing declaration
  • Defining function in multiple source files
  • Including header only in one source file
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