0
0
Cprogramming~20 mins

Splitting code into multiple files - Practice Problems & Coding Challenges

Choose your learning style9 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.