Table of contents for C programming : a modern approach / K.N. King.

Bibliographic record and links to related information available from the Library of Congress catalog.

Note: Contents data are machine generated based on pre-publication provided by the publisher. Contents may have variations from the printed book or be incomplete or contain other coding.


Counter
vii
CONTENTS
Preface xxiii
1 INTRODUCING C 1
1.1 History of C 1
Origins 2
Standardization 2
C-Based Languages 3
1.2 Strengths and Weaknesses of C 4
Strengths 4
Weaknesses 5
Effective Use of C 6
2 C FUNDAMENTALS 11
2.1 Writing a Simple Program 11
Program: Printing a Pun 11
Compiling and Linking 12
Integrated Development Environments 13
2.2 The General Form of a Simple Program 13
Directives 14
Functions 14
Statements 16
Printing Strings 16
2.3 Comments 17
2.4 Variables and Assignment 19
Types 19
Declarations 19
Assignment 20
viii Contents
Printing the Value of a Variable 21
Program: Computing the Dimensional Weight of a Box 21
Initialization 23
Printing Expressions 23
2.5 Reading Input 23
Program: Computing the Dimensional Weight of a Box
(Revisited) 24
2.6 Defining Names for Constants 25
Program: Converting from Fahrenheit to Celsius 25
2.7 Identifiers 27
Keywords 28
2.8 Layout of a C Program 28
3 FORMATTED INPUT/OUTPUT 37
3.1 The printf Function 37
Conversion Specifications 38
Program: Using printf to Format Numbers 40
Escape Sequences 41
3.2 The scanf Function 42
How scanf Works 43
Ordinary Characters in Format Strings 45
Confusing printf with scanf 45
Program: Adding Fractions 46
4 EXPRESSIONS 53
4.1 Arithmetic Operators 54
Operator Precedence and Associativity 55
Program: Computing a UPC Check Digit 56
4.2 Assignment Operators 58
Simple Assignment 58
Lvalues 60
Compound Assignment 60
4.3 Increment and Decrement Operators 61
4.4 Expression Evaluation 63
Order of Subexpression Evaluation 64
4.5 Expression Statements 65
5 SELECTION STATEMENTS 73
5.1 Logical Expressions 74
Relational Operators 74
Equality Operators 75
Logical Operators 75
5.2 The if Statement 76
Compound Statements 77
Contents ix
The else Clause 78
Cascaded if Statements 79
Program: Calculating a Broker¿s Commission 80
The ¿Dangling else¿ Problem 81
Conditional Expressions 82
Boolean Values in C89 83
Boolean Values in C99 84
5.3 The switch Statement 85
The Role of the break Statement 87
Program: Printing a Date in Legal Form 88
6 LOOPS 99
6.1 The while Statement 99
Infinite Loops 101
Program: Printing a Table of Squares 102
Program: Summing a Series of Numbers 102
6.2 The do Statement 103
Program: Calculating the Number of Digits in an Integer 104
6.3 The for Statement 105
for Statement Idioms 106
Omitting Expressions in a for Statement 107
for Statements in C99 108
The Comma Operator 109
Program: Printing a Table of Squares (Revisited) 110
6.4 Exiting from a Loop 111
The break Statement 111
The continue Statement 112
The goto Statement 113
Program: Balancing a Checkbook 114
6.5 The Null Statement 116
7 BASIC TYPES 125
7.1 Integer Types 125
Integer Constants 127
Reading and Writing Integers 129
Integer Types in C99 130
Program: Summing a Series of Numbers (Revisited) 131
7.2 Floating Types 132
Floating Constants 133
Reading and Writing Floating-Point Numbers 133
7.3 Character Types 134
Operations on Characters 135
Signed and Unsigned Characters 135
Arithmetic Types 136
Escape Sequences 137
x Contents
Character-Handling Functions 138
Reading and Writing Characters using scanf and printf 139
Reading and Writing Characters using getchar and
putchar 139
Program: Determining the Length of a Message 141
7.4 Type Conversion 142
The Usual Arithmetic Conversions 143
Conversion During Assignment 145
Implicit Conversions in C99 146
Casting 147
7.5 Type Definitions 148
Advantages of Type Definitions 149
Type Definitions and Portability 149
7.6 The sizeof Operator 150
8 ARRAYS 161
8.1 One-Dimensional Arrays 161
Array Subscripting 162
Program: Reversing a Series of Numbers 164
Array Initialization 164
Designated Initializers 165
Program: Checking a Number for Repeated Digits 166
Using the sizeof Operator with Arrays 167
Program: Computing Interest 168
8.2 Multidimensional Arrays 169
Initializing a Multidimensional Array 170
Constant Arrays 172
Program: Dealing a Hand of Cards 172
8.3 Variable-Length Arrays (C99) 174
9 FUNCTIONS 183
9.1 Defining and Calling Functions 183
Program: Computing Averages 184
Program: Printing a Countdown 185
Program: Printing a Pun (Revisited) 186
Function Definitions 187
Function Calls 189
Program: Testing Whether a Number Is Prime 190
9.2 Function Declarations 191
9.3 Arguments 193
Argument Conversions 194
Array Arguments 195
Variable-Length Array Arguments 198
Using static in Array Arguments 200
Compound Literals 200
Contents xi
9.4 The return Statement 201
9.5 Program Termination 202
The exit Function 203
9.6 Recursive Functions 204
The Quicksort Algorithm 205
Program: Quicksort 207
10 PROGRAM ORGANIZATION 219
10.1 Local Variables 219
Static Local Variables 220
Parameters 221
10.2 External Variables 221
Example: Using External Variables to Implement a Stack 221
Pros and Cons of External Variables 222
Program: Guessing a Number 224
10.3 Blocks 227
10.4 Scope 228
10.5 Organizing a C Program 230
Program: Classifying a Poker Hand 231
11 POINTERS 241
11.1 Pointer Variables 241
Declaring Pointer Variables 242
11.2 The Address and Indirection Operators 243
The Address Operator 243
The Indirection Operator 244
11.3 Pointer Assignment 245
11.4 Pointers as Arguments 247
Program: Finding the Largest and Smallest Elements in an
Array 249
Using const to Protect Arguments 251
11.5 Pointers as Return Values 251
12 POINTERS AND ARRAYS 257
12.1 Pointer Arithmetic 257
Adding an Integer to a Pointer 258
Subtracting an Integer from a Pointer 259
Subtracting Pointers 259
Comparing Pointers 260
Pointers to Compound Literals 260
12.2 Using Pointers for Array Processing 260
Combining the * and ++ Operators 262
12.3 Using an Array Name as a Pointer 263
Program: Reversing a Series of Numbers (Revisited) 264
xii Contents
Array Arguments (Revisited) 265
Using a Pointer as an Array Name 267
12.4 Pointers and Multidimensional Arrays 267
Processing the Elements of a Multidimensional Array 267
Processing the Rows of a Multidimensional Array 268
Processing the Columns of a Multidimensional Array 269
Using the Name of a Multidimensional Array as a Pointer 269
12.5 Pointers and Variable-Length Arrays (C99) 270
13 STRINGS 277
13.1 String Literals 277
Escape Sequences in String Literals 278
Continuing a String Literal 278
How String Literals Are Stored 279
Operations on String Literals 279
String Literals versus Character Constants 280
13.2 String Variables 281
Initializing a String Variable 281
Character Arrays versus Character Pointers 283
13.3 Reading and Writing Strings 284
Writing Strings Using printf and puts 284
Reading Strings Using scanf and gets 285
Reading Strings Character by Character 286
13.4 Accessing the Characters in a String 287
13.5 Using the C String Library 289
The strcpy (String Copy) Function 290
The strcat (String Concatenate) Function 291
The strcmp (String Compare) Function 291
The strlen (String Length) Function 292
Program: Printing a One-Month Reminder List 293
13.6 String Idioms 295
Searching for the End of a String 295
Copying a String 297
13.7 Arrays of Strings 299
Command-Line Arguments 301
Program: Checking Planet Names 303
14 THE PREPROCESSOR 315
14.1 How the Preprocessor Works 315
14.2 Preprocessing Directives 318
14.3 Macro Definition 319
Simple Macros 319
Parameterized Macros 321
The # Operator 324
The ## Operator 325
Contents xiii
General Properties of Macros 326
Parentheses in Macro Definitions 327
Creating Longer Macros 328
Predefined Macros 329
Additional Predefined Macros in C99 330
Empty Macro Arguments 331
Macros with a Variable Number of Arguments 332
The __func__ Variable 333
14.4 Conditional Compilation 334
The #if and #endif Directives 334
The defined Operator 335
The #ifdef and #ifndef Directives 336
The #elif and #else Directives 336
Uses of Conditional Compilation 337
14.5 Miscellaneous Directives 338
The #error Directive 339
The #line Directive 339
The #pragma Directive 341
The _Pragma Operator 341
15 WRITING LARGE PROGRAMS 349
15.1 Source Files 349
15.2 Header Files 351
The #include Directive 351
Sharing Macro Definitions and Type Definitions 353
Sharing Function Prototypes 354
Sharing Variable Declarations 356
Nested Includes 357
Protecting Header Files 357
#error Directives in Header Files 359
15.3 Dividing a Program into Files 359
Program: Text Formatting 360
15.4 Building a Multiple-File Program 366
Makefiles 367
Errors During Linking 368
Rebuilding a Program 369
Defining Macros Outside a Program 371
16 STRUCTURES, UNIONS, AND ENUMERATIONS 377
16.1 Structure Variables 377
Declaring Structure Variables 378
Initializing Structure Variables 379
Designated Initializers 380
Operations on Structures 381
16.2 Structure Types 382
xiv Contents
Declaring a Structure Tag 382
Defining a Structure Type 384
Structures as Arguments and Return Values 384
Compound Literals 385
16.3 Nested Arrays and Structures 387
Nested Structures 387
Arrays of Structures 388
Initializing an Array of Structures 388
Program: Maintaining a Parts Database 389
16.4 Unions 396
Using Unions to Save Space 398
Using Unions to Build Mixed Data Structures 400
Adding a ¿Tag Field¿ to a Union 400
16.5 Enumerations 402
Enumeration Tags and Types 403
Enumerations as Integers 403
Using Enumerations to Declare ¿Tag Fields¿ 404
17 ADVANCED USES OF POINTERS 415
17.1 Dynamic Storage Allocation 416
Memory Allocation Functions 416
Null Pointers 416
17.2 Dynamically Allocated Strings 417
Using malloc to Allocate Memory for a String 418
Using Dynamic Storage Allocation in String Functions 419
Arrays of Dynamically Allocated Strings 420
Program: Printing a One-Month Reminder List (Revisited) 420
17.3 Dynamically Allocated Arrays 422
Using malloc to Allocate Storage for an Array 422
The calloc Function 423
The realloc Function 423
17.4 Deallocating Storage 424
The free Function 425
The ¿Dangling Pointer¿ Problem 426
17.5 Linked Lists 426
Declaring a Node Type 427
Creating Nodes 427
The -> Operator 428
Inserting a Node at the Beginning of a Linked List 429
Searching a Linked List 431
Deleting a Node from a Linked List 433
Ordered Lists 435
Program: Maintaining a Parts Database (Revisited) 435
17.6 Pointers to Pointers 440
17.7 Pointers to Functions 441
Contents xv
Function Pointers as Arguments 442
The qsort Function 442
Other Uses of Function Pointers 444
Program: Tabulating the Trigonometric Functions 445
17.8 Restricted Pointers (C99) 447
17.9 Flexible Array Members (C99) 449
18 DECLARATIONS 459
18.1 Declaration Syntax 459
18.2 Storage Classes 461
Properties of Variables 462
The auto Storage Class 463
The static Storage Class 463
The extern Storage Class 465
The register Storage Class 465
The Storage Class of a Function 466
Summary 467
18.3 Type Qualifiers 468
18.4 Declarators 469
Deciphering Complex Declarations 471
Using Type Definitions to Simplify Declarations 472
18.5 Initializers 473
Uninitialized Variables 474
18.6 Inline Functions (C99) 475
Inline Definitions 475
Restrictions on Inline Functions 477
19 PROGRAM DESIGN 485
19.1 Modules 486
Cohesion and Coupling 488
Types of Modules 488
19.2 Information Hiding 489
A Stack Module 489
19.3 Abstract Data Types 493
Encapsulation 494
Incomplete Types 494
19.4 A Stack Abstract Data Type 495
Defining the Interface for the Stack ADT 495
Implementing the Stack ADT Using a Fixed-Length Array 496
Changing the Item Type in the Stack ADT 498
Implementing the Stack ADT Using a Dynamic Array 499
Implementing the Stack ADT Using a Linked List 501
19.5 Design Issues for Abstract Data Types 504
Naming Conventions 504
Error-Handling 504
xvi Contents
Generic ADTs 505
ADTs in Newer Languages 505
20 LOW-LEVEL PROGRAMMING 511
20.1 Bitwise Operators 511
Bitwise Shift Operators 512
Bitwise Complement, And, Exclusive Or, and Inclusive Or 513
Using the Bitwise Operators to Access Bits 514
Using the Bitwise Operators to Access Bit-Fields 515
Program: XOR Encryption 516
20.2 Bit-Fields in Structures 518
How Bit-Fields Are Stored 519
20.3 Other Low-Level Techniques 520
Defining Machine-Dependent Types 520
Using Unions to Provide Multiple Views of Data 521
Using Pointers as Addresses 522
Program: Viewing Memory Locations 523
The volatile Type Qualifier 525
21 THE STANDARD LIBRARY 531
21.1 Using the Library 531
Restrictions on Names Used in the Library 532
Functions Hidden by Macros 533
21.2 C89 Library Overview 533
21.3 C99 Library Changes 536
21.4 The <stddef.h> Header: Common Definitions 538
21.5 The <stdbool.h> Header: Boolean Type and Values (C99)539
22 INPUT/OUTPUT 543
22.1 Streams 544
File Pointers 544
Standard Streams and Redirection 545
Text Files versus Binary Files 545
22.2 File Operations 547
Opening a File 547
Modes 548
Closing a File 549
Attaching a File to a Stream 550
Obtaining File Names from the Command Line 550
Program: Checking Whether a File Can Be Opened 551
Temporary Files 552
File Buffering 553
Miscellaneous File Operations 555
22.3 Formatted I/O 555
The ¿printf Functions 556
Contents xvii
¿printf Conversion Specifications 556
C99 Changes to ¿printf Conversion Specifications 559
Examples of ¿printf Conversion Specifications 560
The ¿scanf Functions 563
¿scanf Format Strings 563
¿scanf Conversion Specifications 564
C99 Changes to ¿scanf Conversion Specifications 567
scanf Examples 567
Detecting End-of-File and Error Conditions 568
22.4 Character I/O 571
Output Functions 571
Input Functions 572
Program: Copying a File 573
22.5 Line I/O 574
Output Functions 574
Input Functions 575
22.6 Block I/O 576
22.7 File Positioning 577
Program: Modifying a File of Part Records 579
22.8 String I/O 580
Output Functions 581
Input Functions 581
23 LIBRARY SUPPORT FOR NUMBERS AND CHARACTER
DATA 595
23.1 The Header: Characteristics of Floating Types 595
23.2 The <limits.h> Header: Sizes of Integer Types 597
23.3 The Header (C89): Mathematics 599
Errors 599
Trigonometric Functions 600
Hyperbolic Functions 601
Exponential and Logarithmic Functions 601
Power Functions 602
Nearest Integer, Absolute Value, and Remainder Functions 603
23.4 The Header (C99): Mathematics 604
IEEE Floating-Point Standard 604
Types 606
Macros 606
Errors 606
Functions 608
Classification Macros 608
Trigonometric Functions 609
Hyperbolic Functions 610
Exponential and Logarithmic Functions 610
Power and Absolute Value Functions 612
xviii Contents
Error and Gamma Functions 613
Nearest Integer Functions 614
Remainder Functions 615
Manipulation Functions 616
Maximum, Minimum, and Positive Difference Functions 617
Floating Multiply-Add 617
Comparison Macros 618
23.5 The Header: Character Handling 619
Character-Classification Functions 620
Program: Testing the Character-Classification Functions 620
Character Case-Mapping Functions 622
Program: Testing the Case-Mapping Functions 622
23.6 The <string.h> Header: String Handling 622
Copying Functions 623
Concatenation Functions 624
Comparison Functions 625
Search Functions 626
Miscellaneous Functions 629
24 ERROR HANDLING 635
24.1 The <assert.h> Header: Diagnostics 636
24.2 The Header: Errors 637
The perror and strerror Functions 638
24.3 The <signal.h> Header: Signal Handling 639
Signal Macros 639
The signal Function 640
Predefined Signal Handlers 641
The raise Function 642
Program: Testing Signals 642
24.4 The <setjmp.h> Header: Nonlocal Jumps 643
Program: Testing setjmp/longjmp 644
25 INTERNATIONAL FEATURES 649
25.1 The <locale.h> Header: Localization 650
Categories 650
The setlocale Function 651
The localeconv Function 652
25.2 Multibyte Characters and Wide Characters 656
Multibyte Characters 656
Wide Characters 657
Unicode and the Universal Character Set 658
Encodings of Unicode 658
Multibyte/Wide-Character Conversion Functions 660
Multibyte/Wide String Conversion Functions 662
25.3 Digraphs and Trigraphs 662
Contents xix
Trigraphs 663
Digraphs (C99) 664
The <iso646.h> Header: Alternative Spellings (C99) 664
25.4 Universal Character Names (C99) 665
25.5 The Header (C99) 666
Stream Orientation 666
Formatted Wide-Character Input/Output Functions 668
Wide-Character Input/Output Functions 670
General Wide String Utilities 671
Wide String Numeric Conversion Functions 672
Wide String Copying Functions 673
Wide String Concatenation Functions 673
Wide String Comparison Functions 674
Wide String Search Functions 674
Miscellaneous Functions 676
Wide-Character Time Conversion Functions 676
Extended Multibyte/Wide-Character Conversion Utilities 676
Single-Byte/Wide-Character Conversion Functions 677
Conversion State Functions 678
Restartable Multibyte/Wide-Character Conversion Functions 678
Restartable Multibyte/Wide String Conversion Functions 679
25.6 The <wctype.h> Header (C99) 680
Wide-Character Classification Functions 681
Extensible Wide-Character Classification Functions 682
Wide-Character Case-Mapping Functions 682
Extensible Wide-Character Case-Mapping Functions 683
26 MISCELLANEOUS LIBRARY FUNCTIONS 687
26.1 The <stdarg.h> Header: Variable Arguments 687
Calling a Function with a Variable Argument List 689
The v¿printf Functions 690
The v¿scanf Functions (C99) 691
26.2 The <stdlib.h> Header: General Utilities 691
Numeric Conversion Functions 693
Program: Testing the Numeric Conversion Functions 694
Pseudo-Random Sequence Generation Functions 696
Program: Testing the Pseudo-Random Sequence Generation
Functions 697
Communication with the Environment 698
Searching and Sorting Utilities 700
Program: Determining Air Mileage 700
Integer Arithmetic Functions 702
26.3 The Header: Date and Time 702
Time Manipulation Functions 703
Time Conversion Functions 706
Program: Displaying the Date and Time 709
xx Contents
27 ADDITIONAL C99 SUPPORT FOR MATHEMATICS 717
27.1 The <stdint.h> Header: Integer Types 717
<stdint.h> Types 718
Limits of Specified-Width Integer Types 719
Limits of Other Integer Types 720
Macros for Integer Constants 720
27.2 The <inttypes.h> Header 721
Macros for Format Specifiers 722
Functions for Greatest-Width Integer Types 723
27.3 Complex Numbers 724
Definition of Complex Numbers 725
Complex Arithmetic 726
Complex Types in C99 727
Operations on Complex Numbers 727
Conversion Rules for Complex Types 728
27.4 The <complex.h> Header: Complex Arithmetic 729
<complex.h> Macros 729
The CX_LIMITED_RANGE Pragma 730
<complex.h> Functions 731
Trigonometric Functions 732
Hyperbolic Functions 733
Exponential and Logarithmic Functions 734
Power and Absolute-Value Functions 734
Manipulation Functions 735
Program: Finding the Roots of a Quadratic Equation 735
27.5 The <tgmath.h> Header: Type-Generic Math 736
Type-Generic Macros 737
Invoking a Type-Generic Macro 738
27.6 The Header: Floating-Point Environment 739
Floating-Point Status Flags and Control Modes 740
 Macros 741
The FENV_ACCESS Pragma 741
Floating-Point Exception Functions 742
Rounding Functions 743
Environment Functions 744
Appendix A C Operators 749
Appendix B C99 versus C89 751
Appendix C C89 versus K&R C 757
Appendix D Standard Library Functions 761
Appendix E ASCII Character Set 815
Bibliography 817
Index 821

Library of Congress Subject Headings for this publication:

C (Computer program language).