0
0
Cprogramming~10 mins

Splitting code into multiple files - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Splitting code into multiple files
Start: main.c
Include header.h
Call function from utils.c
Link all files
Run program
Output result
The main file includes a header to use functions from another file. The compiler links all files to create the final program.
Execution Sample
C
#include "utils.h"

int main() {
    int result = add(3, 4);
    return result;
}
This main file calls the add function defined in another file.
Execution Table
StepFileActionDetailsOutput/Result
1main.cIncludeIncludes utils.h to use add()No output
2main.cCall functionCalls add(3, 4)No output
3utils.cExecute functionadd returns 7Returns 7
4main.cStore resultresult = 7No output
5main.cReturn from mainReturns 7 to OSProgram ends with code 7
💡 Program ends after main returns the result from add function.
Variable Tracker
VariableStartAfter add(3,4)Final
resultundefined77
Key Moments - 3 Insights
Why do we include a header file instead of the .c file?
We include the header file (utils.h) to tell the compiler about the function's existence and signature. The actual code is in utils.c, which is linked later. See execution_table step 1 and 3.
How does the program know where the add function is?
The compiler uses the header file to check the function call, and the linker connects main.c and utils.c so the function code is found. See execution_table steps 1, 3, and 4.
What happens if we forget to link utils.c?
The linker will give an error because it can't find the add function's code. The program won't compile successfully. This is why linking all files is important (concept_flow step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'result' after step 3?
A3
B7
Cundefined
D4
💡 Hint
Check variable_tracker after add(3,4) and execution_table step 3.
At which step does the program return the final value to the operating system?
AStep 5
BStep 2
CStep 1
DStep 3
💡 Hint
Look at execution_table step 5 where main returns.
If we remove the include of utils.h in main.c, what will happen?
AThe program will compile and run fine.
BThe linker will fail to find add function.
CThe compiler will warn or error about missing function declaration.
DThe program will return 0 instead of 7.
💡 Hint
Refer to key_moments about including header files and compiler checks.
Concept Snapshot
Splitting code into multiple files:
- Put function declarations in header (.h) files.
- Put function definitions in source (.c) files.
- Include headers in files that use those functions.
- Compile all .c files and link them together.
- This keeps code organized and reusable.
Full Transcript
This example shows how to split C code into multiple files. The main.c file includes utils.h to use the add function. The add function is defined in utils.c. When compiling, both files are linked so the program knows where to find add. The variable 'result' stores the return value of add(3,4), which is 7. The program ends by returning this value. Including the header file lets the compiler check the function call. Forgetting to link utils.c causes errors. This method helps organize code by separating declarations and definitions.