knking.com -- Programming Language Books and Training

 C C++ Java

Books by K. N. King

Home
Books by K. N. King
Short courses
Recommended books
Recommended links
FAQ

C Programming: A Modern Approach

Brief Contents

Basic Features of C

1 Introducing C 1
2 C Fundamentals 9
3 Formatted Input/Output 31
4 Expressions 45
5 Selection Statements 63
6 Loops 85
7 Basic Types 109
8 Arrays 139
9 Functions 155
10 Program Organization 185

Advanced Features of C

11 Pointers 205
12 Pointers and Arrays 221
13 Strings 239
14 The Preprocessor 273
15 Writing Large Programs 303
16 Structures, Unions, and Enumerations 329
17 Advanced Uses of Pointers 359
18 Declarations 399
19 Program Design 419
20 Low-Level Programming 451

The Standard C Library

21 The Standard Library 467
22 Input/Output 75
23 Library Support for Numbers and Character Data 519
24 Error Handling 539
25 International Features 551
26 Miscellaneous Library Functions 563

Reference

A C Syntax 587
B C Operators 595
C Standard C versus Classic C 597
D Standard Library Functions 601
E ASCII Character Set 637
Bibliography 639
Index 643


Full Contents

Preface xix

1 Introducing C 1

1.1 History of C 1

Origins 1
Standardization 2
C++ 3

1.2 Strengths and Weaknesses of C 3

Strengths 4
Weaknesses 5
Effective Use of C 6

2 C Fundamentals 9

2.1 Writing a Simple Program 9

PROGRAM: Printing a Pun 9
Compiling and Linking 10

2.2 The General Form of a Simple Program 11

Directives 12
Functions 12
Statements 13
Printing Strings 14

2.3 Comments 14

2.4 Variables and Assignment 16

Types 16
Declarations 17
Assignment 17
Printing the Value of a Variable 18
PROGRAM: Computing the Dimensional Weight of a Box 18
Initialization 19
Printing Expressions 20

2.5 Reading Input 20

PROGRAM: Computing the Dimensional Weight of a Box (Revisited) 21

2.6 Defining Constants 21

PROGRAM: Converting from Fahrenheit to Celsius 22

2.7 Identifiers 23

Keywords 24

2.8 Layout of a C Program 25

3 Formatted Input/Output 31

3.1 The printf Function 31

Conversion Specifications 32
PROGRAM: Using printf to Format Numbers 34
Escape Sequences 35

3.2 The scanf Function 36

How scanf Works 37
Ordinary Characters in Format Strings 39
Confusing printf with scanf 39
PROGRAM: Computing the Value of Stock Holdings 40

4 Expressions 45

4.1 Arithmetic Operators 46

Operator Precedence and Associativity 47
PROGRAM: Computing a UPC Check Digit 48

4.2 Assignment Operators 50

Simple Assignment 50
Lvalues 51
Compound Assignment 52

4.3 Increment and Decrement Operators 53

4.4 Expression Evaluation 54

Order of Subexpression Evaluation 55

4.5 Expression Statements 57

5 Selection Statements 63

5.1 Logical Expressions 64

Relational Operators 64
Equality Operators 65
Logical Operators 65

5.2 The if Statement 66

Compound Statements 67
The else Clause 68
Cascaded if Statements 69
PROGRAM: Calculating a Broker's Commission 70
The "Dangling else" Problem 71
Conditional Expressions 72
Boolean Values 73

5.3 The switch Statement 74

The Role of the break Statement 76
PROGRAM: Printing a Date in Legal Form 77

6 Loops 85

6.1 The while Statement 85

Infinite Loops 87
PROGRAM: Printing a Table of Squares 88
PROGRAM: Summing a Series of Numbers 88

6.2 The do Statement 89

PROGRAM: Calculating the Number of Digits in an Integer 90

6.3 The for Statement 91

for Statement Idioms 92
Omitting Expressions in a for Statement 93
The Comma Operator 94
PROGRAM: Printing a Table of Squares (Revisited) 95

6.4 Exiting from a Loop 96

The break Statement 97
The continue Statement 98
The goto Statement 98
PROGRAM: Balancing a Checkbook 99

6.5 The Null Statement 102

7 Basic Types 109

7.1 Integer Types 109

Integer Constants 111
Reading and Writing Integers 112
PROGRAM: Summing a Series of Numbers (Revisited) 113

7.2 Floating Types 114

Floating Constants 115
Reading and Writing Floating-Point Numbers 116

7.3 Character Types 116

Escape Sequences 118
Character-Handling Functions 120
Reading and Writing Characters 120
PROGRAM: Determining the Length of a Message 122

7.4 The sizeof Operator 123

7.5 Type Conversion 124

The Usual Arithmetic Conversions 125
Conversion During Assignment 127
Casting 127

7.6 Type Definitions 129

8 Arrays 139

8.1 One-Dimensional Arrays 139

Array Subscripting 140
PROGRAM: Reversing a Series of Numbers 142
Array Initialization 142
PROGRAM: Checking a Number for Repeated Digits 143
Using the sizeof Operator with Arrays 144
PROGRAM: Computing Interest 145

8.2 Multidimensional Arrays 146

Initializing a Multidimensional Array 147
Constant Arrays 148
PROGRAM: Dealing a Hand of Cards 149

9 Functions 155

9.1 Defining and Calling Functions 155

PROGRAM: Computing Averages 156
PROGRAM: Printing a Countdown 157
PROGRAM: Printing a Pun (Revisited) 158
Function Definitions 159
Function Calls 161
PROGRAM: Testing Whether a Number Is Prime 162

9.2 Function Declarations 163

9.3 Arguments 165

Argument Conversions 166
Array Arguments 167

9.4 The return Statement 169

9.5 Program Termination 170

The exit Function 171

9.6 Recursive Functions 172

The Quicksort Algorithm 173
PROGRAM: Quicksort 175

10 Program Organization 185

10.1 Local Variables 185

Parameters 186

10.2 External Variables 186

Example: Using External Variables to Implement a Stack 187
Pros and Cons of External Variables 188
PROGRAM: Guessing a Number 189

10.3 Blocks 193

10.4 Scope 194

10.5 Organizing a C Program 195

PROGRAM: Classifying a Poker Hand 196

11 Pointers 205

11.1 Pointer Variables 205

Declaring Pointer Variables 206

11.2 The Address and Indirection Operators 207

The Address Operator 207
The Indirection Operator 208

11.3 Pointer Assignment 209

11.4 Pointers as Arguments 211

PROGRAM: Finding the Largest and Smallest Elements in an Array 213
Using const to Protect Arguments 214

11.5 Pointers as Return Values 215

12 Pointers and Arrays 221

12.1 Pointer Arithmetic 221

Adding an Integer to a Pointer 222
Subtracting an Integer from a Pointer 223
Subtracting Pointers 223
Comparing Pointers 224

12.2 Using Pointers for Array Processing 224

Combining the * and ++ Operators 225

12.3 Using an Array Name as a Pointer 227

PROGRAM: Reversing a Series of Numbers (Revisited) 228
Array Arguments (Revisited) 229
Using a Pointer as an Array Name 230

12.4 Pointers and Multidimensional Arrays 231

Processing the Elements of a Multidimensional Array 231
Processing the Rows of a Multidimensional Array 232
Using the Name of a Multidimensional Array as a Pointer 233

13 Strings 239

13.1 String Literals 239

Escape Sequences in String Literals 240
Continuing a String Literal 240
How String Literals Are Stored 241
Operations on String Literals 241
String Literals versus Character Constants 242

13.2 String Variables 243

Initializing a String Variable 243
Character Arrays versus Character Pointers 245

13.3 Reading and Writing Strings 246

Writing Strings Using printf and puts 246
Reading Strings Using scanf and gets 247
Reading Strings Character by Character 248

13.4 Accessing the Characters in a String 249

13.5 Using the C String Library 251

The strcpy (String Copy) Function 252
The strcat (String Concatenate) Function 253
The strcmp (String Compare) Function 253
The strlen (String Length) Function 254
PROGRAM: Printing a One-Month Reminder List 255

13.6 String Idioms 257

Searching for the End of a String 257
Copying a String 259

13.7 Arrays of Strings 261

Command-Line Arguments 263
PROGRAM: Checking Planet Names 265

14 The Preprocessor 273

14.1 How the Preprocessor Works 273

14.2 Preprocessor Directives 276

14.3 Macro Definition 277

Simple Macros 277
Parameterized Macros 279
The # Operator 282
The ## Operator 282
General Properties of Macros 283
Parentheses in Macro Definitions 285
Creating Longer Macros 286
Predefined Macros 287

14.4 Conditional Compilation 288

The #if and #endif Directives 288
The defined Operator 289
The #ifdef and #ifndef Directives 290
The #elif and #else Directives 291
Uses of Conditional Compilation 291

14.5 Miscellaneous Directives 293

The #error Directive 293
The #line Directive 294
The #pragma Directive 295

15 Writing Large Programs 303

15.1 Source Files 303

15.2 Header Files 305

The #include Directive 305
Sharing Macro Definitions and Type Definitions 306
Sharing Function Prototypes 307
Sharing Variable Declarations 309
Nested Includes 310
Protecting Header Files 311
#error Directives in Header Files 312

15.3 Dividing a Program into Files 313

PROGRAM: Text Formatting 313

15.4 Building a Multiple-File Program 320

Makefiles 320
Errors During Linking 322
Rebuilding a Program 322
Defining Macros Outside a Program 324

16 Structures, Unions, and Enumerations 329

16.1 Structure Variables 329

Declaring Structure Variables 330
Initializing Structure Variables 331
Operations on Structures 332

16.2 Structure Types 333

Declaring a Structure Tag 334
Defining a Structure Type 335
Structures as Arguments and Return Values 335

16.3 Nested Arrays and Structures 337

Nested Structures 337
Arrays of Structures 338
Initializing an Array of Structures 338
PROGRAM: Maintaining a Parts Database 339

16.4 Unions 345

Using Unions to Save Space 347
Using Unions to Build Mixed Data Structures 348
Adding a "Tag Field" to a Union 349

16.5 Enumerations 350

Enumeration Tags and Types 351
Enumerations as Integers 352
Using Enumerations to Declare "Tag Fields" 353

17 Advanced Uses of Pointers 359

17.1 Dynamic Storage Allocation 359

Memory Allocation Functions 360
Null Pointers 360

17.2 Dynamically Allocated Strings 361

Using malloc to Allocate Memory for a String 361
Using Dynamic Storage Allocation in String Functions 362
Arrays of Dynamically Allocated Strings 363
PROGRAM: Printing a One-Month Reminder List (Revisited) 364

17.3 Dynamically Allocated Arrays 365

Using malloc to Allocate Storage for an Array 366
The calloc Function 366
The realloc Function 367

17.4 Deallocating Storage 368

The free Function 369
The "Dangling Pointer" Problem 369

17.5 Linked Lists 370

Declaring a Node Type 370
Creating Nodes 371
The -> Operator 372
Inserting a Node at the Beginning of a Linked List 373
Searching a Linked List 375
Deleting a Node from a Linked List 376
Ordered Lists 378
PROGRAM: Maintaining a Parts Database (Revisited) 379

17.6 Pointers to Pointers 384

17.7 Pointers to Functions 385

Function Pointers as Arguments 385
The qsort Function 386
Other Uses of Function Pointers 388
PROGRAM: Tabulating the Trigonometric Functions 389

18 Declarations 399

18.1 Declaration Syntax 399

18.2 Storage Classes 401

Properties of Variables 401
The auto Storage Class 402
The static Storage Class 403
The extern Storage Class 404
The register Storage Class 405
The Storage Class of a Function 406
Summary 407

18.3 Type Qualifiers 407

18.4 Declarators 408

Deciphering Complex Declarations 410
Using Type Definitions to Simplify Declarations 411

18.5 Initializers 412

Uninitialized Variables 413

19 Program Design 419

19.1 Modules 420

Cohesion and Coupling 422
Types of Modules 422

19.2 Information Hiding 423

A Stack Module 423

19.3 Abstract Data Types 426

Encapsulation 427

19.4 C++ 428

Differences between C and C++ 429
Classes 431
Class Definitions 432
Member Functions 433
Constructors 435
Constructors and Dynamic Storage Allocation 436
Destructors 437
Overloading 438
Object-Oriented Programming 441
Derivation 441
Virtual Functions 443
Templates 445
Exception Handling 446

20 Low-Level Programming 451

20.1 Bitwise Operators 451

Bitwise Shift Operators 452
Bitwise Complement, And, Exclusive Or, and Inclusive Or 453
Using the Bitwise Operators to Access Bits 454
Using the Bitwise Operators to Access Bit-Fields 455
PROGRAM: XOR Encryption 456

20.2 Bit-Fields in Structures 458

How Bit-Fields Are Stored 459

20.3 Other Low-Level Techniques 460

Defining Machine-Dependent Types 460
Using Unions to Provide Multiple Views of Data 461
Using Pointers as Addresses 462
PROGRAM: Toggling the Num Lock Key 463
The volatile Type Qualifier 464

21 The Standard Library 467

21.1 Using the Library 467

Restrictions on Names Used in the Library 468
Functions Hidden by Macros 468

21.2 Library Overview 469

21.3 The <stddef.h> Header: Common Definitions 471

22 Input/Output 475

22.1 Streams 476

File Pointers 476
Standard Streams and Redirection 476
Text Files versus Binary Files 477

22.2 File Operations 478

Opening a File 479
Modes 479
Closing a File 480
Attaching a File to a Stream 481
Obtaining File Names from the Command Line 481
PROGRAM: Checking Whether a File Can Be Opened 482
Temporary Files 483
File Buffering 484
Miscellaneous File Operations 486

22.3 Formatted I/O 486

The ...printf Functions 487
...printf Conversion Specifications 487
Examples of ...printf Conversion Specifications 490
The ...scanf Functions 492
...scanf Format Strings 492
...scanf Conversion Specifications 493
scanf Examples 495
Detecting End-of-File and Error Conditions 496

22.4 Character I/O 498

Output Functions 498
Input Functions 499
PROGRAM: Copying a File 500

22.5 Line I/O 501

Output Functions 501
Input Functions 502

22.6 Block I/O 503

22.7 File Positioning 504

PROGRAM: Modifying a File of Part Records 506

22.8 String I/O 507

23 Library Support for Numbers and Character Data 519

23.1 The <float.h> Header: Characteristics of Floating Types 519

23.2 The <limits.h> Header: Sizes of Integral Types 521

23.3 The <math.h> Header: Mathematics 522

Errors 522
Trigonometric Functions 523
Hyperbolic Functions 523
Exponential and Logarithmic Functions 524
Power Functions 525
Nearest Integer, Absolute Value, and Remainder Functions 525

23.4 The <ctype.h> Header: Character Handling 526

Character-Testing Functions 527
PROGRAM: Testing the Character-Testing Functions 527
Character Case-Mapping Functions 528
PROGRAM: Testing the Case-Mapping Functions 529

23.5 The <string.h> Header: String Handling 529

Copying Functions 530
Concatenation Functions 531
Comparison Functions 531
Search Functions 533
Miscellaneous Functions 536

24 Error Handling 539

24.1 The <assert.h> Header: Diagnostics 539

24.2 The <errno.h> Header: Errors 541

The perror and strerror Functions 541

24.3 The <signal.h> Header: Signal Handling 542

Signal Macros 543
The signal Function 543
Predefined Signal Handlers 544
The raise Function 545
PROGRAM: Testing Signals 545

24.4 The <setjmp.h> Header: Nonlocal Jumps 546

PROGRAM: Testing setjmp/longjmp 547

25 International Features 551

25.1 The <locale.h> Header: Localization 551

Categories 552
The setlocale Function 552
The localeconv Function 553

25.2 Multibyte Characters and Wide Characters 556

Multibyte Characters 556
Wide Characters 557
Multibyte Character Functions 558
Multibyte String Functions 559

25.3 Trigraph Sequences 560

26 Miscellaneous Library Functions 563

26.1 The <stdarg.h> Header: Variable Arguments 563

Calling a Function with a Variable Argument List 565
The ...vprintf Functions 566

26.2 The <stdlib.h> Header: General Utilities 567

String Conversion Functions 567
PROGRAM: Testing the String Conversion Functions 568
Pseudo-Random Sequence Generation Functions 570
PROGRAM: Testing the Pseudo-Random Sequence Generation Functions 571
Communication with the Environment 572
Searching and Sorting Utilities 573
PROGRAM: Determining Air Mileage 574
Integer Arithmetic Functions 575

26.3 The <time.h> Header: Date and Time 576

Time Manipulation Functions 577
Time Conversion Functions 579
PROGRAM: Displaying the Date and Time 580

Appendix A C Syntax 587

Appendix B C Operators 595

Appendix C Standard C versus Classic C 597

Appendix D Standard Library Functions 601

Appendix E ASCII Character Set 637

Bibliography 639

Index 643


Copyright © 1996 W. W. Norton & Company, Inc. All rights reserved.

Google
 
Web knking.com

Copyright © 1999-2009 K. N. King. All rights reserved.