ebook img

Sams Teach Yourself C in 24 Hours PDF

548 Pages·2000·2.94 MB·English
by  ZhangTony.
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Sams Teach Yourself C in 24 Hours

00 067231861x FM 12/11/02 10:01 AM Page i Teach Yourself C 24 in 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, ASSOCIATEPUBLISHER Michael Stephens Second Edition ACQUISITIONSEDITOR Copyright ©2000 by Sams Publishing Carol Ackerman All rights reserved. No part of this book shall be reproduced,stored in a DEVELOPMENTEDITOR retrieval system,or transmitted by any means,electronic,mechanical,photo- Gus A. Miklos copying,recording,or otherwise,without written permission from the pub- MANAGINGEDITOR 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 PROJECTEDITOR omissions. Nor is any liability assumed for damages resulting from the use of Andy Beaster the information contained herein. COPYEDITOR 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 TECHNICALEDITOR Trademarks Bill Mitchell All terms mentioned in this book that are known to be trademarks or service TEAMCOORDINATOR 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. INTERIORDESIGNER Gary Adair Warning and Disclaimer COVERDESIGNER 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. EDITORIALASSISTANT 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 #includeDirective ........................................................................................31 Header Files......................................................................................................32 Angle Brackets (< >) and Double Quotes (“ “)..............................................32 The main()Function..............................................................................................33 The Newline Character (\n) ............................................................................33 The returnStatement......................................................................................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 Statements........................................................................................................45 Statement Blocks..............................................................................................45 Anatomy of a C Function......................................................................................46 Determining a Function’s Type........................................................................46 Giving a Function a Valid Name......................................................................47 Passing Arguments to C Functions..................................................................47 The Beginning and End of a Function ............................................................48 The Function Body ..........................................................................................48 Making Function Calls..........................................................................................49 Summary................................................................................................................51 Q&A......................................................................................................................52 Workshop ..............................................................................................................52 Quiz..................................................................................................................52 Exercises ..........................................................................................................53 Hour 4 Understanding Data Types and Keywords 55 C Keywords ..........................................................................................................56 The charData Type ..............................................................................................57 Character Variables ..........................................................................................58 Character Constants..........................................................................................58 The Escape Character (\)..................................................................................59 Printing Characters ..........................................................................................60 The intData Type ................................................................................................62 Declaring Integer Variables..............................................................................62 Showing the Numeric Values of Characters....................................................63 The floatData Type ............................................................................................64 Declaring Floating-Point Variables..................................................................64 The Floating-Point Format Specifier (%f)........................................................65 The doubleData Type ..........................................................................................67 Using Scientific Notation......................................................................................67 Naming a Variable..................................................................................................68 Summary................................................................................................................68 Q&A......................................................................................................................68 00 067231861x FM 4.10.2000 10:53 AM Page vii Contents vii Workshop ..............................................................................................................69 Quiz..................................................................................................................69 Exercises ..........................................................................................................70 Hour 5 Handling Standard Input and Output 71 Understanding Standard I/O ................................................................................72 Getting Input from the User..................................................................................72 Using the getc()Function ..............................................................................72 Using the getchar()Function ........................................................................74 Printing Output on the Screen ..............................................................................75 Using the putc()Function ..............................................................................75 Another Function for Writing:putchar() ......................................................77 Revisiting the printf()Function..........................................................................78 Converting to Hex Numbers............................................................................79 Specifying the Minimum Field Width..............................................................81 Aligning Output................................................................................................83 Using the Precision Specifier ..........................................................................84 Summary................................................................................................................85 Q&A......................................................................................................................86 Workshop ..............................................................................................................86 Quiz..................................................................................................................87 Exercises ..........................................................................................................87 Part II Operators and Control-flow Statements 89 Hour 6 Manipulating Data 91 Arithmetic Assignment Operators ........................................................................92 The Assignment Operator (=) ..........................................................................92 Combining Arithmetic Operators with =..........................................................92 Getting Negations of Numeric Values ..................................................................95 Incrementing or Decrementing by One ................................................................96 Greater Than or Less Than?..................................................................................98 Using the Cast Operator......................................................................................101 Summary..............................................................................................................102 Q&A....................................................................................................................102 Workshop ............................................................................................................103 Quiz................................................................................................................103 Exercises ........................................................................................................103 Hour 7 Working with Loops 105 The whileLoop ..................................................................................................106 The do-whileLoop..............................................................................................107 Looping Under the forStatement ......................................................................109 00 067231861x FM 4.10.2000 10:53 AM Page viii viii Sams Teach Yourself C in 24 Hours The Null Statement..............................................................................................112 Using Complex Expressions in a forStatement............................................113 Using Nested Loops............................................................................................116 Summary..............................................................................................................118 Q&A....................................................................................................................118 Workshop ............................................................................................................119 Quiz................................................................................................................119 Exercises ........................................................................................................120 Hour 8 Using Conditional Operators 121 Measuring Data Sizes..........................................................................................122 Everything Is Logical..........................................................................................124 The Logical AND Operator (&&)....................................................................124 The Logical OR Operator (||) ......................................................................126 The Logical NEGATION Operator (!)..........................................................128 Manipulating Bits................................................................................................129 Converting Decimal to Hex or Binary ..........................................................129 Using Bitwise Operators................................................................................130 Using Shift Operators ....................................................................................133 What Does x?y:zMean? ....................................................................................135 Summary..............................................................................................................137 Q&A....................................................................................................................137 Workshop ............................................................................................................138 Quiz................................................................................................................138 Exercises ........................................................................................................138 Hour 9 Working with Data Modifiers and Math Functions 141 Enabling or Disabling the Sign Bit......................................................................142 The signedModifier......................................................................................142 The unsignedModifier..................................................................................143 Changing Data Sizes............................................................................................145 The shortModifier........................................................................................145 The longModifier..........................................................................................145 Adding h,l,or Lto printfand fprintfFormat Specifiers..........................147 Mathematical Functions in C..............................................................................148 Calling sin(),cos(),and tan()....................................................................149 Calling pow()and sqrt()..............................................................................150 Summary..............................................................................................................152 Q&A....................................................................................................................153 Workshop ............................................................................................................154 Quiz................................................................................................................154 Exercises ........................................................................................................154 00 067231861x FM 4.10.2000 10:53 AM Page ix Contents ix Hour 10 Controlling Program Flow 155 Always Saying “if…” ..........................................................................................156 The if-elseStatement..................................................................................158 Nested ifStatements......................................................................................160 The switchStatement..........................................................................................161 The breakStatement............................................................................................164 Breaking an Infinite Loop....................................................................................166 The continueStatement......................................................................................167 The gotoStatement..............................................................................................168 Summary..............................................................................................................170 Q&A....................................................................................................................170 Workshop ............................................................................................................171 Quiz................................................................................................................171 Exercises ........................................................................................................172 Part III Pointers and Arrays 173 Hour 11 Understanding Pointers 175 What Is a Pointer?................................................................................................176 Address (Left Value) Versus Content (Right Value)............................................176 The Address-of Operator (&)..........................................................................177 Declaring Pointers................................................................................................179 The Dereference Operator (*)........................................................................182 Null Pointers ..................................................................................................183 Updating Variables via Pointers..........................................................................183 Pointing to the Same Memory Location..............................................................184 Summary..............................................................................................................186 Q&A....................................................................................................................187 Workshop ............................................................................................................188 Quiz................................................................................................................188 Exercises ........................................................................................................188 Hour 12 Understanding Arrays 189 What Is an Array?................................................................................................190 Declaring Arrays............................................................................................190 Indexing Arrays..............................................................................................190 Initializing Arrays ..........................................................................................191 The Size of an Array............................................................................................192 Arrays and Pointers..............................................................................................194 Displaying Arrays of Characters....................................................................196 The Null Character (‘\0’)..............................................................................198 Multidimensional Arrays ....................................................................................199

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.