CYAN YELLOW MAGENTA BLACK PANTONE 123 CV BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN C Companion eBook Available Beginning C: From Novice to Professional, FOURTH EDITION B C Dear Reader, e If you aspire to be a programmer, C is an excellent choice for your first language. g It’s more compact than most programming languages, which means there’s less to remember; but in spite of that, C is extremely powerful, and many operating i n systems are written in C. It’s also supported in a wide variety of computer envi- Author of ronments, which means that there are relatively few computers that do not n have a C compiler available. Ivor Horton’s Beginning i My objective with this book is to take you from beginner to a good level of ANSI C++:The Complete n Language competence in C programming. My book will teach you the complete funda- Beginning mentals of the C language and how to program. All you need in addition to this g Ivor Horton’s Beginning book is one of the widely available free or commercial standard C compilers Java 2,JDK 5 Edition and you’ll soon be writing real C programs. C Ivor Horton’s Beginning I’ll teach you C from first principles without assuming any prior knowledge of Visual C++ 2005 programming, and I’ll explain all the elements of the C language, using step-by- Beginning Visual C++ 6 stepexamples. I’ll help you increase your programming skills by guiding you through the development of fully working C applications that apply what you’ve learned in a practical context. You’ll also gain confidence in programming through working examples that you’ll create and execute yourself. Learning C is a challenge, but I’m sure you’ll have great fun doing it, and you’ll find it very rewarding. As long as you have the enthusiasm and commitment to complete the book, you’ll get the personal satisfaction of having achieved something really worthwhile. You will also be taking a major step into the world of real C programming. From Novice to Professional Ivor Horton Join online discussions: THE APRESS ROADMAP forums.apress.com Cryptography in C Takes you step-by-step from novice to C programmer FOR PROFESSIONALS and C++, Second Edition BY PROFESSIONALS™ Beginning C: From Managed C++ and Companion eBook Novice to Professional, Exploring C++ .NET Development Fourth Edition FOURTH Ivor Horton’s Beginning FOURTH EDITION ANSI C++: The Complete C++/CLI Primer Expert Visual C++/CLI EDITION Language, Third Edition See last page for details Essential Guide to Managed on $10 eBook version Extensions for C++ Ivor Horton H o ISBN 1-59059-735-4 SOURCECODEONLINE r www.apress.com 90000 to n Shelve in Programming Languages/C User level: Beginner–Intermediate 6 89253 59735 4 9 781590 597354 this print for content only—size & color not accurate 7" x 9-1/4" / CASEBOUND / MALLOY (1.25 INCH BULK -- 640 pages -- 50# Thor) Horton_735-4FRONT.fm Page i Saturday, September 23, 2006 5:08 AM Beginning C From Novice to Professional, Fourth Edition ■ ■ ■ Ivor Horton Horton_735-4FRONT.fm Page ii Saturday, September 23, 2006 5:08 AM Beginning C: From Novice to Professional, Fourth Edition Copyright © 2006 by Ivor Horton All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-59059-735-4 ISBN-10 (pbk): 1-59059-735-4 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Matthew Moodie Technical Reviewer: Stan Lippman Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Keir Thomas, Matt Wade Project Manager: Tracy Brown Collins Copy Edit Manager: Nicole LeClerc Copy Editor: Jennifer Whipple Assistant Production Director: Kari Brooks-Copony Production Editor: Kelly Winquist Compositor: Susan Glinert Proofreader: Lori Bring Indexer: John Collin Artist: Kinetic Publishing Services, LLC Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Source Code/Download section. Horton_735-4FRONT.fm Page iii Saturday, September 23, 2006 5:08 AM This book is for the latest member of the family, Henry James Gilbey, who joined us on July 14, 2006. He hasn’t shown much interest in programming so far, but he did smile when I asked him about it so I expect he will. Horton_735-4FRONT.fm Page iv Saturday, September 23, 2006 5:08 AM Horton_735-4FRONT.fm Page v Saturday, September 23, 2006 5:08 AM Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii ■ CHAPTER 1 Programming in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■ CHAPTER 2 First Steps in Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 ■ CHAPTER 3 Making Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 ■ CHAPTER 4 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 ■ CHAPTER 5 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 ■ CHAPTER 6 Applications with Strings and Text . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 ■ CHAPTER 7 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 ■ CHAPTER 8 Structuring Your Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 ■ CHAPTER 9 More on Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 ■ CHAPTER 10 Essential Input and Output Operations . . . . . . . . . . . . . . . . . . . . . . . 373 ■ CHAPTER 11 Structuring Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 ■ CHAPTER 12 Working with Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467 ■ CHAPTER 13 Supporting Facilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529 ■ APPENDIX A Computer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557 ■ APPENDIX B ASCII Character Code Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565 ■ APPENDIX C Reserved Words in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571 ■ APPENDIX D Input and Output Format Specifications . . . . . . . . . . . . . . . . . . . . . . 573 ■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579 v Horton_735-4FRONT.fm Page vi Saturday, September 23, 2006 5:08 AM Horton_735-4FRONT.fm Page vii Saturday, September 23, 2006 5:08 AM Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii ■ CHAPTER 1 Programming in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Creating C Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Compiling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Linking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Executing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Creating Your First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Editing Your First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Dealing with Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Dissecting a Simple Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Preprocessing Directives. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Defining the main() Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Keywords. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 The Body of a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Outputting Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Arguments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Control Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Developing Programs in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Understanding the Problem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Detailed Design. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Implementation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Functions and Modular Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Common Mistakes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Points to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 vii Horton_735-4FRONT.fm Page viii Saturday, September 23, 2006 5:08 AM viii ■ CONTENTS ■ CHAPTER 2 First Steps in Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Memory in Your Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 What Is a Variable? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Variables That Store Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Integer Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Naming Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Using Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Initializing Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Arithmetic Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Variables and Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Integer Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Unsigned Integer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Using Integer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Specifying Integer Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Floating-Point Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Floating-Point Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Division Using Floating-Point Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Controlling the Number of Decimal Places . . . . . . . . . . . . . . . . . . . . 44 Controlling the Output Field Width . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 More Complicated Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Defining Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Knowing Your Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Introducing the sizeof Operator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Choosing the Correct Type for the Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Explicit Type Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Automatic Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Rules for Implicit Conversions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Implicit Conversions in Assignment Statements. . . . . . . . . . . . . . . . 58 More Numeric Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 The Character Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Character Input and Character Output. . . . . . . . . . . . . . . . . . . . . . . . 60 The Wide Character Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Variables to Store Boolean Values . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 The Complex Number Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Horton_735-4FRONT.fm Page ix Saturday, September 23, 2006 5:08 AM ■ CONTENTS ix The op= Form of Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Designing a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 The Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 ■ CHAPTER 3 Making Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 The Decision-Making Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Arithmetic Comparisons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Expressions Involving Relational Operators. . . . . . . . . . . . . . . . . . . . 82 The Basic if Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Extending the if Statement: if-else. . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Using Blocks of Code in if Statements. . . . . . . . . . . . . . . . . . . . . . . . 88 Nested if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 More Relational Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 The Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Operator Precedence: Who Goes First? . . . . . . . . . . . . . . . . . . . . . . 102 Multiple-Choice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Using else-if Statements for Multiple Choices . . . . . . . . . . . . . . . . 106 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 The goto Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 The op= Use of Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Using Bitwise Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Designing a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 The Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Description: