Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ....

42
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : [email protected] TA: 鄭鄭鄭 鄭鄭鄭

Transcript of Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ....

Page 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: 鄭筱親 陳昱豪.

Chapter 13Programming in the Large

Dr. Jiung-yao HuangDept. Comm. Eng.Nat. Chung Cheng Univ.E-mail : [email protected]: 鄭筱親 陳昱豪

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

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

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

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

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

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

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 13-5

Figure 13.1 Preparing a Program for Execution

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

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

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

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

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

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

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 13-9

Figure 13.3 Portion of Program That Uses Functions from a Personal Library

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

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

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

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

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

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

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

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)

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

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

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

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

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 13-16

Figure 13.5 Storage Classes auto and extern as Previously Seen

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 13-17

Figure 13.5 Storage Classes auto and extern as Previously Seen (cont’d)

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

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

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 13-19

Figure 13.7 Use of Variables of Storage Class extern

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

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

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

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

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 13-22

Figure 13.8 Function factorial with Premature Exit on Negative Data

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 13-23

exit Function (1/2)

• SYNTAXexit (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.

*/

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

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

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

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)

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 13-26

Figure 13.9 Conditional Compilation of Tracing printf Calls

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 13-27

Figure 13.10 Conditional Compilation of Tracing printf Calls

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 13-28

Figure 13.11 Header File That Protects Itself from Effects of Duplicate Inclusion

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

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)

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

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)

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

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

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 13-32

Figure 13.12 File Backup Using Arguments to Function main

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

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

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 13-34

Figure 13.13 Program Using a Macro with Formal Parameters

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

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

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

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 n2.– Two versions of the macro definition and the

different program outputs that result.

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 13-37

Figure 13.15 Macro Calls Showing Importance of Parentheses in Macro Body

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 13-38

Figure 13.15 Macro Calls Showing Importance of Parentheses in Macro Body (cont’d)

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 13-39

Figure 13.16 Macro Expansions of Macro Calls from Fig. 13.15

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

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.

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

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.

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

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

Question?