Sams Teach Yourself C in 24 Hours

00 067231861x FM 12/11/02 10:01 AM Page i

Teach Yourself

C

in 24

Hours

Tony Zhang

SECOND EDITION

201 West 103rd St., Indianapolis, Indiana, 46290 USA

00 067231861x FM 12/11/02 10:01 AM Page ii

Sams Teach Yourself C in 24 Hours,

ASSOCIATE PUBLISHER

Michael Stephens

Second Edition

ACQUISITIONS EDITOR

Copyright ©2000 by Sams Publishing

Carol Ackerman

All rights reserved. No part of this book shall be reproduced, stored in a
DEVELOPMENT EDITOR

retrieval system, or transmitted by any means, electronic, mechanical, photo-Gus A. Miklos

copying, recording, or otherwise, without written permission from the pub-MANAGING EDITOR

lisher. No patent liability is assumed with respect to the use of the information Charlotte Clapp

contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or
PROJECT EDITOR

omissions. Nor is any liability assumed for damages resulting from the use of Andy Beaster

the information contained herein.

COPY EDITOR

International Standard Book Number: 0-672-31861-x

Kate Givens

Library of Congress Catalog Card Number: 99-067311

INDEXERS

Christine Nelsen

Printed in the United States of America

Deborah Hittel

First Printing: February, 2000

PROOFREADER

05 04 03

6 5 4 3

Candice Hightower

TECHNICAL EDITOR

Trademarks

Bill Mitchell

All terms mentioned in this book that are known to be trademarks or service
TEAM COORDINATOR

marks have been appropriately capitalized. Sams Publishing cannot attest to the Pamalee Nelson

accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark.

INTERIOR DESIGNER

Gary Adair

Warning and Disclaimer

COVER DESIGNER

Aren Howell

Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on
COPYWRITER

an “as is” basis. The author and the publisher shall have neither liability nor Eric Borgert

responsibility to any person or entity with respect to any loss or damages aris-ing from the information contained in this book.

EDITORIAL ASSISTANT

Angela Boley

PRODUCTION

Stacey DeRome

Mark Walchle

00 067231861x FM 4.10.2000 10:53 AM Page iii

Contents at a Glance

Introduction

1

Part I The Basics of C

9

Hour 1

Taking the First Step

11

2

Writing Your First C Program

27

3

Learning the Structure of a C Program

41

4

Understanding Data Types and Keywords

55

5

Handling Standard Input and Output

71

Part II Operators and Control-flow Statements

89

Hour 6

Manipulating Data

91

7

Working with Loops

105

8

Using Conditional Operators

121

9

Working with Data Modifiers and Math Functions

141

10

Controlling Program Flow

155

Part III Pointers and Arrays

173

Hour 11

Understanding Pointers

175

12

Understanding Arrays

189

13

Manipulating Strings

207

14

Understanding Scope and Storage Classes

223

Part IV Functions and Dynamic Memory Allocation

241

Hour 15

Working with Functions

243

16

Applying Pointers

259

17

Allocating Memory

279

18

Using Special Data Types and Functions

295

00 067231861x FM 4.10.2000 10:53 AM Page iv

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

311

Hour 19

Understanding Structures

313

20

Understanding Unions

333

21

Reading and Writing with Files

355

22

Using Special File Functions

373

23

Compiling Programs: The C Preprocessor

391

24

Where Do You Go from Here?

409

Part VI Appendixes

437

Appendix A

ANSI Standard Header Files

439

B

Answers to Quiz Questions and Exercises

441

Index

503

00 067231861x FM 4.10.2000 10:53 AM Page v

Table of Contents

Introduction

1

Who Should Read This Book? ................................................................................1

Special Features of This Book ................................................................................1

Programming Examples ..........................................................................................2

Q&A and Workshop ................................................................................................4

Conventions Used in This Book ..............................................................................4

What You’ll Learn in 24 Hours................................................................................4

Part I The Basics of C

9

Hour 1 Taking the First Step

11

What Is C? ............................................................................................................12

The ANSI C Standard ..........................................................................................15

Assumptions About You ........................................................................................16

Setting Up Your System ........................................................................................16

Hardware ..........................................................................................................16

Software............................................................................................................16

A Sample C Programming Setup ....................................................................17

Using Microsoft’s Compiler ............................................................................18

Using Borland’s Compiler................................................................................21

Summary ................................................................................................................24

Q&A ......................................................................................................................25

Workshop ..............................................................................................................25

Quiz ..................................................................................................................25

Hour 2 Writing Your First C Program

27

A Simple C Program..............................................................................................28

Comments ..............................................................................................................29

The #include Directive ........................................................................................31

Header Files......................................................................................................32

Angle Brackets (< >) and Double Quotes (“ “) ..............................................32

The main() Function..............................................................................................33

The Newline Character (\n) ............................................................................33

The return Statement ......................................................................................34

The exit() Function ........................................................................................34

Compiling and Linking..........................................................................................34

What’s Wrong with My Program? ........................................................................36

Debugging Your Program ......................................................................................37

00 067231861x FM 4.10.2000 10:53 AM Page vi

vi

Sams Teach Yourself C in 24 Hours

Summary ................................................................................................................37

Q&A ......................................................................................................................38

Workshop ..............................................................................................................38

Quiz ..................................................................................................................38

Exercises ..........................................................................................................39

Hour 3 Learning the Structure of a C Program

41

The Basics of a C Program....................................................................................42

Constants and Variables....................................................................................42

Expressions ......................................................................................................42

Other books

Saint and Scholar by Holley Trent
Britt-Marie Was Here by Fredrik Backman
Spin Control by Niki Burnham
Since You Left Me by Allen Zadoff
Stone Gods by Winterson, Jeanette
The Redemption by Lauren Rowe
Wickedly Magical by Deborah Blake