Bird
Raised Fist0
Cprogramming~20 mins

Header and source file organization - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Header and Source File Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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;
}
ACompilation error due to missing function definitions
BSum: 12\nProduct: 7
CSum: 34\nProduct: 12
DSum: 7\nProduct: 12
Attempts:
2 left
💡 Hint
Remember that the header file declares functions and the source file defines them.
❓ Predict Output
intermediate
2: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);
}
ACompilation error due to multiple definitions of square
BProgram runs and returns 25
CRuntime error due to duplicate function
DWarning but program compiles and runs
Attempts:
2 left
💡 Hint
Think about what happens when the same function is defined twice in one translation unit.
🔧 Debug
advanced
2: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;
}
Agreet() function is declared but not defined in any source file linked
BMissing include guard in greet.h causes multiple definitions
Cgreet() is defined static in another source file
Dmain.c is missing #include <stdio.h>
Attempts:
2 left
💡 Hint
Check if the function greet() has a definition linked during compilation.
📝 Syntax
advanced
2: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?
A#define CONFIG_H\n#ifndef CONFIG_H\n// declarations\n#endif
B#ifndef CONFIG_H\n// declarations\n#define CONFIG_H\n#endif
C#ifndef CONFIG_H\n#define CONFIG_H\n// declarations\n#endif
D#ifdef CONFIG_H\n#define CONFIG_H\n// declarations\n#endif
Attempts:
2 left
💡 Hint
Include guards start with #ifndef and then #define the same macro.
🧠 Conceptual
expert
2:00remaining
Best practice for function definitions in header files
Which statement about placing function definitions in header files is correct?
AFunction definitions must always be in header files to allow inlining by the compiler.
BFunction definitions should be in source files; header files only have declarations to avoid multiple definition errors.
CIt's best to put all function definitions in header files to simplify compilation.
DHeader files should contain only macro definitions and no functions.
Attempts:
2 left
💡 Hint
Think about what happens if a function is defined in a header included by multiple source files.

Practice

(1/5)
1. What is the main purpose of a header file in C programming?
easy
A. To execute the program's main logic
B. To declare functions and variables for sharing between source files
C. To contain the actual implementation code of functions
D. To store compiled machine code

Solution

  1. Step 1: Understand header file role

    Header files declare functions and variables so multiple source files can use them.
  2. Step 2: Differentiate from source files

    Source files contain the actual code, not declarations.
  3. Final Answer:

    To declare functions and variables for sharing between source files -> Option B
  4. Quick Check:

    Header files declare, source files define [OK]
Hint: Headers declare, sources define code [OK]
Common Mistakes:
  • Confusing header files as containing code
  • Thinking header files execute code
  • Mixing compiled code with header files
2. Which of the following is the correct way to include a header file named math_utils.h in a C source file?
easy
A. #include
B. import math_utils.h
C. #include "math_utils.h"
D. include math_utils.h

Solution

  1. Step 1: Identify correct include syntax for user headers

    User-defined headers use double quotes: #include "filename".
  2. Step 2: Differentiate from system headers

    System headers use angle brackets, but user headers like math_utils.h use quotes.
  3. Final Answer:

    #include "math_utils.h" -> Option C
  4. Quick Check:

    User headers use quotes [OK]
Hint: Use quotes for your own headers [OK]
Common Mistakes:
  • Using angle brackets for user headers
  • Using import instead of #include
  • Omitting # in include directive
3. Given these files:
// math_utils.h
int add(int a, int b);

// math_utils.c
#include "math_utils.h"
int add(int a, int b) { return a + b; }

// main.c
#include 
#include "math_utils.h"
int main() {
    printf("%d\n", add(3, 4));
    return 0;
}

What will be the output when running the compiled program?
medium
A. 7
B. 34
C. 0
D. Compilation error

Solution

  1. Step 1: Understand function declaration and definition

    math_utils.h declares add(), math_utils.c defines it to return sum of a and b.
  2. Step 2: Trace main function call

    main.c calls add(3, 4), which returns 7, then prints it.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Function returns sum, so output is 7 [OK]
Common Mistakes:
  • Thinking add concatenates numbers
  • Expecting compilation error due to missing code
  • Confusing printf format
4. Consider this code snippet:
// utils.h
void print_message();

// utils.c
#include "utils.h"
void print_message() {
    printf("Hello!\n");
}

// main.c
#include "utils.h"
int main() {
    print_message();
    return 0;
}

When compiling, an error says implicit declaration of function 'printf'. What is the fix?
medium
A. Add #include <stdio.h> in utils.c
B. Add #include <stdio.h> in utils.h
C. Add #include <stdio.h> in main.c
D. Remove print_message function

Solution

  1. Step 1: Identify cause of error

    printf is declared in stdio.h; utils.c uses printf but lacks this include.
  2. Step 2: Fix by including stdio.h in utils.c

    Adding #include <stdio.h> in utils.c provides printf declaration, fixing error.
  3. Final Answer:

    Add #include <stdio.h> in utils.c -> Option A
  4. Quick Check:

    Include stdio.h where printf is used [OK]
Hint: Include stdio.h in files using printf [OK]
Common Mistakes:
  • Including stdio.h only in header file
  • Including stdio.h only in main.c
  • Ignoring missing printf declaration
5. You want to organize a C project with multiple source files sharing utility functions. Which is the best practice for header and source file organization?
hard
A. Put function definitions in header files and include them everywhere
B. Put function declarations and definitions together in each source file without headers
C. Put all code in a single source file without headers
D. Put all function declarations in one header file and definitions in one source file, then include the header where needed

Solution

  1. Step 1: Understand modular organization

    Separating declarations in headers and definitions in source files allows sharing and reuse.
  2. Step 2: Avoid putting definitions in headers

    Definitions in headers cause multiple definition errors when included in many files.
  3. Final Answer:

    Put all function declarations in one header file and definitions in one source file, then include the header where needed -> Option D
  4. Quick Check:

    Headers declare, sources define, include headers [OK]
Hint: Declare in headers, define in sources, include headers [OK]
Common Mistakes:
  • Defining functions in headers causing linker errors
  • Not using headers causing duplicate declarations
  • Putting all code in one file reducing modularity
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