Modular Programming
Introduction As programs grow larger and larger, it is more desirable to split them into sections or modules. C allows programs to be split into multiple files, compiled separately, and then combined (linked) to form a single program. we will go through a programming example, discussing the C techniques needed to create good modules. You will be shown how to use make to put these modules together to form a pro gram.
Modules A module is a collection of functions that perform related tasks. database functions such as lookup, enter, and sort. An efficient way of splitting up a large project is to assign each programmer a different module. In this manner, each programmer only worries about the internal details of a particular module. Modules are divided into two parts public and private. The public part how to call the functions in the module. It contains the definition of data structures and functions that are to be used outside the module. The private part Anything that is internal to the module is private. Everything that is not directly usable by the outside world should be kept private.
Definition, implementation, and use of the module
The extern Modifier The extern modifier is used to indicate that a variable or function is defined outside the current file. #include /* number of times through the loop */ extern int counter; /* routine to increment the counter */ extern void inc_counter(void); main() { int index; /* loop index */ for (index = 0; index < 10; index++) inc_counter(); printf("Counter is %d\n", counter); return (0); } /* number of times through the loop */ int counter = 0; /* trivial example */ void inc_counter(void) { ++counter; }
Modifiers
Headers Information that is shared between modules should be put in a header file. By convention, all header filenames end with.h. The header should contain all the public information, such as: A comment section describing clearly what the module does and what is available to the user. Common constants. Common structures. Prototypes of all the public functions. extern declarations for public variables.
/******************************************************** * Definitions for the infinite array (ia) package. * * * * An infinite array is an array whose size can grow * * as needed. Adding more elements to the array * * will just cause it to grow. * * * * struct infinite_array Used to hold the information for an infinite * * array. * * * * Routines * * * * ia_init -- Initializes the array. * * ia_store -- Stores an element in the array. * * ia_get -- Gets an element from the array. * ********************************************************/ /* number of elements to store in each cell of the infinite array */ #define BLOCK_SIZE 10 struct infinite_array { /* the data for this block */ float data[BLOCK_SIZE]; /* pointer to the next array */ struct infinite_array *next; }; /******************************************************** * ia_init -- Initializes the infinite array. * * * * Parameters * * array_ptr -- The array to initialize. * ********************************************************/ #define ia_init(array_ptr) {(array_ptr)->next = NULL;}
/******************************************************** * ia_get -- Gets an element from an infinite array. * * * * Parameters * * array_ptr -- Pointer to the array to use. * * index -- Index into the array. * * * * Returns * * The value of the element. * * * * Note: You can get an element that * * has not previously been stored. The value * * of any uninitialized element is zero. * ********************************************************/ extern int ia_get(struct infinite_array *array_ptr, int index); /******************************************************** * ia_store -- Store an element in an infinite array. * * * * Parameters * * array_ptr -- Pointer to the array to use. * * index -- index into the array. * * store_data -- Data to store. * ********************************************************/ extern void ia_store(struct infinite_array * array_ptr, int index, int store_data);
The Makefile for Multiple Files As programs grow, the number of commands needed to create them grows. Typing a series of 10 or 20 commands can be tiresome and error prone, so programmers started writing shell scripts The program make is designed to aid the programmer in compiling and linking programs. The program make was created to make compilation dependent upon whether a file has been updated since the last compilation. The file Makefile (case sensitivity is important in UNIX) contains the rules used by make to decide how to build the program.
Makefile The Makefile contains the following sections: Comments Any line beginning with a hash mark (#) is a comment. Macros A macro has the format: name = data name is any valid identifier and data is the text that will be substituted whenever make sees $(name). Explicit rules Explicit rules tell make what commands are needed to create the program. Default rules make uses a set of built-in rules to determine what command to execute.
Explicit rules Format target is the name of a file to create. It is "made" or created out of the source file source. If the target is created out of several files, they are all listed. The command that generates the target is specified on the next line. Commands are listed one per line. Each is indented by a tab. target: source [source2] [source3] command [command] [command]... hello: hello.c cc -g -ohello hello.c
Makefile CFLAGS = -g OBJ=ia.o hist.o all: hist hist: $(OBJ) $(CC) $(CFLAGS) -o hist $(OBJ) hist.o:ia.h hist.c ia.o:ia.h ia.c Macros Explicit Rules Default Rules $(CC) $(CFLAGS) -c file.c
Example [File: ia/makefile.gcc] # # # Makefile for UNIX systems # # using a GNU C compiler. # # # CC=gcc CFLAGS=-g -Wall -D__USE_FIXED_PROTOTYPES__ -ansi all: hist hist: hist.o ia.o $(CC) $(CFLAGS) -o hist hist.o ia.o hist.o: hist.c ia.h ia.o: ia.c ia.h clean: rm -f hist hist.o ia.o
Modular Programming Exercise Requirements Input the width, length, height from keyboard. Calculate volume, area, girth. Print the value. Files main.c Main function file to call the functions. input.c The functions and data of input-related calc.c The functions and data of calcuate-related print.c The functions and data of print-related global.h The global header file. Makefile
Modules int input_lengths (int * width, int * length, int * height); int calc_volume (int width, int length, int height); int calc_area(int width, int length, int height); int calc_girth(int width, int length, int height); void print_data(char* string, int value);
The End !
Notice Homework Dead Line : PM 1:00 Question & Claim Phone : Visit : 302 dong ho (AM 10:00 ~) Dead Line : 6. 23