Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CHAPTER 3 MODULAR PROGRAMMING. 2 Introduction  A library in C is a collection of general purpose and related functions.  2 types of libraries: Standard.

Similar presentations


Presentation on theme: "1 CHAPTER 3 MODULAR PROGRAMMING. 2 Introduction  A library in C is a collection of general purpose and related functions.  2 types of libraries: Standard."— Presentation transcript:

1 1 CHAPTER 3 MODULAR PROGRAMMING

2 2 Introduction  A library in C is a collection of general purpose and related functions.  2 types of libraries: Standard libraries Programmer defined libraries  Standard library is a collection of functions provided by the vendor of the C IDE as an integral part of it.

3 3 Standard Library  Eg. stdio, math, stdlib, string, etc.  Standard library consist of: A header file, stored in the standard include directory of the C IDE. It contains the declarations of the standard functions (function prototypes) in the library and the declarations of related constants and data types. An implementation file, a compiled version of a C program that contains the implementations of the standard functions declared in the header file.

4 4 Standard Library  If we need some standard functions for our program, we must use the include preprocessor directive for the library that contains these functions.  Eg. to use printf(), scanf() functions, include preprocessor directive #include in our program.

5 C Development Environment Disk Phase 2 : Preprocessor program processes the code. Disk Compiler Phase 3 : Compiler creates object code and stores it on Disk. Preprocessor Disk Linker Phase 4 : Editor Phase 1 : Program is created using the Editor and stored on Disk. Disk Linker links object code with libraries, creates a.out and stores it on Disk

6 C Development Environment (con’t) Loader Phase 5 : :.:. Primary Memory Loader puts Program in Memory C P U (execute) Phase 6 : :.:. Primary Memory CPU takes each instruction and executes it, storing new data values as the program executes.

7 7 Compilation and linking of multiple program files in a project

8 8 Standard Library  By using standard libraries, we are introducing some modules into our application.  So, a modular program is modular because it contains two or more programmer-defined functions, because it uses some standard or programmer-defined libraries, or both.

9 9 Programmer-defined Libraries  To enhance software reusability.  Need to identify some “general-purpose” functions that are expected to use in future in other projects.

10 10 1. Identify the reusable modules during the design phase of software development. 2. Create header file (*.h file) that contains only the declarations of functions, global variables, named constant, typedefs, and programmer-defined data types. 3. Create the implementation file (*.c file) of general functions (function definitions) that we declared in the header file. Programmer-defined Libraries – Step-by-step

11 11 4. Create the application file (source file) to solve a specific problem that use the general- purpose functions. Include the preprocessor directive #include “header_file_name” Include the preprocessor directive of other standard libraries used by your program and/or by the implementation file of general functions. Write the function definitions of for the rest of your program including main() function. Programmer-defined Libraries Step-by-step

12 12 5. Create a new project and insert into it the application file and the implementation files. Go ahead with compilation, linking and executing the project. Programmer-defined Libraries Step-by-step

13 13 Advantages of Using Programmer- defined Libraries  Enhance module reusability.  Libraries make independent coding easier in large-scale software development projects.  Simplify program testing and ensure reliable functions.  Facilitate program maintenance.  Facilitate procedural abstraction and information hiding.  Implementation files can be compiled separately and can be used in their object form in large-scale projects.

14 14 Example :Volumes.h #define pi 3.142 typedef double radius_type; typedef double height_type; typedef double length_type; double right_circular_cylinder(radius_type, height_type); double right_circular_cone(radius_type, height_type); double sphere(radius_type); double ellipsoid(length_type, length_type, length_type);

15 15 Example Volumes.c #include "Volumes.h" double right_circular_cylinder(radius_type r, height_type h) { double volume; volume = pi * r * r * h; return volume; } double right_circular_cone(radius_type r, height_type h) { double volume; volume = pi * r * r * h/3; return volume; }

16 16 double sphere(radius_type r) { double volume; volume = 4 * pi * (r * r * r)/3; return volume; } double ellipsoid(length_type a, length_type b, length_type c) { double volume; volume = 4 * pi * a * b * c/3; return volume; } Example Volumes.c Cont…

17 17 SUMMARY  In this chapter, we have learnt about: 2 types of libraries : Std lib vs programmer defined lib The step-by-step on how to create programmer defined lib The advantages of programmer defined lib The example of program


Download ppt "1 CHAPTER 3 MODULAR PROGRAMMING. 2 Introduction  A library in C is a collection of general purpose and related functions.  2 types of libraries: Standard."

Similar presentations


Ads by Google