310 likes | 328 Views
Learn how to reduce complexity in large software systems using procedural and data abstractions along with personal libraries in C programming. The chapter discusses header files, implementation files, and storage classes.
E N D
Chapter 13 Programming in the Large
Objectives • Difficulties of Developing Large Software Systems • Reduce the complexity of development and maintenance. • Flexible Macros • Storage classes of variables and functions • Library of reusable code • Additional preprocessor directives
Using Abstraction to Manage Complexity (1/2) • Procedural Abstraction: separation of whata function does from how the function accomplishes its purpose. • Data Abstraction: separation of the logical view of a data object (what is stored) from the physical view (how the information is stored).
Using Abstraction to Manage Complexity (2/2) • Both enable the designer to make implementation decision in a piecemeal fashion Information Hiding • Information Hiding: protecting the implementation details of a low-level module from direct access by a high-level module. • Reusable code • Encapsulate a data object and its operators in a personal library. #include
Personal Libraries: Header Files • Copying the code of the functions into other programs to allow reuse is possible but cumbersome. • C permits source code files to be complied separately and then linked prior to loading and execution.
Header files • To create a personal library, we must first make a header file. • Header file: text file containing the interface information about a library needed • by a complier to translate a program system that used the library or • by a person to understand and use the library.
Typical Contents of a Header File • A block comment summarizing the library’s purpose • #define directives naming constant macros • Type definitions • Block comments stating the purpose of each library function and declarations of the form.
Notes for Header File Design • The common boundary between two separate parts of a solution is called the interface. • The header file’s purpose is to define the interface between a library and any program that uses the library. • Cautionary Notes: the constant macro defined (PLANET_STRSIZ) has a long name that begins with the library name. This reduces the conflict likelihood.
Personal Libraries: Implementation Files • Implementation File: file containing the C source code of a library’s functions and any other information needed for compilation of these functions. • The elements of an implementation file: • A block comment summarizing the library’s purpose. • #include directives for this library’s header file and for other libraries used by the functions in this library. • #define directives naming constant macros used only inside this library. • Type definitions used only inside this library. • Function definitions including the usual comments.
Use Functions from a Personal Library • Angular brackets <stdio.h>: indicate to the preprocessor that a header file is to be found in a system directory. • Quotes “planet.h”: a library belonging to the programmer.
Storage Classes • C has five storage classes • auto • extern • global variable • static • register
Auto and Extern Variables • auto: default storage class of function parameters and local variables • Storage is automatically allocated on the stack at the time of a function call and deallocated when the function returns. • extern: storage class of names known to linker. • the name of the functions themselves
Global Variables • A variable that may be accessed by many functions in a program. • Only the defining declaration, the one in eg1.c, allocates spaces for global_var_x. A declaration beginning with the keyword extern allocated no memory; it simply provides information for the computer. /* eg1.c */ int global_var_x; void afun(int n) … /* eg2.c */ extern int global_var_x; void bfun(int n) …
Static Variables • static: storage class of variables allocated only once, prior to program execution. int fun_frag(int n) { staticint once = 0; int many = 0; } • many is allocated space on the stack each time fun_frag is called. Every time fun_frag returns, many is deallocated. • Static variable once is allocated and initialized one time, prior to program execution. It remains allocated until the entire program terminates.
Register variables • Storage class of automatic variable that the programmer would like to have stored in registers. • It is closely related to storage class auto and may be applied only to local variables and parameters. • By choosing storage class register, the programmer indicates an expectation that the program would run faster if a register, a special high-speed memory location inside the central processor, could be used for the variable.
Modifying Function for Inclusion in a Library • Error: return an error code or display an error message.
Conditional Compilation • C allows the user to select parts of a program to be complied and parts to be omitted. • This ability can be helpful. • Example 1: one can build in debugging printf calls when writing a function and then include these statements in the complied program only when they are needed.
Conditional Compilation • Example 2 Library sp_one (uses library sp) Library sp_two (uses library sp also) A program uses the facilities of both sp_one & sp_two. This would lead to inclusion of sp.h twice. C prohibits such duplicate declarations. • Example 3: Design a system for use on a variety of computers • Allows one to compile only the code appropriate for the current computer.
Arguments to Function main int main(int argc, /*input - argument count */ char *argv[])/*input - argument vector*/ • Command line arguments: options specified in the statement that activates a program. • Example: prog opt1 opt2 opt3 argc 4 argv[0] prog argv[1] opt1 argv[2] opt2 argv[3] opt3
See Fig 13.12 backup old.txt new.txt