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.
2 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. Sams Publishing cannot attest to the accuracy of this information.
3 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.
4 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.
5 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.
6 Getting Started with C 5A Brief History of the C Language ..6 Why Use C? ..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).
7 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 ..30 Summary ..32Q&A ..33 Workshop ..33 Quiz ..33 Exercises ..34 LESSON 3: Storing Information: Variables and Constants 37 Understanding Your Computer s Memory.
8 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.
9 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 ..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.
10 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.