Bird
Raised Fist0
Cprogramming~20 mins

Splitting code into multiple files - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Multi-file Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a multi-file C program

Consider a C program split into two files: main.c and helper.c. The helper.c file defines a function int add(int a, int b) that returns the sum of two integers. The main.c file calls this function and prints the result.

What will be the output when the program is compiled and run?

C
/* helper.c */
int add(int a, int b) {
    return a + b;
}

/* main.c */
#include <stdio.h>

int add(int a, int b); // function prototype

int main() {
    int result = add(3, 4);
    printf("Result: %d\n", result);
    return 0;
}
ACompilation error: undefined reference to 'add'
BResult: 34
CRuntime error: segmentation fault
DResult: 7
Attempts:
2 left
💡 Hint

Remember that the function add is defined in another file and declared in main.c.

❓ Predict Output
intermediate
2:00remaining
Effect of missing header file in multi-file C program

Given two files, main.c and math_ops.c. The math_ops.c file defines a function int multiply(int x, int y). The main.c file calls multiply but does not include a header file declaring it.

What is the most likely outcome when compiling and running this program?

C
/* math_ops.c */
int multiply(int x, int y) {
    return x * y;
}

/* main.c */
#include <stdio.h>

int main() {
    int result = multiply(5, 6);
    printf("Product: %d\n", result);
    return 0;
}
AProduct: 30
BCompilation error: implicit declaration of function 'multiply'
CRuntime error: segmentation fault
DProduct: 0
Attempts:
2 left
💡 Hint

Check if the function multiply is declared before use in main.c.

🔧 Debug
advanced
2:00remaining
Debugging linker error in multi-file C program

You have two files: main.c and utils.c. The main.c calls a function void greet() defined in utils.c. You compile with gcc main.c only. What error will you get?

C
/* utils.c */
#include <stdio.h>

void greet() {
    printf("Hello!\n");
}

/* main.c */

void greet();

int main() {
    greet();
    return 0;
}
ACompilation error: missing prototype for greet
BProgram runs and prints 'Hello!'
CLinker error: undefined reference to 'greet'
DRuntime error: segmentation fault
Attempts:
2 left
💡 Hint

Think about what happens if you compile only one source file but call a function defined in another.

📝 Syntax
advanced
2:00remaining
Correct way to declare external function in multi-file C program

Which of the following is the correct way to declare a function int compute(int a, int b) defined in another file so it can be used in main.c?

Aint compute(int a, int b);
Bextern int compute();
Cint compute();
Dvoid compute(int a, int b);
Attempts:
2 left
💡 Hint

Function declarations should match the function signature exactly.

🚀 Application
expert
2:00remaining
Number of object files needed for multi-file C project

You have a C project with 4 source files: main.c, math.c, io.c, and utils.c. You compile each source file separately into object files and then link them into an executable.

How many object files will you have before linking?

A4
B1
C3
D5
Attempts:
2 left
💡 Hint

Each source file produces one object file when compiled separately.

Practice

(1/5)
1. What is the main reason to split C code into multiple files?
easy
A. To reduce the number of variables
B. To make the program run faster
C. To organize code and make it reusable
D. To avoid using header files

Solution

  1. Step 1: Understand code organization

    Splitting code into multiple files helps keep related code parts separate and easier to manage.
  2. Step 2: Recognize code reuse benefit

    It also allows reusing code in different programs without rewriting it.
  3. Final Answer:

    To organize code and make it reusable -> Option C
  4. Quick Check:

    Splitting code = Organize and reuse [OK]
Hint: Think about code clarity and reuse benefits [OK]
Common Mistakes:
  • Assuming splitting files makes code run faster
  • Thinking header files are not needed
  • Believing it reduces variables automatically
2. Which of the following is the correct way to include a header file named utils.h in a C source file?
easy
A. #include utils.h
B. #include
C. include "utils.h"
D. #include "utils.h"

Solution

  1. Step 1: Understand header file inclusion syntax

    To include your own header files, use double quotes around the filename.
  2. Step 2: Differentiate system and user headers

    Angle brackets are for system headers, double quotes for user headers like utils.h.
  3. Final Answer:

    #include "utils.h" -> Option D
  4. Quick Check:

    User header files use "filename" [OK]
Hint: Use quotes for your headers, angle brackets for system [OK]
Common Mistakes:
  • Using angle brackets for user headers
  • Omitting # in include directive
  • Not using quotes or brackets around filename
3. Given two files:
// file1.c
#include 
void greet();
int main() {
    greet();
    return 0;
}
// file2.c
#include 
void greet() {
    printf("Hello from file2!\n");
}

What is the output when compiled and run together?
medium
A. Hello from file2!
B. Compilation error: greet not declared
C. No output
D. Runtime error

Solution

  1. Step 1: Check function declaration and definition

    file1.c declares greet() and calls it; file2.c defines greet() printing the message.
  2. Step 2: Understand linking multiple files

    Compiling both files together links greet() correctly, so the program prints the message.
  3. Final Answer:

    Hello from file2! -> Option A
  4. Quick Check:

    Function declared and defined across files prints message [OK]
Hint: Declare before use, define in another file, link both [OK]
Common Mistakes:
  • Forgetting to compile both files together
  • Missing function declaration in file1.c
  • Expecting compilation error due to separate files
4. You have two files:
// main.c
#include "helper.h"
int main() {
    print_message();
    return 0;
}
// helper.c
#include 
void print_message() {
    printf("Hi!\n");
}
// helper.h
void print_message();

Compiling with gcc main.c gives an error: undefined reference to 'print_message'. What is the likely cause?
medium
A. Missing semicolon in helper.h
B. helper.c was not compiled or linked properly
C. print_message() is not declared in main.c
D. main.c does not include stdio.h

Solution

  1. Step 1: Understand the error message

    "undefined reference" means the linker can't find the function definition.
  2. Step 2: Check compilation command

    If helper.c is not compiled or linked, the function print_message() is missing at link time.
  3. Final Answer:

    helper.c was not compiled or linked properly -> Option B
  4. Quick Check:

    Linker error = missing compiled file [OK]
Hint: Always compile and link all source files together [OK]
Common Mistakes:
  • Not compiling helper.c with main.c
  • Ignoring linker errors thinking it's a syntax error
  • Assuming declaration alone is enough
5. You want to split a program into main.c and math_ops.c. math_ops.c defines int add(int a, int b). How should you organize files and compile to use add in main.c correctly?
hard
A. Declare add in math_ops.h, include it in main.c, compile with gcc main.c math_ops.c
B. Define add in main.c and call it from math_ops.c, compile separately
C. Include math_ops.c directly in main.c with #include, compile main.c only
D. Declare add in main.c only, compile math_ops.c separately

Solution

  1. Step 1: Create a header file math_ops.h

    Declare the function prototype int add(int a, int b); in math_ops.h.
  2. Step 2: Include math_ops.h in main.c and compile both files

    Include math_ops.h in main.c to use add(), then compile with gcc main.c math_ops.c to link both.
  3. Final Answer:

    Declare add in math_ops.h, include it in main.c, compile with gcc main.c math_ops.c -> Option A
  4. Quick Check:

    Header declares, source defines, compile both [OK]
Hint: Use header for declarations, compile all sources together [OK]
Common Mistakes:
  • Including .c files instead of headers
  • Not declaring functions before use
  • Compiling only 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