top of page

AI制药

Public·12 members

Johnny Daniels
Johnny Daniels

Sams Teach Yourself C In 24 Hours


2. Sams Teach Yourself C in 24 Hours,Second EditionCopyright2000 by Sams PublishingAll rights reserved. No part of this bookshall be reproduced, stored in aretrieval system, or transmitted byany means, electronic, mechanical, photo-copying,recording, orotherwise, without written permission from the pub-lisher.No patentliability is assumed with respect to the use of theinformationcontained herein. Although every precaution has beentaken in the preparationof this book, the publisher and authorassume no responsibility for errors oromissions. Nor is anyliability assumed for damages resulting from the use oftheinformation contained herein.International Standard Book Number:0-672-31861-xLibrary of Congress Catalog Card Number:99-067311Printed in the United States of AmericaFirst Printing:February, 200005 04 03 6 5 4 3TrademarksAll terms mentioned in thisbook that are known to be trademarks or servicemarks have beenappropriately capitalized. Sams Publishing cannot attest totheaccuracy of this information. Use of a term in this book shouldnot be regardedas affecting the validity of any trademark orservice mark.Warning and DisclaimerEvery effort has been made tomake this book as complete and as accurate aspossible, but nowarranty or fitness is implied. The information provided is onan asis basis. The author and the publisher shall have neither liabilitynorresponsibility to any person or entity with respect to any lossor damages aris-ingfrom the information contained in thisbook.ASSOCIATE PUBLISHERMichael StephensACQUISITIONS EDITORCarolAckermanDEVELOPMENT EDITORGus A. MiklosMANAGING EDITORCharlotteClappPROJECT EDITORAndy BeasterCOPY EDITORKateGivensINDEXERSChristine NelsenDeborah HittelPROOFREADERCandiceHightowerTECHNICAL EDITORBill MitchellTEAM COORDINATORPamaleeNelsonINTERIOR DESIGNERGary AdairCOVER DESIGNERArenHowellCOPYWRITEREric BorgertEDITORIAL ASSISTANTAngelaBoleyPRODUCTIONStacey DeRomeMark Walchle 3. Contents at aGlanceIntroduction 1Part I The Basics of C 9Hour 1 Taking the FirstStep 112 Writing Your First C Program 273 Learning the Structure ofa C Program 414 Understanding Data Types and Keywords 555 HandlingStandard Input and Output 71Part II Operators and Control-flowStatements 89Hour 6 Manipulating Data 917 Working with Loops 1058Using Conditional Operators 1219 Working with Data Modifiers andMath Functions 14110 Controlling Program Flow 155Part III Pointersand Arrays 173Hour 11 Understanding Pointers 17512 UnderstandingArrays 18913 Manipulating Strings 20714 Understanding Scope andStorage Classes 223Part IV Functions and Dynamic Memory Allocation241Hour 15 Working with Functions 24316 Applying Pointers 25917Allocating Memory 27918 Using Special Data Types and Functions 2954. Part V Structure, Union, File I/O, and More 311Hour 19Understanding Structures 31320 Understanding Unions 33321 Readingand Writing with Files 35522 Using Special File Functions 37323Compiling Programs: The C Preprocessor 39124 Where Do You Go fromHere? 409Part VI Appendixes 437Appendix A ANSI Standard HeaderFiles 439B Answers to Quiz Questions and Exercises 441Index 503 5.Table of ContentsIntroduction 1Who Should Read This Book?................................................................................1SpecialFeatures of This Book................................................................................1ProgrammingExamples..........................................................................................2Q&Aand Workshop................................................................................................4ConventionsUsed in This Book..............................................................................4WhatYoull Learn in 24Hours................................................................................4PartI The Basics of C 9Hour 1 Taking the First Step 11What Is C?............................................................................................................12TheANSI C Standard..........................................................................................15AssumptionsAbout You........................................................................................16SettingUp Your System........................................................................................16Hardware..........................................................................................................16Software............................................................................................................16ASample C Programming Setup....................................................................17UsingMicrosofts Compiler............................................................................18UsingBorlandsCompiler................................................................................21Summary................................................................................................................24Q&A......................................................................................................................25Workshop..............................................................................................................25Quiz..................................................................................................................25Hour2 Writing Your First C Program 27A Simple CProgram..............................................................................................28Comments..............................................................................................................29The#include Directive........................................................................................31HeaderFiles......................................................................................................32AngleBrackets () and Double Quotes ( )..............................................32The main()Function..............................................................................................33TheNewline Character (n)............................................................................33Thereturn Statement......................................................................................34Theexit() Function........................................................................................34CompilingandLinking..........................................................................................34WhatsWrong with My Program?........................................................................36DebuggingYour Program......................................................................................376. vi Sams Teach Yourself C in 24HoursSummary................................................................................................................37Q&A......................................................................................................................38Workshop..............................................................................................................38Quiz..................................................................................................................38Exercises..........................................................................................................39Hour3 Learning the Structure of a C Program 41The Basics of a CProgram....................................................................................42ConstantsandVariables....................................................................................42Expressions......................................................................................................42Statements........................................................................................................45StatementBlocks..............................................................................................45Anatomyof a C Function......................................................................................46Determininga Functions Type........................................................................46Givinga Function a Valid Name......................................................................47PassingArguments to C Functions..................................................................47TheBeginning and End of a Function............................................................48TheFunction Body..........................................................................................48MakingFunction Calls..........................................................................................49Summary................................................................................................................51Q&A......................................................................................................................52Workshop..............................................................................................................52Quiz..................................................................................................................52Exercises..........................................................................................................53Hour4 Understanding Data Types and Keywords 55C Keywords..........................................................................................................56Thechar Data Type..............................................................................................57CharacterVariables..........................................................................................58CharacterConstants..........................................................................................58TheEscape Character ()..................................................................................59PrintingCharacters..........................................................................................60Theint Data Type................................................................................................62DeclaringInteger Variables..............................................................................62Showingthe Numeric Values of Characters....................................................63The floatData Type............................................................................................64DeclaringFloating-Point Variables..................................................................64TheFloating-Point Format Specifier (%f)........................................................65Thedouble Data Type..........................................................................................67UsingScientific Notation......................................................................................67NamingaVariable..................................................................................................68Summary................................................................................................................68Q&A......................................................................................................................687. Contents viiWorkshop..............................................................................................................69Quiz..................................................................................................................69Exercises..........................................................................................................70Hour5 Handling Standard Input and Output 71Understanding Standard I/O................................................................................72GettingInput from the User..................................................................................72Usingthe getc() Function..............................................................................72Usingthe getchar() Function........................................................................74PrintingOutput on the Screen..............................................................................75Usingthe putc() Function..............................................................................75AnotherFunction for Writing: putchar()......................................................77Revisitingthe printf()Function..........................................................................78Convertingto Hex Numbers............................................................................79Specifyingthe Minimum FieldWidth..............................................................81AligningOutput................................................................................................83Usingthe Precision Specifier..........................................................................84Summary................................................................................................................85Q&A......................................................................................................................86Workshop..............................................................................................................86Quiz..................................................................................................................87Exercises..........................................................................................................87PartII Operators and Control-flow Statements 89Hour 6 Manipulating Data91Arithmetic Assignment Operators........................................................................92TheAssignment Operator (=)..........................................................................92CombiningArithmetic Operators with=..........................................................92GettingNegations of Numeric Values..................................................................95Incrementingor Decrementing by One................................................................96GreaterThan or Less Than?..................................................................................98Usingthe Cast Operator......................................................................................101Summary..............................................................................................................102Q&A....................................................................................................................102Workshop............................................................................................................103Quiz................................................................................................................103Exercises........................................................................................................103Hour7 Working with Loops 105The while Loop..................................................................................................106Thedo-whileLoop..............................................................................................107LoopingUnder the for Statement......................................................................1098. viii Sams Teach Yourself C in 24 HoursThe Null Statement..............................................................................................112UsingComplex Expressions in a forStatement............................................113UsingNested Loops............................................................................................116Summary..............................................................................................................118Q&A....................................................................................................................118Workshop............................................................................................................119Quiz................................................................................................................119Exercises........................................................................................................120Hour8 Using Conditional Operators 121Measuring Data Sizes..........................................................................................122EverythingIs Logical..........................................................................................124TheLogical AND Operator (&&)....................................................................124TheLogical OR Operator ()......................................................................126TheLogical NEGATION Operator (!)..........................................................128ManipulatingBits................................................................................................129ConvertingDecimal to Hex or Binary..........................................................129UsingBitwise Operators................................................................................130UsingShift Operators....................................................................................133WhatDoes x?y:z Mean?....................................................................................135Summary..............................................................................................................137Q&A....................................................................................................................137Workshop............................................................................................................138Quiz................................................................................................................138Exercises........................................................................................................138Hour9 Working with Data Modifiers and Math Functions 141Enabling orDisabling the SignBit......................................................................142Thesigned Modifier......................................................................................142Theunsigned Modifier..................................................................................143ChangingDataSizes............................................................................................145Theshort Modifier........................................................................................145Thelong Modifier..........................................................................................145Addingh, l, or L to printf and fprintf FormatSpecifiers..........................147Mathematical Functions in C..............................................................................148Callingsin(), cos(), and tan()....................................................................149Callingpow() and sqrt()..............................................................................150Summary..............................................................................................................152Q&A....................................................................................................................153Workshop............................................................................................................154Quiz................................................................................................................154Exercises........................................................................................................1549. Contents ixHour 10 Controlling Program Flow 155Always Saying if..........................................................................................156Theif-else Statement..................................................................................158NestedifStatements......................................................................................160TheswitchStatement..........................................................................................161ThebreakStatement...............


About

Welcome to the group! Connect with other members, get updates and share media.

Members

  • Maverick Parker
    Maverick Parker
  • Anan Salupponh
    Anan Salupponh
  • 777
  • Martin Kotov
    Martin Kotov
  • Adrian Watson
    Adrian Watson
bottom of page