Sams Teach Yourself C in 24 Hours (4 page)

BOOK: Sams Teach Yourself C in 24 Hours
13.76Mb size Format: txt, pdf, ePub
ads

Why Use typedef? ..............................................................................................300

Recursive Functions ............................................................................................303

Revisiting the main() Function............................................................................305

Command-Line Arguments ............................................................................305

Receiving Command-Line Arguments ..........................................................306

Summary ..............................................................................................................308

Q&A ....................................................................................................................308

Workshop ............................................................................................................309

Quiz ................................................................................................................309

Exercises ........................................................................................................310

Part V Structure, Union, File I/O, and More

311

Hour 19 Understanding Structures

313

What Is a Structure? ............................................................................................314

Declaring Structures ......................................................................................314

Defining Structure Variables ..........................................................................315

Referencing Structure Members with the Dot Operator ....................................315

Initializing Structures ..........................................................................................317

Structures and Function Calls..............................................................................319

Referencing Structures with Pointers ............................................................322

Referencing a Structure Member with -> ......................................................324

Arrays of Structures ............................................................................................324

Nested Structures............................................................................................327

Summary ..............................................................................................................330

00 067231861x FM 4.10.2000 10:53 AM Page xiii

Contents

xiii

Q&A ....................................................................................................................330

Workshop ............................................................................................................331

Quiz ................................................................................................................331

Exercises ........................................................................................................332

Hour 20 Understanding Unions

333

What Is a Union? ................................................................................................334

Declaring Unions............................................................................................334

Defining Union Variables ..............................................................................334

Referencing a Union with . or -> ..................................................................335

Unions versus Structures ....................................................................................337

Initializing a Union ........................................................................................337

The Size of a Union........................................................................................339

Using Unions ......................................................................................................341

Referencing the Same Memory Location Differently....................................341

Making Structures Flexible ............................................................................343

Defining Bit Fields with struct ....................................................................347

Summary ..............................................................................................................350

Q&A ....................................................................................................................351

Workshop ............................................................................................................352

Quiz ................................................................................................................352

Exercises ........................................................................................................353

Hour 21 Reading and Writing with Files

355

Files Versus Streams ............................................................................................356

What Is a File?................................................................................................356

What Is a Stream? ..........................................................................................356

Buffered I/O....................................................................................................356

The Basics of Disk File I/O ................................................................................357

Pointers of FILE ............................................................................................357

Opening a File ................................................................................................357

Closing a File..................................................................................................358

Reading and Writing Disk Files ..........................................................................360

One Character at a Time ................................................................................360

One Line at a Time ........................................................................................363

One Block at a Time ......................................................................................366

Summary ..............................................................................................................370

Q&A ....................................................................................................................370

Workshop ............................................................................................................371

Quiz ................................................................................................................371

Exercises ........................................................................................................372

00 067231861x FM 4.10.2000 10:53 AM Page xiv

xiv

Sams Teach Yourself C in 24 Hours

Hour 22 Using Special File Functions

373

Random Access to Disk Files ..............................................................................374

The fseek() and ftell() Functions..............................................................374

The rewind() Function ..................................................................................378

More Examples of Disk File I/O ........................................................................378

Reading and Writing Binary Data ..................................................................378

The fscanf() and fprintf() Functions ........................................................381

Redirecting the Standard Streams with freopen() ........................................384

Summary ..............................................................................................................387

Q&A ....................................................................................................................387

Workshop ............................................................................................................388

Quiz ................................................................................................................388

Exercises ........................................................................................................389

Hour 23 Compiling Programs: The C Preprocessor

391

What Is the C Preprocessor?................................................................................392

The C Preprocessor versus the Compiler ............................................................392

The #define and #undef Directives ....................................................................393

Defining Function-Like Macros with #define ..............................................394

Nested Macro Definitions ..............................................................................396

Compiling Your Code Under Conditions ............................................................397

The #ifdef and #endif Directives ................................................................397

The #ifndef Directive ....................................................................................397

The #if, #elif, and #else Directives ............................................................399

Nested Conditional Compilation ....................................................................402

Summary ..............................................................................................................405

Q&A ....................................................................................................................405

Workshop ............................................................................................................406

Quiz ................................................................................................................406

Exercises ........................................................................................................407

Hour 24 Where Do You Go from Here?

409

Creating a Linked List ........................................................................................410

Programming Style ..............................................................................................418

Modular Programming ........................................................................................419

Debugging............................................................................................................420

What You Have Learned ......................................................................................420

C Keywords ....................................................................................................420

Operators ........................................................................................................421

Constants ........................................................................................................422

Data Types ......................................................................................................423

Expressions and Statements ..........................................................................426

Control Flow Statements ................................................................................426

00 067231861x FM 4.10.2000 10:53 AM Page xv

Contents

xv

Pointers ..........................................................................................................430

Functions ........................................................................................................432

Input and Output (I/O)....................................................................................433

The C Preprocessor ........................................................................................434

The Road Ahead… ..............................................................................................434

Summary ..............................................................................................................435

Part VI Appendixes

437

Appendix A ANSI Standard Header Files

439

Appendix B Answers to Quiz Questions and Exercises

441

Hour 1, “Taking the First Step” ..........................................................................441

Quiz ................................................................................................................441

Hour 2, “Writing Your First C Program” ............................................................442

Quiz ................................................................................................................442

Exercises ........................................................................................................442

Hour 3, “Learning the Structure of a C Program” ..............................................443

Quiz ................................................................................................................443

Exercises ........................................................................................................444

Hour 4, “Understanding Data Types and Keywords”..........................................445

Quiz ................................................................................................................445

Exercises ........................................................................................................445

BOOK: Sams Teach Yourself C in 24 Hours
13.76Mb size Format: txt, pdf, ePub
ads

Other books

Under New Management by June Hopkins
Dancer by Viola Grace
I Shall Not Want by Debbie Viguie
If by Nina G. Jones
MEG: Nightstalkers by Steve Alten
Still by Mayburn, Ann
Takes the Cake by Lynn Chantale