Transcription of Sams Teach Yourself C Programming in One Hour …
1 In One Hour a DayC ProgrammingSamsTeach YourselfBradley L. JonesPeter AitkenDean Miller800 East 96th Street, Indianapolis, Indiana 46240 Seventh EditionSams Teach Yourself C Programming in One Hour a Day, Seventh EditionCopyright 2014 by Pearson EducationAll rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or other-wise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained : 0-7897-5199-2 ISBN-13: 978-0-7897-5199-7 Library of Congress Control Number: 2013949045 Printed in the United States of AmericaFirst Printing: October 2013 TrademarksAll terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized.
2 Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service and DisclaimerEvery 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 an as is basis. The authors and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this SalesSams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please Corporate and Government sales outside of the , please contactInternational EditorMark TaberManaging EditorSandra SchroederProject EditorMandie FrankCopy EditorApostrophe Editing ServicesIndexerKen JohnsonProofreaderAnne GoebelTechnical EditorSiddhartha SinghTeam CoordinatorVanessa EvansDesignerMark ShirarLayoutMary SudulContents at a Glance Introduction 1 PART I: Fundamentals of C 1 Getting Started with C 5 2 The Components of a C Program 23 3 Storing Information: Variables and Constants 37 4 The Pieces of a C Program: Statements, Expressions, and Operators 57 5 Packaging Code in Functions 91 6 Basic Program Control 119 7 Fundamentals of Reading and Writing Information 145 PART II.
3 Putting C to Work 8 Using Numeric Arrays 167 9 Understanding Pointers 187 10 Working with Characters and Strings 213 11 Implementing Structures, Unions, and TypeDefs 241 12 Understanding Variable Scope 279 13 Advanced Program Control 299 14 Working with the Screen, Printer, and Keyboard 325 PART III: Advanced C 15 Pointers to Pointers and Arrays of Pointers 361 16 Pointers to Functions and Linked Lists 385 17 Using Disk Files 417 18 Manipulating Strings 455 19 Getting More from Functions 487 20 Exploring the C Function Library 505 21 Working with Memory 533 22 Advanced Compiler Use 559 PART IV: Appendixes A ASCII Chart 583 B C/C++ Reserved Words 589 C Common C Functions 593 D Answers 599 Index 645 Table of ContentsIntroduction 1 PART I: Fundamentals of C LESSON 1: Getting Started with C 5A Brief History of the C Language ..6 Why Use C?
4 6 Preparing to Program ..8 The Program Development Cycle ..8 Creating the Source Code ..9 Compiling the Source Code ..9 Linking to Create an Executable File ..10 Completing the Development Cycle ..11 Your First C Program ..13 Entering and Compiling ..14 Summary ..18Q&A ..18 Workshop ..19 Quiz ..19 Exercises ..20 LESSON 2: The Components of a C Program 23A Short C Program ..24 The Program s Components ..25 The main() Function (Lines 9 Through 23) ..25 The #include and #define Directives (Lines 2 and 3) ..25 The Variable Definition (Line 5) ..26 The Function Prototype (Line 7) ..26 Program Statements (Lines 12, 13, 14, 17, 19, 20, 22, and 28) ..26 The Function Definition (Lines 26 Through 29) ..27 Program Comments (Lines 1, 11, 16, and 25) ..27 Using Braces (Lines 10, 23, 27, and 29) ..29 Running the Program ..29A Note on Accuracy ..29 ContentsvA Review of the Parts of a Program.
5 30 Summary ..32Q&A ..33 Workshop ..33 Quiz ..33 Exercises ..34 LESSON 3: Storing Information: Variables and Constants 37 Understanding Your Computer s Memory ..38 Storing Information with Variables ..39 Variable Names ..39 Numeric Variable typedef Keyword ..45 Initializing Variables ..45 Constants ..47 Literal Constants ..47 Symbolic Constants ..48 Summary ..53Q&A ..53 Workshop ..54 Quiz ..54 Exercises ..55 LESSON 4: The Pieces of a C Program: Statements, Expressions, and Operators 57 Statements ..58 The Impact of White Space on Statements ..58 Creating a Null Statement ..59 Working with Compound Statements ..59 Understanding Expressions ..60 Simple Expressions ..60 Complex Assignment Operator ..62 The Mathematical Operators ..62 Operator Precedence and Parentheses ..67viSams Teach Yourself C Programming in One Hour a DayOrder of Subexpression Evaluation ..69 The Relational Operators.
6 70 The if Statement ..71 The else Clause ..74 Evaluating Relational Expressions ..77 The Precedence of Relational Operators ..79 The Logical Operators ..80 More on True/False Values ..82 The Precedence of Operators ..82 Compound Assignment Operators ..84 The Conditional Operator ..85 The Comma Operator ..85 Operator Precedence Revisited ..86 Summary ..87Q&A ..88 Workshop ..88 Quiz ..89 Exercises ..89 LESSON 5: Packaging Code in Functions 91 Understanding Functions ..92A Function Defined ..92A Function Illustrated ..92 How a Function Works ..95 Functions and Structured Programming ..97 The Advantages of Structured Programming ..97 Planning a Structured Program ..97 The Top-Down Approach ..99 Writing a Function ..100 The Function Header ..100 The Function Body ..103 The Function Prototype ..109 Passing Arguments to a Function ..110 Calling Functions ..110 Recursion ..112 Where the Functions Belong.
7 114 Working with Inline Functions ..115 Summary ..115Q&A ..116 Workshop ..116 Quiz ..116 Exercises ..117 LESSON 6: Basic Program Control 119 Arrays: The Basics ..120 Controlling Program Execution ..121 The for Statement ..121 Nesting for Statements ..127 The while Statement ..130 Nesting while Statements ..134 The Loop ..136 Nested Loops ..141 Summary ..142Q&A ..142 Workshop ..143 Quiz ..143 Exercises ..143 LESSON 7: Fundamentals of Reading and Writing Information 145 Displaying Information Onscreen ..146 The printf() Function ..146 The printf() Format Strings ..146 Displaying Messages with puts() ..155 Inputting Numeric Data with scanf() ..156 Using Trigraph Sequences ..161 Summary ..162Q&A ..163 Workshop ..163 Quiz ..163 Exercises ..164 ContentsviiviiiSams Teach Yourself C Programming in One Hour a DayPART II: Putting C to Work LESSON 8: Using Numeric Arrays 167 What Is an Array?
8 168 Using Single-Dimensional Arrays ..169 Using Multidimensional Arrays ..173 Naming and Declaring Arrays ..174 Initializing Arrays ..178 Initializing Multidimensional ..182Q&A ..183 Workshop ..184 Quiz ..184 Exercises ..184 LESSON 9: Understanding Pointers 187 What Is a Pointer? ..188 Your Computer s Memory ..188 Creating a Pointer ..188 Pointers and Simple Variables ..189 Declaring Pointers ..189 Initializing Pointers ..190 Using Pointers ..190 Pointers and Variable and Arrays ..194 The Array Name as a Pointer ..194 Array Element Storage ..195 Pointer Arithmetic ..198 Pointer Cautions ..202 Array Subscript Notation and Pointers ..203 Passing Arrays to Functions ..204 Summary ..209Q&A ..210 Workshop ..210 Quiz ..210 Exercises ..211 ContentsixLESSON 10: Working with Characters and Strings 213 The char Data Type ..214 Using Character Variables ..215 Using Strings ..218 Arrays of Characters.
9 218 Initializing Character Arrays ..219 Strings and Pointers ..219 Strings Without Arrays ..220 Allocating String Space at Compilation ..220 The malloc() Function ..221 Using the malloc() Function ..222 Displaying Strings and Characters ..226 The puts() Function ..226 The printf() Function ..227 Reading Strings from the Keyboard ..228 Inputting Strings Using the gets() Function ..228 Inputting Strings Using the scanf() Function ..232 Summary ..235Q&A ..235 Workshop ..237 Quiz ..237 Exercises ..238 LESSON 11: Implementing Structures, Unions, and TypeDefs 241 Working with Simple Structures ..242 Defining and Declaring Structures ..242 Accessing Members of a Structure ..243 Using Structures That Are More Complex ..246 Including Structures Within Other Structures ..246 Structures That Contain Arrays ..250 Arrays of Structures ..252 Initializing Structures ..256 Structures and Pointers.
10 259 Including Pointers as Structure Members ..259 Creating Pointers to Structures ..261 Working with Pointers and Arrays of Structures ..264 Passing Structures as Arguments to Functions ..267xSams Teach Yourself C Programming in One Hour a DayUnderstanding Unions ..268 Defining, Declaring, and Initializing Unions ..269 Accessing Union Members ..269 Creating Synonyms for Structures with ..275Q&A ..275 Workshop ..276 Quiz ..276 Exercises ..277 LESSON 12: Understanding Variable Scope 279 What Is Scope? ..280A Demonstration of Scope ..280 The Importance of Scope ..282 Creating External Variables ..282 External Variable Scope ..283 When to Use External Variables ..283 The extern Keyword ..283 Creating Local Variables ..285 Static Versus Automatic Variables ..285 The Scope of Function Parameters ..288 External Static Variables ..289 Register Variables ..289 Local Variables and the main() Function.