1Modules and InterfacesCS 2172Review: Constants• C has several ways to define a constant• Use #define #define MAX_VALUE 10000 Substitution by preprocessing (will talk about this later)• Use “const” const double x = 1.56; Qualifier to declare that a variable is a constant• Declare an enumerate constant type enum color { WHITE, YELLOW, BLUE, RED };enum color c2; Offers the chance of checking3Modules• Programs are made up of many modules• Each module is small and does one thing Set, stack, queue, list, etc. String manipulation Mathematical functions• Deciding how to break up a program into modules is a key to good software design4Clients, Interfaces, Implementations• Interfaces (Application Programming Interfaces or APIs) are contracts between clients and implementations Clients must use interface correctly Implementations must do what they advertiseClientInterfaceImplementation5Interfaces• An interface defines what the module does Decouple clients from implementation Hide implementation details• An interface specifies… Data types and variables Functions that may be invokedextern int counter;extern void counter_init();extern void counter_inc();counter1.h6Interfaces• An interface defines what the module does Decouple clients from implementation Hide implementation details• An interface specifies… Data types and variables Functions that may be invokedtypedef struct {StrList *entries;int size;} StrList;extern StrList *StrList_create(void);extern void StrList_delete(StrList *list);extern void StrList_insert(StrList *list, char *string);extern void StrList_remove(StrList *list, char *string);extern int StrList_write(StrList *list);typedef struct {StrList *entries;int size;} StrList;extern StrList *StrList_create(void);extern void StrList_delete(StrList *list);extern void StrList_insert(StrList *list, char *string);extern void StrList_remove(StrList *list, char *string);extern int StrList_write(StrList *list);strlist.h:7Implementations• An implementationdefines how the module does it• Can have many implementations for one interface Different algorithms for different situations Machine dependencies, efficiency, etc.#include "counter1.h"int counter;void counter_init() {counter = 0;}void counter_inc() {counter++;}counter1.c8Implementations• An implementationdefines how the module does it• Can have many implementations for one interface Different algorithms for different situations Machine dependencies, efficiency, etc.#include “strlist.h”StrList *StrList_create(void){StrList *list = malloc(sizeof(StrList));list->entries = NULL;list->size = 0;}void StrList_delete(StrList *list){free(list);}. . .#include “strlist.h”StrList *StrList_create(void){StrList *list = malloc(sizeof(StrList));list->entries = NULL;list->size = 0;}void StrList_delete(StrList *list){free(list);}. . .9Clients• A client uses a module via its interface• Clients see only the interface Can use module without knowing its implementation• Client is unaffected if implementation changes As long as interface stays the same#include <stdio.h>#include "counter1.h"main() {counter_init();counter_inc();counter_inc();printf("%d\n", counter);}test1.c10Clients• A client uses a module via its interface• Clients see only the interface Can use module without knowing its implementation• Client is unaffected if implementation changes As long as interface stays the same#include “strlist.h”int main(){StrList *list = StrList_create();StrList_insert(list, “CS217”);StrList_insert(list, “is”);StrList_insert(list, “fun”);StrList_write(list);StrList_delete(list);}#include “strlist.h”int main(){StrList *list = StrList_create();StrList_insert(list, “CS217”);StrList_insert(list, “is”);StrList_insert(list, “fun”);StrList_write(list);StrList_delete(list);}11C Programming Conventions• Interfaces are defined in header files (.h)strlist.htypedef struct {StrList *entries;int size;} StrList;extern StrList *StrList_create(void);extern void StrList_delete(StrList *list);extern void StrList_insert(StrList *list, char *string);extern void StrList_remove(StrList *list, char *string);extern int StrList_write(StrList *list);typedef struct {StrList *entries;int size;} StrList;extern StrList *StrList_create(void);extern void StrList_delete(StrList *list);extern void StrList_insert(StrList *list, char *string);extern void StrList_remove(StrList *list, char *string);extern int StrList_write(StrList *list);extern int counter;extern void counter_init();extern void counter_inc();counter1.h12C Programming Conventions• Implementations are described in source files (.c)#include "counter1.h"int counter;void counter_init() {counter = 0;}void counter_inc() {counter++;}counter1.cstrlist.c#include “strlist.h”StrList *StrList_create(void){StrList *list = malloc(sizeof(StrList));list->entries = NULL;list->size = 0;}void StrList_delete(StrList *list){free(list);}. . .#include “strlist.h”StrList *StrList_create(void){StrList *list = malloc(sizeof(StrList));list->entries = NULL;list->size = 0;}void StrList_delete(StrList *list){free(list);}. . .13#include <stdio.h>#include "counter1.h"main() {counter_init();counter_inc();counter_inc();printf("%d\n", counter);}C Programming Conventions• Clients “include” header filesmain.c#include “strlist.h”int main(){StrList *list = StrList_create();StrList_insert(list, “CS217”);StrList_insert(list, “is”);StrList_insert(list, “fun”);StrList_write(list);StrList_delete(list);}#include “strlist.h”int main(){StrList *list = StrList_create();StrList_insert(list, “CS217”);StrList_insert(list, “is”);StrList_insert(list, “fun”);StrList_write(list);StrList_delete(list);}test1.c14Standard C Librariesassert.hctype.herrno.hmath.hlimits.hsignal.hstdarg.hstddef.hstdio.hstdlib.hstring.htime.hassertionscharacter mappingserror numbersmath functionsmetrics for intssignal handlingvariable length arg listsstandard definitionsstandard I/Ostandard library functionsstring functionsdate/type functions15Standard C Libraries, cont’d• Utility functions stdlib.hatof, atoi, rand, qsort, getenv, calloc, malloc, free, abort, exit• String handling string.hstrcmp, strncmp, strcpy, strncpy, strcat,strncat, strchr, strlen, memcpy, memcmp• Character classifications ctypes.hisdigit, isalpha, isspace, isupper, islower• Mathematical functions math.hsin, cos, tan,
View Full Document