Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.

Similar presentations


Presentation on theme: "Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪."— Presentation transcript:

1 Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

2 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-2 Outline 13.1 USING ABSRACTION TO MANAGE COMPLEXITY 13.2 PERSONAL LIBRARIES: HEADER FILES 13.3 PERSONAL LIBRARIES: IMPLEMENTATION FILES 13.4 STORAGE CLASSES 13.5 MODIFYING FUNCTIONS FOR INCLUSION IN A LIBRARY 13.6 CONDITIONAL COMPILATION 13.7 ARGUMENTS TO FUNCTION MAIN 13.8 DEFINING MACROS WITH PARAMETERS 13.9 COMMON PROGRAMMING ERRORS

3 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-3 13.1 USING ABSRACTION TO MANAGE COMPLEXITY procedural abstraction –separation of what a function does from the details of 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) information hiding –protecting the implementation details of a lower-level module from direct access by a higher-level module encapsulate –packaging as a unit a data object and its operators

4 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-4 13.2 PERSONAL LIBRARIES: HEADER FILES header file –text file containing the interface information about a library needed by a compiler to translate a program system that uses the library or by a person to understand and use the library

5 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-5 Figure 13.1 Preparing a Program for Execution

6 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-6 HEADER FILES 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 extern prototype

7 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-7 Figure 13.2 Header File planet.h for Personal Library with Data Type and Associated Functions

8 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-8 Figure 13.2 Header File planet.h for Personal Library with Data Type and Associated Functions

9 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-9 Figure 13.3 Portion of Program That Uses Functions from a Personal Library

10 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-10 Cautionary Notes for Header File Design Notice that the constant macro defined has a long name that begins with the library name –This naming strategy reduces the likelihood that the name associated with a constant in the header file will conflict with other constant macro names in the program

11 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-11 13.3 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

12 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-12 Figure 13.4 Implementation File planet.c Containing Library with Planet Data Type and Operators

13 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-13 Figure 13.4 Implementation File planet.c Containing Library with Planet Data Type and Operators (cont’d)

14 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-14 Using a Personal Library Creation –C1 Create a header file containing the interface information for a program needing the library –C2 Create an implementation file containing the code of the library functions and other details of the implementation that are hidden from the user program –C3 Compile the implementation file. This step must be repeated any time either the header file or the implementation file is revised Use –U1 Include the library’s header file in the user program through an #include directive –U2 After compiling the user program, include both its object file and the object file created in C3 in the command that activates the linker

15 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-15 13.4 STORAGE CLASSES 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 the linker

16 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-16 Figure 13.5 Storage Classes auto and extern as Previously Seen

17 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-17 Figure 13.5 Storage Classes auto and extern as Previously Seen (cont’d)

18 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-18 Global Variables global variable –a variable that may be accessed by many functions in a program

19 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-19 Figure 13.7 Use of Variables of Storage Class extern

20 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-20 Storage Classes static and register static –storage class of variables allocated only once, prior to program execution register –storage class of automatic variables that the programmer would like to have stored in registers

21 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-21 13.5 MODIFYING FUNCTIONS FOR INCLUSION IN A LIBRARY In previous work, functions have dealt with an error either by returning an error code or by displaying an error message and returning a value that should permit continued execution C’s exit function from the standard library stdlib can be used in these types of situations to terminate execution prematurely

22 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-22 Figure 13.8 Function factorial with Premature Exit on Negative Data

23 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-23 exit Function (1/2) SYNTAX exit (return_value); EXAMPLE /* * Gets next positive number from input stream. * Returns EOF if end of file is encountered. * Exits program with error message if erroneous * input is encountered. */

24 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-24 exit Function (2/2) int get_positive(void) { int n, status; char ch; for (status=scanf(“%d”,&n); status ==1 && n<=0; status=scanf(“%d”,&n)) { } if (status==0){ scanf(“%c”, &ch); printf(“\n***Function get_positive”); printf(“reports ERROR in data at”); printf(“>>%c<<***\n”, ch); exit(1); } else if (status==EOF) return (status); else return (n); }

25 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-25 13.6 CONDITIONAL COMPILATION C’s preprocessor recognizes commands that allow the user to select parts of a program to be compiled and parts to be omitted. Conditional compilation allows one to compile only the code appropriate for the current computer Figure 13.9 shows a recursive function containing printf calls to create a trace of its execution –Complication of these statements depends on the value of the condition defined (TRACE)

26 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-26 Figure 13.9 Conditional Compilation of Tracing printf Calls

27 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-27 Figure 13.10 Conditional Compilation of Tracing printf Calls

28 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-28 Figure 13.11 Header File That Protects Itself from Effects of Duplicate Inclusion

29 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-29 Figure 13.11 Header File That Protects Itself from Effects of Duplicate Inclusion (cont’d)

30 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-30 Figure 13.11 Header File That Protects Itself from Effects of Duplicate Inclusion (cont’d)

31 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-31 13.7 ARGUMENTS TO FUNCTION MAIN command line arguments –options specified in the statement that activates a program

32 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-32 Figure 13.12 File Backup Using Arguments to Function main

33 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-33 13.8 DEFINING MACROS WITH PARAMETERS macro –facility for naming a commonly used statement or operation –#define macro_name(parameter list) macro body macro expansion –process of replacing a macro call by its meaning

34 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-34 Figure 13.13 Program Using a Macro with Formal Parameters

35 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-35 Figure 13.14 Macro Expansion of Second Macro Call of Program in Fig. 13.13

36 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-36 Use of Parentheses in Macro Body The use of adequate parentheses in a macro’s body is essential for correct evaluation. In Fig. 13.15, a program fragment uses a macro to compute n 2. –Two versions of the macro definition and the different program outputs that result.

37 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-37 Figure 13.15 Macro Calls Showing Importance of Parentheses in Macro Body

38 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-38 Figure 13.15 Macro Calls Showing Importance of Parentheses in Macro Body (cont’d)

39 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-39 Figure 13.16 Macro Expansions of Macro Calls from Fig. 13.15

40 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-40 13.9 COMMON PROGRAMMING ERRORS The most common problem in the development of large systems by terms of programmers is a lack of agreement regarding the details of a system’s design. When developing personal libraries, it is easy to forget the long-range goal of having reusable functions in the rush of completing a current project. It is easy to slip and type a blank after the macro name in the definition of a macro with parameters, causing the preprocessor to misinterpret the definition.

41 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-41 Chapter Review C’s facility for creating a personal library provides a means of encapsulating an abstract data type. Dividing a library definition into a header file and an implementation file provides a natural separation of the description of what the library functions do from how they do it. The exit function allows premature termination of program execution Conditional compilation provides a means of customizing code for different implementations and of creating library header file that protect themselves from duplicate inclusion.

42 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13-42 Question?


Download ppt "Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪."

Similar presentations


Ads by Google