C for Programmers
Total Page:16
File Type:pdf, Size:1020Kb
C FOR PROGRAMMERS WITH AN INTRODUCTION TO C11 DEITEL® DEVELOPER SERIES Deitel® Series Page Deitel® Developer Series CourseSmart Web Books CforProgrammers www.deitel.com/books/CourseSmart/ C++ for Programmers, 2/E C++ How to Program, 7/E, 8/E & 9/E Android™ for Programmers: An App-Driven Simply C++: An App-Driven Tutorial Approach Approach Java™ How to Program, 7/E, 8/E & 9/E C# 2010 for Programmers, 3/E Simply Visual Basic® 2010: An App-Driven Dive Into® iOS 6: An App-Driven Approach Approach, 4/E Java™ for Programmers, 2/E Visual Basic® 2012 How to Program JavaScript for Programmers Visual Basic® 2010 How to Program Visual C#® 2012 How to Program, 5/E LiveLessons Video Learning Products Visual C#® 2010 How to Program, 4/E www.deitel.com/books/LiveLessons/ Android® App Development Fundamentals How To Program Series C++ Fundamentals Android™ How to Program C# Fundamentals C How to Program, 7/E iOS® 6 App Development Fundamentals C++ How to Program, 9/E Java™ Fundamentals C++ How to Program, Late Objects Version, 7/E JavaScript Fundamentals Java™ How to Program, 9/E Visual Basic® Fundamentals Java™ How to Program, Late Objects Version, 8/E Internet & World Wide Web How to Program, 5/E Simply Series Visual Basic® 2012 How to Program Simply C++: An App-Driven Tutorial Approach Visual C#® 2012 How to Program, 5/E Simply Java™ Programming: An App-Driven Visual C++® 2008 How to Program, 2/E Tutorial Approach Small Java™ How to Program, 6/E Simply Visual Basic® 2010, 4/E: An App-Driven Small C++ How to Program, 5/E Tutorial Approach 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 and join the Deitel communities on Twitter® @deitel Facebook® facebook.com/DeitelFan Google+ gplus.to/deitel and LinkedIn bit.ly/DeitelLinkedIn 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 Pearson/Deitel publications visit: www.deitel.com www.pearsonhighered.com/deitel/ Visit the Deitel Resource Centers that will help you master programming languages, software devel- opment, Android™ and iPhone®/iPad® app development, and Internet- and web-related topics: www.deitel.com/ResourceCenters.html C FOR PROGRAMMERS WITH AN INTRODUCTION TO C11 DEITEL® DEVELOPER SERIES Paul Deitel Deitel & Associates, Inc. Harvey Deitel Deitel & Associates, Inc. Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U. S. Corporate and Government Sales (800) 382-3419 [email protected] For sales outside the U. S., please contact: International Sales [email protected] Visit us on the Web: informit.com/ph Library of Congress Cataloging-in-Publication Data On file © 2013 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmis- sion in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290. ISBN-13: 978-0-13-346206-7 ISBN-10: 0-13-346206-4 Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana. First printing, April 2013 In memory of Dennis Ritchie, creator of the C programming language and co-creator of the UNIX operating system. Paul and Harvey Deitel Trademarks DEITEL, the double-thumbs-up bug and DIVE INTO are registered trademarks of Deitel and Associates, Inc. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUBLISHED AS PART OF THE SERVICES FOR ANY PURPOSE. ALL SUCH DOCUMENTS AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMA- TION, INCLUDING ALL WARRANTIES AND CONDITIONS OF MERCHANTABILITY, WHETHER EXPRESS, IMPLIED OR STATUTORY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PER- FORMANCE OF INFORMATION AVAILABLE FROM THE SERVICES. THE DOCUMENTS AND RELATED GRAPHICS CONTAINED HEREIN COULD INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS. CHANGES ARE PERIODI- CALLY ADDED TO THE INFORMATION HEREIN. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS MAY MAKE IMPROVEMENTS AND/OR CHANGES IN THE PRODUCT(S) AND/ OR THE PROGRAM(S) DESCRIBED HEREIN AT ANY TIME. PARTIAL SCREEN SHOTS MAY BE VIEWED IN FULL WITHIN THE SOFTWARE VERSION SPECIFIED. Throughout this book, trademarks are used. Rather than put a trademark symbol in every occurrence of a trademarked name, we state that we are using the names in an editorial fashion only and to the benefit of the trademark owner, with no intention of infringement of the trademark. Contents Preface xv 1 Introduction 1 1.1 Introduction 2 1.2 The C Programming Language 2 1.3 C Standard Library 4 1.4 C++ and Other C-Based Languages 4 1.5 Typical C Program Development Environment 5 1.5.1 Phase 1: Creating a Program 6 1.5.2 Phases 2 and 3: Preprocessing and Compiling a C Program 7 1.5.3 Phase 4: Linking 7 1.5.4 Phase 5: Loading 7 1.5.5 Phase 6: Execution 7 1.5.6 Standard Input, Standard Output and Standard Error Streams 8 1.6 Test-Driving a C Application in Windows, Linux and Mac OS X 8 1.6.1 Running a C Application from the Windows Command Prompt 9 1.6.2 Running a C Application Using GNU C with Linux 11 1.6.3 Running a C Application Using GNU C with Mac OS X 14 1.7 Operating Systems 16 1.7.1 Windows—A Proprietary Operating System 17 1.7.2 Linux—An Open-Source Operating System 17 1.7.3 Apple’s Mac OS X; Apple’s iOS® for iPhone®, iPad® and iPod Touch® Devices 17 1.7.4 Google’s Android 18 2 Introduction to C Programming 19 2.1 Introduction 20 2.2 A Simple C Program: Printing a Line of Text 20 2.3 Another Simple C Program: Adding Two Integers 24 2.4 Arithmetic in C 27 2.5 Decision Making: Equality and Relational Operators 31 2.6 Secure C Programming 35 viii Contents 3 Control Statements: Part I 37 3.1 Introduction 38 3.2 Control Structures 38 3.3 The if Selection Statement 40 3.4 The if…else Selection Statement 40 3.5 The while Repetition Statement 43 3.6 Class Average with Counter-Controlled Repetition 44 3.7 Class Average with Sentinel-Controlled Repetition 46 3.8 Nested Control Statements 49 3.9 Assignment Operators 51 3.10 Increment and Decrement Operators 52 3.11 Secure C Programming 55 4 Control Statements: Part II 57 4.1 Introduction 58 4.2 Repetition Essentials 58 4.3 Counter-Controlled Repetition 59 4.4 for Repetition Statement 60 4.5 for Statement: Notes and Observations 63 4.6 Examples Using the for Statement 64 4.7 switch Multiple-Selection Statement 67 4.8 do…while Repetition Statement 73 4.9 break and continue Statements 75 4.10 Logical Operators 77 4.11 Confusing Equality (==) and Assignment (=) Operators 80 4.12 Secure C Programming 81 5 Functions 83 5.1 Introduction 84 5.2 Program Modules in C 84 5.3 Math Library Functions 85 5.4 Functions 86 5.5 Function Definitions 87 5.6 Function Prototypes: A Deeper Look 91 5.7 Function Call Stack and Stack Frames 94 5.8 Headers 97 5.9 Passing Arguments By Value and By Reference 98 5.10 Random Number Generation 99 5.11 Example: A Game of Chance 104 5.12 Storage Classes 107 5.13 Scope Rules 109 5.14 Recursion 112 5.15 Example Using Recursion: Fibonacci Series 116 Contents ix 5.16 Recursion vs. Iteration 119 5.17 Secure C Programming 121 6 Arrays 122 6.1 Introduction 123 6.2 Arrays 123 6.3 Defining Arrays 124 6.4 Array Examples 125 6.5 Passing Arrays to Functions 138 6.6 Sorting Arrays 142 6.7 Case Study: Computing Mean, Median and Mode Using Arrays 144 6.8 Searching