Top-Down Design Approach Using Multi-File Programming in C
Top-Down Design Approach Using Multi-File Programming in C
&
Multi-File Programming in C
CSC 1401
Computer Programming
Mouna Kettani
Intended Learning Outcome (ILO)
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
// Function prototypes
int add(int a, int b);
int subtract(int a, int b);
#endif
Source file/Library: functions.c
#include "functions.h"
#include <stdio.h>
#include "functions.h"
int main() {
int a = 10, b = 5;
printf("Addition: %d\n", add(a, b));
printf("Subtraction: %d\n", subtract(a, b));
return 0;
}
Commands to compile, link and run a
multi-file project
Before the implementation of the functions source files/library, we can partially compile
main.c using the command : gcc – c main.c
After the implementation of the library (functions.c) file, we need to compile it using the
command: gcc – c functions.c.
If no errors are generated, we can link our main and source files and generate the
executable using the command:
gcc main.o functions.o –o App.exe
Then, we run/execute the whole application using this command:
./App
Note that these commands can be combined depending on the question asked (partially compile,
compile, link object files, load then execute).
Benefits of Top-Down Design