Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Top-Down Design Approach Using Multi-File Programming in C

The document discusses using a top-down design approach and multi-file programming in C. It explains how to break problems into modules, use header files for function prototypes and source files for definitions. Benefits include scalability, collaboration, and maintenance. Example code is provided to demonstrate the concepts.

Uploaded by

Hiba Ait ijjou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Top-Down Design Approach Using Multi-File Programming in C

The document discusses using a top-down design approach and multi-file programming in C. It explains how to break problems into modules, use header files for function prototypes and source files for definitions. Benefits include scalability, collaboration, and maintenance. Example code is provided to demonstrate the concepts.

Uploaded by

Hiba Ait ijjou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Top-Down Design Approach

&
Multi-File Programming in C

CSC 1401
Computer Programming
Mouna Kettani
Intended Learning Outcome (ILO)

ILO5: Apply the top-down design for more advanced


problems along with multi-file programming using
at least a source file for the main function, a header
file for functions prototypes, and a source file for
functions definitions
Overview
• Top-Down Design:
• Breaks down complex problems into smaller,
manageable modules.
• Multi-File Programming:
• Utilizes separate source files for better
organization.
• Enhances code modularity and reusability.
Importance of Multi-File Programming

 Scalability: handles large code by dividing it into


smaller files
 Collaboration:multiple programmers can work on
different modules at the same time
 Maintenance: becomes easier by isolating
changes to specific modules only
Components of Multi-File Project

 1. Main.c: Contains the main() function


 2. Header.h: includes the function declarations/prototypes
 3. Library.c: includes the function implementation/definition
Top-Down Design Process

 1. Identify Modules: Break down the problem into smaller, cohesive


modules (functions)
 Define Interfaces: Design clear interfaces for communication
between modules.
 Implement Modules: Develop each module (function)
independently.
 Integration: combine modules into a cohesive program
Example: Top-Down Design using
multi-file approach

Main program: main.c


Header File: functions.h
Library (function source file): functions.c
Header File: functions.h

#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"

int add(int a, int b) {


return a + b;
}

int subtract(int a, int b) {


return a - b;
}
Main Program: main.c

#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

 Clarity:Clearly defines function boundaries and


interactions
 Incremental
Development: Facilitates gradual
development and testing of modules.
 Flexibility: Allows easy updates and addition of
modules
Summary
• Multi-file programming using a top-down design
approach:
• Enhances code organization, modularity, and maintainability.
• Facilitates collaboration and scalability.
• Apply these techniques to effectively manage complex C
projects.

You might also like