Header and source file organization - Time & Space Complexity
When organizing code into header and source files, it's important to understand how this affects the program's execution steps.
We want to see how the program's running time changes as the size of the input or code grows.
Analyze the time complexity of the following code snippet.
// header.h
#ifndef HEADER_H
#define HEADER_H
void printNumbers(int n);
#endif
// source.c
#include "header.h"
#include <stdio.h>
void printNumbers(int n) {
for (int i = 1; i <= n; i++) {
printf("%d\n", i);
}
}
int main() {
printNumbers(5);
return 0;
}
This code separates the function declaration in a header file and the function definition in a source file. The function prints numbers from 1 to n.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop inside
printNumbersthat prints numbers. - How many times: It runs exactly
ntimes, once for each number from 1 ton.
As n grows, the number of print operations grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print calls |
| 100 | 100 print calls |
| 1000 | 1000 print calls |
Pattern observation: The work increases evenly as n increases. Double the input, double the work.
Time Complexity: O(n)
This means the program takes longer in direct proportion to the input size n.
[X] Wrong: "Splitting code into header and source files makes the program run faster."
[OK] Correct: Organizing code this way helps keep code clean and manageable, but it does not change how many steps the program takes when running.
Understanding how code organization relates to program speed shows you think about both writing clean code and how it runs, a useful skill in real projects.
"What if the function printed numbers twice inside the loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand header file role
Header files declare functions and variables so multiple source files can use them.Step 2: Differentiate from source files
Source files contain the actual code, not declarations.Final Answer:
To declare functions and variables for sharing between source files -> Option BQuick Check:
Header files declare, source files define [OK]
- Confusing header files as containing code
- Thinking header files execute code
- Mixing compiled code with header files
math_utils.h in a C source file?Solution
Step 1: Identify correct include syntax for user headers
User-defined headers use double quotes: #include "filename".Step 2: Differentiate from system headers
System headers use angle brackets, but user headers like math_utils.h use quotes.Final Answer:
#include "math_utils.h" -> Option CQuick Check:
User headers use quotes [OK]
- Using angle brackets for user headers
- Using import instead of #include
- Omitting # in include directive
// 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?
Solution
Step 1: Understand function declaration and definition
math_utils.h declares add(), math_utils.c defines it to return sum of a and b.Step 2: Trace main function call
main.c calls add(3, 4), which returns 7, then prints it.Final Answer:
7 -> Option AQuick Check:
3 + 4 = 7 [OK]
- Thinking add concatenates numbers
- Expecting compilation error due to missing code
- Confusing printf format
// 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?Solution
Step 1: Identify cause of error
printf is declared in stdio.h; utils.c uses printf but lacks this include.Step 2: Fix by including stdio.h in utils.c
Adding #include <stdio.h> in utils.c provides printf declaration, fixing error.Final Answer:
Add #include <stdio.h> in utils.c -> Option AQuick Check:
Include stdio.h where printf is used [OK]
- Including stdio.h only in header file
- Including stdio.h only in main.c
- Ignoring missing printf declaration
Solution
Step 1: Understand modular organization
Separating declarations in headers and definitions in source files allows sharing and reuse.Step 2: Avoid putting definitions in headers
Definitions in headers cause multiple definition errors when included in many files.Final Answer:
Put all function declarations in one header file and definitions in one source file, then include the header where needed -> Option DQuick Check:
Headers declare, sources define, include headers [OK]
- Defining functions in headers causing linker errors
- Not using headers causing duplicate declarations
- Putting all code in one file reducing modularity
Ready to prove it?
Pass the test and this pattern moves unknown
