Linking multiple files in C - Time & Space Complexity
When we link multiple files in C, the program combines code from different places. We want to see how this affects the time it takes to run.
How does adding more files change the total work the program does?
Analyze the time complexity of the following code snippet.
// file1.c
int sum(int a, int b) {
return a + b;
}
// file2.c
#include <stdio.h>
int sum(int a, int b);
int main() {
int result = sum(5, 7);
printf("Sum is %d\n", result);
return 0;
}
This code shows two files: one defines a function, the other calls it. They are linked to make one program.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The function call to
sumhappens once. - How many times: Exactly one time during program run.
Since the function is called once, the total work stays about the same no matter how many files we link.
| Input Size (number of files) | Approx. Operations |
|---|---|
| 1 | 1 function call |
| 5 | 1 function call |
| 10 | 1 function call |
Pattern observation: Adding more files does not increase the number of times the function runs.
Time Complexity: O(1)
This means the program runs in constant time because linking files does not add repeated work during execution.
[X] Wrong: "More files always make the program slower because it has more code to run."
[OK] Correct: The program only runs the code it calls. Extra files add code but do not slow down execution unless their functions are used repeatedly.
Understanding how linking affects program speed helps you explain how code organization impacts performance. This skill shows you think about both code structure and efficiency.
"What if the linked files contained functions called inside loops? How would the time complexity change?"
Practice
Solution
Step 1: Understand code organization
Linking multiple files allows splitting code into parts for better organization and reuse.Step 2: Understand linking process
After compiling separately, linking combines these parts into one executable program.Final Answer:
To organize code into separate files and combine them into one program -> Option AQuick Check:
Linking = Combining files [OK]
- Confusing linking with running multiple programs
- Thinking linking converts code to machine language
- Believing linking is for writing comments
main.c and utils.c together using GCC?Solution
Step 1: Understand GCC command structure
The correct syntax is: gcc [source files] -o [output file].Step 2: Check options order
The standard GCC syntax isgcc [source files] -o [output], as ingcc main.c utils.c -o program.Final Answer:
gcc main.c utils.c -o program -> Option CQuick Check:
gcc source files -o output [OK]
- Placing -o before source files
- Mixing order of files and output option
- Using output file name as source file
// file1.c
#include
void greet() { printf("Hello from file1\n"); }
// file2.c
void greet();
int main() { greet(); return 0; }What is the output after compiling and linking both files?
Solution
Step 1: Check function declaration and definition
file1.c defines greet() printing "Hello from file1"; file2.c declares greet() and calls it in main.Step 2: Linking files
Compiling and linking both files connects the call to greet() with its definition, so it prints the message.Final Answer:
Hello from file1 -> Option BQuick Check:
Function call linked correctly = prints message [OK]
- Forgetting to link both files
- Missing function declaration causing errors
- Expecting runtime errors without linking
// file1.c
void greet() { printf("Hi\n"); }
// file2.c
int main() { greet(); return 0; }When compiling separately and linking, it gives an error. Why?
Solution
Step 1: Check function declaration in file2.c
file2.c calls greet() but does not declare it or include a header with its prototype.Step 2: Understand compiler error
Without declaration, compiler errors occur because it doesn't know greet() signature.Final Answer:
greet() is not declared before use in file2.c -> Option DQuick Check:
Declare functions before use [OK]
- Assuming definition is enough without declaration
- Ignoring missing #include (not cause here)
- Thinking main() must be in same file as greet()
// math_ops.c
int add(int a, int b) { return a + b; }
// math_ops.h
int add(int a, int b);
// main.c
#include
#include "math_ops.h"
int main() {
printf("Sum: %d\n", add(3, 4));
return 0;
}Which is the correct way to compile and link these files to produce an executable named
calc?Solution
Step 1: Compile source files separately
Use gcc -c to compile main.c and math_ops.c into object files main.o and math_ops.o.Step 2: Link object files
Link main.o and math_ops.o together with gcc to create executable calc.Final Answer:
gcc main.c -c; gcc math_ops.c -c; gcc main.o math_ops.o -o calc -> Option AQuick Check:
Compile separately, then link object files [OK]
- Compiling header files directly
- Overwriting executable by compiling twice
- Not linking object files after compiling
Ready to prove it?
Pass the test and this pattern moves unknown
