这是一本关于C编程的基础、全面而经典的图书。作者从不同的角度出发,全面介绍了C编程中涉及到的数据类型、算术运算、逻辑运算、变量、条件语句、函数、数组、指针、字符串、结构、文件操作、位操作、宏、库函数等基本内容,使读者在学习之后就能很快掌握C编程的精髓。本书还专门介绍了C++的一些重要特性,如抽象数据类型、类和类的继承等。本书还介绍了Visual C++ 6.0和C++ Builder的使用方法,并且每章都附有大量习题,附录还列出了主要习题的答案。
Part 1 Fundamentals
Chapter 1 Getting started
1.1 Introduction to Programming
Algorithms
From Algorithms to Programs
Program Translation
Exercises 1.1
1.2 Introduction to Modularity
Functions
The main() Function
Exercises 1.2
1.3 The printf() Function
Exercises 1.3
1.4 Programming Style
Comments
Exercises 1.4
1.5 Top-Down Program Development
Modularity and Top-Down Design
Exercises 1.5
1.6 Common Programming Errors
1.7 Chapter Summary
Chapter 2 Data Types, Declarations, and Displays
2.1 Data Types
Integer Values
Floating Point and Double Precision Numbers
Exponential Notation
Character Type
Escape Sequences
Exercises 2.1
2.2 Arithmetic Operators
Integer Division
A Unary Operator (Negation)
Operator Precedence and Associativity
Exercises 2.2
2.3 Displaying Numerical Results
Formatted Output
Format Modifiers
Other Number Bases
Exercises 2.3
2.4 Variables and Declarations
Declaration Statements
Declaration Statements as Definition Statements
Exercises 2.4
2.5 Integer Qualifiers
Determining Storage Size
Exercises 2.5
2.6 Common Programming Errors
2.7 Chapter Summary
2.8 Chapter Supplement: Bits, Bytes, Addresses, and Number Codes
Words and Addresses
Two’s Complement Numbers
Chapter 3 Assignments, Addresses, and Interactive Input
3.1 Assignment
Assignment Operators
Accumulating
Counting
Exercises 3.1
3.2 Addresses
Storing Addresses
Using Addresses
Declaring Pointers
Exercises 3.2
3.3 The scanf() Function
Exercises 3.3
3.4 scanf() with Buffered Input
3.5 Symbolic Constants
Exercises 3.5
3.6 Common Programming Errors
3.7 Chapter Summary
3.8 Chapter Supplement: Introduction to Abstraction
Built-in and Abstract Data Types
Procedural Abstraction
Part 2 Flow of Control
Chapter 4 Selection
4.1 Relational Expressions
Logical Operators
A Numerical Accuracy Problem
Exercises 4.1
4.2 The if-else Statement
Compound Statements
One-Way Selection
Problems Associated with the if-else Statement
Exercises 4.2
4.3 Nested if Statements
The if-else Chain
Exercises 4.3
4.4 The switch Statement
Exercises 4.4
4.5 Common Programming Errors
4.6 Chapter Summary
4.7 Chapter Supplement: Errors, Testing, and Debugging
Compile-Time and Run-Time Errors
Syntax and Logic Errors
Testing and Debugging
Chapter 5 Repetition
5.1 The while Statement
Exercises 5.1
5.2 scanf() within a while Loop
Sentinels
break and continue Statements
The Null Statement
Exercises 5.2
5.3 The for Statement
scanf() within a for Loop
Nested Loops
Exercises 5.3
5.4 The do Statement
Validity Checks
Exercises 5.4
5.5 Common Programming Errors
5.6 Chapter Summary
5.7 Chapter Supplement: Character-Oriented Graphics
Character-at-a-time Display
Exercises 5.7
Part 3 Functions
Chapter 6 Writing Your Own Functions
6.1 Function Definitions and Declarations
Defining a Function
Function Prototypes
Calling a Function
Placement of Statements
Function Stubs
Functions with Empty Parameter Lists
Exercises 6.1
6.2 Standard Library Functions
Input/Output Library Functions
Mathematical Library Functions
Casts
String Library Functions
Miscellaneous Routines
Exercises 6.2
6.3 Variable Scope
Misuse of Global Variables
Exercises 6.3
6.4 Variable Storage Class
Local Variable Storage Classes
Global Variable Storage Classes
Exercises 6.4
6.5 Passing Addresses
Passing, Storing, and Using Addresses
Exercises 6.5
6.6 Common Programming Errors
6.7 Chapter Summary
6.8 Chapter Supplement: Generating Random Numbers
Scaling
Part 4 Complex Data Types
Chapter 7 Arrays
7.1 Single-Dimensional Arrays
Input and Output of Array Values
Exercises 7.1
7.2 Array Initialization
Exercises 7.2
7.3 Passing Arrays
Exercises 7.3
7.4 Two-Dimensional Arrays
Larger Dimensional Arrays
Exercises 7.4
7.5 Common Programming Errors
7.6 Chapter Summary
7.7 Chapter Supplement: Searching and Sorting Methods
Search Algorithms
Linear Search
Return the index value
Binary Search
Big O Notation
Sort Algorithms
The Selection Sort
An Exchange ("Bubble") Sort
Chapter 8 Arrays, Addresses, and Pointers
8.1 Array Names as Pointers
Exercises 8.1
8.2 Pointer Arithmetic
Pointer Initialization
Exercises 8.2
8.3 Passing and Using Array Addresses
Advanced Pointer Notation
Exercises 8.3
8.4 Common Programming Errors
8.5 Chapter Summary
Chapter 9 Character Strings
9.1 String Fundamentals
String Input and Output
String Processing
Character-by-Character Input
Exercises 9.1
9.2 Pointers and Library Functions
Library Functions
Exercises 9.2
9.3 String Definitions and Pointer Arrays
Pointer Arrays
Exercises 9.3
9.4 Formatting Strings
In-Memory String Conversions
Format Strings
Exercises 9.4
9.5 Common Programming Errors
9.6 Chapter Summary
Chapter 10 Structures
10.1 Single Structures
Exercises 10.1
10.2 Arrays of Structures
Exercises 10.2
10.3 Passing and Returning Structures
Returning Structures
Exercises 10.3
10.4 Linked Lists
Exercises 10.4
10.5 Dynamic Memory Allocation
Dynamically Linked Lists
Exercises 10.5
10.6 Unions
Exercises 10.6
10.7 Common Programming Errors
10.8 Chapter Summary
Part 5 Additional Topics
Chapter 11 Data Files
11.1 Declaring, Opening, and Closing Files
Opening a File
Closing a File
Exercises 11.1
11.2 Reading and Writing Files
Standard Device Files
Other Devices
Exercises 11.2
11.3 Random File Access
Exercises 11.3
11.4 Passing and Returning File Names
Exercises 11.4
11.5 Common Programming Errors
11.6 Chapter Summary
11.7 Chapter Supplement: Control Codes
Chapter 12 Additional Capabilities
12.1 Additional Features
The typedef Declaration Statement
The enum Specifier
Casts
Conditional Expressions
The goto Statement
12.2 Bitwise Operations
The AND Operator
The Inclusive OR Operator
The Exclusive OR Operator
The Complement Operator
Different-Sized Data Items
The Shift Operators
12.3 Macros
12.4 Command-Line Arguments
A showad three blind mice
Exercises
12.5 Chapter Summary
Part 6 Introduction to C++
Chapter 13 Introduction to C++
13.1 Object-Based Programming and Abstract Data Types
Abstract Data Types
Comments in C++
Exercises 13.1
13.2 Input and Output in C++
Output in C++
Formatted Output 3
Input in C++
Exercises 13.2
13.3 Function Enhancements in C++
Default Arguments364
Reusing Function Names (Overloading)
Function Templates6
Inline Functions7
Reference Parameters
Passing and Using References
References versus Pointers
Exercises 13.3
13.4 Abstract Data Types in C++ (Classes)
Class Construction
Terminology
Exercises 13.4
13.5 Constructors and Destructors
Calling Constructors
Overloaded and Inline Constructors
Destructors
Arrays of Objects
Exercises 13.5
13.6 An Application
Exercises 13.6
13.7 Common Programming Errors
13.8 Chapter Summary
Chapter 14 Additional Class Capabilities
14.1 Assignment
Copy Constructors
Base/Member Initialization
Exercises 14.1
14.2 Additional Class Features
Class Scope
Static Class Members
The this Pointer
Friend Functions
Exercises 14.2
14.3 Operator Functions
The Assignment Operator Revisited
Exercises 14.3
14.4 Data Type Conversions
Built-in to Built-in Conversion
Built-in to User-defined (Class) Conversion
User-defined (Class) to Built-in Conversion
User-defined (Class) to User-defined (Class) Conversion
Exercises 14.4
14.5 Class Inheritance
Inheritance
Access Specifications
An Example
Polymorphism
Exercises 14.5
14.6 Common Programming Errors
14.7 Chapter Summary
Appendix A Operator Precedence Table
Appendix B ASCII Character Codes
Appendix C Input, Output, and Standard Error Redirection
Appendix D The Standard C Library
Appendix E The Standard Template Library (C++)
Appendix F Program Entry, Compilation, and Execution
Appendix G Using Visual C++ Version 6.0
Appendix H Using C++ Builder
Appendix I Solutions
A First Book of ANSI C is written for students who need to know the ANSI C language and how to write its syntax. The pedagogy helps students with common programming pitfalls, which makes this book more useful to a student than the typical reference-type book. It assumes no programming background, but can be used by someone who wants to learn this language. The primary purpose of the first two editions of this text was to make C more accessible as an applications programming language than was possible with texts that focused on C as an advanced operating systems language. The success of these editions and the many comments received from students and faculty stating that the book really did help them learn and teach C, respectively, have been extremely gratifying.
Based on suggestions and more in-depth responses from adopters, numerous new pedagogical features and material have been incorporated into this third edition. The most noticeable of these changes is that, in all program examples, main’s header line has been changed from void main(void) to int main(), and that the main function always returns a value to the operating system. This change was made to reflect current programming practice that the main function explicitly return a value. Additional changes to this edition include the following:
● New material on function templates has been added.
● Programming Notes, a single highlighted and informational reference, incorporates the previous Closer Look and Tips From the Pros boxes as well as new material.
● Information on creating C programs under both the Microsoft* Visual C++ 6.0 and the Borland* C++ Builder replaces previous information on outdated C compilers.
● All function prototypes are global.
● A chapter supplement on random number generation has been added.
● New material on C++’s Standard Template Library has been included.
To facilitate using C as a basis for learning C++, the two chapters introducing C++ that were introduced in the second edition have been retained and updated to ANSI C++ specifications. Thus, as with the second edition, this text can be used as an introduction to programming in general, as an introduction to the C language in particular, and as a basis for further study of the C++ language.
The basic requirement of this third edition, however, remains the same as the first two editions: that is, that all topics be presented in a clear, unambiguous, and accessible manner to beginning students. Toward this end, the heart of the first two editions, which consisted of Chapters 1 through 11, remains essentially unchanged in the present edition. Thus, all of the topics, examples, explanations, and figures in the first 11 chapters of the previous editions remain in the third edition with the addition of the new material.
Distinctive Features of This Book
Writing Style. I firmly believe that introductory texts do not teach students--rofessors teach students. An introductory textbook, if it is to be useful, must be the primary “supporting actor” to the “leading role” of the professor. Once the professor sets the stage, however, the textbook must encourage, nurture, and assist the student in acquiring and “owning” the material presented in class. To do this the text must be written in a manner that makes sense to the student. My primary concern, and one of the distinctive features of this book, is that it has been written for the student. As one of my reviewers has said of the first edition, “This book addresses the student and not the professional.” Thus, first and foremost, I feel the writing style used to convey the concepts presented is the most important aspect of the text.
Modularity. C, by its nature, is a modular language. Thus, the connection between C functions and modules is made early in the text, in Section 1.2, and continues throughout the book. To stress the modular nature of C, the first complete main() function illustrates calling four other functions. The first program that can be compiled, which calls the printf() function, is then presented.
The idea of argument passing into modules is also made early, in Section 1.3, with the use of the printf() function. In this manner, students are introduced to functions and argument passing as a natural technique of programming.
Software Engineering. As with the first two editions, this revised edition introduces students to the fundamentals of software engineering right from the start. This introduction begins in Section 1.1, which introduces algorithms and the various ways that an algorithm can be described. The example illustrating three algorithms for summing the numbers from 1 to 100 (Figure 1.4) has been retained from the earlier editions.
The increased emphasis on software engineering is supported in the text, starting with Section 1.5, which introduces top-down program development. Here the importance of understanding the problem and selecting an appropriate algorithm is highlighted and the relationship between analysis, design, coding, and testing is introduced. Problem solving within this context is stressed throughout the text.
Introduction to Pointers. One of the unique features of the first edition was the early introduction of pointer concepts. This was done by simply using the printf() function initially to display the addresses of variables, and then using variables to store addresses. This approach always seemed a more logical and intuitive method of understanding pointer variables than the indirection description in vogue at the time the first edition was released.
Since the first edition I have been pleased to see that the use of the printf() function to display addresses has become a standard way of introducing pointers. Although this approach, therefore, is no longer a unique feature of my book, I am very proud of its presentation and continue to use it in this new edition.
Program Testing. Every C program in this text has been successfully compiled and run under the Microsoft Visual C++ 6.0 compiler. All programs have been written following the current ANSI C standard. Source code for all program examples used in the text is available online. This permits students both to experiment with and extend the existing programs and to modify them more easily as required by a number of end-of-section exercises.
Pedagogical Features
To facilitate my goal of making C accessible as a first-level course, the text includes the following pedagogical features.
End of Section Exercises. Almost every section in the book contains numerous and diverse skill builder and programming exercises. Additionally, solutions to selected odd-numbered exercises are provided in an appendix.
Pseudocode and Flowchart Descriptions. As in the first two editions, pseudocode is stressed throughout the text. Material on flowchart symbols and the use of flowcharts in visually presenting flow-of-control constructs is also presented.
Common Programming Errors and Chapter Review. Each chapter ends with a section on common programming errors and a review of the main topics covered in the chapter.
Programming Notes. A set of shaded boxes that highlight important concepts and useful technical points and programming techniques used by professional programmers is provided.
Enrichment Sections. Given the many different emphases that can be applied in teaching C, a number of Enrichment Sections have been included as chapter supplements. These allow you to provide different emphasis with different students or different C class sections.
Appendices and Supplements. As with the first two editions, an expanded set of appendices is provided in this third edition. These include appendices on operator precedence; ASCII codes; I/O and standard error redirection; the Standard C Library; the Standard Template Library (STL); program entry, compilation, and execution; using the Microsoft Visual C++ 6.0 compiler; and using the Borland C++ Builder compiler.
A final appendix offers solutions to selected odd-numbered problems. Source code for all program examples used in the text is available at
http://www.brookscole.com
An instructor’s manual including chapter outlines and answers to selected even-numbered problems is also available.
Acknowledgments
The writing of this third edition is a direct result of the success (and limitations) of the first two editions. In this regard, my most heartfelt acknowledgment and appreciation is to the instructors and students who found these editions to be of service to them in their respective quests to teach and learn C.
Once a third edition was planned, its completion depended on the encouragement, skills, and efforts of many other people. For this I especially want to thank the staff of Brooks/Cole Publishing Company for their many contributions. First and foremost, this includes my project editor, Kallie Swanson. Additionally, I am very grateful to editorial assistants Grace Fujimoto and Meg Weist for handling numerous scheduling and review details that permitted me to concentrate on the actual writing of the text.
I also wish to express my gratitude to the individual reviewers:
Taret Alameldin, California State University at Fresno
John Avitabile, College of Saint Rose
Larry Booth, Highline Community College
Li-hsiang Cheo, William Paterson University
Yonina S. Cooper, University of Las Vegas
Ralph Ewton, University of Texas at El Paso
James Gips, Boston College
Richard A. Hatch, San Diego State University
Rick L. Homkes, Indiana University at Kokomo
Michael Milligan, Front Range Community College
Robert E. Norton, San Diego Mesa College
Robert Probasco, University of Idaho
Ashraf Saad, University of Cincinnati
Anthony Sako, Columbia Basin College
Robert Weaver, Mt. Holyoke College
Lee Westbrock, Trident Technical College
Bob Willis, Rogers State University
Each of them supplied extremely detailed and constructive reviews of both the original manuscript and a number of revisions. Their suggestions, attention to detail, and comments were extraordinarily helpful in the preparation of the various editions.
Finally, the task of turning the final manuscript into a textbook again required a dedicated production staff. For this I especially want to thank copy editor Philip Jochnowitz, production editor Robin Gold of Forbes Mill Press, manuscript keyer Sue Boshers, and compositor Linda Weidemann of Wolf Creek Press. Their dedication, attention to detail, and their high standards have helped immensely to improve the quality of this edition. Almost from the moment the book moved to the production stage they seemed to take personal ownership of the text, and I am very grateful to them.
Special thanks also go to Janie Schwark, Academic Product Manager of Developer Tools at Microsoft, for providing invaluable support and product information.
I also gratefully acknowledge the direct encouragement and support provided by my dean Dr. Paul Lerman, my associate dean, Dr. Ron Heim, and my chairperson, Dr. Youngboem Kim. Without their support, this text could not have been written.
Finally, I deeply appreciate the patience, understanding, and love provided by my wife, friend, and partner, Rochelle.
Gary Bronson