modular organization prepared by mmd, edited by msy1

17
CHAPTER 2 MODULAR ORGANIZATION Prepared by MMD, Edited by MSY 1

Upload: malcolm-preston

Post on 17-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1

CHAPTER 2MODULAR ORGANIZATION

Prepared by MMD, Edited by MSY 1

Page 2: MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1

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.

Prepared by MMD, Edited by MSY 2

Introduction

Page 3: MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1

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.

Prepared by MMD, Edited by MSY 3

Standard Library

Page 4: MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1

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 <stdio.h> in our program.

Prepared by MMD, Edited by MSY 4

Standard Library

Page 5: MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1

Prepared by MMD, Edited by MSY 5

Compilation and linking of multiple program files in a project

Page 6: MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1

Phases of C Programs:◦Edit◦Preprocess◦Compile◦Link◦Load◦Execute

Prepared by MMD, Edited by MSY 6

Compilation and linking of multiple program files in a project

Deitel&Deitel: page 13-15

Page 7: MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1

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.

Prepared by MMD, Edited by MSY 7

Standard Library

Page 8: MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1

To enhance software reusability. Need to identify some “general-purpose”

functions that are expected to use in future in other projects.

Prepared by MMD, Edited by MSY 8

Programmer-defined Libraries

Page 9: MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1

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.

Prepared by MMD, Edited by MSY 9

Programmer-defined Libraries – Step-by-step

Page 10: MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1

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.

Prepared by MMD, Edited by MSY 10

Programmer-defined Libraries Step-by-step

Page 11: MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1

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.

Prepared by MMD, Edited by MSY 11

Programmer-defined Libraries Step-by-step

Page 12: MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1

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.

Prepared by MMD, Edited by MSY 12

Advantages of Using Programmer-defined Libraries

Page 13: MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1

#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);

Prepared by MMD, Edited by MSY 13

Example :Volumes.h

Page 14: MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1

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

}

Prepared by MMD, Edited by MSY 14

Example Volumes.c

Page 15: MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1

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;

}

Prepared by MMD, Edited by MSY 15

Example Volumes.c Cont…

Page 16: MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1

Prepared by MMD, Edited by MSY 16

Example VolumesApp.c

VolumesApp.c – application file

Page 17: MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1

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

Prepared by MMD, Edited by MSY 17

SUMMARY