LEARN C THE HARD WAY - Pearsoncmg

Transcription

LEARN CTHE HARD WAY

Zed Shaw’s Hard Way SeriesVisit informit.com/hardway for a complete list of available publications.Zed Shaw’s Hard Way Series emphasizes instruction and making things asthe best way to get started in many computer science topics. Each book in theseries is designed around short, understandable exercises that take you through acourse of instruction that creates working software. All exercises are thoroughlytested to verify they work with real students, thus increasing your chance ofsuccess. The accompanying video walks you through the code in each exercise.Zed adds a bit of humor and inside jokes to make you laugh while you’re learning.Make sure to connect with us!informit.com/socialconnect

LEARN CTHE HARD WAYPractical Exercises on theComputational Subjects You KeepAvoiding (Like C)Zed A. ShawNew York Boston Indianapolis San FranciscoToronto Montreal London Munich Paris MadridCapetown Sydney Tokyo Singapore Mexico City

Many of the designations used by manufacturers and sellers to distinguish their products are claimed astrademarks. 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 author and publisher have taken care in the preparation of this book, but make no expressed or impliedwarranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidentalor consequential damages in connection with or arising out of the use of the information or programscontained herein.For information about buying this title in bulk quantities, or for special sales opportunities (which may includeelectronic versions; custom cover designs; and content particular to your business, training goals, marketingfocus, or branding interests), please contact our corporate sales department at corpsales@pearsoned.com or(800) 382-3419.For government sales inquiries, please contact governmentsales@pearsoned.com.For questions about sales outside the U.S., please contact international@pearsoned.com.Visit us on the Web: informit.com/awLibrary of Congress Cataloging-in-Publication DataShaw, Zed, author.Learn C the hard way : practical exercises on the computational subjects you keep avoiding (like C) /Zed A. Shaw.pages cmIncludes index.ISBN 978-0-321-88492-3 (pbk. : alk. paper)—ISBN 0-321-88492-2 (pbk. : alk. paper)1. C (Computer program language)—Problems, exercises, etc. I. Title.QA76.73.C15S473 2016005.13’3—dc232015020858Copyright 2016 Zed A. ShawAll rights reserved. Printed in the United States of America. This publication is protected by copyright, andpermission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrievalsystem, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, orlikewise. To obtain permission to use material from this work, please submit a written request to PearsonEducation, Inc., Permissions Department, 200 Old Tappan Road, Old Tappan, New Jersey 07657, or you may faxyour request to (201) 236-3290.ISBN-13: 978-0-321-88492-3ISBN-10: 0-321-88492-2Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana.First printing, August 2015

vContentsAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xivThis Book Is Not Really about C . . . . . . . . . . . . . . . . . . . . . . . xvThe Undefined Behaviorists . . . . . . . . . . . . . . . . . . . . . . . xviC Is a Pretty and Ugly Language . . . . . . . . . . . . . . . . . . . . . xviiWhat You Will Learn . . . . . . . . . . . . . . . . . . . . . . . . . . xviiiHow to Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . xviiiThe Videos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixThe Core Competencies . . . . . . . . . . . . . . . . . . . . . . . . . . xixReading and Writing . . . . . . . . . . . . . . . . . . . . . . . . . xixAttention to Detail. . . . . . . . . . . . . . . . . . . . . . . . . . xxSpotting Differences . . . . . . . . . . . . . . . . . . . . . . . . . . xxPlanning and Debugging . . . . . . . . . . . . . . . . . . . . . . . xxExercise 0 The Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3Text Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3Do Not Use an IDE . . . . . . . . . . . . . . . . . . . . . . . . . . .4Exercise 1 Dust Off That Compiler . . . . . . . . . . . . . . . . . . . . .6Breaking It Down . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . .7How to Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8Exercise 2 Using Makefiles to Build . . . . . . . . . . . . . . . . . . . 10Using Make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 11How to Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Exercise 3 Formatted Printing . . . . . . . . . . . . . . . . . . . . . . . 14What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 15External Research . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

viCONTENTSHow to Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16Exercise 4 Using a Debugger . . . . . . . . . . . . . . . . . . . . . . . . 18GDB Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18GDB Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . 18LLDB Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Exercise 5 Memorizing C Operators . . . . . . . . . . . . . . . . . . . . 20How to Memorize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20The List of Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Exercise 6 Memorizing C Syntax . . . . . . . . . . . . . . . . . . . . . . 26The Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26Syntax Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27A Word of Encouragement . . . . . . . . . . . . . . . . . . . . . . . . 30A Word of Warning . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Exercise 7 Variables and Types . . . . . . . . . . . . . . . . . . . . . . . 32What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 34How to Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Exercise 8 If, Else-If, Else . . . . . . . . . . . . . . . . . . . . . . . . 36What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 37How to Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38Exercise 9 While-Loop and Boolean Expressions . . . . . . . . . . . . 40What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 40How to Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41Exercise 10 Switch Statements . . . . . . . . . . . . . . . . . . . . . . . 42What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 43How to Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Exercise 11 Arrays and Strings . . . . . . . . . . . . . . . . . . . . . . . 46What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 47How to Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48Exercise 12 Sizes and Arrays . . . . . . . . . . . . . . . . . . . . . . . . 50What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

CONTENTSHow to Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53Exercise 13 For-Loops and Arrays of Strings . . . . . . . . . . . . . . 54What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 56Understanding Arrays of Strings . . . . . . . . . . . . . . . . . . . . . 56How to Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57Exercise 14 Writing and Using Functions . . . . . . . . . . . . . . . . . 58What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 59How to Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60Exercise 15 Pointers, Dreaded Pointers . . . . . . . . . . . . . . . . . . 62What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 64Explaining Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65Practical Pointer Usage . . . . . . . . . . . . . . . . . . . . . . . . . . 66The Pointer Lexicon . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66Pointers Aren’t Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 67How to Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67Exercise 16 Structs and Pointers to Them . . . . . . . . . . . . . . . . . 68What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 71Explaining Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 71How to Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72Exercise 17 Heap and Stack Memory Allocation . . . . . . . . . . . . . 74What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Heap versus Stack Allocation . . . . . . . . . . . . . . . . . . . . . . . 80How to Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82Exercise 18 Pointers to Functions . . . . . . . . . . . . . . . . . . . . . 84What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 88How to Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89Exercise 19 Zed’s Awesome Debug Macros . . . . . . . . . . . . . . . . 90The C Error-Handling Problem . . . . . . . . . . . . . . . . . . . . . . 90The Debug Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91Using dbg.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93vii

viiiCONTENTSWhat You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 95How the CPP Expands Macros . . . . . . . . . . . . . . . . . . . . . . 96Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98Exercise 20 Advanced Debugging Techniques . . . . . . . . . . . . . . 100Debug Printing versus GDB . . . . . . . . . . . . . . . . . . . . . . . . 100A Debugging Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . 101Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102Exercise 21 Advanced Data Types and Flow Control. . . . . . . . . . 104Available Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 104Type Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104Type Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105Type Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105Type Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106Available Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108Math Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108Data Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109Logic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109Bit Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 110Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . 110Available Control Structures . . . . . . . . . . . . . . . . . . . . . . . 110Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111Exercise 22 The Stack, Scope, and Globals . . . . . . . . . . . . . . . . 112ex22.h and ex22.c . . . . . . . . . . . . . . . . . . . . . . . . . . . 112ex22 main.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 117Scope, Stack, and Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . 118How to Break It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Exercise 23 Meet Duff’s Device . . . . . . . . . . . . . . . . . . . . . . . 120What You Should See . . . . . . . . . . . . . . . . . . . . . . . . . . . 124Solving the Puzzle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124Why Bother? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124Extra Credit . . . . . .

Z ed Shaw s Hard Way Series emphasizes instruction and making things as the best way to get started in many computer science topics. Each book in the series is designed around short, understandable exercises that take you through a course of instruction that File Size: 760KBPage Count: 45