D e i t e l ® S e r i e s P a g e How To Program Series C How to Program, 6/E C++ How to Program, 7/E Java How to Program, 8/E, Early Objects Version Java How to Program, 8/E, Late Objects Version Internet & World Wide Web How to Program, 4/E Visual Basic® 2008 How to Program Visual C#® 2008 How to Program, 3/E Visual C++® 2008 How to Program, 2/E Small Java™ How to Program, 6/E Small C++ How to Program, 5/E Simply Series Simply C++: An Application-Driven Simply C#: An Application-Driven Tutorial Approach Tutorial Approach Simply Java™ Programming: An Simply Visual Basic® 2008, 3/E: An Application-Driven Tutorial Application-Driven Tutorial Approach Approach CourseSmart Web Books www.deitel.com/books/CourseSmart.html C++ How to Program, 5/E, 6/E & 7/E Small C++ How to Program, 5/E Java How to Program, 6/E, 7/E & 8/E Small Java How to Program, 6/E Simply C++: An Application-Driven Visual Basic® 2008 How to Program Tutorial Approach Visual C#® 2008 How to Program, 3/E Simply Visual Basic 2008: An Application-Driven Tutorial Approach, 3/E Deitel® Developer Series AJAX, Rich Internet Applications and iPhone for Programmers: Web Development for Programmers An App-Driven Approach C++ for Programmers Java for Programmers C# 2008 for Programmers, 3/E Javascript for Programmers LiveLessons Video Learning Products www.deitel.com/books/LiveLessons/ Java Fundamentals Parts 1 and 2 C++ Fundamentals Parts 1 and 2 C# Fundamentals Parts 1 and 2 JavaScript Fundamentals Parts 1 and 2 To receive updates on Deitel publications, Resource Centers, training courses, partner offers and more, please register for the free Deitel® Buzz Online e-mail newsletter at: www.deitel.com/newsletter/subscribe.html follow us on Twitter® @deitel and Facebook® www.deitel.com/deitelfan/ To communicate with the authors, send e-mail to: [email protected] For information on government and corporate Dive-Into® Series on-site seminars offered by Deitel & Associates, Inc. worldwide, visit: www.deitel.com/training/ or write to [email protected] For continuing updates on Prentice Hall/Deitel publications visit: www.deitel.com www.pearsonhighered.com/deitel Check out our Resource Centers for valuable web resources that will help you master Java, other important programming languages, software and Internet- and web-related topics: www.deitel.com/ResourceCenters.html Library of Congress Cataloging-in-Publication Data On file Vice President and Editorial Director, ECS: Marcia J. Horton Editor-in-Chief, Computer Science: Michael Hirsch Associate Editor: Carole Snyder Supervisor/Editorial Assistant: Dolores Mars Director of Team-Based Project Management: Vince O’Brien Senior Managing Editor: Scott Disanno Managing Editor: Robert Engelhardt A/V Production Editor: Greg Dulles Art Director: Kristine Carney Cover Design: Abbey S. Deitel, Harvey M. Deitel, Francesco Santalucia, Kristine Carney Interior Design: Harvey M. Deitel, Kristine Carney Manufacturing Manager: Alexis Heydt-Long Manufacturing Buyer: Lisa McDowell Director of Marketing: Margaret Waples Marketing Manager: Erin Davis © 2010 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 The authors and publisher of this book have used their best efforts in preparing this book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and pub- lisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in this book. The authors and publisher shall not be liable in any event for incidental or consequential dam- ages in connection with, or arising out of, the furnishing, performance, or use of these programs. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks and registered trademarks. Where those designations appear in this book, and Prentice Hall and the authors were aware of a trademark claim, the designations have been printed in initial caps or all caps. All product names mentioned remain trademarks or registered trademarks of their respective owners. All rights reserved. No part of this book may be reproduced, in any form or by any means, without permission in writing from the publisher. Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 ISBN-10: 0-13-612356-2 ISBN-13: 978-0-13-612356-9 Pearson Education Ltd., London Pearson Education Australia Pty. Ltd., Sydney Pearson Education Singapore, Pte. Ltd. Pearson Education North Asia Ltd., Hong Kong Pearson Education Canada, Inc., Toronto Pearson Educación de Mexico, S.A. de C.V. Pearson Education–Japan, Tokyo Pearson Education Malaysia, Pte. Ltd. Pearson Education, Inc., Upper Saddle River, New Jersey P. J. Deitel Deitel & Associates, Inc. H. M. Deitel Deitel & Associates, Inc. Upper Saddle River, New Jersey 07458 Trademarks DEITEL, the double-thumbs-up bug and DIVE INTO are registered trademarks of Deitel and Associates, Inc. Microsoft, Visual C++, Internet Explorer and the Windows logo are either registered trademarks or trade- marks of Microsoft Corporation in the United States and/or other countries. In Memory of Clifford Stephens: Your friendship, bright smile and infectious laugh will be truly missed. Paul and Harvey Deitel Deitel Resource Centers Our Resource Centers focus on the vast amounts of free content available online. Find resources, downloads, tutorials, documentation, books, e-books, journals, articles, blogs, RSS feeds and more on many of today’s hottest programming and technology topics. For the most up-to-date list of our Resource Centers, visit: www.deitel.com/ResourceCenters.html Let us know what other Resource Centers you’d like to see! Also, please register for the free Deitel® Buzz Online e-mail newsletter at: www.deitel.com/newsletter/subscribe.html Computer Science UML Java Design Patterns Open Source Functional Programming Visual Basic Java EE 5 Search Engines Regular Expressions Visual C++ Java SE 6 SharePoint Visual Studio Team Sys- Java SE 7 (Dolphin) Skype Programming tem Resource Center Web Servers ASP.NET 3.5 Web 3D Technologies JavaFX Wikis Adobe Flex Web Services Windows Vista Ajax Windows Presentation Microsoft Apex Foundation ASP.NET Web 2.0 ASP.NET Ajax XHTML ASP.NET 3.5 Alert Services ASP.NET XML ASP.NET Ajax Attention Economy C Apple C# Blogging C++ DotNetNuke (DNN) Building Web C++ Boost Libraries iPhone Internet Explorer 7 (IE7) Communities C++ Game Programming Objective-C Microsoft LINQ Community Generated C# Cocoa .NET Content Code Search Engines and Games and Game .NET 3.0 Facebook Developer Code Sites Programming .NET 3.5 Platform Computer Game SharePoint Facebook Social Ads Programming Computer Game Pro- Silverlight Google Base CSS 2.1 gramming Visual Basic Google Video Dojo Computer Games Visual C++ Google Web Toolkit Facebook Developer Plat- Mobile Gaming Visual Studio Team (GWT) form Sudoku System Internet Video Flash 9 Internet Business Windows Presentation Joost Functional Programming Affiliate Programs Foundation Location-Based Services Java Competitive Analysis Windows Vista Mashups Java Certification and Facebook Social Ads Microsoft Popfly Microformats JJJJJJJMMaaaaaaSvvvvvvOiiARaaaaaacc FSsrreNDESSsooscXeoEEssEresoo ui s sp67ff5mritttgc (neLPeD n oICPNtop aelTftpQnlteyhteseritrnnin)s g GGGIIILLOPnnnoioooonntttIdceeeooonlkacrrrigggi ntnnnatBilllseieeeeoeeatu t ttniLtSAA iniABP-leevddngBuraudeSiavadbnsvleisi ylecgnneGitrecdesits eceis R sSnss i eeenlrrgavatiticiooensns OADEFLMOPPeiHicpropnyrpLleailteSuPfpecNAnoQxhsn xeeMeSL tSoNPuouru ckSreetc a(eDc k&N N) RRSSSSVWWWooooeSieer(ifccccSdSttbbiiiowuaaaga mlll23aaea lNGM..Strm 00seW)re eeaatdonwpsi rdhaaoled rSrsk e Sirnyvgsitc eem s .NET Search Engine Optimiza- Python Dive Into® Web 2.0 ..NNEETT 33..05 Seltliionng Digital Content Ruby eBook OpenGL Sitemaps Software Web 2 eBook Perl Web Analytics Apache Other Topics PPPrHyothgPro anmming Projects WYoeubTsuitbee M anodn eAtidzSaetinosne DEcolitpNseet Nuke (DNN) CCoommppuutteinr gG Jaombses Regular Expressions Java Firefox Gadgets and Gizmos Ruby Java Internet Explorer 7 (IE7) Ring Tones Ruby on Rails Java Certification and Linux Sudoku MySQL Silverlight Assessment Testing Contents Appendices E through I are PDF documents posted online at the book’s Companion Website (located at www.pearsonhighered.com/deitel). Preface xxi 1 Introduction to Computers, the Internet and the Web 1 1.1 Introduction 2 1.2 Computers: Hardware and Software 3 1.3 Computer Organization 4 1.4 Personal, Distributed and Client/Server Computing 5 1.5 The Internet and the World Wide Web 5 1.6 Machine Languages, Assembly Languages and High-Level Languages 6 1.7 History of C 7 1.8 C Standard Library 8 1.9 C++ 9 1.10 Java 9 1.11 Fortran, COBOL, Pascal and Ada 10 1.12 BASIC, Visual Basic, Visual C++, C# and .NET 10 1.13 Key Software Trend: Object Technology 11 1.14 Typical C Program Development Environment 12 1.15 Hardware Trends 14 1.16 Notes About C and This Book 15 1.17 Web Resources 16 2 Introduction to C Programming 23 2.1 Introduction 24 2.2 A Simple C Program: Printing a Line of Text 24 2.3 Another Simple C Program: Adding Two Integers 28 2.4 Memory Concepts 33 2.5 Arithmetic in C 34 2.6 Decision Making: Equality and Relational Operators 38 3 Structured Program Development in C 54 3.1 Introduction 55 3.2 Algorithms 55