C How to Program, 6e, is ideal for introductory courses in C Programming. Also for courses in Programming for Engineers, Programming for Business, and Programming for Technology. This text provides a valuable reference for programmers and anyone interested in learning the C programming language. The Deitels' groundbreaking How to Program series offers unparalleled breadth and depth of object-oriented programming concepts and intermediate-level topics for further study. Using the Deitels’ signature “Live-Code™ Approach,” this complete, authoritative introduction to C programming offers strong treatment of structured algorithm and program development in ANSI/ISO C with 150 working C programs. Includes rich, 300-page treatment of object-oriented programming in C++ that helps readers interpret the code more effectively.
Author(s): Paul Deitel, Harvey M. Deitel
Edition: 6th Edition
Publisher: Prentice Hall
Year: 2009
Language: English
Pages: 1000
Cover ......Page 1
Copyright......Page 5
Title Page......Page 6
Contents......Page 10
Preface......Page 22
1 Introduction to Computers, the Internet and the Web......Page 34
1.1 Introduction......Page 35
1.2 Computers: Hardware and Software......Page 36
1.3 Computer Organization......Page 37
1.5 The Internet and the World Wide Web......Page 38
1.6 Machine Languages, Assembly Languages and High-Level Languages......Page 39
1.7 History of C......Page 40
1.8 C Standard Library......Page 41
1.10 Java......Page 42
1.12 BASIC, Visual Basic, Visual C++, C# and .NET......Page 43
1.13 Key Software Trend: Object Technology......Page 44
1.14 Typical C Program Development Environment......Page 45
1.15 Hardware Trends......Page 47
1.16 Notes About C and This Book......Page 48
1.17 Web Resources......Page 49
2 Introduction to C Programming......Page 56
2.2 A Simple C Program: Printing a Line of Text......Page 57
2.3 Another Simple C Program: Adding Two Integers......Page 61
2.4 Memory Concepts......Page 66
2.5 Arithmetic in C......Page 67
2.6 Decision Making: Equality and Relational Operators......Page 71
3 Structured Program Development in C......Page 87
3.3 Pseudocode......Page 88
3.4 Control Structures......Page 89
3.5 The if Selection Statement......Page 91
3.6 The if…else Selection Statement......Page 92
3.7 The while Repetition Statement......Page 96
3.8 Formulating Algorithms Case Study 1: Counter-Controlled Repetition......Page 97
3.9 Formulating Algorithms with Top-Down, Stepwise Refinement Case Study 2: Sentinel-Controlled Repetition......Page 99
3.10 Formulating Algorithms with Top-Down, Stepwise Refinement Case Study 3: Nested Control Structures......Page 106
3.11 Assignment Operators......Page 110
3.12 Increment and Decrement Operators......Page 111
4 C Program Control......Page 130
4.2 Repetition Essentials......Page 131
4.3 Counter-Controlled Repetition......Page 132
4.4 for Repetition Statement......Page 133
4.6 Examples Using the for Statement......Page 136
4.7 switch Multiple-Selection Statement......Page 140
4.8 do…while Repetition Statement......Page 146
4.9 break and continue Statements......Page 147
4.10 Logical Operators......Page 149
4.11 Confusing Equality (==) and Assignment (=) Operators......Page 152
4.12 Structured Programming Summary......Page 154
5 C Functions......Page 173
5.2 Program Modules in C......Page 174
5.3 Math Library Functions......Page 175
5.5 Function Definitions......Page 177
5.6 Function Prototypes......Page 181
5.8 Headers......Page 184
5.9 Calling Functions By Value and By Reference......Page 185
5.10 Random Number Generation......Page 186
5.11 Example: A Game of Chance......Page 191
5.12 Storage Classes......Page 194
5.13 Scope Rules......Page 197
5.14 Recursion......Page 200
5.15 Example Using Recursion: Fibonacci Series......Page 203
5.16 Recursion vs. Iteration......Page 207
6 C Arrays......Page 228
6.2 Arrays......Page 229
6.4 Array Examples......Page 231
6.5 Passing Arrays to Functions......Page 245
6.6 Sorting Arrays......Page 249
6.7 Case Study: Computing Mean, Median and Mode Using Arrays......Page 251
6.8 Searching Arrays......Page 256
6.9 Multiple-Subscripted Arrays......Page 262
7 C Pointers......Page 286
7.2 Pointer Variable Definitions and Initialization......Page 287
7.3 Pointer Operators......Page 288
7.4 Passing Arguments to Functions by Reference......Page 290
7.5 Using the const Qualifier with Pointers......Page 294
7.6 Bubble Sort Using Call-by-Reference......Page 300
7.7 sizeof Operator......Page 303
7.8 Pointer Expressions and Pointer Arithmetic......Page 306
7.9 Relationship between Pointers and Arrays......Page 308
7.11 Case Study: Card Shuffling and Dealing Simulation......Page 313
7.12 Pointers to Functions......Page 318
8 C Characters and Strings......Page 342
8.2 Fundamentals of Strings and Characters......Page 343
8.3 Character-Handling Library......Page 345
8.4 String-Conversion Functions......Page 350
8.5 Standard Input/Output Library Functions......Page 355
8.6 String-Manipulation Functions of the String-Handling Library......Page 359
8.7 Comparison Functions of the String-Handling Library......Page 362
8.8 Search Functions of the String-Handling Library......Page 364
8.9 Memory Functions of the String-Handling Library......Page 370
8.10 Other Functions of the String-Handling Library......Page 374
9 C Formatted Input/Output......Page 389
9.3 Formatting Output with printf......Page 390
9.4 Printing Integers......Page 391
9.5 Printing Floating-Point Numbers......Page 392
9.6 Printing Strings and Characters......Page 394
9.7 Other Conversion Specifiers......Page 395
9.8 Printing with Field Widths and Precision......Page 396
9.9 Using Flags in the printf Format Control String......Page 399
9.10 Printing Literals and Escape Sequences......Page 401
9.11 Reading Formatted Input with scanf......Page 402
10 C Structures, Unions, Bit Manipulations and Enumerations......Page 415
10.2 Structure Definitions......Page 416
10.4 Accessing Structure Members......Page 419
10.6 typedef......Page 421
10.7 Example: High-Performance Card Shuffling and Dealing Simulation......Page 422
10.8 Unions......Page 424
10.9 Bitwise Operators......Page 427
10.10 Bit Fields......Page 436
10.11 Enumeration Constants......Page 439
11 C File Processing......Page 450
11.2 Data Hierarchy......Page 451
11.3 Files and Streams......Page 453
11.4 Creating a Sequential-Access File......Page 454
11.5 Reading Data from a Sequential-Access File......Page 459
11.6 Random-Access Files......Page 463
11.7 Creating a Random-Access File......Page 464
11.8 Writing Data Randomly to a Random-Access File......Page 466
11.9 Reading Data from a Random-Access File......Page 469
11.10 Case Study: Transaction-Processing Program......Page 470
12 C Data Structures......Page 487
12.1 Introduction......Page 488
12.3 Dynamic Memory Allocation......Page 489
12.4 Linked Lists......Page 491
12.5 Stacks......Page 499
12.6 Queues......Page 505
12.7 Trees......Page 511
13 C Preprocessor......Page 528
13.3 #define Preprocessor Directive: Symbolic Constants......Page 529
13.4 #define Preprocessor Directive: Macros......Page 530
13.5 Conditional Compilation......Page 532
13.7 # and ## Operators......Page 533
13.9 Predefined Symbolic Constants......Page 534
13.10 Assertions......Page 535
14 Other C Topics......Page 540
14.2 Redirecting I/O......Page 541
14.3 Variable-Length Argument Lists......Page 542
14.4 Using Command-Line Arguments......Page 544
14.5 Notes on Compiling Multiple-Source-File Programs......Page 545
14.6 Program Termination with exit and atexit......Page 547
14.7 volatile Type Qualifier......Page 548
14.9 More on Files......Page 549
14.10 Signal Handling......Page 551
14.11 Dynamic Memory Allocation: Functions calloc and realloc......Page 553
14.12 Unconditional Branching with goto......Page 554
15 C++ as a Better C; Introducing Object Technology......Page 561
15.2 C++......Page 562
15.3 A Simple Program: Adding Two Integers......Page 563
15.4 C++ Standard Library......Page 565
15.5 Header Files......Page 566
15.6 Inline Functions......Page 568
15.7 References and Reference Parameters......Page 570
15.9 Default Arguments......Page 575
15.10 Unary Scope Resolution Operator......Page 577
15.11 Function Overloading......Page 578
15.12 Function Templates......Page 581
15.13 Introduction to Object Technology and the UML......Page 584
15.14 Wrap-Up......Page 587
16 Introduction to Classes and Objects......Page 593
16.2 Classes, Objects, Member Functions and Data Members......Page 594
16.3 Defining a Class with a Member Function......Page 595
16.4 Defining a Member Function with a Parameter......Page 599
16.5 Data Members, set Functions and get Functions......Page 602
16.6 Initializing Objects with Constructors......Page 609
16.7 Placing a Class in a Separate File for Reusability......Page 612
16.8 Separating Interface from Implementation......Page 616
16.9 Validating Data with set Functions......Page 622
16.10 Wrap-Up......Page 627
17 Classes: A Deeper Look, Part 1......Page 634
17.1 Introduction......Page 635
17.2 Time Class Case Study......Page 636
17.3 Class Scope and Accessing Class Members......Page 642
17.4 Separating Interface from Implementation......Page 644
17.5 Access Functions and Utility Functions......Page 645
17.6 Time Class Case Study: Constructors with Default Arguments......Page 648
17.7 Destructors......Page 653
17.8 When Constructors and Destructors are Called......Page 654
17.9 Time Class Case Study: A Subtle Trap—Returning a Reference to a private Data Member......Page 657
17.10 Default Memberwise Assignment......Page 660
17.11 Wrap-Up......Page 662
18 Classes: A Deeper Look, Part 2......Page 668
18.2 const (Constant) Objects and const Member Functions......Page 669
18.3 Composition: Objects as Members of Classes......Page 678
18.4 friend Functions and friend Classes......Page 684
18.5 Using the this Pointer......Page 687
18.6 static Class Members......Page 692
18.7 Data Abstraction and Information Hiding......Page 697
18.8 Wrap-Up......Page 699
19 Operator Overloading......Page 705
19.1 Introduction......Page 706
19.2 Fundamentals of Operator Overloading......Page 707
19.3 Restrictions on Operator Overloading......Page 708
19.4 Operator Functions as Class Members vs. Global Function......Page 709
19.5 Overloading Stream Insertion and Stream Extraction Operators......Page 711
19.6 Overloading Unary Operators......Page 714
19.8 Dynamic Memory Management......Page 715
19.9 Case Study: Array Class......Page 717
19.10 Converting between Types......Page 729
19.11 Building a String Class......Page 730
19.12 Overloading ++ and --......Page 731
19.13 Case Study: A Date Class......Page 733
19.14 Standard Library Class string......Page 737
19.15 explicit Constructors......Page 741
19.16 Proxy Classes......Page 744
19.17 Wrap-Up......Page 748
20 Object-Oriented Programming: Inheritance......Page 760
20.1 Introduction......Page 761
20.2 Base Classes and Derived Classes......Page 762
20.4 Relationship between Base Classes and Derived Classes......Page 765
20.4.1 Creating and Using a CommissionEmployee Class......Page 766
20.4.2 Creating a BasePlusCommissionEmployee Class Without Using Inheritance......Page 771
20.4.3 Creating a CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy......Page 776
20.4.4 CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using protected Data......Page 781
20.4.5 CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using private Data......Page 788
20.5 Constructors and Destructors in Derived Classes......Page 795
20.6 public, protected and private Inheritance......Page 803
20.7 Software Engineering with Inheritance......Page 804
20.8 Wrap-Up......Page 805
21 Object-Oriented Programming: Polymorphism......Page 811
21.1 Introduction......Page 812
21.2 Polymorphism Examples......Page 813
21.3 Relationships Among Objects in an Inheritance Hierarchy......Page 814
21.3.1 Invoking Base-Class Functions from Derived-Class Objects......Page 815
21.3.2 Aiming Derived-Class Pointers at Base-Class Objects......Page 822
21.3.3 Derived-Class Member-Function Calls via Base-Class Pointers......Page 823
21.3.4 Virtual Functions......Page 825
21.3.5 Summary of the Allowed Assignments Between Base-Class and Derived-Class Objects and Pointers......Page 831
21.5 Abstract Classes and Pure virtual Functions......Page 832
21.6 Case Study: Payroll System Using Polymorphism......Page 834
21.6.1 Creating Abstract Base Class Employee......Page 836
21.6.2 Creating Concrete Derived Class SalariedEmployee......Page 839
21.6.3 Creating Concrete Derived Class HourlyEmployee......Page 841
21.6.4 Creating Concrete Derived Class CommissionEmployee......Page 844
21.6.5 Creating Indirect Concrete Derived Class BasePlusCommissionEmployee......Page 846
21.6.6 Demonstrating Polymorphic Processing......Page 847
21.7 (Optional) Polymorphism, Virtual Functions and Dynamic Binding “Under the Hood”......Page 851
21.8 Case Study: Payroll System Using Polymorphism and Runtime Type Information with Downcasting, dynamic_cast, typeid and type_info......Page 855
21.10 Wrap-Up......Page 859
22 Templates......Page 865
22.2 Function Templates......Page 866
22.4 Class Templates......Page 870
22.5 Nontype Parameters and Default Types for Class Templates......Page 877
22.7 Notes on Templates and Friends......Page 878
22.9 Wrap-Up......Page 879
23 Stream Input/Output......Page 884
23.1 Introduction......Page 885
23.2.1 Classic Streams vs. Standard Streams......Page 886
23.2.3 Stream Input/Output Classes and Objects......Page 887
23.3.2 Character Output Using Member Function put......Page 890
23.4.1 get and getline Member Functions......Page 891
23.5 Unformatted I/O Using read, write and gcount......Page 894
23.6 Introduction to Stream Manipulators......Page 895
23.6.1 Integral Stream Base: dec, oct, hex and setbase......Page 896
23.6.2 Floating-Point Precision (precision, setprecision)......Page 897
23.6.3 Field Width (width, setw)......Page 898
23.6.4 User-Defined Output Stream Manipulators......Page 899
23.7.1 Trailing Zeros and Decimal Points (showpoint)......Page 901
23.7.2 Justification (left, right and internal)......Page 902
23.7.3 Padding (fill, setfill)......Page 904
23.7.4 Integral Stream Base (dec, oct, hex, showbase)......Page 905
23.7.5 Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed)......Page 906
23.7.7 Specifying Boolean Format (boolalpha)......Page 907
23.7.8 Setting and Resetting the Format State via Member Function flags......Page 908
23.8 Stream Error States......Page 910
23.10 Wrap-Up......Page 912
24 Exception Handling......Page 922
24.1 Introduction......Page 923
24.3 Example: Handling an Attempt to Divide by Zero......Page 924
24.4 When to Use Exception Handling......Page 930
24.5 Rethrowing an Exception......Page 931
24.6 Exception Specifications......Page 933
24.8 Stack Unwinding......Page 934
24.9 Constructors, Destructors and Exception Handling......Page 936
24.11 Processing new Failures......Page 937
24.12 Class auto_ptr and Dynamic Memory Allocation......Page 940
24.13 Standard Library Exception Hierarchy......Page 942
24.14 Other Error-Handling Techniques......Page 944
24.15 Wrap-Up......Page 945
A: Operator Precedence Charts......Page 952
B: ASCII Character Set......Page 956
C: Number Systems......Page 957
C.1 Introduction......Page 958
C.2 Abbreviating Binary Numbers as Octal and Hexadecimal Numbers......Page 961
C.4 Converting from Binary, Octal or Hexadecimal to Decimal......Page 962
C.5 Converting from Decimal to Binary, Octal or Hexadecimal......Page 963
C.6 Negative Binary Numbers: Two’s Complement Notation......Page 965
D.1 Introduction......Page 970
D.3 Solution Strategies......Page 971
D.4 Programming Sudoku Puzzle Solvers......Page 975
D.5 Generating New Sudoku Puzzles......Page 976
D.6 Conclusion......Page 978
Appendices on the Web......Page 979
A......Page 980
B......Page 981
C......Page 982
D......Page 984
F......Page 986
G......Page 987
I......Page 988
L......Page 989
M......Page 990
O......Page 991
P......Page 992
Q......Page 993
S......Page 994
T......Page 997
V......Page 998
Z......Page 999