# C语言编程读书笔记（2 详细目录）

1 Introduction 1

2 Some Fundamentals 5

2.1 Programming 5

2.2 Higher-Level Languages 6

2.3 Operating Systems 6

2.4 Compiling Programs 7

2.5 Integrated Development Environments 10

2.6 Language Interpreters 10

3 Compiling and Running Your First Program 11

3.3 Understanding Your First Program 13

3.4 Displaying the Values of Variables 15

Exercises 19

4 Variables, Data Types, and Arithmetic Expressions 21

4.1 Working with Variables 21

4.2 Understanding Data Types and Constants 23

4.2.1 The Basic Integer Type int23

4.2.2 The Floating Number Typefloat24

4.2.3 The Extended Precision Typedouble25

4.2.4 The Single Character Type char25

4.2.5 The Boolean Data Type_Bool26

4.2.6 Type Specifiers:long,long long,short,unsigned, andsigned28

4.3 Working with Arithmetic Expressions 30

4.3.1 Integer Arithmetic and the Unary Minus

4.3.2 Operator 33

4.3.3 The Modulus Operator 35

4.3.4 Integer and Floating-Point Conversions 36

4.4 Combining Operations with Assignment:The Assignment Operators 38

4.5 Types_Complexand_Imaginary39

Exercises 40

5 Program Looping 43

5.1 The for Statement 44

5.1.1 Relational Operators 46

5.1.2 Aligning Output 50

5.1.3 Program Input 51

5.1.4 Nested for Loops 53

5.2 for Loop Variants 54

5.3 The while Statement 56

5.4 The do Statement 60

5.5 The break Statement 62

5.6 The continue Statement 62

Exercises 63

6 Making Decisions 65

6.1 The if Statement 65

6.1.1 The if-else Construct 69

6.1.2 Compound Relational Tests 72

6.1.3 Nested if Statements 75

6.1.4 The else if Construct 76

6.2 The switch Statement 84

6.3 Boolean Variables 87

6.4 The Conditional Operator 91

Exercises 93

7 Working with Arrays 95

7.1 Defining an Array 96

7.1.1 Using Array Elements as Counters 100

7.1.2 Generating Fibonacci Numbers 103

7.1.3 Using an Array to Generate Prime Numbers 104

7.2 Initializing Arrays 106

7.3 Character Arrays 108

7.3.1 Base Conversion Using Arrays 109

7.3.2 The const Qualifier 111

7.4 Multidimensional Arrays 113

7.5 Variable-Length Arrays 115

Exercises 117

8 Working with Functions 119

8.1 Defining a Function 119

8.2 Arguments and Local Variables 122

8.2.1 Function Prototype Declaration 124

8.2.2 Automatic Local Variables 124

8.3 Returning Function Results 126

8.4 Functions Calling… 131

8.4.1 Declaring Return Types and Argument Types 134

8.4.2 Checking Function Arguments 135

8.5 Top-Down Programming 137

8.6 Functions and Arrays 137

8.6.1 Assignment Operators 142

8.6.2 Sorting Arrays 143

8.6.3 Multidimensional Arrays 146

8.7 Global Variables 152

8.8 Automatic and Static Variables 156

8.9 Recursive Functions 159

Exercises 162

9 Working with Structures 165

9.1 A Structure for Storing the Date 166

9.1.1 Using Structures in Expressions 168

9.2 Functions and Structures 171

9.2.1 A Structure for Storing the Time 177

9.3 Initializing Structures 180

9.3.1 Compound Literals 181

9.4 Arrays of Structures 182

9.5 Structures Containing Structures 185

9.6 Structures Containing Arrays 187

9.7 Structure Variants 190

Exercises 191

10 Character Strings 195

10.1 Arrays of Characters 196

10.2 Variable-Length Character Strings 198

10.2.1 Initializing and Displaying Character Strings 201

10.2.2 Testing Two Character Strings for Equality 204

10.2.3 Inputting Character Strings 206

10.2.4 Single-Character Input 208

10.2.5 The Null String 213

10.3 Escape Characters 216

10.4 More on Constant Strings 218

10.5 Character Strings, Structures, and Arrays 219

10.5.1 A Better Search Method 222

10.6 Character Operations 227

Exercises 230

11 Pointers 235

11.1 Defining a Pointer Variable 235

11.2 Using Pointers in Expressions 239

11.3 Working with Pointers and Structures 240

11.3.1 Structures Containing Pointers 243

11.4 The Keywordconstand Pointers 253

11.5 Pointers and Functions 254

11.6 Pointers and Arrays 259

11.6.1 A Slight Digression About Program Optimization 263

11.6.2 Is It an Array or Is It a Pointer? 264

11.6.3 Pointers to Character Strings 266

11.6.4 Constant Character Strings and Pointers 267

11.6.5 The Increment and Decrement Operators Revisited 268

11.7 Operations on Pointers 272

11.8 Pointers to Functions 273

11.9 Pointers and Memory Addresses 274

Exercises 276

12 Operations on Bits 279

12.1 Bit Operators 280

12.1.1 The Bitwise AND Operator 281

12.1.2 The Bitwise Inclusive-OR Operator 283

12.1.3 The Bitwise Exclusive-OR Operator 284

12.1.4 The Ones Complement Operator 285

12.1.5 The Left Shift Operator 287

12.1.6 The Right Shift Operator 287

12.1.7 A Shift Function 288

12.1.8 Rotating Bits 290

12.2 Bit Fields 292

Exercises 297

13 The Preprocessor 299

13.1 The#defineStatement 299

13.1.1 Program Extendability 303

13.1.2 Program Portability 305

13.1.3 More Advanced Types of Definitions 306

13.1.4 The#Operator 312

13.1.5 The##Operator 313

13.2 The#includeStatement 313

13.2.1 System Include Files 316

13.3 Conditional Compilation 316

13.3.1 The#ifdef,#endif,#else, and#ifndefStatements 316

13.3.2 The#ifand#elifPreprocessor Statements 318

13.3.3.The#undefStatement 319

Exercises 320

14 More on Data Types 321

14.1 Enumerated Data Types 321

14.2 The typedef Statement 325

14.3 Data Type Conversions 327

14.3.1 Sign Extension 329

14.3.2 Argument Conversion 329

Exercises 330

15 Working with Larger Programs 333

15.1 Dividing Your Program into Multiple Files 333

15.1.1 Compiling Multiple Source Files from the Command Line 334

15.2 ommunication Between Modules 336

15.2.1 External Variables 336

15.2.2StaticVersusExternVariables and Functions 339

15.2.3 Using Header Files Effectively 341

15.3 Other Utilities for Working with Larger Programs 342

15.3.1 The make Utility 343

15.3.2 The cvs Utility 344

15.3.3 Unix Utilities:ar,grep,sed, and so on 345

16 Input and Output Operations in C 347

16.1 Character I/O:getcharandputchar348

16.2 Formatted I/O:printfandscanf348

16.2.1 The printf Function 348

16.2.2 The scanf Function 355

16.3 Input and Output Operations with Files 359

16.3.1 Redirecting I/O to a File 359

16.3.2 End of File 361

16.4 Special Functions for Working with Files 363

16.4.1 The fopen Function 363

16.4.2 The getc and putc Functions 365

16.4.3 The fclose Function 365

16.4.4 The feof Function 367

16.4.5 The fprintf and fscanf Functions 368

16.4.6 The fgets and fputs Functions 368

16.4.7stdin,stdout, andstderr369

16.4.8 The exit Function 370

16.4.9 Renaming and Removing Files 371

Exercises 371

17 Miscellaneous and Advanced Features 373

17.1 Miscellaneous Language Statements 373

17.1.1 The goto Statement 373

17.1.2 The null Statement 374

17.2 Working with Unions 375

17.3 The Comma Operator 378

17.4 Type Qualifiers 378

17.4.1 The register Qualifier 378

17.4.2 The volatile Qualifier 379

17.4.3 The restrict Qualifier 379

17.5 Command-Line Arguments 380

17.6 Dynamic Memory Allocation 383

17.6.1 The calloc and malloc Functions 384

17.6.2 The sizeof Operator 385

17.6.3 The free Function 387

18 Debugging Programs 389

18.1 Debugging with the Preprocessor 389

18.2 Debugging Programs withgdb395

18.2.1 Working with Variables 398

18.2.2 Source File Display 399

18.2.3 Controlling Program Execution 400

18.2.4 Getting a Stack Trace 405

18.2.5 Calling Functions and Setting Arrays and Structures 405

18.2.6 Getting Help with gdb Commands 406

18.2.7 Odds and Ends 408

19 Object-Oriented Programming 411

19.1 What Is an Object Anyway? 411

19.2 Instances and Methods 412

19.3 Writing a C Program to Work with Fractions 413

19.4 Defining an Objective-C Class to Work with Fractions 414

19.5 Defining a C++ Class to Work with Fractions 419

19.6 Defining a C# Class to Work with Fractions 422

A C Language Summary 425

1.0 Digraphs and Identifiers 425

1.1 Digraph Characters 425

1.2 Identifiers 425

3.0 Constants 427

3.1 Integer Constants 427

3.2 Floating-Point Constants 427

3.3 Character Constants 428

3.4 Character String Constants 429

3.5 Enumeration Constants 430

4.0 Data Types and Declarations 430

4.1 Declarations 430

4.2 Basic Data Types 430

4.3 Derived Data Types 432

4.4 Enumerated Data Types 438

4.5 The typedef Statement 438

4.6 Type Modifiers const,volatile, and restrict 439

5.0 Expressions 439

5.1 Summary of C Operators 440

5.2 Constant Expressions 442

5.3 Arithmetic Operators 443

5.4 Logical Operators 444

5.5 Relational Operators 444

5.6 Bitwise Operators 445

5.7 Increment and Decrement Operators 445

5.8 Assignment Operators 446

5.9 Conditional Operators 446

5.10 Type Cast Operator 446

5.11sizeofOperator 447

5.12 Comma Operator 447

5.13 Basic Operations with Arrays 447

5.14 Basic Operations with Structures 448

5.15 Basic Operations with Pointers 448

5.16 Compound Literals 450

5.17 Conversion of Basic Data Types 451

6.0 Storage Classes and Scope 452

6.1 Functions 452

6.2 Variables 452

7.0 Functions 454

7.1 Function Definition 454

7.2 Function Call 455

7.3 Function Pointers 456

8.0 Statements 456

8.1 Compound Statements 456

8.2 The break Statement 456

8.3 The continue Statement 457

8.4 The do Statement 457

8.5 The for Statement 457

8.6 The goto Statement 458

8.7 The if Statement 458

8.8 The null Statement 458

8.9 The return Statement 459

8.10 The switch Statement 459

8.11 The while Statement 460

9.0 The Preprocessor 460

9.1 Trigraph Sequences 460

9.2 Preprocessor Directives 461

9.3 Predefined Identifiers 466

B The Standard C Library 467

1.1 stddef.h 467

1.2 limits.h 468

1.3 stdbool.h 469

1.4 float.h 469

1.5 stdint.h 469

2 String Functions 470

3 Memory Functions 472

4 Character Functions 473

5 I/O Functions 473

6 In-Memory Format Conversion Functions 478

7 String-to-Number Conversion 479

8 Dynamic Memory Allocation Functions 481

9 Math Functions 482

9.1 Complex Arithmetic 488

10 General Utility Functions 490

C Compiling Programs withgcc493

General Command Format 493

Command-Line Options 494

D Common Programming Mistakes 497

E Resources 501

1 Answers to Exercises, Errata, etc. 501

2 The C Programming Language 501

2.1 Books 501

2.2 Web Sites 502

2.3 Newsgroups 502

3 C Compilers and Integrated Development Environments 502

3.1 gcc 502

3.2 MinGW 502

3.3 CygWin 502

3.4 Visual Studio 503

3.5 CodeWarrior 503

3.6 Kylix 503

4 Miscellaneous 503

4.1 Object-Oriented Programming 503

4.2 The C++ Language 503

4.3 The C# Language 503

4.4 The Objective-C Language 503

4.5 Development Tools 504

【书名、作者及其它信息】

Programming in C

A complete introduction to the C programming language

International Standard Book Number: 0-672-32666-3

Library of Congress Catalog Card Number: 2004093272

Printed in the United States of America

First Printing: July 2004

Author:Stephen G. Kochan

www.samspublishing.com