ebook img

C Programming: A Modern Approach PDF

861 Pages·2008·37.015 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: A Modern Approach

HALFTITLE.fm Page i Friday, February 22, 2008 3:49 PM C Programming A Modern Approach Second Edition HALFTITLE.fm Page ii Friday, February 22, 2008 3:49 PM TITLE.fm Page iii Friday, February 22, 2008 3:57 PM C Programming A Modern Approach Second Edition K. N. KING Department of Computer Science Georgia State University W • W (cid:127) Norton & Company New York (cid:127) London TITLE.fm Page iv Friday, February 22, 2008 3:57 PM In memory of my father, Paul Ellsworth King W. W. Norton & Company has been independent since its founding in 1923, when William Warder Norton and Mary D. Herter Norton first published lectures delivered at the People’s Institute, the adult education division of New York City’s Cooper Union. The Nortons soon expanded their program beyond the Institute, publishing books by celebrated academics from America and abroad. By mid-century, the two major pillars of Norton’s publishing program—trade books and college texts—were firmly estab- lished. In the 1950s, the Norton family transferred control of the company to its employees, and today— with a staff of four hundred and a comparable number of trade, college, and professional titles published each year—W. W. Norton & Company stands as the largest and oldest publishing house owned wholly by its employees. Copyright  2008, 1996 by W. W. Norton & Company, Inc. All rights reserved. Printed in the United States of America. Editors: Fred McFarland and Aaron Javsicas Managing Editor, College: Marian Johnson Associate Managing Editor, College: Kim Yi Copy Editor: Mary Kelly Production Manager: Roy Tedoff Editorial Assistants: Alexis Hilts and Carly Fraser Composition by K. N. King. Manufacturing by Quebecor World Taunton. Book design by K. N. King. Library of Congress Cataloging-in-Publication Data King, K. N. (Kim N.) C programming : a modern approach / K.N. King. — 2nd ed. p. cm. Includes bibliographical references and index. ISBN 978-0-393-97950-3 (pbk.) 1. C (Computer program language) I. Title. QA76.73.C15K49 2008 005.13'3—dc22 2007049425 W. W. Norton & Company, Inc., 500 Fifth Avenue, New York, NY 10110 www.wwnorton.com W. W. Norton & Company Ltd., Castle House, 75/76 Wells Street, London W1T 3QT 1 2 3 4 5 6 7 8 9 0 CBookBTC.fm Page v Sunday, February 17, 2008 10:51 PM BRIEF CONTENTS Basic Features of C The Standard C Library 1 Introducing C 1 21 The Standard Library 529 2 C Fundamentals 9 22 Input/Output 539 3 Formatted Input/Output 37 23 Library Support for Numbers 4 Expressions 53 and Character Data 589 5 Selection Statements 73 24 Error Handling 627 6 Loops 99 25 International Features 641 7 Basic Types 125 26 Miscellaneous Library 8 Arrays 161 Functions 677 9 Functions 183 27 Additional C99 Support for 10 Program Organization 219 Mathematics 705 Advanced Features of C Reference 11 Pointers 241 A C Operators 735 12 Pointers and Arrays 257 B C99 versus C89 737 13 Strings 277 C C89 versus K&R C 743 14 The Preprocessor 315 D Standard Library Functions 747 15 Writing Large Programs 349 E ASCII Character Set 801 16 Structures, Unions, and Bibliography 803 Enumerations 377 Index 807 17 Advanced Uses of Pointers 413 18 Declarations 457 19 Program Design 483 20 Low-Level Programming 509 v CBookBTC.fm Page vi Sunday, February 17, 2008 10:51 PM CBookTOC.fm Page vii Sunday, February 17, 2008 10:39 PM CONTENTS Preface xxi 1 INTRODUCING C 1 1.1 History of C 1 Origins 1 Standardization 2 C-Based Languages 3 1.2 Strengths and Weaknesses of C 4 Strengths 4 Weaknesses 5 Effective Use of C 6 2 C FUNDAMENTALS 9 2.1 Writing a Simple Program 9 Program: Printing a Pun 9 Compiling and Linking 10 Integrated Development Environments 11 2.2 The General Form of a Simple Program 12 Directives 12 Functions 13 Statements 14 Printing Strings 14 2.3 Comments 15 2.4 Variables and Assignment 17 Types 17 Declarations 17 Assignment 18 vii CBookTOC.fm Page viii Sunday, February 17, 2008 10:39 PM viii Contents Printing the Value of a Variable 19 Program: Computing the Dimensional Weight of a Box 20 Initialization 21 Printing Expressions 22 2.5 Reading Input 22 Program: Computing the Dimensional Weight of a Box (Revisited) 22 2.6 Defining Names for Constants 23 Program: Converting from Fahrenheit to Celsius 24 2.7 Identifiers 25 Keywords 26 2.8 Layout of a C Program 27 3 FORMATTED INPUT/OUTPUT 37 3.1 The printf Function 37 Conversion Specifications 38 Program: Using printf to Format Numbers 40 Escape Sequences 41 3.2 The scanf Function 42 How scanf Works 43 Ordinary Characters in Format Strings 45 Confusing printf with scanf 45 Program: Adding Fractions 46 4 EXPRESSIONS 53 4.1 Arithmetic Operators 54 Operator Precedence and Associativity 55 Program: Computing a UPC Check Digit 56 4.2 Assignment Operators 58 Simple Assignment 58 Lvalues 59 Compound Assignment 60 4.3 Increment and Decrement Operators 61 4.4 Expression Evaluation 62 Order of Subexpression Evaluation 64 4.5 Expression Statements 65 5 SELECTION STATEMENTS 73 5.1 Logical Expressions 74 Relational Operators 74 Equality Operators 75 Logical Operators 75 5.2 The if Statement 76 Compound Statements 77 CBookTOC.fm Page ix Sunday, February 17, 2008 10:39 PM Contents ix The else Clause 78 Cascaded if Statements 80 Program: Calculating a Broker’s Commission 81 The “Dangling else” Problem 82 Conditional Expressions 83 Boolean Values in C89 84 Boolean Values in C99 85 5.3 The switch Statement 86 The Role of the break Statement 88 Program: Printing a Date in Legal Form 89 6 LOOPS 99 6.1 The while Statement 99 Infinite Loops 101 Program: Printing a Table of Squares 102 Program: Summing a Series of Numbers 102 6.2 The do Statement 103 Program: Calculating the Number of Digits in an Integer 104 6.3 The for Statement 105 for Statement Idioms 106 Omitting Expressions in a for Statement 107 for Statements in C99 108 The Comma Operator 109 Program: Printing a Table of Squares (Revisited) 110 6.4 Exiting from a Loop 111 The break Statement 111 The continue Statement 112 The goto Statement 113 Program: Balancing a Checkbook 114 6.5 The Null Statement 116 7 BASIC TYPES 125 7.1 Integer Types 125 Integer Types in C99 128 Integer Constants 128 Integer Constants in C99 129 Integer Overflow 130 Reading and Writing Integers 130 Program: Summing a Series of Numbers (Revisited) 131 7.2 Floating Types 132 Floating Constants 133 Reading and Writing Floating-Point Numbers 134 7.3 Character Types 134 Operations on Characters 135 Signed and Unsigned Characters 136

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.