Stephen prata c primer plus 2005

983 912 1
Stephen prata   c primer plus  2005

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Đây là quyển sách tiếng anh về lĩnh vực công nghệ thông tin cho sinh viên và những ai có đam mê. Quyển sách này trình về lý thuyết ,phương pháp lập trình cho ngôn ngữ C và C++.

800 East 96th St., Indianapolis, Indiana, 46240 USA Primer Plus Fifth Edition C Stephen Prata 00 0672326965 FM 10/19/04 1:52 PM Page i C Primer Plus Copyright © 2005 by Sams Publishing All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the infor- mation contained herein. International Standard Book Number: 0-672-32696-5 Library of Congress Catalog Card Number: 2004095068 Printed in the United States of America First Printing: November, 2004 07 06 05 04 4321 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this infor- mation. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact U.S. Corporate and Government Sales 1-800-382-3419 corpsales@pearsontechgroup.com For sales outside of the U.S., please contact International Sales international@pearsoned.com ASSOCIATE PUBLISHER Michael Stephens ACQUISITIONS EDITOR Loretta Yates MANAGING EDITOR Charlotte Clapp DEVELOPMENT EDITOR Songlin Qiu PROJECT EDITOR George E. Nedeff COPY EDITOR Bart Reed INDEXER Chris Barrick PROOFREADER Paula Lowell TECHNICAL EDITOR Greg Perry PUBLISHING COORDINATOR Cindy Teeters MULTIMEDIA DEVELOPER Dan Scherf BOOK DESIGNER Gary Adair PAGE LAYOUT Bronkella Publishing 00 0672326965 FM 10/19/04 1:52 PM Page ii CONTENTS AT A GLANCE PREFACE XX CHAPTER 1 Getting Ready . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 CHAPTER 2 Introducing C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23 CHAPTER 3 Data and C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49 CHAPTER 4 Character Strings and Formatted Input/Output . . . . . . . . . . . . . . .89 CHAPTER 5 Operators, Expressions, and Statements . . . . . . . . . . . . . . . . . . .129 CHAPTER 6 C Control Statements: Looping . . . . . . . . . . . . . . . . . . . . . . . . . .169 CHAPTER 7 C Control Statements: Branching and Jumps . . . . . . . . . . . . . . . .221 CHAPTER 8 Character Input/Output and Input Validation . . . . . . . . . . . . . . .271 CHAPTER 9 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 CHAPTER 10 Arrays and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .347 CHAPTER 11 Character Strings and String Functions . . . . . . . . . . . . . . . . . . . .399 CHAPTER 12 Storage Classes, Linkage, and Memory Management . . . . . . . . . .453 CHAPTER 13 File Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .499 CHAPTER 14 Structures and Other Data Forms . . . . . . . . . . . . . . . . . . . . . . . .535 CHAPTER 15 Bit Fiddling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .597 CHAPTER 16 The C Preprocessor and the C Library . . . . . . . . . . . . . . . . . . . . .629 CHAPTER 17 Advanced Data Representation . . . . . . . . . . . . . . . . . . . . . . . . . .681 APPENDIXES APPENDIX A Answers to the Review Questions . . . . . . . . . . . . . . . . . . . . . . . .759 APPENDIX B Reference Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .799 INDEX 889 00 0672326965 FM 10/19/04 1:52 PM Page iii TABLE OF CONTENTS CHAPTER 1 Getting Ready . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Whence C? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Why C? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 Design Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Power and Flexibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Programmer Oriented . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Shortcomings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 Whither C? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 What Computers Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 High-level Computer Languages and Compilers . . . . . . . . . . . . . . . . . . . . . . . .6 Using C: Seven Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 Step 1: Define the Program Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8 Step 2: Design the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8 Step 3: Write the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8 Step 4: Compile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 Step 5: Run the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 Step 6: Test and Debug the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 Step 7: Maintain and Modify the Program . . . . . . . . . . . . . . . . . . . . . . . . .10 Commentary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10 Programming Mechanics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10 Object Code Files, Executable Files, and Libraries . . . . . . . . . . . . . . . . . . .11 Unix System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13 Linux System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14 Integrated Development Environments (Windows) . . . . . . . . . . . . . . . . . .15 DOS Compilers for the IBM PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16 C on the Macintosh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17 Language Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17 The First ANSI/ISO C Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17 The C99 Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18 How This Book Is Organized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19 Conventions Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19 Typeface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19 Program Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20 Special Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21 00 0672326965 FM 10/19/04 1:52 PM Page iv Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22 Programming Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22 CHAPTER 2 Introducing C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23 A Simple Example of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23 The Example Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24 Pass 1: Quick Synopsis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24 Pass 2: Program Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26 The Structure of a Simple Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 Tips on Making Your Programs Readable . . . . . . . . . . . . . . . . . . . . . . . . . . . .36 Taking Another Step in Using C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 Multiple Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38 Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38 Printing Multiple Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38 While You’re at It—Multiple Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38 Introducing Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40 Syntax Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41 Semantic Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41 Program State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42 Keywords and Reserved Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43 Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45 Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47 CHAPTER 3 Data and C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49 A Sample Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49 What’s New in This Program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 Data Variables and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52 Data: Data-Type Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52 Integer Versus Floating-Point Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54 The Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54 The Floating-Point Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54 Basic C Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55 The int Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55 Other Integer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59 Using Characters: Type char . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64 The _Bool Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70 Portable Types: inttypes.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70 Types float, double, and long double . . . . . . . . . . . . . . . . . . . . . . . . . . .72 00 0672326965 FM 10/19/04 1:52 PM Page v vi C PRIMER PLUS Complex and Imaginary Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76 Beyond the Basic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77 Type Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79 Using Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80 Arguments and Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81 One More Example: Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83 What Happens When the Program Runs . . . . . . . . . . . . . . . . . . . . . . . . . .83 Flushing the Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .84 Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86 Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88 CHAPTER 4 Character Strings and Formatted Input/Output . . . . . . . . . . . . . . . .89 Introductory Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89 Character Strings: An Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91 Type char Arrays and the Null Character . . . . . . . . . . . . . . . . . . . . . . . . . .91 Using Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92 The strlen() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93 Constants and the C Preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95 The const Modifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98 Manifest Constants on the Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98 Exploring and Exploiting printf() and scanf() . . . . . . . . . . . . . . . . . . . . .101 The printf() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101 Using printf() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102 Conversion Specification Modifiers for printf() . . . . . . . . . . . . . . . . . . .104 What Does a Conversion Specification Convert? . . . . . . . . . . . . . . . . . . .110 Using scanf() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116 The * Modifier with printf() and scanf() . . . . . . . . . . . . . . . . . . . . . .120 Usage Tips for printf() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122 Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124 Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127 CHAPTER 5 Operators, Expressions, and Statements . . . . . . . . . . . . . . . . . . . . .129 Introducing Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129 Fundamental Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .132 Assignment Operator: = . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .132 Addition Operator: + . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134 Subtraction Operator: – . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134 00 0672326965 FM 10/19/04 1:52 PM Page vi viiCONTENTS Sign Operators: – and + . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134 Multiplication Operator: * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135 Division Operator: / . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137 Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138 Precedence and the Order of Evaluation . . . . . . . . . . . . . . . . . . . . . . . . .140 Some Additional Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141 The sizeof Operator and the size_t Type . . . . . . . . . . . . . . . . . . . . . . .142 Modulus Operator: % . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142 Increment and Decrement Operators: ++ and . . . . . . . . . . . . . . . . . . .144 Decrementing: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148 Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149 Don’t Be Too Clever . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149 Expressions and Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151 Compound Statements (Blocks) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .154 Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .156 The Cast Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158 Function with Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159 A Sample Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161 Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .164 Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167 CHAPTER 6 C Control Statements: Looping . . . . . . . . . . . . . . . . . . . . . . . . . . .169 Revisiting the while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170 Program Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170 C-Style Reading Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172 The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172 Terminating a while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173 When a Loop Terminates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174 while: An Entry-Condition Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174 Syntax Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .175 Which Is Bigger: Using Relational Operators and Expressions . . . . . . . . . . .176 What Is Truth? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .178 What Else Is True? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .179 Troubles with Truth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .180 The New _Bool Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182 Precedence of Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183 00 0672326965 FM 10/19/04 1:52 PM Page vii viii C PRIMER PLUS Indefinite Loops and Counting Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .186 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187 Using for for Flexibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .188 More Assignment Operators: +=, -=, *=, /=, %= . . . . . . . . . . . . . . . . . . . . . . .193 The Comma Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .193 Zeno Meets the for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .196 An Exit-Condition Loop: do while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198 Which Loop? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201 Program Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202 A Nested Variation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202 Introducing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203 Using a for Loop with an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 A Loop Example Using a Function Return Value . . . . . . . . . . . . . . . . . . . . .207 Program Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .209 Using Functions with Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . .210 Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .212 Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217 CHAPTER 7 C Control Statements: Branching and Jumps . . . . . . . . . . . . . . . . .221 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222 Adding else to the if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .224 Another Example: Introducing getchar() and putchar() . . . . . . . . . . .225 The ctype.h Family of Character Functions . . . . . . . . . . . . . . . . . . . . . .228 Multiple Choice else if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .230 Pairing else with if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .233 More Nested ifs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .234 Let’s Get Logical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .238 Alternate Spellings: The iso646.h Header File . . . . . . . . . . . . . . . . . . . .239 Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .240 Order of Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .240 Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242 A Word-Count Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242 The Conditional Operator: ?: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .246 Loop Aids: continue and break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .248 The continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .248 The break Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251 Multiple Choice: switch and break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253 Using the switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255 Reading Only the First Character of a Line . . . . . . . . . . . . . . . . . . . . . . . .256 00 0672326965 FM 10/19/04 1:52 PM Page viii ixCONTENTS Multiple Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .257 switch and if else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .259 The goto Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .259 Avoiding goto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260 Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .263 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .263 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264 Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .267 CHAPTER 8 Character Input/Output and Input Validation . . . . . . . . . . . . . . . .271 Single-Character I/O: getchar() and putchar() . . . . . . . . . . . . . . . . . . . . .272 Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .273 Terminating Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .274 Files, Streams, and Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . .274 The End of File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .275 Redirection and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .278 Unix, Linux, and DOS Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .279 Creating a Friendlier User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .283 Working with Buffered Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .283 Mixing Numeric and Character Input . . . . . . . . . . . . . . . . . . . . . . . . . . .285 Input Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .288 Analyzing the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .292 The Input Stream and Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .293 Menu Browsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .293 Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .294 Toward a Smoother Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .294 Mixing Character and Numeric Input . . . . . . . . . . . . . . . . . . . . . . . . . . .296 Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .299 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .299 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .300 Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .301 CHAPTER 9 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Reviewing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Creating and Using a Simple Function . . . . . . . . . . . . . . . . . . . . . . . . . . .304 Analyzing the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .305 Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .308 Defining a Function with an Argument: Formal Parameters . . . . . . . . . . .309 Prototyping a Function with Arguments . . . . . . . . . . . . . . . . . . . . . . . . .310 Calling a Function with an Argument: Actual Arguments . . . . . . . . . . . . .311 The Black-Box Viewpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .311 Returning a Value from a Function with return . . . . . . . . . . . . . . . . . . .312 Function Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .315 00 0672326965 FM 10/19/04 1:52 PM Page ix ANSI C Function Prototyping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316 The ANSI Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318 No Arguments and Unspecified Arguments . . . . . . . . . . . . . . . . . . . . . . .319 Hooray for Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .320 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .320 Recursion Revealed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .320 Recursion Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .322 Tail Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .323 Recursion and Reversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325 Recursion Pros and Cons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .327 Compiling Programs with Two or More Source Code Files . . . . . . . . . . . . . .328 Unix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .328 Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .328 DOS Command-Line Compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .329 Windows and Macintosh Compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . .329 Using Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .329 Finding Addresses: The & Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .333 Altering Variables in the Calling Function . . . . . . . . . . . . . . . . . . . . . . . . . . .334 Pointers: A First Look . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .336 The Indirection Operator: * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337 Declaring Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .338 Using Pointers to Communicate Between Functions . . . . . . . . . . . . . . . .339 Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .343 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .343 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .343 Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .345 CHAPTER 10 Arrays and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .347 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .347 Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .348 Designated Initializers (C99) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .352 Assigning Array Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .353 Array Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .354 Specifying an Array Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .355 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .356 Initializing a Two-Dimensional Array . . . . . . . . . . . . . . . . . . . . . . . . . . . .359 More Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .360 Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361 Functions, Arrays, and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .364 Using Pointer Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .366 Comment: Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .369 x C PRIMER PLUS 00 0672326965 FM 10/19/04 1:52 PM Page x [...]... 1.4 Compiler and linker concrete .c source code Compiler concrete.obj object code library code Linker start-up code concrete.exe executable code In short, an object file and an executable file both consist of machine language instructions However, the object file contains the machine language translation only for the code you used, but the executable file also has machine code for the library routines... Listing 1.2 concrete .c LISTING 1.2 The concrete .c Program #include int main(void) { printf(“Concrete contains gravel and cement.\n”); return 0; } Don’t worry about the details of the source code file shown in Listing 1.2; you’ll learn about them in Chapter 2 Object Code Files, Executable Files, and Libraries The basic strategy in C programming is to use programs that convert your source code file... more conceptual view of what happens Recall that the compiler is a program whose job is to convert source code into executable code Executable code is code in the native language, or machine language, of your computer This language consists of detailed instructions expressed in a numeric code As you read earlier, different computers have different machine languages, and a C compiler translates C into... best C is a leader in portability C compilers (programs that convert your C code into the instructions a computer uses internally) are available for about 40 systems, running from 8-bit microprocessors to Cray supercomputers Note, however, that the portions of a program written specifically to access particular hardware devices, such as a display monitor, or special features of an operating system, such... least in this simplistic account, is quite simple It fetches an instruction from memory and executes it It fetches the next instruction from memory and executes it, and so on (A gigahertz CPU can do this about a billion times a second, so the CPU can lead its boring life at a tremendous pace.) The CPU has its own small workspace, consisting of several registers, each of which can hold a number One register... source code file to an executable file, which is a file containing ready-to-run machine language code C implementations do this in two steps: compiling and linking The compiler converts your source code to an intermediate code, and the linker combines this with other code to produce the executable file C uses this two-part approach to facilitate the modularization of programs You can compile individual modules... an object code file, or object file for short (This assumes 11 01 0672326965 CH01 12 10/19/04 1:53 PM Page 12 C PRIMER PLUS that your source code consists of a single file.) Although the object file contains machine language code, it is not ready to run The object file contains the translation of your source code, but it is not yet a complete program The first element missing from the object code file... another The second missing element is the code for library routines Nearly all C programs make use of routines (called functions) that are part of the standard C library For example, concrete .c uses the function printf() The object code file does not contain the code for this function; it merely contains instructions saying to use the printf() function The actual code is stored in another file, called a... a numeric code The instructions that a computer loads into its registers are stored as numbers; each instruction in the instruction set has a numeric code Second, computer programs ultimately have to be expressed in this numeric instruction code, or what is called machine language One consequence of how computers work is that if you want a computer to do something, you have to feed a particular list... the control features found desirable by the theory and practice of computer science Its design makes it natural for top-down planning, structured programming, and modular design The result is a more reliable, understandable program Efficiency C is an efficient language Its design takes advantage of the capabilities of current computers C programs tend to be compact and to run quickly In fact, C exhibits . Multibyte and Wide-Character Utilities: wchar.h (C9 9) . . . . . .861 Wide Character Classification and Mapping Utilities: wctype.h (C9 9) . . .868 Section VI: Extended. a friendly, easy-to-use, self-study guide. To accomplish that objective, C Primer Plus employs the following strategies: •Programming concepts are explained,

Ngày đăng: 19/03/2014, 14:13

Từ khóa liên quan

Mục lục

  • C Primer Plus, Fifth Edition

    • Copyright © 2005 by Sams Publishing

    • CONTENTS AT A GLANCE

    • TABLE OF CONTENTS

    • PREFACE

      • Approach and Goals

      • ABOUT THE AUTHOR

      • WE WANT TO HEAR FROM YOU!

      • CHAPTER 1 GETTING READY

        • Whence C?

        • Why C?

        • Whither C?

        • What Computers Do

        • High-level Computer Languages and Compilers

        • Using C: Seven Steps

        • Programming Mechanics

        • Language Standards

        • How This Book Is Organized

        • Conventions Used in This Book

        • Summary

        • Review Questions

        • Programming Exercise

        • CHAPTER 2 INTRODUCING C

          • A Simple Example of C

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan