ebook img

C Programming for the Absolute Beginner PDF

334 Pages·2007·14.34 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview C Programming for the Absolute Beginner

C Programming for the Absolute Beginner, Second Edition MICHAEL VINE © 2008 Thomson Course Technology, a division of Thomson Learning Publisher and General Inc. All rights reserved. No part of this book may be reproduced or trans- Manager, Thomson Course mitted in any form or by any means, electronic or mechanical, including Technology PTR: photocopying, recording, or by any information storage or retrieval sys- Stacy L. Hiquet tem without written permission from Thomson Course Technology PTR, Associate Director of except for the inclusion of brief quotations in a review. Marketing: The Thomson Course Technology PTR logo and related trade dress are Sarah O’Donnell trademarks of Thomson Course Technology, a division of Thomson Manager of Editorial Learning Inc., and may not be used without written permission. Services: All trademarks are the property of their respective owners. Heather Talbot Important: Thomson Course Technology PTR cannot provide software Marketing Manager: support. Please contact the appropriate software manufacturer’s tech- Mark Hughes nical support line or Web site for assistance. Acquisitions Editor: Thomson Course Technology PTR and the author have attempted Mitzi Koontz throughout this book to distinguish proprietary trademarks from Project Editor: descriptive terms by following the capitalization style used by the Jenny Davidson manufacturer. Technical Reviewer: Information contained in this book has been obtained by Thomson Greg Perry Course Technology PTR from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, PTR Editorial Services Thomson Course Technology PTR, or others, the Publisher does not Coordinator: guarantee the accuracy, adequacy, or completeness of any information Erin Johnson and is not responsible for any errors or omissions or the results obtained from use of such information. Readers should be particularly aware of Copy Editor: the fact that the Internet is an ever-changing entity. Some facts may have Heather Urschel changed since this book went to press. Interior Layout Tech: Educational facilities, companies, and organizations interested in mul- Value-Chain Intl. tiple copies or licensing of this book should contact the Publisher for Cover Designer: quantity discount information. Training manuals, CD-ROMs, and por- Mike Tanamachi tions of this book are also available individually or can be tailored for specific needs. Indexer: Kevin Broccoli ISBN-10: 1-59863-480-1 ISBN-13: 978-1-59863-480-8 Proofreader: eISBN-10: 1-59863-634-0 Sandi Wilson Library of Congress Catalog Card Number: 2007935959 Printed in the United States of America 08 09 10 11 12 TW 10 9 8 7 6 5 4 3 2 1 Thomson Course Technology PTR, a division of Thomson Learning Inc. 25 Thomson Place Boston, MA 02210 http://www.courseptr.com To my son, Spencer—Go Bruins! A CKNOWLEDGMENTS W riting a book is not easy, especially a technical programming book. It takes many great, patient, and talented people to write, edit, design, market, finance, and produce a book. Without the assistance of Mitzi Koontz, Jenny Davidson, and Heather Urschel, it would be impossible for me to share with you my knowledge of programming in such a professional and fun manner. I would also like to give special thanks to our technical editor, Greg Perry, who is a true C expert. For two editions now, Greg has kept me honest and accurate in the complicated and often misunderstood world of C programming. Thanks, Greg! A A BOUT THE UTHOR M ichael Vine has taught computer programming, web design, and database classes at Indiana University/Purdue University in Indianapolis, IN, and at MTI College of Business and Technology in Sacramento, CA. Michael has over 13 years’ experience in the information technology profession. He currently works full-time in a Fortune 100 company as an IT Project Manager overseeing the development of enterprise data warehouses. This page intentionally left blank Table of Contents Chapter 1 Getting Started with C Programming..................... 1 Installing and Configuring the Cygwin Environment................................................. 2 main() Function...................................................................................................................... 4 Comments................................................................................................................................ 7 Keywords................................................................................................................................... 8 Program Statements.............................................................................................................. 9 Escape Sequence \n.................................................................................................... 11 Escape Sequence \t..................................................................................................... 12 Escape Sequence \r..................................................................................................... 12 Escape Sequence \\..................................................................................................... 13 Escape Sequence \”..................................................................................................... 14 Escape Sequence \’...................................................................................................... 14 Directives................................................................................................................................ 15 gcc Compiler......................................................................................................................... 15 How to Debug C Programs................................................................................................. 17 Common Error #1: Missing Program Block Identifiers..................................... 20 Common Error #2: Missing Statement Terminators......................................... 21 Common Error #3: Invalid Preprocessor Directives........................................... 21 Common Error #4: Invalid Escape Sequences..................................................... 22 Common Error #5: Invalid Comment Blocks....................................................... 23 Summary................................................................................................................................ 24 Challenges.............................................................................................................................. 25 Chapter 2 Primary Data Types................................................. 27 Memory Concepts................................................................................................................ 28 Data Types.............................................................................................................................. 29 Integers.......................................................................................................................... 29 Floating-Point Numbers............................................................................................. 29 Characters..................................................................................................................... 30 Initializing Variables and the Assignment Operator................................................. 31 Printing Variable Contents................................................................................................ 32 Conversion Specifiers.......................................................................................................... 33 Displaying Integer Data Types with printf()......................................................... 34 viii C Programming for the Absolute Beginner, Second Edition Displaying Floating-Point Data Types with printf()............................................ 34 Displaying Character Data Types with printf().................................................... 35 Constants............................................................................................................................... 36 Programming Conventions and Styles........................................................................... 37 White Space.................................................................................................................. 37 Variable Naming Conventions................................................................................. 38 Identifying Data Types with a Prefix...................................................................... 39 Using Uppercase and Lowercase Letters Appropriately.................................... 40 Give Variables Meaningful Names.......................................................................... 41 scanf()...................................................................................................................................... 41 Arithmetic in C..................................................................................................................... 43 Operator Precedence........................................................................................................... 45 Chapter Program–Profit Wiz............................................................................................ 46 Summary................................................................................................................................ 47 Challenges.............................................................................................................................. 48 Chapter 3 Conditions............................................................... 49 Algorithms for Conditions................................................................................................ 50 Expressions and Conditional Operators............................................................... 50 Pseudo Code.................................................................................................................. 50 Flowcharts..................................................................................................................... 53 Simple if Structures............................................................................................................ 56 Nested if Structures............................................................................................................. 59 Introduction to Boolean Algebra..................................................................................... 62 and Operator................................................................................................................ 62 or Operator.................................................................................................................... 63 not Operator................................................................................................................. 63 Order of Operations.................................................................................................... 64 Building Compound Conditions with Boolean Operators............................... 65 Compound if Structures and Input Validation........................................................... 66 && Operator.................................................................................................................. 66 || Operator................................................................................................................... 66 Checking for Upper- and Lowercase....................................................................... 67 Checking for a Range of Values............................................................................... 68 isdigit() Function......................................................................................................... 69 The switch Structure........................................................................................................... 71 Random Numbers................................................................................................................ 74 Chapter Program–Fortune Cookie.................................................................................. 76 Summary................................................................................................................................ 78 Challenges.............................................................................................................................. 79 Contents ix Chapter 4 Looping Structures................................................. 81 Pseudo Code for Looping Structures.............................................................................. 82 Flowcharts for Looping Structures.................................................................................. 84 Operators Continued.......................................................................................................... 88 ++ Operator................................................................................................................... 88 -- Operator...................................................................................................................... 91 += Operator................................................................................................................... 92 -= Operator..................................................................................................................... 94 The while Loop...................................................................................................................... 95 The do while Loop................................................................................................................ 98 The for Loop........................................................................................................................... 99 break and continue Statements.................................................................................... 102 System Calls........................................................................................................................ 104 Chapter Program–Concentration.................................................................................. 105 Summary.............................................................................................................................. 107 Challenges........................................................................................................................... 108 Chapter 5 Structured Programming.................................... 109 Introduction to Structured Programming................................................................. 109 Top-Down Design....................................................................................................... 110 Code Reusability........................................................................................................ 112 Information Hiding.................................................................................................. 113 Function Prototypes.......................................................................................................... 114 Function Definitions......................................................................................................... 116 Function Calls..................................................................................................................... 119 Variable Scope.................................................................................................................... 122 Local Scope.................................................................................................................. 122 Global Scope............................................................................................................... 124 Chapter Program–Trivia.................................................................................................. 125 Summary.............................................................................................................................. 129 Challenges........................................................................................................................... 130 Chapter 6 Arrays....................................................................... 131 Introduction to Arrays..................................................................................................... 131 One-Dimensional Arrays.................................................................................................. 132 Creating One-Dimensional Arrays........................................................................ 133 Initializing One-Dimensional Arrays................................................................... 133 Searching One-Dimensional Arrays...................................................................... 138 Two-Dimensional Arrays.................................................................................................. 140 Initializing Two-Dimensional Arrays................................................................... 141 Searching Two-Dimensional Arrays..................................................................... 143

Description:
If you are new to programming with C and are looking for a solid introduction, this is the book for you. Developed by computer science instructors, books in the for the absolute beginner? series teach the principles of programming through simple game creation. C is not an easy language to learn, but
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.