C++ Programming

profilesnavy80
deitel__deitel_c_how_to_program-_6th_editionwww.itkhiladi.com_.pdf

Deitel® Ser How To Program Series

C How to Program, 6/E

C++ How to Program, 7/E

Java How to Program, 8/E, Early Objects Version

Java How to Program, 8/E, Late Objects Version

Internet & World Wide Web How to Program, 4/E

Visual Basic® 2008 How to Program

Visual C#® 2008 How to Program, 3/E

Visual C++® 2008 How to Program, 2/E

Small Java™ How to Program, 6/E

Small C++ How to Program, 5/E

Simply Series Simply C++: An Application-Driven

Tutorial Approach

Simply Java™ Programming: An Application-Driven Tutorial Approach

Simply C#: An Application-Driven Tutorial Approach

Simply Visual Basic® 2008, 3/E: An Application-Driven Tutorial Approach

CourseSmart Web Books www.deitel.com/books/CourseSmart.html

C++ How to Program, 5/E, 6/E & 7/E

Java How to Program, 6/E, 7/E & 8/E

Simply C++: An Application-Driven Tutorial Approach

Simply Visual Basic 2008: An Application-Driven Tutorial Approach, 3/E

Small C++ How to Program, 5/E

Small Java How to Program, 6/E

Visual Basic® 2008 How to Program

Visual C#® 2008 How to Program, 3/E

ies Page

Deitel® Developer Series AJAX, Rich Internet Applications and

Web Development for Programmers

C++ for Programmers

C# 2008 for Programmers, 3/E

iPhone for Programmers: An App-Driven Approach

Java for Programmers

Javascript for Programmers

LiveLessons Video Learning Products www.deitel.com/books/LiveLessons/

Java Fundamentals Parts 1 and 2

C# Fundamentals Parts 1 and 2

C++ Fundamentals Parts 1 and 2

JavaScript Fundamentals Parts 1 and 2

To receive updates on Deitel publications, Resource Centers, training courses, partner offers and more, please register for the free Deitel® Buzz Online e-mail newsletter at: www.deitel.com/newsletter/subscribe.html

follow us on Twitter®

@deitel

and Facebook®

www.deitel.com/deitelfan/

To communicate with the authors, send e-mail to: [email protected]

For information on government and corporate Dive-Into® Series on-site seminars offered by Deitel & Associates, Inc. worldwide, visit: www.deitel.com/training/

or write to [email protected]

For continuing updates on Prentice Hall/Deitel publications visit: www.deitel.com www.pearsonhighered.com/deitel

Check out our Resource Centers for valuable web resources that will help you master Java, other important programming languages, software and Internet- and web-related topics: www.deitel.com/ResourceCenters.html

Library of Congress Cataloging-in-Publication Data On file

Vice President and Editorial Director, ECS: Marcia J. Horton Editor-in-Chief, Computer Science: Michael Hirsch Associate Editor: Carole Snyder Supervisor/Editorial Assistant: Dolores Mars Director of Team-Based Project Management: Vince O’Brien Senior Managing Editor: Scott Disanno Managing Editor: Robert Engelhardt A/V Production Editor: Greg Dulles Art Director: Kristine Carney Cover Design: Abbey S. Deitel, Harvey M. Deitel, Francesco Santalucia, Kristine Carney Interior Design: Harvey M. Deitel, Kristine Carney Manufacturing Manager: Alexis Heydt-Long Manufacturing Buyer: Lisa McDowell Director of Marketing: Margaret Waples Marketing Manager: Erin Davis

© 2010 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458

The authors and publisher of this book have used their best efforts in preparing this book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and pub- lisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in this book. The authors and publisher shall not be liable in any event for incidental or consequential dam- ages in connection with, or arising out of, the furnishing, performance, or use of these programs.

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks and registered trademarks. Where those designations appear in this book, and Prentice Hall and the authors were aware of a trademark claim, the designations have been printed in initial caps or all caps. All product names mentioned remain trademarks or registered trademarks of their respective owners.

All rights reserved. No part of this book may be reproduced, in any form or by any means, without permission in writing from the publisher.

Printed in the United States of America

10 9 8 7 6 5 4 3 2 1

ISBN-10: 0-13-612356-2

ISBN-13: 978-0-13-612356-9

Pearson Education Ltd., London Pearson Education Australia Pty. Ltd., Sydney Pearson Education Singapore, Pte. Ltd. Pearson Education North Asia Ltd., Hong Kong Pearson Education Canada, Inc., Toronto Pearson Educación de Mexico, S.A. de C.V. Pearson Education–Japan, Tokyo Pearson Education Malaysia, Pte. Ltd. Pearson Education, Inc., Upper Saddle River, New Jersey

Upper Saddle River, New Jersey 07458

P. J. Deitel Deitel & Associates, Inc.

H. M. Deitel Deitel & Associates, Inc.

Trademarks DEITEL, the double-thumbs-up bug and DIVE INTO are registered trademarks of Deitel and Associates, Inc.

Microsoft, Visual C++, Internet Explorer and the Windows logo are either registered trademarks or trade- marks of Microsoft Corporation in the United States and/or other countries.

In Memory of Clifford Stephens:

Your friendship, bright smile and infectious laugh will be truly missed.

Paul and Harvey Deitel

Deitel Resource Centers Our Resource Centers focus on the vast amounts of free content available online. Find resources, downloads, tutorials, documentation, books, e-books, journals, articles, blogs, RSS feeds and more on many of today’s hottest programming and technology topics. For the most up-to-date list of our Resource Centers, visit:

www.deitel.com/ResourceCenters.html

Let us know what other Resource Centers you’d like to see! Also, please register for the free Deitel®

Buzz Online e-mail newsletter at:

www.deitel.com/newsletter/subscribe.html

Computer Science Functional Programming Regular Expressions

Programming ASP.NET 3.5 Adobe Flex Ajax Apex ASP.NET Ajax ASP.NET C C++ C++ Boost Libraries C++ Game Programming C# Code Search Engines and

Code Sites Computer Game

Programming CSS 2.1 Dojo Facebook Developer Plat-

form Flash 9 Functional Programming Java Java Certification and

Assessment Testing Java Design Patterns Java EE 5 Java SE 6 Java SE 7 (Dolphin)

Resource Center JavaFX JavaScript JSON Microsoft LINQ Microsoft Popfly .NET .NET 3.0 .NET 3.5 OpenGL Perl PHP Programming Projects Python Regular Expressions Ruby Ruby on Rails Silverlight

UML Visual Basic Visual C++ Visual Studio Team Sys-

tem Web 3D Technologies Web Services Windows Presentation

Foundation XHTML XML

Apple iPhone Objective-C Cocoa

Games and Game Programming

Computer Game Pro- gramming

Computer Games Mobile Gaming Sudoku

Internet Business Affiliate Programs Competitive Analysis Facebook Social Ads Google AdSense Google Analytics Google Services Internet Advertising Internet Business

Initiative Internet Public Relations Link Building Location-Based Services Online Lead Generation Podcasting Search Engine Optimiza-

tion Selling Digital Content Sitemaps Web Analytics Website Monetization YouTube and AdSense

Java Java Java Certification and

Assessment Testing

Java Design Patterns Java EE 5 Java SE 6 Java SE 7 (Dolphin)

Resource Center JavaFX

Microsoft ASP.NET ASP.NET 3.5 ASP.NET Ajax C# DotNetNuke (DNN) Internet Explorer 7 (IE7) Microsoft LINQ .NET .NET 3.0 .NET 3.5 SharePoint Silverlight Visual Basic Visual C++ Visual Studio Team

System Windows Presentation

Foundation Windows Vista Microsoft Popfly

Open Source & LAMP Stack

Apache DotNetNuke (DNN) Eclipse Firefox Linux MySQL Open Source Perl PHP Python Ruby

Software Apache DotNetNuke (DNN) Eclipse Firefox Internet Explorer 7 (IE7) Linux MySQL

Open Source Search Engines SharePoint Skype Web Servers Wikis Windows Vista

Web 2.0 Alert Services Attention Economy Blogging Building Web

Communities Community Generated

Content Facebook Developer

Platform Facebook Social Ads Google Base Google Video Google Web Toolkit

(GWT) Internet Video Joost Location-Based Services Mashups Microformats Recommender Systems RSS Social Graph Social Media Social Networking Software as a Service

(SaaS) Virtual Worlds Web 2.0 Web 3.0 Widgets

Dive Into® Web 2.0 eBook

Web 2 eBook

Other Topics Computer Games Computing Jobs Gadgets and Gizmos Ring Tones Sudoku

Appendices E through I are PDF documents posted online at the book’s Companion Website (located at www.pearsonhighered.com/deitel).

Preface xxi

1 Introduction to Computers, the Internet and the Web 1

1.1 Introduction 2 1.2 Computers: Hardware and Software 3 1.3 Computer Organization 4 1.4 Personal, Distributed and Client/Server Computing 5 1.5 The Internet and the World Wide Web 5 1.6 Machine Languages, Assembly Languages and High-Level Languages 6 1.7 History of C 7 1.8 C Standard Library 8 1.9 C++ 9 1.10 Java 9 1.11 Fortran, COBOL, Pascal and Ada 10 1.12 BASIC, Visual Basic, Visual C++, C# and .NET 10 1.13 Key Software Trend: Object Technology 11 1.14 Typical C Program Development Environment 12 1.15 Hardware Trends 14 1.16 Notes About C and This Book 15 1.17 Web Resources 16

2 Introduction to C Programming 23 2.1 Introduction 24 2.2 A Simple C Program: Printing a Line of Text 24 2.3 Another Simple C Program: Adding Two Integers 28 2.4 Memory Concepts 33 2.5 Arithmetic in C 34 2.6 Decision Making: Equality and Relational Operators 38

3 Structured Program Development in C 54 3.1 Introduction 55 3.2 Algorithms 55

Contents

x Contents

3.3 Pseudocode 55 3.4 Control Structures 56 3.5 The if Selection Statement 58 3.6 The if…else Selection Statement 59 3.7 The while Repetition Statement 63 3.8 Formulating Algorithms Case Study 1: Counter-Controlled Repetition 64 3.9 Formulating Algorithms with Top-Down, Stepwise Refinement

Case Study 2: Sentinel-Controlled Repetition 66 3.10 Formulating Algorithms with Top-Down, Stepwise Refinement

Case Study 3: Nested Control Structures 73 3.11 Assignment Operators 77 3.12 Increment and Decrement Operators 78

4 C Program Control 97 4.1 Introduction 98 4.2 Repetition Essentials 98 4.3 Counter-Controlled Repetition 99 4.4 for Repetition Statement 100 4.5 for Statement: Notes and Observations 103 4.6 Examples Using the for Statement 103 4.7 switch Multiple-Selection Statement 107 4.8 do…while Repetition Statement 113 4.9 break and continue Statements 114 4.10 Logical Operators 116 4.11 Confusing Equality (==) and Assignment (=) Operators 119 4.12 Structured Programming Summary 121

5 C Functions 140 5.1 Introduction 141 5.2 Program Modules in C 141 5.3 Math Library Functions 142 5.4 Functions 144 5.5 Function Definitions 144 5.6 Function Prototypes 148 5.7 Function Call Stack and Activation Records 151 5.8 Headers 151 5.9 Calling Functions By Value and By Reference 152 5.10 Random Number Generation 153 5.11 Example: A Game of Chance 158 5.12 Storage Classes 161 5.13 Scope Rules 164 5.14 Recursion 167 5.15 Example Using Recursion: Fibonacci Series 170 5.16 Recursion vs. Iteration 174

Contents xi

6 C Arrays 195 6.1 Introduction 196 6.2 Arrays 196 6.3 Defining Arrays 198 6.4 Array Examples 198 6.5 Passing Arrays to Functions 212 6.6 Sorting Arrays 216 6.7 Case Study: Computing Mean, Median and Mode Using Arrays 218 6.8 Searching Arrays 223 6.9 Multiple-Subscripted Arrays 229

7 C Pointers 253 7.1 Introduction 254 7.2 Pointer Variable Definitions and Initialization 254 7.3 Pointer Operators 255 7.4 Passing Arguments to Functions by Reference 257 7.5 Using the const Qualifier with Pointers 261 7.6 Bubble Sort Using Call-by-Reference 267 7.7 sizeof Operator 270 7.8 Pointer Expressions and Pointer Arithmetic 273 7.9 Relationship between Pointers and Arrays 275 7.10 Arrays of Pointers 280 7.11 Case Study: Card Shuffling and Dealing Simulation 280 7.12 Pointers to Functions 285

8 C Characters and Strings 309 8.1 Introduction 310 8.2 Fundamentals of Strings and Characters 310 8.3 Character-Handling Library 312 8.4 String-Conversion Functions 317 8.5 Standard Input/Output Library Functions 322 8.6 String-Manipulation Functions of the String-Handling Library 326 8.7 Comparison Functions of the String-Handling Library 329 8.8 Search Functions of the String-Handling Library 331 8.9 Memory Functions of the String-Handling Library 337 8.10 Other Functions of the String-Handling Library 341

9 C Formatted Input/Output 356 9.1 Introduction 357 9.2 Streams 357 9.3 Formatting Output with printf 357 9.4 Printing Integers 358 9.5 Printing Floating-Point Numbers 359

xii Contents

9.6 Printing Strings and Characters 361 9.7 Other Conversion Specifiers 362 9.8 Printing with Field Widths and Precision 363 9.9 Using Flags in the printf Format Control String 366 9.10 Printing Literals and Escape Sequences 368 9.11 Reading Formatted Input with scanf 369

10 C Structures, Unions, Bit Manipulations and Enumerations 382

10.1 Introduction 383 10.2 Structure Definitions 383 10.3 Initializing Structures 386 10.4 Accessing Structure Members 386 10.5 Using Structures with Functions 388 10.6 typedef 388 10.7 Example: High-Performance Card Shuffling and Dealing Simulation 389 10.8 Unions 391 10.9 Bitwise Operators 394 10.10 Bit Fields 403 10.11 Enumeration Constants 406

11 C File Processing 417 11.1 Introduction 418 11.2 Data Hierarchy 418 11.3 Files and Streams 420 11.4 Creating a Sequential-Access File 421 11.5 Reading Data from a Sequential-Access File 426 11.6 Random-Access Files 430 11.7 Creating a Random-Access File 431 11.8 Writing Data Randomly to a Random-Access File 433 11.9 Reading Data from a Random-Access File 436 11.10 Case Study: Transaction-Processing Program 437

12 C Data Structures 454 12.1 Introduction 455 12.2 Self-Referential Structures 456 12.3 Dynamic Memory Allocation 456 12.4 Linked Lists 458 12.5 Stacks 466 12.6 Queues 472 12.7 Trees 478

13 C Preprocessor 495 13.1 Introduction 496

Contents xiii

13.2 #include Preprocessor Directive 496 13.3 #define Preprocessor Directive: Symbolic Constants 496 13.4 #define Preprocessor Directive: Macros 497 13.5 Conditional Compilation 499 13.6 #error and #pragma Preprocessor Directives 500 13.7 # and ## Operators 500 13.8 Line Numbers 501 13.9 Predefined Symbolic Constants 501 13.10 Assertions 502

14 Other C Topics 507 14.1 Introduction 508 14.2 Redirecting I/O 508 14.3 Variable-Length Argument Lists 509 14.4 Using Command-Line Arguments 511 14.5 Notes on Compiling Multiple-Source-File Programs 512 14.6 Program Termination with exit and atexit 514 14.7 volatile Type Qualifier 515 14.8 Suffixes for Integer and Floating-Point Constants 516 14.9 More on Files 516 14.10 Signal Handling 518 14.11 Dynamic Memory Allocation: Functions calloc and realloc 520 14.12 Unconditional Branching with goto 521

15 C++ as a Better C; Introducing Object Technology 528

15.1 Introduction 529 15.2 C++ 529 15.3 A Simple Program: Adding Two Integers 530 15.4 C++ Standard Library 532 15.5 Header Files 533 15.6 Inline Functions 535 15.7 References and Reference Parameters 537 15.8 Empty Parameter Lists 542 15.9 Default Arguments 542 15.10 Unary Scope Resolution Operator 544 15.11 Function Overloading 545 15.12 Function Templates 548 15.13 Introduction to Object Technology and the UML 551 15.14 Wrap-Up 554

16 Introduction to Classes and Objects 560 16.1 Introduction 561 16.2 Classes, Objects, Member Functions and Data Members 561

xiv Contents

16.3 Defining a Class with a Member Function 562 16.4 Defining a Member Function with a Parameter 566 16.5 Data Members, set Functions and get Functions 569 16.6 Initializing Objects with Constructors 576 16.7 Placing a Class in a Separate File for Reusability 579 16.8 Separating Interface from Implementation 583 16.9 Validating Data with set Functions 589 16.10 Wrap-Up 594

17 Classes: A Deeper Look, Part 1 601 17.1 Introduction 602 17.2 Time Class Case Study 603 17.3 Class Scope and Accessing Class Members 609 17.4 Separating Interface from Implementation 611 17.5 Access Functions and Utility Functions 612 17.6 Time Class Case Study: Constructors with Default Arguments 615 17.7 Destructors 620 17.8 When Constructors and Destructors are Called 621 17.9 Time Class Case Study: A Subtle Trap—Returning a Reference to a

private Data Member 624 17.10 Default Memberwise Assignment 627 17.11 Wrap-Up 629

18 Classes: A Deeper Look, Part 2 635 18.1 Introduction 636 18.2 const (Constant) Objects and const Member Functions 636 18.3 Composition: Objects as Members of Classes 645 18.4 friend Functions and friend Classes 651 18.5 Using the this Pointer 654 18.6 static Class Members 659 18.7 Data Abstraction and Information Hiding 664 18.8 Wrap-Up 666

19 Operator Overloading 672 19.1 Introduction 673 19.2 Fundamentals of Operator Overloading 674 19.3 Restrictions on Operator Overloading 675 19.4 Operator Functions as Class Members vs. Global Function 676 19.5 Overloading Stream Insertion and Stream Extraction Operators 678 19.6 Overloading Unary Operators 681 19.7 Overloading Binary Operators 682 19.8 Dynamic Memory Management 682 19.9 Case Study: Array Class 684 19.10 Converting between Types 696

Contents xv

19.11 Building a String Class 697 19.12 Overloading ++ and -- 698 19.13 Case Study: A Date Class 700 19.14 Standard Library Class string 704 19.15 explicit Constructors 708 19.16 Proxy Classes 711 19.17 Wrap-Up 715

20 Object-Oriented Programming: Inheritance 727 20.1 Introduction 728 20.2 Base Classes and Derived Classes 729 20.3 protected Members 732 20.4 Relationship between Base Classes and Derived Classes 732

20.4.1 Creating and Using a CommissionEmployee Class 733 20.4.2 Creating a BasePlusCommissionEmployee Class Without

Using Inheritance 738 20.4.3 Creating a CommissionEmployee–BasePlusCommissionEmployee

Inheritance Hierarchy 743 20.4.4 CommissionEmployee–BasePlusCommissionEmployee

Inheritance Hierarchy Using protected Data 748 20.4.5 CommissionEmployee–BasePlusCommissionEmployee

Inheritance Hierarchy Using private Data 755 20.5 Constructors and Destructors in Derived Classes 762 20.6 public, protected and private Inheritance 770 20.7 Software Engineering with Inheritance 771 20.8 Wrap-Up 772

21 Object-Oriented Programming: Polymorphism 778 21.1 Introduction 779 21.2 Polymorphism Examples 780 21.3 Relationships Among Objects in an Inheritance Hierarchy 781

21.3.1 Invoking Base-Class Functions from Derived-Class Objects 782 21.3.2 Aiming Derived-Class Pointers at Base-Class Objects 789 21.3.3 Derived-Class Member-Function Calls via Base-Class Pointers 790 21.3.4 Virtual Functions 792 21.3.5 Summary of the Allowed Assignments Between Base-Class

and Derived-Class Objects and Pointers 798 21.4 Type Fields and switch Statements 799 21.5 Abstract Classes and Pure virtual Functions 799 21.6 Case Study: Payroll System Using Polymorphism 801

21.6.1 Creating Abstract Base Class Employee 803 21.6.2 Creating Concrete Derived Class SalariedEmployee 806 21.6.3 Creating Concrete Derived Class HourlyEmployee 808 21.6.4 Creating Concrete Derived Class CommissionEmployee 811

xvi Contents

21.6.5 Creating Indirect Concrete Derived Class BasePlusCommissionEmployee 813

21.6.6 Demonstrating Polymorphic Processing 814 21.7 (Optional) Polymorphism, Virtual Functions and Dynamic Binding

“Under the Hood” 818 21.8 Case Study: Payroll System Using Polymorphism and Runtime Type

Information with Downcasting, dynamic_cast, typeid and type_info 822 21.9 Virtual Destructors 826 21.10 Wrap-Up 826

22 Templates 832 22.1 Introduction 833 22.2 Function Templates 833 22.3 Overloading Function Templates 837 22.4 Class Templates 837 22.5 Nontype Parameters and Default Types for Class Templates 844 22.6 Notes on Templates and Inheritance 845 22.7 Notes on Templates and Friends 845 22.8 Notes on Templates and static Members 846 22.9 Wrap-Up 846

23 Stream Input/Output 851 23.1 Introduction 852 23.2 Streams 853

23.2.1 Classic Streams vs. Standard Streams 853 23.2.2 iostream Library Header Files 854 23.2.3 Stream Input/Output Classes and Objects 854

23.3 Stream Output 857 23.3.1 Output of char * Variables 857 23.3.2 Character Output Using Member Function put 857

23.4 Stream Input 858 23.4.1 get and getline Member Functions 858 23.4.2 istream Member Functions peek, putback and ignore 861 23.4.3 Type-Safe I/O 861

23.5 Unformatted I/O Using read, write and gcount 861 23.6 Introduction to Stream Manipulators 862

23.6.1 Integral Stream Base: dec, oct, hex and setbase 863 23.6.2 Floating-Point Precision (precision, setprecision) 864 23.6.3 Field Width (width, setw) 865 23.6.4 User-Defined Output Stream Manipulators 866

23.7 Stream Format States and Stream Manipulators 868 23.7.1 Trailing Zeros and Decimal Points (showpoint) 868 23.7.2 Justification (left, right and internal) 869 23.7.3 Padding (fill, setfill) 871 23.7.4 Integral Stream Base (dec, oct, hex, showbase) 872

Contents xvii

23.7.5 Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed) 873

23.7.6 Uppercase/Lowercase Control (uppercase) 874 23.7.7 Specifying Boolean Format (boolalpha) 874 23.7.8 Setting and Resetting the Format State via Member

Function flags 875 23.8 Stream Error States 877 23.9 Tying an Output Stream to an Input Stream 879 23.10 Wrap-Up 879

24 Exception Handling 889 24.1 Introduction 890 24.2 Exception-Handling Overview 891 24.3 Example: Handling an Attempt to Divide by Zero 891 24.4 When to Use Exception Handling 897 24.5 Rethrowing an Exception 898 24.6 Exception Specifications 900 24.7 Processing Unexpected Exceptions 901 24.8 Stack Unwinding 901 24.9 Constructors, Destructors and Exception Handling 903 24.10 Exceptions and Inheritance 904 24.11 Processing new Failures 904 24.12 Class auto_ptr and Dynamic Memory Allocation 907 24.13 Standard Library Exception Hierarchy 909 24.14 Other Error-Handling Techniques 911 24.15 Wrap-Up 912

A Operator Precedence Charts 919

B ASCII Character Set 923

C Number Systems 924 C.1 Introduction 925 C.2 Abbreviating Binary Numbers as Octal and Hexadecimal Numbers 928 C.3 Converting Octal and Hexadecimal Numbers to Binary Numbers 929 C.4 Converting from Binary, Octal or Hexadecimal to Decimal 929 C.5 Converting from Decimal to Binary, Octal or Hexadecimal 930 C.6 Negative Binary Numbers: Two’s Complement Notation 932

D Game Programming: Solving Sudoku 937 D.1 Introduction 937 D.2 Deitel Sudoku Resource Center 938 D.3 Solution Strategies 938

xviii Contents

D.4 Programming Sudoku Puzzle Solvers 942 D.5 Generating New Sudoku Puzzles 943 D.6 Conclusion 945

Appendices on the Web 946 Appendices E through I are PDF documents posted online at the book’s Companion Website (located at www.pearsonhighered.com/deitel).

E Game Programming with the Allegro C Library I E.1 Introduction II E.2 Installing Allegro II E.3 A Simple Allegro Program III E.4 Simple Graphics: Importing Bitmaps and Blitting IV E.5 Animation with Double Buffering IX E.6 Importing and Playing Sounds XVI E.7 Keyboard Input XX E.8 Fonts and Displaying Text XXV E.9 Implementing the Game of Pong XXXI E.10 Timers in Allegro XXXVII E.11 The Grabber and Allegro Datafiles XLII E.12 Other Allegro Capabilities LI E.13 Allegro Resource Center LII

F Sorting: A Deeper Look LVIII F.1 Introduction LIX F.2 Big O Notation LIX F.3 Selection Sort LX F.4 Insertion Sort LXIV F.5 Merge Sort LXVII

G Introduction to C99 LXXVIII G.1 Introduction LXXIX G.2 Support for C99 LXXIX G.3 New C99 Headers LXXX G.4 // Comments LXXX G.5 Mixing Declarations and Executable Code LXXXI G.6 Declaring a Variable in a for Statement Header LXXXII G.7 Designated Initializers and Compound Literals LXXXIV G.8 Type bool LXXXVII G.9 Implicit int in Function Declarations LXXXVIII G.10 Complex Numbers LXXXIX G.11 Variable-Length Arrays XC

Contents xix

G.12 The snprintf Function: Helping Avoid Hacker Attacks XCIII G.13 Additions to the Preprocessor XCV G.14 Other C99 Features XCVI G.15 Web Resources XCIX

H Using the Visual Studio Debugger CIV H.1 Introduction CV H.2 Breakpoints and the Continue Command CV H.3 Locals and Watch Windows CIX H.4 Controlling Execution Using the Step Into, Step Over, Step Out

and Continue Commands CXII H.5 Autos Window CXIV H.6 Wrap-Up CXVI

I Using the GNU Debugger CXVIII I.1 Introduction CXIX I.2 Breakpoints and the run, stop, continue and print Commands CXIX I.3 print and set Commands CXXIV I.4 Controlling Execution Using the step, finish and next Commands CXXVI I.5 watch Command CXXVIII I.6 Wrap-Up CXXX

Index 947

This page intentionally left blank

Welcome to the C programming language—and to C++, too! This book presents leading- edge computing technologies for students, instructors and software development profes- sionals.

At the heart of the book is the Deitel signature “live-code approach.” Concepts are presented in the context of complete working programs, rather than in code snippets. Each code example is immediately followed by one or more sample executions. All the source code is available at www.deitel.com/books/chtp6/.

We believe that this book and its support materials will give you an informative, inter- esting, challenging and entertaining introduction to C.

As you read the book, if you have questions, send an e-mail to [email protected]; we’ll respond promptly. For updates on this book and its supporting C and C++ software, and for the latest news on all Deitel publications and services, visit www.deitel.com.

New and Updated Features Here are the updates we’ve made for C How to Program, 6/e:

• “Making a Difference” Exercises Set. We encourage you to use computers and the Internet to research and solve problems that really matter. These new exercises are meant to increase awareness of important issues the world is facing. We hope you’ll approach them with your own values, politics and beliefs.

• Tested All Code on Windows and Linux. We’ve tested every program (the exam- ples and the exercises) using both Visual C++ 2008 and GNU GCC 4.3. The code examples and exercise code solutions were also tested using Visual Studio 2010 Beta.

• New Design. The book has a new interior design that graphically serves to orga- nize, clarify and highlight the information, and enhances the book’s pedagogy.

• Improved Terminology Sections. We’ve added page numbers for the defining oc- currences of all terms in the terminology lists for easy reference.

• Updated Coverage of C++ and Object-Oriented Programming. We updated Chapters 15–24 on object-oriented programming in C++ with material from our just published C++ How to Program, 7/e.

• Titled Programming Exercises. We’ve titled all the programming exercises. This helps instructors tune assignments for their classes.

• New Web Appendices. Chapters 15–17 from the previous edition are now search- able PDF Appendices E–G, available on the Companion Website (see the access card at the front of the book).

Preface

xxii Preface

• New Debugger Appendices. We also added new debugging appendices for Visual C++® 2008 and GNU gdb.

• Order of Evaluation. We added cautions about order of evaluation issues.

• We replaced all uses of gets (from <stdio.h>) with fgets, because gets is now deprecated.

• Additional Exercises. We added more function pointer exercises. We also added the Fibonacci exercise project that improves the Fibonacci recursion example (tail recursion).

• Secure C Programming Resource Center. We’ve posted a new Secure C Program- ming Resource Center at www.deitel.com/SecureC/. We’ve also added notes about secure C programming to the introductions in Chapter 7, Pointers, and Chapter 8, Strings.

• Game Programming with Allegro. We updated the chapter on game program- ming with the Allegro C library. In particular, we added instructions on installing the Allegro libraries for use with Visual C++® 2008 and GNU GCC 4.3.

• Coverage of the C99 Standard. We updated and enhanced the detailed appendix on C99, which was reviewed by John Benito, Convener of ISO WG14—the Working Group responsible for the C Programming Language Standard. Each C99 concept is now keyed to the section where it can be taught earlier in the book. C99 is not incorporated throughout the book because Microsoft does not yet support it and a large percentage of C courses use Microsoft's Visual C++®

compiler. For additional information, check out the C99 Standard section in our C Resource center at www.deitel.com/C/. You'll find features of C99, articles from experts, the differences between Standard C and C99, FAQs, downloads and more.

• C++-Style // Comments. We discuss C++-style // comments early for instructors and students who’d prefer to use them. Although Microsoft C does not yet sup- port C99, it does support C99’s comments, which are borrowed from C++.

• C Standard Library. Section 1.8 now references P.J. Plauger’s Dinkumware web- site (www.dinkumware.com/manuals/default.aspx) where students can find thorough searchable documentation for the C Standard Library functions.

Other Features Other features of C How to Program, 6/e, include:

Game Programming with the Allegro C Game Programming Library Appendix E introduces the Allegro game programming C library. This library—originally developed by Climax game programmer Shawn Hargreaves—was created to be a powerful tool for programming games in C while still remaining relatively simple compared to oth- er, more complicated graphics libraries such as DirectX and OpenGL. In Appendix E, we use Allegro’s capabilities to create the simple game of Pong. Along the way, we demon- strate how to display graphics, play sounds, receive input from the keyboard and create timed events—features you can use to create games of your own. Students and instructors

Web-Based Materials xxiii

alike will find Allegro challenging and entertaining. We include extensive web resources in our Allegro Resource Center (www.deitel.com/allegro), one of which offers more than 1000 open-source Allegro games.

Sorting: A Deeper Look Sorting places data in order, based on one or more sort keys. We begin our presentation of sorting with a simple algorithm in Chapter 6. In Appendix F, we present a deeper look at sorting. We consider several algorithms and compare them with regard to their memory consumption and processor demands. For this purpose, we introduce Big O notation, which indicates how hard an algorithm may have to work to solve a problem. Through examples and exercises, Appendix F discusses the selection sort, insertion sort, recursive merge sort, recursive selection sort, bucket sort and recursive Quicksort.

Web-Based Materials This book is supported by substantial online materials. The book’s Companion Website (www.pearsonhighered.com/deitel; see the access card at the front of the book) contains the following appendices in searchable PDF format:

• Appendix E, Game Programming with the Allegro C Library

• Appendix F, Sorting: A Deeper Look

• Appendix G, Introduction to C99

• Appendix H, Using the Visual Studio Debugger

• Appendix I, Using the GNU Debugger

Dependency Charts The dependency charts in Figs. 1–2 show the dependencies among the chapters to help instructors plan their syllabi. C How to Program, 6/e is appropriate for CS1 and CS2 cours- es, and intermediate-level C and C++ programming courses. The C++ part of the book assumes that you have studied the C part.

Teaching Approach C How to Program, 6/e, contains a rich collection of examples. We concentrate on demon- strating the principles of good software engineering and stressing program clarity.

Live-Code Approach. C How to Program, 6/e, is loaded with “live-code” examples. Most new concepts are presented in the context of complete working C applications, followed by one or more executions showing program inputs and outputs.

Syntax Shading For readability, we syntax shade the code, similar to the way most integrated-development environments and code editors syntax color code. Our syntax-shading conventions are:

comments appear like this keywords appear like this constants and literal values appear like this all other code appears in black

xxiv Preface

Code Highlighting We place gray rectangles around the key code.

Using Fonts for Emphasis. We place the key terms and the index’s page reference for each defining occurrence in bold blue text for easy reference. We emphasize on-screen compo- nents in the bold Helvetica font (e.g., the File menu) and C program text in the Lucida font (for example, int x = 5;).

Web Access. All of the source-code examples are available for download from:

Fig. 1 | C chapter dependency chart.

www.deitel.com/books/chtp6/

Introduction 1 Introduction to Computers,

the Internet and the Web

Intro to Programming 2 Intro to C Programming

Control Statements, Functions and Arrays

3 Structured Program Development in C

4 C Program Control

5 C Functions

6 C Arrays

Pointers and Strings

8 C Characters and Strings

7 C Pointers

5.14–5.16 Recursion

12 C Data Structures

F Sorting: A Deeper Look

Data Structures

Other Topics, Game Programming and C99

C Chapter Dependency Chart [Note: Arrows pointing into a chapter indicate that chapter’s dependencies.]

G Introduction to C99

E Game Programming with the Allegro C Library

10 C Structures, Unions, Bit Manipulations and Enumerations

Aggregate Types

Streams and Files

11 C File Processing

9 C Formatted Input/Output

13 C Preprocessor

14 Other C Topics

Teaching Approach xxv

Quotations. Each chapter begins with quotations. We hope that you enjoy relating these to the chapter material.

Objectives. The quotes are followed by a list of chapter objectives.

Illustrations/Figures. Abundant charts, tables, line drawings, UML diagrams, programs and program output are included.

Programming Tips We include programming tips to help you focus on important aspects of program devel- opment. These tips and practices represent the best we’ve gleaned from a combined seven decades of programming and teaching experience.

Fig. 2 | C++ chapter dependency chart.

Good Programming Practice The Good Programming Practices call attention to techniques that will help you pro- duce programs that are clearer, more understandable and more maintainable.

Common Programming Error Pointing out these Common Programming Errors reduces the likelihood that you’ll make them.

Error-Prevention Tip These tips contain suggestions for exposing and removing bugs from your programs; many describe aspects of C that prevent bugs from getting into programs in the first place.

Object-Based Programming

C++ Chapter Dependency Chart

18 Classes: A Deeper Look, Part 2

17 Classes: A Deeper Look, Part 1

19 Operator Overloading

Object-Oriented Programming

23 Stream Input/Output

20 OOP: Inheritance

22 Templates21 OOP: Polymorphism

24 Exception Handling

15 C++ as a Better C; Intro to Object Technology

16 Intro to Classes and Objects

[Note: Arrows pointing into a chapter indicate that chapter’s dependencies.]

xxvi Preface

Summary Bullets. We present a section-by-section, bullet-list summary of the chapter.

Terminology. We include an alphabetized list of the important terms defined in each chap- ter with the page number of each term’s defining occurrence for easy reference.

Self-Review Exercises and Answers. Extensive self-review exercises and answers are includ- ed for self-study.

Exercises. Each chapter concludes with a substantial set of exercises including:

• simple recall of important terminology and concepts,

• identifying the errors in code samples,

• writing individual C statements,

• writing small portions of functions and classes,

• writing complete C functions, classes and programs, and

• major projects.

Instructors can use these exercises to form homework assignments, short quizzes, major ex- aminations and term projects. [NOTE: Please do not write to us requesting access to the Pearson Instructor’s Resource Center which contains the book’s instructor supple- ments, including the exercise solutions. Access is limited strictly to college instructors teaching from the book. Instructors may obtain access only through their Pearson rep- resentatives. Solutions are not provided for “project” exercises.] Check out our Program- ming Projects Resource Center (www.deitel.com/ProgrammingProjects/) for lots of additional exercise and project possibilities.

Index. We’ve included an extensive index, which is especially useful when you use the book as a reference. Defining occurrences of key terms are highlighted with a bold blue page number.

Student Resources Many C and C++ development tools are available. We wrote C How to Program, 6/e primar- ily using Microsoft’s free Visual C++® Express Edition (which is available free for download at www.microsoft.com/express/vc/) and the free GNU C++ (gcc.gnu.org/install/ binaries.html), which is already installed on most Linux systems and can be installed on

Performance Tip These tips highlight opportunities for making your programs run faster or minimizing the amount of memory that they occupy.

Portability Tip The Portability Tips help you write code that will run on a variety of platforms.

Software Engineering Observation The Software Engineering Observations highlight architectural and design issues that affect the construction of software systems, especially large-scale systems.

CourseSmart Web Books xxvii

Mac OS X and Windows systems as well. You can learn more about Visual C++® Express at msdn.microsoft.com/vstudio/express/visualc. You can learn more about GNU C++ at gcc.gnu.org. Apple includes GNU C++ in their Xcode development tools, which Max OS X users can download from developer.apple.com/tools/xcode.

You can download the book’s examples and additional resources from:

For additional resources and software downloads see our C Resource Center:

For other C and C++ compilers that are available free for download:

CourseSmart Web Books Today’s students and instructors have increasing demands on their time and money. Pear- son has responded to that need by offering digital texts and course materials online through CourseSmart. CourseSmart allows faculty to review course materials online sav- ing time and costs. It is also environmentally sound and offers students a high-quality dig- ital version of the text for as much as 50% off the cost of a print copy of the text. Students receive the same content offered in the print textbook enhanced by search, note-taking, and printing tools. For more information, visit www.coursesmart.com.

Software for the Book This book includes the Microsoft® Visual Studio® 2008 Express Editions All-in-One DVD, which contains the Visual C++® 2008 Express Edition (and other Microsoft devel- opment tools). You can also download the latest version of Visual C++ Express Edition from:

Per Microsoft’s website, Express Editions are “lightweight, easy-to-use and easy-to-learn tools for the hobbyist, novice and student developer.” They are appropriate for academic courses and for professionals who do not have access to a complete version of Visual Studio 2008.

With the exception of one example in Chapter 9, C Formatted Input/Output, and the examples in Appendix G, Introduction to C99, all of the examples in this book com- pile and run in Visual C++® 2008 and the beta version of Visual C++® 2010. All of the examples compile and run in GNU GCC 4.3. GCC is available for most platforms, including Linux, Mac OS X (via Xcode) and Windows—via tools like Cygwin (www.cygwin.com) and MinGW (www.mingw.org).

Instructor Supplements The following supplements are available to qualified instructors only through Pearson Education’s Instructor Resource Center (www.pearsonhighered.com/irc):

• Solutions Manual with solutions to most of the end-of-chapter exercises.

www.deitel.com/books/chtp6/

www.deitel.com/c/

www.thefreecountry.com/developercity/ccompilers.shtml www.compilers.net/Dir/Compilers/CCpp.htm

www.microsoft.com/express/vc

xxviii Preface

• Test Item File of multiple-choice questions (approximately two per book section)

• Customizable PowerPoint® slides containing all the code and figures in the text, plus bulleted items that summarize key points in the text

If you are not already a registered faculty member, contact your Pearson representative or visit www.pearsonhighered.com/educator/replocator/.

Deitel® Buzz Online Free E-mail Newsletter The Deitel® Buzz Online e-mail newsletter will keep you posted about issues related to C How to Program, 6/e. It also includes commentary on industry trends and developments, links to free articles and resources from our published books and upcoming publications, product-release schedules, errata, challenges, anecdotes, information on our corporate in- structor-led training courses and more. To subscribe, visit

The Deitel Online Resource Centers Our website www.deitel.com provides more than 100 Resource Centers on various topics including programming languages, software development, Web 2.0, Internet business and open-source projects—see the list of Resource Centers in the first few pages of this book and visit www.deitel.com/ResourceCenters.html. We’ve found many exceptional re- sources online, including tutorials, documentation, software downloads, articles, blogs, podcasts, videos, code samples, books, e-books and more—most of them are free. Each week we announce our latest Resource Centers in our newsletter, the Deitel® Buzz Online. Some of the Resource Centers you might find helpful while studying this book are C, C++, C++ Boost Libraries, C++ Game Programming, Visual C++, UML, Code Search Engines and Code Sites, Game Programming and Programming Projects.

Follow Deitel on Twitter and Facebook To receive updates on Deitel publications, Resource Centers, training courses, partner offers and more, follow us on Twitter®

and join the Deitel & Associates group on Facebook®

Acknowledgments It’s a pleasure to acknowledge the efforts of people whose names do not appear on the cov- er, but whose hard work, cooperation, friendship and understanding were crucial to the book’s production. Many people at Deitel & Associates, Inc., devoted long hours to this project—thanks especially to Abbey Deitel and Barbara Deitel.

We would also like to thank the participants of our Honors Internship program who contributed to this publication—Christine Chen, an Operations Research and Informa- tion Engineering major at Cornell University; and Matthew Pearson, a Computer Science graduate of Cornell University.

www.deitel.com/newsletter/subscribe.html

@deitel

www.deitel.com/deitelfan/

Acknowledgments xxix

We are fortunate to have worked on this project with the dedicated team of publishing professionals at Pearson. We appreciate the efforts of Marcia Horton, Editorial Director of Pearson’s Engineering and Computer Science Division, and Michael Hirsch, Editor-in- Chief of Computer Science. Carole Snyder recruited the book’s review team and managed the review process. Francesco Santalucia (an independent artist) and Kristine Carney of Pearson designed the book’s cover—we provided the concept, and they made it happen. Scott Disanno and Bob Engelhardt managed the book’s production. Erin Davis and Mar- garet Waples marketed the book through academic and professional channels.

C How to Program, 6/e Reviewers We wish to acknowledge the efforts of our reviewers. Adhering to a tight time schedule, they scrutinized the text and the programs and provided countless suggestions for improv- ing the accuracy and completeness of the presentation:

• John Benito, Blue Pilot Consulting, Inc. and Convener of ISO WG14—the Working Group responsible for the C Programming Language Standard.

• Xiaolong Li, Indiana State University

• Tom Rethard, The University of Texas at Arlington

C How to Program, 5/e Reviewers • Alireza Fazelpour (Palm Beach Community College)

• Don Kostuch (Independent Consultant)

• Ed James Beckham (Altera)

• Gary Sibbitts (St. Louis Community College at Meramec)

• Ian Barland (Radford University)

• Kevin Mark Jones (Hewlett Packard)

• Mahesh Hariharan (Microsoft)

• William Mike Miller (Edison Design Group, Inc.)

• Benjamin Seyfarth (Univeristy of Southern Mississippi)

• William Albrecht (University of South Florida)

• William Smith (Tulsa Community College)

Allegro Reviewers for C How to Program, 5/e • Shawn Hargreaves (Software Design Engineer, Microsoft Xbox)

• Matthew Leverton (Founder and Webmaster of Allegro.cc)

• Ryan Patterson (Independent Consultant)

• Douglas Walls (Senior Staff Engineer, C compiler, Sun Microsystems)

C99 Reviewers for C How to Program, 5/e • Lawrence Jones, (UGS Corp.)

• Douglas Walls (Senior Staff Engineer, C compiler, Sun Microsystems)

xxx Preface

Well, there you have it! C is a powerful programming language that will help you write programs quickly and effectively. C scales nicely into the realm of enterprise systems development to help organizations build their business-critical and mission-critical infor- mation systems. As you read the book, we would sincerely appreciate your comments, crit- icisms, corrections and suggestions for improving the text. Please address all correspondence to:

We’ll respond promptly, and post corrections and clarifications on:

We hope you enjoy working with C How to Program, Sixth Edition as much as we enjoyed writing it!

Paul Deitel Harvey Deitel Maynard, Massachusetts August 2009

About the Authors Paul J. Deitel, CEO and Chief Technical Officer of Deitel & Associates, Inc., is a graduate of MIT’s Sloan School of Management, where he studied Information Technology. Through Deitel & Associates, Inc., he has delivered C, C++, Java, C#, Visual Basic and Internet programming courses to industry clients, including Cisco, IBM, Sun Microsys- tems, Dell, Lucent Technologies, Fidelity, NASA at the Kennedy Space Center, the Na- tional Severe Storm Laboratory, White Sands Missile Range, Rogue Wave Software, Boeing, SunGard Higher Education, Stratus, Cambridge Technology Partners, Open En- vironment Corporation, One Wave, Hyperion Software, Adra Systems, Entergy, Cable- Data Systems, Nortel Networks, Puma, iRobot, Invensys and many more. He holds the Java Certified Programmer and Java Certified Developer certifications and has been des- ignated by Sun Microsystems as a Java Champion. He has also lectured on Java and C++ for the Boston Chapter of the Association for Computing Machinery. He and his co- author, Dr. Harvey M. Deitel, are the world’s best-selling programming-language text- book authors.

Dr. Harvey M. Deitel, Chairman and Chief Strategy Officer of Deitel & Associates, Inc., has 48 years of academic and industry experience in the computer field. Dr. Deitel earned B.S. and M.S. degrees from MIT and a Ph.D. from Boston University. He has extensive college teaching experience, including earning tenure and serving as the Chairman of the Computer Science Department at Boston College before founding Deitel & Associates, Inc., with his son, Paul J. Deitel. He and Paul are the co-authors of dozens of books and multimedia packages and they are writing many more. With transla- tions published in Japanese, German, Russian, Traditional Chinese, Simplified Chinese, Spanish, Korean, French, Polish, Italian, Portuguese, Greek, Urdu and Turkish, the Dei- tels’ texts have earned international recognition. Dr. Deitel has delivered hundreds of pro- fessional seminars to major corporations, academic institutions, government organizations and the military.

[email protected]

www.deitel.com/books/chtp6/

About Deitel & Associates, Inc. xxxi

About Deitel & Associates, Inc. Deitel & Associates, Inc., is an internationally recognized authoring and corporate train- ing organization specializing in computer programming languages, Internet and web soft- ware technology, object-technology education and iPhone applications development. The company provides instructor-led courses delivered at client sites worldwide on major programming languages and platforms, such as C, C++, Visual C++®, Java™, Visual C#®, Visual Basic®, XML®, Python®, object technology, Internet and web programming, iP- hone programming and a growing list of additional programming and software-develop- ment-related courses. The founders of Deitel & Associates, Inc., are Paul J. Deitel and Dr. Harvey M. Deitel. The company’s clients include many of the world’s largest companies, government agencies, branches of the military, and academic institutions. Through its 33- year publishing partnership with Prentice Hall/Pearson, Deitel & Associates, Inc., pub- lishes leading-edge programming textbooks, professional books, interactive multimedia Cyber Classrooms, LiveLessons video courses (online at www.safaribooksonline.com and on DVD at www.deitel.com/books/livelessons/), and e-content for popular course- management systems.

Deitel & Associates, Inc., and the authors can be reached via e-mail at:

To learn more about Deitel & Associates, Inc., its publications and its Dive Into®

Series Corporate Training curriculum delivered at client locations worldwide, visit:

and subscribe to the free Deitel® Buzz Online e-mail newsletter at:

Individuals wishing to purchase Deitel books, and LiveLessons DVD and web-based training courses can do so through www.deitel.com. Bulk orders by corporations, the gov- ernment, the military and academic institutions should be placed directly with Pearson. For more information, visit www.prenhall.com/mischtm/support.html#order.

[email protected]

www.deitel.com/training/

www.deitel.com/newsletter/subscribe.html

This page intentionally left blank

1Introduction to Computers, the Internet and the Web The chief merit of language is clearness. —Galen

Our life is frittered away by detail. … Simplify, simplify. —Henry David Thoreau

He had a wonderful talent for packing thought close, and rendering it portable. —Thomas B. Macaulay

Man is still the most extraordinary computer of all. —John F. Kennedy

O b j e c t i v e s In this chapter, you’ll learn:

■ Basic computer concepts.

■ The different types of programming languages.

■ The history of the C programming language.

■ The purpose of the C Standard Library.

■ The elements of a typical C program development environment.

■ How C provides a foundation for further study of programming languages in general and of C++, Java and C# in particular.

■ The history of the Internet and the World Wide Web.

2 Chapter 1 Introduction to Computers, the Internet and the Web

1.1 Introduction Welcome to C and C++! We’ve worked hard to create what we hope you’ll find to be an informative, entertaining and challenging learning experience. C is a powerful computer programming language that is appropriate for technically oriented people with little or no programming experience and for experienced programmers to use in building substantial information systems. C How to Program, Sixth Edition, is an effective learning tool for each of these audiences.

The core of the book emphasizes achieving program clarity through the proven tech- niques of structured programming. You’ll learn programming the right way from the beginning. We’ve attempted to write in a clear and straightforward manner. The book is abundantly illustrated. Perhaps most important, the book presents hundreds of complete working programs and shows the outputs produced when those programs are run on a computer. We call this the “live-code approach.” All of these example programs may be downloaded from our website www.deitel.com/books/chtp6/.

Most people are familiar with the exciting tasks computers perform. Using this text- book, you’ll learn how to command computers to perform those tasks. It’s software (i.e., the instructions you write to command computers to perform actions and make decisions) that controls computers (often referred to as hardware). This text introduces program- ming in C, which was standardized in 1989 as ANSI X3.159-1989 in the United States through the American National Standards Institute (ANSI), then worldwide through the efforts of the International Standards Organization (ISO). We call this Standard C. We also introduce C99 (ISO/IEC 9899:1999)—the latest version of the C standard. C99 has not yet been universally adopted, so we chose to discuss it in (optional) Appendix G. A new C standard, which has been informally named C1X, is under development and likely to be published around 2012.

Optional Appendix E presents the Allegro game programming C library. The appendix shows how to use Allegro to create a simple game. We show how to display graphics and smoothly animate objects, and we explain additional features such as sound,

1.1 Introduction 1.2 Computers: Hardware and Software 1.3 Computer Organization 1.4 Personal, Distributed and Client/

Server Computing 1.5 The Internet and the World Wide

Web 1.6 Machine Languages, Assembly

Languages and High-Level Languages 1.7 History of C 1.8 C Standard Library 1.9 C++

1.10 Java 1.11 Fortran, COBOL, Pascal and Ada 1.12 BASIC, Visual Basic, Visual C++, C#

and .NET 1.13 Key Software Trend: Object

Technology 1.14 Typical C Program Development

Environment 1.15 Hardware Trends 1.16 Notes About C and This Book 1.17 Web Resources

Summary |Terminology | Self-Review Exercises | Answers to Self-Review Exercises | Exercises | Making a Difference

1.2 Computers: Hardware and Software 3

keyboard input and text output. The appendix includes web links and resources that point you to over 1000 Allegro games and to tutorials on advanced Allegro techniques.

Computer use is increasing in most fields of endeavor. Computing costs have decreased dramatically due to rapid developments in both hardware and software technologies. Com- puters that might have filled large rooms and cost millions of dollars a few decades ago can now be inscribed on silicon chips smaller than a fingernail, costing a few dollars each. Those large computers were called mainframes and current versions are widely used today in busi- ness, government and industry. Fortunately, silicon is one of the most abundant materials on earth—it’s an ingredient in common sand. Silicon chip technology has made computing so economical that more than a billion general-purpose computers are in use worldwide, helping people in business, industry and government, and in their personal lives. Billions more special purpose computers are used in intelligent electronic devices like car navigation systems, energy-saving appliances and game controllers.

C++, an object-oriented programming language based on C, is of such interest today that we’ve included a detailed introduction to C++ and object-oriented programming in Chapters 15–24. In the programming languages marketplace, many key vendors market a combined C/C++ product rather than offering separate products. This enables users to con- tinue programming in C if they wish, then gradually migrate to C++ when it’s appropriate.

To keep up to date with C and C++ developments at Deitel & Associates, register for our free e-mail newsletter, the Deitel® Buzz Online, at

Check out our growing list of C and related Resource Centers at

Some Resource Centers that will be valuable to you as you read the C portion of this book are C, Code Search Engines and Code Sites, Computer Game Programming and Pro- gramming Projects. Each week we announce our latest Resource Centers in the newsletter. Errata and updates for this book are posted at

You’re embarking on a challenging and rewarding path. As you proceed, if you have any questions, send e-mail to

We’ll respond promptly. We hope that you’ll enjoy C How to Program, Sixth Edition.

1.2 Computers: Hardware and Software A computer is a device that can perform computations and make logical decisions billions of times faster than human beings can. For example, many of today’s personal computers can perform several billion additions per second. A person operating a desk calculator could spend an entire lifetime performing calculations and still not complete as many cal- culations as a powerful personal computer can perform in one second! (Points to ponder: How would you know whether the person added the numbers correctly? How would you know whether the computer added the numbers correctly?) Today’s fastest supercomput- ers can perform thousands of trillions (quadrillions) of instructions per second! To put that

www.deitel.com/newsletter/subscribe.html

www.deitel.com/ResourceCenters.html

www.deitel.com/books/chtp6/

[email protected]

4 Chapter 1 Introduction to Computers, the Internet and the Web

in perspective, a quadrillion-instruction-per-second computer can perform more than 100,000 calculations per second for every person on the planet!

Computers process data under the control of sets of instructions called computer pro- grams. These programs guide the computer through orderly sets of actions specified by people called computer programmers.

A computer consists of various devices referred to as hardware (e.g., the keyboard, screen, mouse, hard disk, memory, DVDs and processing units). The programs that run on a computer are referred to as software. Hardware costs have been declining dramatically in recent years, to the point that personal computers have become a commodity. In this book, you’ll learn proven methods that are reducing software development costs—structured pro- gramming (in the C chapters) and object-oriented programming (in the C++ chapters).

1.3 Computer Organization Regardless of differences in physical appearance, virtually every computer may be envi- sioned as divided into six logical units or sections:

1. Input unit. This “receiving” section obtains information (data and computer programs) from input devices and places it at the disposal of the other units so that it can be processed. Humans typically enter information into computers through keyboards and mouse devices. Information also can be entered in many other ways, including by speaking to your computer, scanning images and bar- codes, reading from secondary storage devices (like hard drives, CD drives, DVD drives and USB drives—also called “thumb drives”) and having your computer receive information from the Internet (such as when you download videos from YouTube™, e-books from Amazon and the like).

2. Output unit. This “shipping” section takes information that the computer has processed and places it on various output devices to make it available for use out- side the computer. Most information that is output from computers today is dis- played on screens, printed on paper, played on audio players (such as Apple’s popular iPods), or used to control other devices. Computers also can output their information to networks, such as the Internet.

3. Memory unit. This rapid-access, relatively low-capacity “warehouse” section re- tains information that has been entered through the input unit, making it imme- diately available for processing when needed. The memory unit also retains processed information until it can be placed on output devices by the output unit. Information in the memory unit is volatile—it’s typically lost when the comput- er’s power is turned off. The memory unit is often called either memory or pri- mary memory.

4. Arithmetic and logic unit (ALU). This “manufacturing” section performs calcu- lations, such as addition, subtraction, multiplication and division. It also contains the decision mechanisms that allow the computer, for example, to compare two items from the memory unit to determine whether they’re equal. In today’s sys- tems, the ALU is usually implemented as part of the next logical unit, the CPU.

5. Central processing unit (CPU). This “administrative” section coordinates and su- pervises the operation of the other sections. The CPU tells the input unit when to

1.4 Personal, Distributed and Client/Server Computing 5

read information into the memory unit, tells the ALU when information from the memory unit should be used in calculations and tells the output unit when to send information from the memory unit to certain output devices. Many of today’s computers have multiple CPUs and, hence, can perform many operations simul- taneously—such computers are called multiprocessors. A multi-core processor implements multiprocessing on a single integrated circuit chip—for example a dual-core processor has two CPUs and a quad-core processor has four CPUs.

6. Secondary storage unit. This is the long-term, high-capacity “warehousing” sec- tion. Programs or data not actively being used by the other units normally are placed on secondary storage devices (e.g., your hard drive) until they’re again needed, possibly hours, days, months or even years later. Therefore, information on secondary storage devices is said to be persistent—it is preserved even when the computer’s power is turned off. Secondary storage information takes much longer to access than information in primary memory, but the cost per unit of secondary storage is much less than that of primary memory. Examples of second- ary storage devices include CDs, DVDs and flash drives (sometimes called mem- ory sticks), which can hold hundreds of millions to billions of characters.

1.4 Personal, Distributed and Client/Server Computing In 1977, Apple Computer popularized personal computing. Computers became so eco- nomical that people could buy them for their own personal or business use. In 1981, IBM, the world’s largest computer vendor, introduced the IBM Personal Computer (PC). This quickly legitimized personal computing in business, industry and government organiza- tions, where IBM mainframes were heavily used.

These computers were “stand-alone” units—people transported disks back and forth between them to share information (this was often called “sneakernet”). These machines could be linked together in computer networks, sometimes over telephone lines and some- times in local area networks (LANs) within an organization. This led to the phenomenon of distributed computing, in which an organization’s computing, instead of being per- formed only at some central computer installation, is distributed over networks to the sites where the organization’s work is performed. Personal computers were powerful enough to handle the computing requirements of individual users as well as the basic communica- tions tasks of passing information between computers electronically.

Today’s personal computers are as powerful as the million-dollar machines of just a few decades ago. Information is shared easily across computer networks, where computers called servers (file servers, database servers, web servers, etc.) offer capabilities that may be used by client computers distributed throughout the network, hence the term client/ server computing. C is widely used for writing software for operating systems, for com- puter networking and for distributed client/server applications. Today’s popular operating systems such as UNIX, Linux, Mac OS X and Microsoft’s Windows-based systems pro- vide the kinds of capabilities discussed in this section.

1.5 The Internet and the World Wide Web The Internet—a global network of computers—was initiated in the late 1960s with fund- ing supplied by the U.S. Department of Defense. Originally designed to connect the main

6 Chapter 1 Introduction to Computers, the Internet and the Web

computer systems of about a dozen universities and research organizations, the Internet today is accessible by computers worldwide.

With the introduction of the World Wide Web—which allows computer users to locate and view multimedia-based documents on almost any subject over the Internet— the Internet has exploded into the world’s premier communication mechanism.

The Internet and the World Wide Web are surely among humankind’s most impor- tant and profound creations. In the past, most computer applications ran on computers that were not connected to one another. Today’s applications can be written to commu- nicate among the world’s computers. The Internet mixes computing and communications technologies. It makes our work easier. It makes information instantly and conveniently accessible worldwide. It enables individuals and local small businesses to get worldwide exposure. It’s changing the way business is done. People can search for the best prices on virtually any product or service. Special-interest communities can stay in touch with one another. Researchers can be made instantly aware of the latest breakthroughs.

1.6 Machine Languages, Assembly Languages and High- Level Languages Programmers write instructions in various programming languages, some directly under- standable by computers and others requiring intermediate translation steps. Hundreds of computer languages are in use today. These may be divided into three general types:

1. Machine languages

2. Assembly languages

3. High-level languages

Any computer can directly understand only its own machine language. Machine lan- guage is the “natural language” of a computer and as such is defined by its hardware design. [Note: Machine language is often referred to as object code. This term predates “object- oriented programming.” These two uses of “object” are unrelated.] Machine languages generally consist of strings of numbers (ultimately reduced to 1s and 0s) that instruct com- puters to perform their most elementary operations one at a time. Machine languages are machine dependent (i.e., a particular machine language can be used on only one type of computer). Such languages are cumbersome for humans, as illustrated by the following section of an early machine-language program that adds overtime pay to base pay and stores the result in gross pay:

Machine-language programming was simply too slow, tedious and error prone for most programmers. Instead of using the strings of numbers that computers could directly understand, programmers began using English-like abbreviations to represent elementary operations. These abbreviations formed the basis of assembly languages. Translator pro- grams called assemblers were developed to convert early assembly-language programs to machine language at computer speeds. The following section of an assembly-language pro- gram also adds overtime pay to base pay and stores the result in gross pay:

+1300042774 +1400593419 +1200274027

1.7 History of C 7

Although such code is clearer to humans, it’s incomprehensible to computers until trans- lated to machine language.

Computer usage increased rapidly with the advent of assembly languages, but pro- grammers still had to use many instructions to accomplish even the simplest tasks. To speed the programming process, high-level languages were developed in which single statements could be written to accomplish substantial tasks. Translator programs called compilers convert high-level language programs into machine language. High-level lan- guages allow programmers to write instructions that look almost like everyday English and contain commonly used mathematical notations. A payroll program written in a high-level language might contain a statement such as

From your standpoint, obviously, high-level languages are preferable to machine and assembly language. C, C++, Microsoft’s .NET languages (e.g., Visual Basic, Visual C++ and Visual C#) and Java are among the most widely used high-level programming lan- guages.

The process of compiling a high-level language program into machine language can take a considerable amount of computer time. Interpreter programs were developed to execute high-level language programs directly (without the delay of compilation), although slower than compiled programs run.

1.7 History of C C evolved from two previous languages, BCPL and B. BCPL was developed in 1967 by Martin Richards as a language for writing operating-systems software and compilers. Ken Thompson modeled many features in his B language after their counterparts in BCPL, and in 1970 he used B to create early versions of the UNIX operating system at Bell Labora- tories. Both BCPL and B were “typeless” languages—every data item occupied one “word” in memory, and the burden of typing variables fell on the shoulders of the programmer.

The C language was evolved from B by Dennis Ritchie at Bell Laboratories and was originally implemented on a DEC PDP-11 computer in 1972. C uses many of the impor- tant concepts of BCPL and B while adding data typing and other powerful features. C ini- tially became widely known as the development language of the UNIX operating system. Today, virtually all new major operating systems are written in C and/or C++. C is avail- able for most computers. C is mostly hardware independent. With careful design, it’s pos- sible to write C programs that are portable to most computers.

By the late 1970s, C had evolved into what is now referred to as “traditional C.” The publication in 1978 of Kernighan and Ritchie’s book, The C Programming Language, drew wide attention to the language. This became one of the most successful computer science books of all time.

The rapid expansion of C over various types of computers (sometimes called hard- ware platforms) led to many variations that were similar but often incompatible. This was a serious problem for programmers who needed to develop code that would run on several platforms. It became clear that a standard version of C was needed. In 1983, the X3J11

load basepay add overpay store grosspay

grossPay = basePay + overTimePay;

8 Chapter 1 Introduction to Computers, the Internet and the Web

technical committee was created under the American National Standards Committee on Computers and Information Processing (X3) to “provide an unambiguous and machine- independent definition of the language.” In 1989, the standard was approved; this stan- dard was updated in 1999. The standards document is referred to as INCITS/ISO/IEC 9899-1999. Copies may be ordered from the American National Standards Institute (www.ansi.org) at webstore.ansi.org/ansidocstore.

C99 is a revised standard for the C programming language that refines and expands the capabilities of C. Not all popular C compilers support C99. Of those that do, most implement only a subset of the new features. Chapters 1–14 of this book are based on the widely adopted international Standard (ANSI/ISO) C. Appendix G introduces C99 and provides links to popular C99 compilers and IDEs.

1.8 C Standard Library As you’ll learn in Chapter 5, C programs consist of modules or pieces called functions. You can program all the functions you need to form a C program, but most C program- mers take advantage of a rich collection of existing functions called the C Standard Li- brary. Thus, there are really two pieces to learning how to program in C. The first is learning the C language itself, and the second is learning how to use the functions in the C Standard Library. Throughout the book, we discuss many of these functions. P.J. Plauger’s book The Standard C Library is must reading for programmers who need a deep understanding of the library functions, how to implement them and how to use them to write portable code. We use and explain many C library functions throughout this text. Visit the following website for the complete C Standard Library documentation, including the C99 features:

This textbook encourages a building-block approach to creating programs. Avoid reinventing the wheel. Instead, use existing pieces—this is called software reusability, and it’s a key to the field of object-oriented programming, as you’ll see in our treatment of C++ beginning in Chapter 15. When programming in C you’ll typically use the following building blocks:

• C Standard Library functions

• Functions you create yourself

• Functions other people have created and made available to you

The advantage of creating your own yfunctions is that you’ll know exactly how they work. You’ll be able to examine the C code. The disadvantage is the time-consuming effort that goes into designing, developing and debugging new functions.

If you use existing functions, you can avoid reinventing the wheel. In the case of the Standard C functions, you know that they’re carefully written, and you know that because you’re using functions that are available on virtually all Standard C implementations, your programs will have a greater chance of being portable and error-free.

Portability Tip 1.1 Because C is a hardware-independent, widely available language, applications written in C can run with little or no modifications on a wide range of different computer systems.

www.dinkumware.com/manuals/default.aspx#Standard%20C%20Library

1.9 C++ 9

1.9 C++ C++ was developed by Bjarne Stroustrup at Bell Laboratories. It has its roots in C, provid- ing a number of features that “spruce up” the C language. More important, it provides ca- pabilities for object-oriented programming. C++ has become a dominant language in both industry and the colleges.

Objects are essentially reusable software components that model items in the real world. Using a modular, object-oriented design and implementation approach can make software development groups much more productive than is possible with previous pro- gramming techniques.

Many people feel that the best educational strategy today is to master C, then study C++. Therefore, in Chapters 15–24 of C How to Program, 6/e, we present a condensed treatment of C++ selected from our book C++ How to Program, 7/e. As you study C++, check out our online C++ Resource Center at www.deitel.com/cplusplus/.

1.10 Java Microprocessors are having a profound impact in intelligent consumer electronic devices. Recognizing this, Sun Microsystems in 1991 funded an internal corporate research project code-named Green. The project resulted in the development of a C++-based language that its creator, James Gosling, called Oak after an oak tree outside his window at Sun. It was later discovered that there already was a computer language called Oak. When a group of Sun people visited a local coffee shop, the name Java was suggested and it stuck.

The Green project ran into some difficulties. The marketplace for intelligent con- sumer electronic devices did not develop in the early 1990s as quickly as Sun had antici- pated. The project was in danger of being canceled. By sheer good fortune, the World Wide Web exploded in popularity in 1993, and Sun saw the immediate potential of using Java to add dynamic content (e.g., interactivity, animations and the like) to web pages. This breathed new life into the project.

Sun formally announced Java at an industry conference in May 1995. Java garnered the attention of the business community because of the phenomenal interest in the World Wide Web. Java is now used to develop large-scale enterprise applications, to enhance the functionality of web servers (the computers that provide the content we see in our web browsers), to provide applications for consumer devices (e.g., cell phones, pagers and per- sonal digital assistants) and for many other purposes.

Performance Tip 1.1 Using Standard C library functions instead of writing your own comparable versions can improve program performance, because these functions are carefully written to perform ef- ficiently.

Performance Tip 1.2 Using Standard C library functions instead of writing your own comparable versions can improve program portability, because these functions are used in virtually all Standard C implementations.

10 Chapter 1 Introduction to Computers, the Internet and the Web

1.11 Fortran, COBOL, Pascal and Ada Hundreds of high-level languages have been developed, but few have achieved broad ac- ceptance. FORTRAN (FORmula TRANslator) was developed by IBM Corporation in the mid-1950s to be used for scientific and engineering applications that require complex mathematical computations. Fortran is still widely used in engineering applications.

COBOL (COmmon Business Oriented Language) was developed in the late 1950s by computer manufacturers, the U.S. government and industrial computer users. COBOL is used for commercial applications that require precise and efficient manipula- tion of large amounts of data. Much business software is still programmed in COBOL.

During the 1960s, many large software development efforts encountered severe diffi- culties. Software deliveries were often late, costs greatly exceeded budgets and the finished products were unreliable. People realized that software development was a more complex activity than they had imagined. Research in the 1960s resulted in the evolution of struc- tured programming—a disciplined approach to writing programs that are clearer and easier to test, debug and modify than large programs produced with previous techniques.

One of the more tangible results of this research was the development of the Pascal programming language by Professor Niklaus Wirth in 1971. Named after the seventeenth- century mathematician and philosopher Blaise Pascal, it was designed for teaching struc- tured programming and rapidly became the preferred programming language in most col- leges. Pascal lacked many features needed in commercial, industrial and government applications, so it was not widely accepted outside academia.

The Ada language was developed under the sponsorship of the U.S. Department of Defense (DoD) during the 1970s and early 1980s. Hundreds of separate languages were being used to produce the DoD’s massive command-and-control software systems. The DoD wanted one language that would fill most of its needs. The Ada language was named after Lady Ada Lovelace, daughter of the poet Lord Byron. Lady Lovelace is credited with writing the world’s first computer program in the early 1800s (for the Analytical Engine mechanical computing device designed by Charles Babbage). One important capability of Ada, called multitasking, allows programmers to specify that many activities are to occur in parallel. Although multithreading is not part of standard C, it’s available through var- ious add-on libraries.

1.12 BASIC, Visual Basic, Visual C++, C# and .NET The BASIC (Beginner’s All-purpose Symbolic Instruction Code) programming language was developed in the mid-1960s at Dartmouth College as a means of writing simple pro- grams. BASIC’s primary purpose was to familiarize novices with programming techniques. Microsoft’s Visual Basic language, introduced in the early 1990s to simplify the develop- ment of Microsoft Windows applications, has become one of the most popular program- ming languages in the world.

Microsoft’s latest development tools are part of its corporate-wide strategy for inte- grating the Internet and the web into computer applications. This strategy is implemented in Microsoft’s .NET platform, which provides the capabilities developers need to create and run computer applications that can execute on computers distributed across the Internet. Microsoft’s three primary programming languages are Visual Basic (based on the original BASIC), Visual C++ (based on C++) and Visual C# (a new language based on

1.13 Key Software Trend: Object Technology 11

C++ and Java that was developed expressly for the .NET platform). Visual C++ can also be used to compile and run C programs.

1.13 Key Software Trend: Object Technology One of the authors, Harvey Deitel, remembers the great frustration felt in the 1960s by software development organizations, especially those working on large-scale projects. Dur- ing his undergraduate years at MIT, he worked summers at a leading computer vendor on the teams developing timesharing, virtual memory operating systems. This was a great ex- perience for a college student. But, in the summer of 1967, reality set in when the compa- ny “decommitted” from producing as a commercial product the particular system on which hundreds of people had been working for many years. It was difficult to get this soft- ware right—software is “complex stuff.”

Improvements to software technology did emerge, with the benefits of structured pro- gramming (and the related disciplines of structured systems analysis and design) being realized in the 1970s. Not until the technology of object-oriented programming became widely used in the 1990s, though, did software developers feel they had the necessary tools for making major strides in the software development process.

Actually, object technology dates back to the mid 1960s. The C++ programming lan- guage, developed at AT&T by Bjarne Stroustrup in the early 1980s, is based on two lan- guages—C and Simula 67, a simulation programming language developed at the Norwegian Computing Center and released in 1967. C++ absorbed the features of C and added Simula’s capabilities for creating and manipulating objects. Neither C nor C++ was originally intended for wide use beyond the AT&T research laboratories. But grass roots support rapidly developed for each.

Object technology is a packaging scheme that helps us create meaningful software units. There are date objects, time objects, paycheck objects, invoice objects, audio objects, video objects, file objects, record objects and so on. In fact, almost any noun can be rea- sonably represented as an object.

We live in a world of objects. There are cars, planes, people, animals, buildings, traffic lights, elevators and the like. Before object-oriented languages appeared, procedural pro- gramming languages (such as Fortran, COBOL, Pascal, BASIC and C) were focused on actions (verbs) rather than on things or objects (nouns). Programmers living in a world of objects programmed primarily using verbs. This made it awkward to write programs. Now, with the availability of popular object-oriented languages such as C++, Java and C#, programmers continue to live in an object-oriented world and can program in an object- oriented manner. This is a more natural process than procedural programming and has resulted in significant productivity gains.

A key problem with procedural programming is that the program units do not effec- tively mirror real-world entities, so these units are not particularly reusable. It isn’t unusual for programmers to “start fresh” on each new project and have to write similar software “from scratch.” This wastes time and money, as people repeatedly “reinvent the wheel.” With object technology, the software entities created (called classes), if properly designed, tend to be reusable on future projects. Using libraries of reusable componentry can greatly reduce effort required to implement certain kinds of systems (compared to the effort that would be required to reinvent these capabilities on new projects).

12 Chapter 1 Introduction to Computers, the Internet and the Web

Some organizations report that the key benefit of object-oriented programming is not software reuse but, rather, that the software they produce is more understandable, better organized and easier to maintain, modify and debug. This can be significant, because per- haps as much as 80 percent of software costs are associated not with the original efforts to develop the software, but with the continued evolution and maintenance of that software throughout its lifetime. Whatever the perceived benefits, it’s clear that object-oriented programming will be the key programming methodology for the next several decades.

1.14 Typical C Program Development Environment C systems generally consist of several parts: a program development environment, the lan- guage and the C Standard Library. The following discussion explains the typical C devel- opment environment shown in Fig. 1.1.

C programs typically go through six phases to be executed (Fig. 1.1). These are: edit, preprocess, compile, link, load and execute. Although C How to Program, 6/e is a generic C textbook (written independently of the details of any particular operating system), we concentrate in this section on a typical Linux-based C system. [Note: The programs in this book will run with little or no modification on most current C systems, including Micro- soft Windows-based systems.] If you’re not using a Linux system, refer to the manuals for your system or ask your instructor how to accomplish these tasks in your environment. Check out our C Resource Center at www.deitel.com/C to locate “getting started” tuto- rials for popular C compilers and development environments.

Phase 1: Creating a Program Phase 1 consists of editing a file. This is accomplished with an editor program. Two edi- tors widely used on Linux systems are vi and emacs. Software packages for the C/C++ in- tegrated program development environments such as Eclipse and Microsoft Visual Studio have editors that are integrated into the programming environment. You type a C program with the editor, make corrections if necessary, then store the program on a secondary stor- age device such as a hard disk. C program file names should end with the .c extension.

Phases 2 and 3: Preprocessing and Compiling a C Program In Phase 2, the you give the command to compile the program. The compiler translates the C program into machine language-code (also referred to as object code). In a C system, a preprocessor program executes automatically before the compiler’s translation phase be- gins. The C preprocessor obeys special commands called preprocessor directives, which indicate that certain manipulations are to be performed on the program before compila- tion. These manipulations usually consist of including other files in the file to be compiled and performing various text replacements. The most common preprocessor directives are discussed in the early chapters; a detailed discussion of preprocessor features appears in Chapter 13. In Phase 3, the compiler translates the C program into machine-language code.

Software Engineering Observation 1.1 Extensive class libraries of reusable software components are available on the Internet. Many of these libraries are free.

1.14 Typical C Program Development Environment 13

Phase 4: Linking The next phase is called linking. C programs typically contain references to functions de- fined elsewhere, such as in the standard libraries or in the private libraries of groups of pro- grammers working on a particular project. The object code produced by the C compiler typically contains “holes” due to these missing parts. A linker links the object code with

Fig. 1.1 | Typical C development environment.

Compiler

Phase 3: Compiler creates object code and stores it on disk.

Linker

Phase 4: Linker links the object code with the libraries, creates an executable file and stores it on disk.

Disk

Disk

Disk

Disk

Disk

Editor

Phase 1: Programmer creates program in the editor and stores it on disk.

Preprocessor Phase 2: Preprocessor program processes the code.

Loader

Phase 5: Loader puts program in memory.

. . .

CPU Phase 6: CPU takes each instruction and executes it, possibly storing new data values as the program executes....

Primary Memory

Primary Memory

14 Chapter 1 Introduction to Computers, the Internet and the Web

the code for the missing functions to produce an executable image (with no missing piec- es). On a typical Linux system, the command to compile and link a program is called cc (or gcc). To compile and link a program named welcome.c type

at the Linux prompt and press the Enter key (or Return key). [Note: Linux commands are case sensitive; make sure that you type lowercase c’s and that the letters in the filename are in the appropriate case.] If the program compiles and links correctly, a file called a.out is produced. This is the executable image of our welcome.c program.

Phase 5: Loading The next phase is called loading. Before a program can be executed, the program must first be placed in memory. This is done by the loader, which takes the executable image from disk and transfers it to memory. Additional components from shared libraries that support the program are also loaded.

Phase 6: Execution Finally, the computer, under the control of its CPU, executes the program one instruction at a time. To load and execute the program on a Linux system, type ./a.out at the Linux prompt and press Enter.

Problems That May Occur at Execution Time Programs do not always work on the first try. Each of the preceding phases can fail because of various errors that we’ll discuss. For example, an executing program might attempt to divide by zero (an illegal operation on computers just as in arithmetic). This would cause the computer to display an error message. You would then return to the edit phase, make the necessary corrections and proceed through the remaining phases again to determine that the corrections work properly.

Most C programs input and/or output data. Certain C functions take their input from stdin (the standard input stream), which is normally the keyboard, but stdin can be connected to another stream. Data is often output to stdout (the standard output stream), which is normally the computer screen, but stdout can be connected to another stream. When we say that a program prints a result, we normally mean that the result is displayed on a screen. Data may be output to devices such as disks and printers. There is also a standard error stream referred to as stderr. The stderr stream (normally con- nected to the screen) is used for displaying error messages. It’s common to route regular output data, i.e., stdout, to a device other than the screen while keeping stderr assigned to the screen so that the user can be immediately informed of errors.

1.15 Hardware Trends Every year, people generally expect to pay at least a little more for most products and ser- vices. The opposite has been the case in the computer and communications fields, espe-

cc welcome.c

Common Programming Error 1.1 Errors like division-by-zero occur as a program runs, so these errors are called runtime er- rors or execution-time errors. Divide-by-zero is generally a fatal error, i.e., an error that causes the program to terminate immediately without successfully performing its job. Non- fatal errors allow programs to run to completion, often producing incorrect results.

1.16 Notes About C and This Book 15

cially with regard to the costs of hardware supporting these technologies. For many decades, hardware costs have fallen rapidly, if not precipitously. Every year or two, the ca- pacities of computers have approximately doubled without any increase in price. This of- ten is called Moore’s Law, named after the person who first identified and explained the trend, Gordon Moore, cofounder of Intel—the company that manufactures the vast ma- jority of the processors in today’s personal computers. Moore’s Law and similar trends are especially true in relation to the amount of memory that computers have for programs, the amount of secondary storage (such as disk storage) they have to hold programs and data over longer periods of time, and their processor speeds—the speeds at which computers execute programs (i.e., do their work). Similar growth has occurred in the communica- tions field, in which costs have plummeted as soaring demand for communications band- width has attracted intense competition. We know of no other fields in which technology improves so quickly and costs fall so rapidly. Such improvement in the computing and communications fields is truly fostering the so-called Information Revolution.

1.16 Notes About C and This Book Experienced C programmers sometimes take pride in creating weird, contorted, convolut- ed usages of the language. This is a poor programming practice. It makes programs more difficult to read, more likely to behave strangely, more difficult to test and debug and more difficult to adapt to changing requirements. This book is geared for novice programmers, so we stress program clarity. The following is our first “good programming practice.”

You may have heard that C is a portable language and that programs written in C can run on many different computers. Portability is an elusive goal. The Standard C document contains a lengthy list of portability issues, and complete books have been written that dis- cuss portability.

We’ve done a careful walkthrough of the C Standard and audited our presentation against it for completeness and accuracy. However, C is a rich language, and there are some subtleties in the language and some advanced subjects we have not covered. If you need additional technical details on C, we suggest that you read the C Standard document itself or the book by Kernighan and Ritchie (The C Programming Language, Second Edition).

Good Programming Practice 1.1 Write your C programs in a simple and straightforward manner. This is sometimes re- ferred to as KIS (“keep it simple”). Do not “stretch” the language by trying bizarre usages.

Portability Tip 1.2 Although it’s possible to write portable C programs, there are many problems between dif- ferent C compilers and different computers that make portability difficult to achieve. Sim- ply writing programs in C does not guarantee portability. You’ll often need to deal directly with computer variations.

Software Engineering Observation 1.2 Read the manuals for the version of C you’re using. Reference these manuals frequently to be sure you’re aware of the rich collection of C features and that you’re using these features correctly.

16 Chapter 1 Introduction to Computers, the Internet and the Web

1.17 Web Resources This section provides links to our C and related Resource Centers that will be useful to you as you learn C. These Resource Centers include various C resources, including, blogs, articles, whitepapers, compilers, development tools, downloads, FAQs, tutorials, webcasts, wikis and links to resources for C game programming with the Allegro libraries.

Deitel & Associates Websites www.deitel.com/books/chtp6/

The Deitel & Associates C How to Program, 6/e site. Here you’ll find links to the book’s examples and other resources. www.deitel.com/C/ www.deitel.com/visualcplusplus/ www.deitel.com/codesearchengines/ www.deitel.com/programmingprojects/

Check these Resource Centers for compilers, code downloads, tutorials, documentation, books, e- books, articles, blogs, RSS feeds and more that will help you develop C applications. www.deitel.com

Check this site for updates, corrections and additional resources for all Deitel publications. www.deitel.com/newsletter/subscribe.html

Subscribe here for the Deitel® Buzz Online e-mail newsletter to follow the Deitel & Associates pub- lishing program, including updates and errata to C How to Program, 6/e.

Software Engineering Observation 1.3 Your computer and compiler are good teachers. If you’re not sure how a C feature works, write a program with that feature, compile and run the program and see what happens.

Summary Section 1.1 Introduction • Software (i.e., the instructions you write to command computers to perform actions and make

decisions) controls computers (often referred to as hardware).

• C was standardized in 1989 in the United States through the American National Standards In- stitute (ANSI) then worldwide through the International Standards Organization (ISO).

• Silicon-chip technology has made computing so economical that more than a billion general- purpose computers are in use worldwide.

Section 1.2 Computers: Hardware and Software • A computer is capable of performing computations and making logical decisions at speeds bil-

lions of times faster than human beings can.

• Computers process data under the control of sets of instructions called computer programs, which guide the computer through orderly sets of actions specified by computer programmers.

• The various devices that comprise a computer system are referred to as hardware.

• The computer programs that run on a computer are referred to as software.

Section 1.3 Computer Organization • The input unit is the “receiving” section of the computer. It obtains information from input de-

vices and places it at the disposal of the other units for processing.

Summary 17

• The output unit is the “shipping” section of the computer. It takes information processed by the computer and places it on output devices to make it available for use outside the computer.

• The memory unit is the rapid-access, relatively low-capacity “warehouse” section of the comput- er. It retains information that has been entered through the input unit, making it immediately available for processing when needed, and retains information that has already been processed until it can be placed on output devices by the output unit.

• The arithmetic and logic unit (ALU) is the “manufacturing” section of the computer. It’s respon- sible for performing calculations and making decisions.

• The central processing unit (CPU) is the “administrative” section of the computer. It coordinates and supervises the operation of the other sections.

• The secondary storage unit is the long-term, high-capacity “warehousing” section of the comput- er. Programs or data not being used by the other units are normally placed on secondary storage devices (e.g., disks) until they’re needed, possibly hours, days, months or even years later.

Section 1.4 Personal, Distributed and Client/Server Computing • Apple Computer popularized personal computing.

• IBM’s Personal Computer quickly legitimized personal computing in business, industry and government organizations, where IBM mainframes are heavily used.

• Early personal computers could be linked together in computer networks. This led to the phe- nomenon of distributed computing.

• Information is shared easily across networks, where computers called servers (file servers, database servers, web servers, etc.) offer capabilities that may be used by client computers distributed throughout the network, hence the term client/server computing.

• C has become widely used for writing software for operating systems, for computer networking and for distributed client/server applications.

Section 1.5 The Internet and the World Wide Web • The Internet—a global network of computers—was initiated almost four decades ago with fund-

ing supplied by the U.S. Department of Defense.

• With the introduction of the World Wide Web—which allows computer users to locate and view multimedia-based documents on almost any subject over the Internet—the Internet has ex- ploded into the world’s premier communication mechanism.

Section 1.6 Machine Languages, Assembly Languages and High-Level Languages • Any computer can directly understand only its own machine language, which generally consists

of strings of numbers that instruct computers to perform their most elementary operations.

• English-like abbreviations form the basis of assembly languages. Translator programs called as- semblers convert assembly-language programs to machine language.

• Compilers translate high-level language programs into machine-language programs. High-level languages (like C) contain English words and conventional mathematical notations.

• Interpreter programs directly execute high-level language programs, eliminating the need to compile them into machine language.

Section 1.7 History of C • C evolved from two previous languages, BCPL and B. BCPL was developed in 1967 by Martin

Richards as a language for writing operating systems software and compilers. Ken Thompson modeled many features in his B language after their counterparts in BCPL and used B to create early versions of the UNIX operating system.

18 Chapter 1 Introduction to Computers, the Internet and the Web

• The C language was evolved from B by Dennis Ritchie at Bell Laboratories. C uses many of the important concepts of BCPL and B while adding data typing and other powerful features.

• C initially became widely known as the development language of the UNIX operating system.

• C is available for most computers. C is mostly hardware independent.

• The publication in 1978 of Kernighan and Ritchie’s book, The C Programming Language, drew wide attention to the language.

• In 1989, the C standard was approved; this standard was updated in 1999. The standards docu- ment is referred to as INCITS/ISO/IEC 9899-1999.

• C99 is a revised standard for the C programming language that refines and expands the capabil- ities of C, but it has not be universally adopted.

Section 1.8 C Standard Library • When programming in C you’ll typically use C Standard Library functions, functions you create

yourself and functions other people have created and made available to you.

Section 1.9 C++ • C++ was developed by Bjarne Stroustrup at Bell Laboratories. It has its roots in C and provides

capabilities for object-oriented programming.

• Objects are essentially reusable software components that model items in the real world.

• Using a modular, object-oriented design and implementation approach makea software develop- ment groups much more productive than is possible with conventional programming techniques.

Section 1.10 Java • Java is used to create dynamic and interactive content for web pages, develop enterprise applica-

tions, enhance web server functionality, provide applications for consumer devices and more.

Section 1.11 Fortran, COBOL, Pascal and Ada • FORTRAN was developed by IBM Corporation in the 1950s for scientific and engineering ap-

plications that require complex mathematical computations.

• COBOL was developed in the 1950s for commercial applications that require precise and effi- cient data manipulation.

• Pascal was designed for teaching structured programming.

• Ada was developed under the sponsorship of the United States Department of Defense (DoD) during the 1970s and early 1980s. Ada provides multitasking, which allows programmers to specify that many activities are to occur in parallel.

Section 1.12 BASIC, Visual Basic, Visual C++, C# and .NET • BASIC was developed in the 1960s at Dartmouth College for programming novices.

• Visual Basic was introduced in the 1990s to simplify developing Windows applications.

• Microsoft has a corporate-wide strategy for integrating the Internet and the web into computer applications. This strategy is implemented in Microsoft’s .NET platform.

• The .NET platform’s three primary programming languages are Visual Basic (based on the orig- inal BASIC), Visual C++ (based on C++) and Visual C# (a new language based on C++ and Java that was developed expressly for the .NET platform).

Section 1.13 Key Software Trend: Object Technology • Not until object-oriented programming became widely used in the 1990s did software develop-

ers feel they had the tools to make major strides in the software development process.

• C++ absorbed the features of C and added Simula’s object capabilities.

Terminology 19

• Object technology is a packaging scheme that helps us create meaningful software units.

• With object technology, the software entities created (called classes), if properly designed, tend to be reusable on future projects.

• Some organizations report the key benefit of object-oriented programming is the production of software which is more understandable, better organized and easier to maintain and debug.

Section 1.14 Typical C Program Development Environment • You create a program by editing a file with an editor program. Software packages for the C/C++

integrated program development environments such as Eclipse and Microsoft Visual Studio have editors that are integrated into the programming environment.

• C program file names should end with the .c extension.

• Compilers translate programs into machine-language code (also referred to as object code).

• A preprocessor program executes automatically before the compiler’s translation phase begins. The C preprocessor obeys special commands called preprocessor directives that usually consist of including other files in the file to be compiled and performing various text replacements.

• A linker links object code with the code for library functions to produce an executable image.

• Before a program can execute, it must first be placed in memory. This is done by the loader. Ad- ditional components from shared libraries that support the program are also loaded.

• The computer, under the control of its CPU, executes a program one instruction at a time.

Section 1.15 Hardware Trends • Every year, people generally expect to pay at least a little more for most products and services.

The opposite has been the case in the computer and communications fields, especially with re- gard to the costs of hardware supporting these technologies. For many decades, hardware costs have fallen rapidly, if not precipitously.

• Every year or two, the capacities of computers have approximately doubled without any increase in price. This often is called Moore’s Law, named after the person who first identified and ex- plained the trend, Gordon Moore, cofounder of Intel—the company that manufactures the vast majority of the processors in today’s personal computers.

• Moore’s Law is especially true in relation to the amount of memory that computers have for pro- grams, the amount of secondary storage they have to hold programs and data over longer periods of time, and their processor speeds—the speeds at which computers execute their programs.

Terminology actions (computers perform) 2 Ada programming language 10 Allegro 2 American National Standards Institute (ANSI) 2 arithmetic and logic unit (ALU) 4 assembler 6 assembly language 6 BASIC (Beginner’s All-Purpose Symbolic In-

struction Code) 10 building-block approach 8 C preprocessor 12 C Standard Library 8 cc compilation command 14 central processing unit (CPU) 4

class 11 client computer 5 client/server computing 5 COBOL (COmmon Business Oriented

Language) 10 compile 12 compile phase 12 compiler 7 components (software) 9 computer 3 computer program 4 computer programmer 4 data 4 decisions (made by computers) 2

20 Chapter 1 Introduction to Computers, the Internet and the Web

distributed computing 5 dynamic content 9 edit phase 12 editor program 12 executable image 14 execute 14 execute phase 12 FORTRAN (FORmula TRANslator) 10 function 8 gcc compilation command 14 hardware 2 hardware platform 7 high-level language 7 input device 4 input unit 4 International Standards Organization (ISO) 2 Internet 5 interpreter 7 Java 9 link phase 12 linker 13 linking 13 load phase 12 loader 14 loading 14 local area network (LAN) 5 logical unit 4 machine dependent 6 machine language 6 mainframe 3 memory 4 memory unit 4 Moore’s Law 15 multi-core processor 5

multiprocessor 5 multitasking 10 .NET platform 10 object 9 object code 12 object-oriented programming (OOP) 9 output device 4 output unit 4 Pascal 10 persistent information 5 personal computing 5 portable program 7 preprocess phase 12 preprocessor 12 preprocessor directive 12 primary memory 4 program clarity 15 secondary storage unit 5 server 5 software 2 software reusability 8 standard error stream (stderr) 14 standard input stream (stdin) 14 standard output stream (stdout) 14 structured programming 10 supercomputer 3 translation 6 translator programs 6 Visual Basic 10 Visual C++ 10 Visual C# 10 volatile information 4 World Wide Web 6

Self-Review Exercises 1.1 Fill in the blanks in each of the following:

a) The company that popularized personal computing was . b) The computer that made personal computing legitimate in business and industry was

the . c) Computers process data under the control of sets of instructions called computer

. d) The six key logical units of the computer are the , , ,

, and the . e) The three types of languages we discussed are , , and . f) The programs that translate high-level language programs into machine language are

called . g) C is widely known as the development language of the operating system. h) The Department of Defense developed the Ada language with a capability called

, which allows programmers to specify activities that can proceed in parallel.

Answers to Self-Review Exercises 21

1.2 Fill in the blanks in each of the following sentences about the C environment. a) C programs are normally typed into a computer using a(n) program. b) In a C system, a(n) program automatically executes before the translation

phase begins. c) The two most common kinds of preprocessor directives are and . d) The program combines the output of the compiler with various library func-

tions to produce an executable image. e) The program transfers the executable image from disk to memory. f) To load and execute the most recently compiled program on a Linux system, type

.

Answers to Self-Review Exercises 1.1 a) Apple. b) IBM Personal Computer. c) programs. d) input unit, output unit, memory unit, arithmetic and logic unit, central processing unit, secondary storage unit. e) machine languag- es, assembly languages and high-level languages. f) compilers. g) UNIX. h) multitasking.

1.2 a) editor. b) preprocessor. c) including other files in the file to be compiled, replacing special symbols with program text. d) linker. e) loader. f) ./a.out.

Exercises 1.3 Categorize each of the following items as either hardware or software:

a) CPU b) C++ compiler c) ALU d) C++ preprocessor e) input unit f) an editor program

1.4 Why might you want to write a program in a machine-independent language instead of a machine-dependent language? Why might a machine-dependent language be more appropriate for writing certain types of programs?

1.5 Fill in the blanks in each of the following statements: a) Which logical unit of the computer receives information from outside the computer for

use by the computer? . b) The process of instructing the computer to solve specific problems is called . c) What type of computer language uses English-like abbreviations for machine-language

instructions? . d) Which logical unit of the computer sends information that has already been processed

by the computer to various devices so that the information may be used outside the computer? .

e) Which logical units of the computer retain information? . f) Which logical unit of the computer performs calculations? . g) Which logical unit of the computer makes logical decisions? . h) The level of computer language most convenient for you to write programs quickly and

easily is . i) The only language that a computer directly understands is called that computer's

. j) Which logical unit of the computer coordinates the activities of all the other logical

units? .

22 Chapter 1 Introduction to Computers, the Internet and the Web

1.6 State whether each of the following is true or false. If false, explain your answer. a) Machine languages are generally machine dependent. b) Like other high-level languages, C is generally considered to be machine independent.

1.7 Discuss the meaning of each of the following names: a) stdin b) stdout c) stderr

1.8 Why is so much attention today focused on object-oriented programming?

1.9 Which programming language is best described by each of the following? a) Developed by IBM for scientific and engineering applications. b) Developed specifically for business applications. c) Developed for teaching structured programming. d) Named after the world’s first computer programmer. e) Developed to familiarize novices with programming techniques. f) Specifically developed to help programmers migrate to .NET. g) Known as the development language of UNIX. h) Formed primarily by adding object-oriented programming to C. i) Succeeded initially because of its ability to create web pages with dynamic content.

Making a Difference 1.10 (Test-Drive: Carbon Footprint Calculator) Some scientists believe that carbon emissions, especially from the burning of fossil fuels, contribute significantly to global warming and that this can be combatted if individuals take steps to limit their use of carbon-based fuels. Organizations and individuals are increasingly concerned about their “carbon footprints.” Websites such as TerraPass

www.terrapass.com/carbon-footprint-calculator/

and Carbon Footprint

www.carbonfootprint.com/calculator.aspx

provide carbon footprint calculators. Test-drive these calculators to estimate your carbon footprint. Exercises in later chapters will ask you to program your own carbon footprint calculator. To pre- pare for this, use the web to research the formulas for calculating carbon footprints.

1.11 (Test-Drive: Body Mass Index Calculator) By recent estimates, two-thirds of the people in the United States are overweight and about half of those are obese. This causes significant increases in illnesses such as diabetes and heart disease. To determine whether a person is overweight or obese, you can use a measure called the body mass index (BMI). The United States Department of Health and Human Services provides a BMI calculator at www.nhlbisupport.com/bmi/. Use it to calculate your own BMI. An exercise in Chapter 2 will ask you to program your own BMI calculator. To pre- pare for this, use the web to research the formulas for calculating BMI.

1.12 (Gender Neutrality) Many people want to eliminate sexism in all forms of communication. You’ve been asked to create a program that can process a paragraph of text and replace gender-spe- cific words with gender-neutral ones. Assuming that you’ve been given a list of gender-specific words and their gender-neutral replacements (e.g., replace “wife” with “spouse,” “man” with “per- son,” “daughter” with “child” and so on), explain the procedure you’d use to read through a para- graph of text and manually perform these replacements. How might your procedure generate a strange term like “woperchild,” which is actually listed in the Urban Dictionary (www.urbandic- tionary.com)? In Chapter 4, you’ll learn that a more formal term for “procedure” is “algorithm,” and that an algorithm specifies the steps to be performed and the order in which to perform them.

2Introduction to C Programming What’s in a name? That which we call a rose By any other name would smell as sweet. —William Shakespeare

When faced with a decision, I always ask, “What would be the most fun?” —Peggy Walker

“Take some more tea,” the March Hare said to Alice, very earnestly. “I’ve had nothing yet,” Alice replied in an offended tone: “so I can’t take more.” “You mean you can’t take less,” said the Hatter: “it’s very easy to take more than nothing.” —Lewis Carroll

High thoughts must have high language. —Aristophanes

O b j e c t i v e s In this chapter, you’ll learn

■ To write simple computer programs in C.

■ To use simple input and output statements.

■ To use the fundamental data types.

■ Computer memory concepts.

■ To use arithmetic operators.

■ The precedence of arithmetic operators.

■ To write simple decision- making statements.

24 Chapter 2 Introduction to C Programming

2.1 Introduction The C language facilitates a structured and disciplined approach to computer program de- sign. In this chapter we introduce C programming and present several examples that illus- trate many important features of C. Each example is analyzed one statement at a time. In Chapters 3 and 4 we present an introduction to structured programming in C. We then use the structured approach throughout the remainder of the C portion of the text.

2.2 A Simple C Program: Printing a Line of Text C uses some notations that may appear strange to people who have not programmed com- puters. We begin by considering a simple C program. Our first example prints a line of text. The program and its screen output are shown in Fig. 2.1.

Even though this program is simple, it illustrates several important features of the C language. Lines 1 and 2

begin with /* and end with */ indicating that these two lines are a comment. You insert comments to document programs and improve program readability. Comments do not cause the computer to perform any action when the program is run. Comments are ignored by the C compiler and do not cause any machine-language object code to be

2.1 Introduction 2.2 A Simple C Program: Printing a Line

of Text 2.3 Another Simple C Program: Adding

Two Integers

2.4 Memory Concepts 2.5 Arithmetic in C 2.6 Decision Making: Equality and

Relational Operators

Summary | Terminology | Self-Review Exercises | Answers to Self-Review Exercises | Exercises | Making a Difference

1 /* Fig. 2.1: fig02_01.c 2 A first program in C */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main( void ) 7 { 8 printf( "Welcome to C!\n" ); 9

10 return 0; /* indicate that program ended successfully */ 11 } /* end function main */

Welcome to C!

Fig. 2.1 | A first program in C.

/* Fig. 2.1: fig02_01.c A first program in C */

2.2 A Simple C Program: Printing a Line of Text 25

generated. The preceding comment simply describes the figure number, file name and purpose of the program. Comments also help other people read and understand your pro- gram, but too many comments can make a program difficult to read.

C99 also includes the C++ language’s // single-line comments in which everything from // to the end of the line is a comment. These can be used as standalone comments on lines by themselves or as end-of-line comments to the right of a partial line of code. Some programmers prefer // comments because they’re shorter and they eliminate the common programming errors that occur with /* */ comments.

Line 3

is a directive to the C preprocessor. Lines beginning with # are processed by the prepro- cessor before the program is compiled. Line 3 tells the preprocessor to include the contents of the standard input/output header (<stdio.h>) in the program. This header contains information used by the compiler when compiling calls to standard input/output library functions such as printf. We explain the contents of headers in more detail in Chapter 5.

Line 6

is a part of every C program. The parentheses after main indicate that main is a program building block called a function. C programs contain one or more functions, one of which must be main. Every program in C begins executing at the function main. Functions can return information. The keyword int to the left of main indicates that main “returns” an integer (whole number) value. We’ll explain what it means for a function to “return a val- ue” when we demonstrate how to create your own functions in Chapter 5. For now, sim- ply include the keyword int to the left of main in each of your programs. Functions also can receive information when they’re called upon to execute. The void in parentheses here means that main does not receive any information. In Chapter 14, Other C Topics, we’ll show an example of main receiving information.

A left brace, {, begins the body of every function (line 7). A corresponding right brace ends each function (line 11). This pair of braces and the portion of the program between the braces is called a block. The block is an important program unit in C.

Line 8

Common Programming Error 2.1 Forgetting to terminate a comment with */.

Common Programming Error 2.2 Starting a comment with the characters */ or ending a comment with the characters /*.

#include <stdio.h>

int main( void )

Good Programming Practice 2.1 Every function should be preceded by a comment describing the purpose of the function.

printf( "Welcome to C!\n" );

26 Chapter 2 Introduction to C Programming

instructs the computer to perform an action, namely to print on the screen the string of characters marked by the quotation marks. A string is sometimes called a character string, a message or a literal. The entire line, including printf, its argument within the paren- theses and the semicolon (;), is called a statement. Every statement must end with a semi- colon (also known as the statement terminator). When the preceding printf statement is executed, it prints the message Welcome to C! on the screen. The characters normally print exactly as they appear between the double quotes in the printf statement. Notice that the characters \n were not printed on the screen. The backslash (\) is called an escape character. It indicates that printf is supposed to do something out of the ordinary. When encountering a backslash in a string, the compiler looks ahead at the next character and combines it with the backslash to form an escape sequence. The escape sequence \n means newline. When a newline appears in the string output by a printf, the newline causes the cursor to position to the beginning of the next line on the screen. Some common escape sequences are listed in Fig. 2.2.

The last two escape sequences in Fig. 2.2 may seem strange. Because the backslash has special meaning in a string, i.e., the compiler recognizes it as an escape character, we use a double backslash (\\) to place a single backslash in a string. Printing a double quote also presents a problem because double quotes mark the boundary of a string—such quotes are not printed. By using the escape sequence \" in a string to be output by printf, we indi- cate that printf should display a double quote.

Line 10

is included at the end of every main function. The keyword return is one of several means we’ll use to exit a function. When the return statement is used at the end of main as shown here, the value 0 indicates that the program has terminated successfully. In Chapter 5 we discuss functions in detail, and the reasons for including this statement will become clear. For now, simply include this statement in each program, or the compiler might produce a warning on some systems. The right brace, }, (line 12) indicates that the end of main has been reached.

Escape sequence Description

\n Newline. Position the cursor at the beginning of the next line.

\t Horizontal tab. Move the cursor to the next tab stop.

\a Alert. Sound the system bell.

\\ Backslash. Insert a backslash character in a string.

\" Double quote. Insert a double-quote character in a string.

Fig. 2.2 | Some common escape sequences .

return 0; /* indicate that program ended successfully */

Good Programming Practice 2.2 Add a comment to the line containing the right brace, }, that closes every function, in- cluding main.

2.2 A Simple C Program: Printing a Line of Text 27

We said that printf causes the computer to perform an action. As any program executes, it performs a variety of actions and makes decisions. At the end of this chapter, we discuss decision making. In Chapter 3, we discuss this action/decision model of pro- gramming in depth.

Standard library functions like printf and scanf are not part of the C programming language. For example, the compiler cannot find a spelling error in printf or scanf. When the compiler compiles a printf statement, it merely provides space in the object program for a “call” to the library function. But the compiler does not know where the library functions are—the linker does. When the linker runs, it locates the library func- tions and inserts the proper calls to these library functions in the object program. Now the object program is complete and ready to be executed. For this reason, the linked program is called an executable. If the function name is misspelled, it is the linker which will spot the error, because it will not be able to match the name in the C program with the name of any known function in the libraries.

The printf function can print Welcome to C! several different ways. For example, the program of Fig. 2.3 produces the same output as the program of Fig. 2.1. This works because each printf resumes printing where the previous printf stopped printing. The first printf (line 8) prints Welcome followed by a space and the second printf (line 9) begins printing on the same line immediately following the space.

Common Programming Error 2.3 Typing the name of the output function printf as print in a program.

Good Programming Practice 2.3 Indent the entire body of each function one level of indentation (we recommend three spaces) within the braces that define the body of the function. This indentation emphasizes the functional structure of programs and helps make programs easier to read.

Good Programming Practice 2.4 Set a convention for the size of indent you prefer and then uniformly apply that conven- tion. The tab key may be used to create indents, but tab stops may vary. We recommend using three spaces per level of indent.

1 /* Fig. 2.3: fig02_03.c 2 Printing on one line with two printf statements */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main( void ) 7 { 8 9

10 11 return 0; /* indicate that program ended successfully */ 12 } /* end function main */

Fig. 2.3 | Printing on one line with two printf statements. (Part 1 of 2.)

printf( "Welcome " ); printf( "to C!\n" );

28 Chapter 2 Introduction to C Programming

One printf can print several lines by using additional newline characters as in Fig. 2.4. Each time the \n (newline) escape sequence is encountered, output continues at the beginning of the next line.

2.3 Another Simple C Program: Adding Two Integers Our next program uses the Standard Library function scanf to obtain two integers typed by a user at the keyboard, computes the sum of these values and prints the result using printf. The program and sample output are shown in Fig. 2.8. [In the input/output dia- log of Fig. 2.8, we emphasize the numbers input by the user in bold.]

Welcome to C!

1 /* Fig. 2.4: fig02_04.c 2 Printing multiple lines with a single printf */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main( void ) 7 { 8 printf( "Welcome to C!\n" ); 9

10 return 0; /* indicate that program ended successfully */ 11 } /* end function main */

Welcome to C!

Fig. 2.4 | Printing multiple lines with a single printf.

1 /* Fig. 2.5: fig02_05.c 2 Addition program */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main( void ) 7 { 8 9

10 11 12 printf( "Enter first integer\n" ); /* prompt */ 13 14 15 printf( "Enter second integer\n" ); /* prompt */ 16

Fig. 2.5 | Addition program. (Part 1 of 2.)

Fig. 2.3 | Printing on one line with two printf statements. (Part 2 of 2.)

\n \n

int integer1; /* first number to be input by user */ int integer2; /* second number to be input by user */ int sum; /* variable in which sum will be stored */

scanf( "%d", &integer1 ); /* read an integer */

scanf( "%d", &integer2 ); /* read an integer */

2.3 Another Simple C Program: Adding Two Integers 29

The comment in lines 1–2 states the purpose of the program. As we stated earlier, every program begins execution with main. The left brace { (line 7) marks the beginning of the body of main and the corresponding right brace } (line 24) marks the end of main.

Lines 8–10

are definitions. The names integer1, integer2 and sum are the names of variables. A variable is a location in memory where a value can be stored for use by a program. These definitions specify that the variables integer1, integer2 and sum are of type int, which means that these variables will hold integer values, i.e., whole numbers such as 7, –11, 0, 31914 and the like. All variables must be defined with a name and a data type immediately after the left brace that begins the body of main before they can be used in a program. There are other data types besides int in C. The preceding definitions could have been combined into a single definition statement as follows:

but that would have made it difficult to describe the variables in corresponding comments as we did in lines 8–10.

A variable name in C is any valid identifier. An identifier is a series of characters con- sisting of letters, digits and underscores (_) that does not begin with a digit. An identifier can be of any length, but only the first 31 characters are required to be recognized by C compilers according to the C standard. C is case sensitive—uppercase and lowercase let- ters are different in C, so a1 and A1 are different identifiers.

17 18 19 20 21 22 return 0; /* indicate that program ended successfully */ 23 } /* end function main */

Enter first integer 45 Enter second integer 72 Sum is 117

int integer1; /* first number to be input by user */ int integer2; /* second number to be input by user */ int sum; /* variable in which sum will be stored */

int integer1, integer2, sum;

Common Programming Error 2.4 Using a capital letter where a lowercase letter should be used (for example, typing Main instead of main).

Error-Prevention Tip 2.1 Avoid starting identifiers with the underscore character (_) to prevent conflicts with com- piler-generated identifiers and standard library identifiers.

Fig. 2.5 | Addition program. (Part 2 of 2.)

sum = integer1 + integer2; /* assign total to sum */

printf( "Sum is %d\n", sum ); /* print sum */

30 Chapter 2 Introduction to C Programming

Definitions must be placed after the left brace of a function and before any executable statements. For example, in the program illustrated in Fig. 2.5, inserting the definitions after the first printf would cause a syntax error. A syntax error is caused when the com- piler cannot recognize a statement. The compiler normally issues an error message to help you locate and fix the incorrect statement. Syntax errors are violations of the language. Syntax errors are also called compile errors, or compile-time errors.

Line 12

prints the literal Enter first integer on the screen and positions the cursor to the begin- ning of the next line. This message is called a prompt because it tells the user to take a specific action.

The next statement

uses scanf to obtain a value from the user. The scanf function reads from the standard input, which is usually the keyboard. This scanf has two arguments, "%d" and &integer1. The first argument, the format control string, indicates the type of data that should be input by the user. The %d conversion specifier indicates that the data should be an integer

Portability Tip 2.1 Use identifiers of 31 or fewer characters. This helps ensure portability and can avoid some subtle programming errors.

Good Programming Practice 2.5 Choosing meaningful variable names helps make a program self-documenting, i.e., fewer comments are needed.

Good Programming Practice 2.6 The first letter of an identifier used as a simple variable name should be a lowercase letter. Later in the text we’ll assign special significance to identifiers that begin with a capital letter and to identifiers that use all capital letters.

Good Programming Practice 2.7 Multiple-word variable names can help make a program more readable. Avoid running the separate words together as in totalcommissions. Rather, separate the words with un- derscores as in total_commissions, or, if you do wish to run the words together, begin each word after the first with a capital letter as in totalCommissions. The latter style is preferred.

Common Programming Error 2.5 Placing variable definitions among executable statements causes syntax errors.

Good Programming Practice 2.8 Separate the definitions and executable statements in a function with one blank line to emphasize where the definitions end and the executable statements begin.

printf( "Enter first integer\n" ); /* prompt */

scanf( "%d", &integer1 ); /* read an integer */

2.3 Another Simple C Program: Adding Two Integers 31

(the letter d stands for “decimal integer”). The % in this context is treated by scanf (and printf as we’ll see) as a special character that begins a conversion specifier. The second argument of scanf begins with an ampersand (&)—called the address operator in C— followed by the variable name. The ampersand, when combined with the variable name, tells scanf the location (or address) in memory at which the variable integer1 is stored. The computer then stores the value for integer1 at that location. The use of ampersand (&) is often confusing to novice programmers or to people who have programmed in other languages that do not require this notation. For now, just remember to precede each vari- able in every call to scanf with an ampersand. Some exceptions to this rule are discussed in Chapters 6 and 7. The use of the ampersand will become clear after we study pointers in Chapter 7.

When the computer executes the preceding scanf, it waits for the user to enter a value for variable integer1. The user responds by typing an integer, then pressing the Enter key to send the number to the computer. The computer then assigns this number, or value, to the variable integer1. Any subsequent references to integer1 in this program will use this same value. Functions printf and scanf facilitate interaction between the user and the computer. Because this interaction resembles a dialogue, it is often called conversational computing or interactive computing.

Line 15

displays the message Enter second integer on the screen, then positions the cursor to the beginning of the next line. This printf also prompts the user to take action.

The statement

obtains a value for variable integer2 from the user. The assignment statement in line 18

calculates the sum of variables integer1 and integer2 and assigns the result to variable sum using the assignment operator =. The statement is read as, “sum gets the value of integer1 + integer2.” Most calculations are performed in assignments. The = operator and the + operator are called binary operators because each has two operands. The + op- erator’s two operands are integer1 and integer2. The = operator’s two operands are sum and the value of the expression integer1 + integer2.

Good Programming Practice 2.9 Place a space after each comma (,) to make programs more readable.

printf( "Enter second integer\n" ); /* prompt */

scanf( "%d", &integer2 ); /* read an integer */

sum = integer1 + integer2; /* assign total to sum */

Good Programming Practice 2.10 Place spaces on either side of a binary operator. This makes the operator stand out and makes the program more readable.

Common Programming Error 2.6 A calculation in an assignment statement must be on the right side of the = operator. It is a compilation error to place a calculation on the left side of an assignment operator.

32 Chapter 2 Introduction to C Programming

Line 20

calls function printf to print the literal Sum is followed by the numerical value of variable sum on the screen. This printf has two arguments, "Sum is %d\n" and sum. The first ar- gument is the format control string. It contains some literal characters to be displayed, and it contains the conversion specifier %d indicating that an integer will be printed. The sec- ond argument specifies the value to be printed. Notice that the conversion specifier for an integer is the same in both printf and scanf. This is the case for most C data types.

Calculations can also be performed inside printf statements. We could have com- bined the previous two statements into the statement

Line 22

passes the value 0 back to the operating-system environment in which the program is being executed. This value indicates to the operating system that the program executed success- fully. For information on how to report a program failure, see the manuals for your par- ticular operating-system environment. The right brace, }, at line 24 indicates that the end of function main has been reached.

printf( "Sum is %d\n", sum ); /* print sum */

printf( "Sum is %d\n", integer1 + integer2 );

return 0; /* indicate that program ended successfully */

Common Programming Error 2.7 Forgetting one or both of the double quotes surrounding the format control string in a printf or scanf.

Common Programming Error 2.8 Forgetting the % in a conversion specification in the format control string of a printf or scanf.

Common Programming Error 2.9 Placing an escape sequence such as \n outside the format control string of a printf or scanf.

Common Programming Error 2.10 Forgetting to include the expressions whose values are to be printed in a printf containing conversion specifiers.

Common Programming Error 2.11 Not providing a conversion specifier when one is needed in a printf format control string to print the value of an expression.

Common Programming Error 2.12 Placing inside the format control string the comma that is supposed to separate the format control string from the expressions to be printed.

2.4 Memory Concepts 33

On many systems, the preceding execution-time error causes a “segmentation fault” or “access violation.” Such an error occurs when a user’s program attempts to access a part of the computer’s memory to which it does not have access privileges. The precise cause of this error will be explained in Chapter 7.

2.4 Memory Concepts Variable names such as integer1, integer2 and sum actually correspond to locations in the computer’s memory. Every variable has a name, a type and a value.

In the addition program of Fig. 2.5, when the statement (line 13)

is executed, the value typed by the user is placed into a memory location to which the name integer1 has been assigned. Suppose the user enters the number 45 as the value for integer1. The computer will place 45 into location integer1 as shown in Fig. 2.6.

Whenever a value is placed in a memory location, the value replaces the previous value in that location; thus, placing a new value into a memory location is said to be destructive.

Returning to our addition program again, when the statement (line 16)

executes, suppose the user enters the value 72. This value is placed into location integer2, and memory appears as in Fig. 2.7. These locations are not necessarily adjacent in memory.

Once the program has obtained values for integer1 and integer2, it adds these values and places the sum into variable sum. The statement (line 18)

that performs the addition also replaces whatever value was stored in sum. This occurs when the calculated sum of integer1 and integer2 is placed into location sum (destroying the value already in sum). After sum is calculated, memory appears as in Fig. 2.8. The values of

Common Programming Error 2.13 Using the incorrect format conversion specifier when reading data with scanf.

Common Programming Error 2.14 Forgetting to precede a variable in a scanf statement with an ampersand when that vari- able should, in fact, be preceded by an ampersand.

Common Programming Error 2.15 Preceding a variable included in a printf statement with an ampersand when, in fact, that variable should not be preceded by an ampersand.

scanf( "%d", &integer1 ); /* read an integer */

Fig. 2.6 | Memory location showing the name and value of a variable.

scanf( "%d", &integer2 ); /* read an integer */

sum = integer1 + integer2; /* assign total to sum */

45integer1

34 Chapter 2 Introduction to C Programming

integer1 and integer2 appear exactly as they did before they were used in the calculation. They were used, but not destroyed, as the computer performed the calculation. Thus, when a value is read from a memory location, the process is said to be nondestructive.

2.5 Arithmetic in C Most C programs perform arithmetic calculations. The C arithmetic operators are sum- marized in Fig. 2.9. Note the use of various special symbols not used in algebra. The as- terisk (*) indicates multiplication and the percent sign (%) denotes the remainder operator, which is introduced below. In algebra, if we want to multiply a times b, we can simply place these single-letter variable names side by side as in ab. In C, however, if we were to do this, ab would be interpreted as a single, two-letter name (or identifier). There- fore, C (and other programming languages, in general) require that multiplication be ex- plicitly denoted by using the * operator as in a * b.

The arithmetic operators are all binary operators. For example, the expression 3 + 7 contains the binary operator + and the operands 3 and 7.

Integer division yields an integer result. For example, the expression 7 / 4 evaluates to 1 and the expression 17 / 5 evaluates to 3. C provides the remainder operator, %, which

Fig. 2.7 | Memory locations after both variables are input.

Fig. 2.8 | Memory locations after a calculation.

C operation Arithmetic operator Algebraic expression C expression

Addition + f + 7 f + 7

Subtraction – p – c p - c

Multiplication * bm b * m

Division / x / y or or x ÷ y x / y

Remainder % r mod s r % s

Fig. 2.9 | Arithmetic operators.

45

72

integer1

integer2

45

72

117

integer1

integer2

sum

x y --

2.5 Arithmetic in C 35

yields the remainder after integer division. The remainder operator is an integer operator that can be used only with integer operands. The expression x % y yields the remainder after x is divided by y. Thus, 7 % 4 yields 3 and 17 % 5 yields 2. We’ll discuss many inter- esting applications of the remainder operator.

Arithmetic Expressions in Straight-Line Form Arithmetic expressions in C must be written in straight-line form to facilitate entering programs into the computer. Thus, expressions such as “a divided by b” must be written as a/b so that all operators and operands appear in a straight line. The algebraic notation

is generally not acceptable to compilers, although some special-purpose software packages do support more natural notation for complex mathematical expressions.

Parentheses for Grouping Subexpressions Parentheses are used in C expressions in the same manner as in algebraic expressions. For example, to multiply a times the quantity b + c we write a * ( b + c ).

Rules of Operator Precedence C applies the operators in arithmetic expressions in a precise sequence determined by the following rules of operator precedence, which are generally the same as those in algebra:

1. Operators in expressions contained within pairs of parentheses are evaluated first. Thus, parentheses may be used to force the order of evaluation to occur in any sequence you desire. Parentheses are said to be at the “highest level of precedence.” In cases of nested, or embedded, parentheses, such as

the operators in the innermost pair of parentheses are applied first.

2. Multiplication, division and remainder operations are applied first. If an ex- pression contains several multiplication, division and remainder operations, eval- uation proceeds from left to right. Multiplication, division and remainder are said to be on the same level of precedence.

3. Addition and subtraction operations are evaluated next. If an expression contains several addition and subtraction operations, evaluation proceeds from left to right. Addition and subtraction also have the same level of precedence, which is lower than the precedence of the multiplication, division and remainder operations.

The rules of operator precedence specify the order C uses to evaluate expressions.1

When we say evaluation proceeds from left to right, we’re referring to the associativity of the operators. We’ll see that some operators associate from right to left. Figure 2.10 sum- marizes these rules of operator precedence.

Common Programming Error 2.16 An attempt to divide by zero is normally undefined on computer systems and generally re- sults in a fatal error, i.e., an error that causes the program to terminate immediately with- out having successfully performed its job. Nonfatal errors allow programs to run to completion, often producing incorrect results.

( ( a + b ) + c )

a b --

36 Chapter 2 Introduction to C Programming

Sample Algebraic and C Expressions Now let’s consider several expressions in light of the rules of operator precedence. Each example lists an algebraic expression and its C equivalent. The following example calcu- lates the arithmetic mean (average) of five terms.

The parentheses are required to group the additions because division has higher prece- dence than addition. The entire quantity ( a + b + c + d + e ) should be divided by 5. If the parentheses are erroneously omitted, we obtain a + b + c + d + e / 5 which evaluates incorrectly as

The following example is the equation of a straight line:

No parentheses are required. The multiplication is evaluated first because multiplication has a higher precedence than addition.

The following example contains remainder (%), multiplication, division, addition, subtraction and assignment operations:

1. We use simple examples to explain the order of evaluation of expressions. Subtle issues occur in more complex expressions that you’ll encounter later in the book. We’ll discuss these issues as they arise.

Operator(s) Operation(s) Order of evaluation (precedence)

( ) Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evalu- ated first. If there are several pairs of parenthe- ses “on the same level” (i.e., not nested), they’re evaluated left to right.

* / %

Multiplication Division Remainder

Evaluated second. If there are several, they’re evaluated left to right.

+ -

Addition Subtraction

Evaluated last. If there are several, they’re eval- uated left to right.

Fig. 2.10 | Precedence of arithmetic operators.

Algebra:

Java: m = ( a + b + c + d + e ) / 5;

Algebra: y = mx + b

C: y = m * x + b;

m a b c d e+ + + + 5

-------------------------------------=

a b c d e 5 ---+ + + +

z

6 1 2 4 3 5

= p * r % q + w / x - y;

z = pr%q + w/x – yAlgebra: C:

2.5 Arithmetic in C 37

The circled numbers indicate the order in which C evaluates the operators. The multipli- cation, remainder and division are evaluated first in left-to-right order (i.e., they associate from left to right) since they have higher precedence than addition and subtraction. The addition and subtraction are evaluated next. They’re also evaluated left to right.

Not all expressions with several pairs of parentheses contain nested parentheses. For example, the following expression does not contain nested parentheses—instead, the parentheses are said to be “on the same level.”

Evaluation of a Second-Degree Polynomial To develop a better understanding of the rules of operator precedence, let’s see how C eval- uates a second-degree polynomial.

The circled numbers under the statement indicate the order in which C performs the oper- ations. There is no arithmetic operator for exponentiation in C, so we have represented x2

as x * x. The C Standard Library includes the pow (“power”) function to perform expo- nentiation. Because of some subtle issues related to the data types required by pow, we defer a detailed explanation of pow until Chapter 4.

Suppose variables a, b, c and x in the preceding second-degree polynomial are initial- ized as follows: a = 2, b = 3, c = 7 and x = 5. Figure 2.11 illustrates the order in which the operators are applied.

a * ( b + c ) + c * ( d + e )

Fig. 2.11 | Order in which a second-degree polynomial is evaluated.

6 1 2 4 3 5

y = a * x * x + b * x + c;

(Leftmost multiplication)

(Leftmost multiplication)

(Multiplication before addition)

(Leftmost addition)

(Last addition)

(Last operation—place 72 in y)

Step 1. y = 2 * 5 * 5 + 3 * 5 + 7;

2 * 5 is 10

Step 2. y = 10 * 5 + 3 * 5 + 7;

10 * 5 is 50

Step 3. y = 50 + 3 * 5 + 7;

3 * 5 is 15

Step 4. y = 50 + 15 + 7;

50 + 15 is 65

Step 5. y = 65 + 7;

65 + 7 is 72

Step 6. y = 72

38 Chapter 2 Introduction to C Programming

As in algebra, it is acceptable to place unnecessary parentheses in an expression to make the expression clearer. These are called redundant parentheses. For example, the preceding statement could be parenthesized as follows:

2.6 Decision Making: Equality and Relational Operators Executable C statements either perform actions (such as calculations or input or output of data) or make decisions (we’ll soon see several examples of these). We might make a deci- sion in a program, for example, to determine if a person’s grade on an exam is greater than or equal to 60 and if it is to print the message “Congratulations! You passed.” This section introduces a simple version of C’s if statement that allows a program to make a decision based on the truth or falsity of a statement of fact called a condition. If the condition is met (i.e., the condition is true) the statement in the body of the if statement is executed. If the condition is not met (i.e., the condition is false) the body statement is not executed. Whether the body statement is executed or not, after the if statement completes, execu- tion proceeds with the next statement after the if statement.

Conditions in if statements are formed by using the equality operators and relational operators summarized in Fig. 2.12. The relational operators all have the same level of precedence and they associate left to right. The equality operators have a lower level of precedence than the relational operators and they also associate left to right. [Note: In C, a condition may actually be any expression that generates a zero (false) or nonzero (true) value. We’ll see many applications of this throughout the book.]

y = ( a * x * x ) + ( b * x ) + c;

Good Programming Practice 2.11 Using redundant parentheses in complex arithmetic expressions can make the expressions clearer.

Algebraic equality or relational operator

C equality or relational operator

Example of C condition Meaning of C condition

Equality operators = == x == y x is equal to y ≠ != x != y x is not equal to y Relational operators > > x > y x is greater than y < < x < y x is less than y ≥ >= x >= y x is greater than or equal to y ≤ <= x <= y x is less than or equal to y

Fig. 2.12 | Equality and relational operators.

Common Programming Error 2.17 A syntax error occurs if the two symbols in any of the operators ==, !=, >= and <= are sep- arated by spaces.

2.6 Decision Making: Equality and Relational Operators 39

To avoid this confusion, the equality operator should be read “double equals” and the assignment operator should be read “gets” or “is assigned the value of.” As we’ll soon see, confusing these operators may not necessarily cause an easy-to-recognize compilation error, but may cause extremely subtle logic errors.

Figure 2.13 uses six if statements to compare two numbers input by the user. If the condition in any of these if statements is true, the printf statement associated with that if executes. The program and three sample execution outputs are shown in the figure.

Common Programming Error 2.18 A syntax error occurs if the two symbols in any of the operators !=, >= and <= are reversed as in =!, => and =<, respectively.

Common Programming Error 2.19 Confusing the equality operator == with the assignment operator.

Common Programming Error 2.20 Placing a semicolon immediately to the right of the right parenthesis after the condition in an if statement.

1 /* Fig. 2.13: fig02_13.c 2 Using if statements, relational 3 operators, and equality operators */ 4 #include <stdio.h> 5 6 /* function main begins program execution */ 7 int main( void ) 8 { 9 int num1; /* first number to be read from user */

10 int num2; /* second number to be read from user */ 11 12 printf( "Enter two integers, and I will tell you\n" ); 13 printf( "the relationships they satisfy: " ); 14 15 scanf( "%d%d", &num1, &num2 ); /* read two integers */ 16 17 18 19 20 21 if ( ) { 22 printf( "%d is not equal to %d\n", num1, num2 ); 23 } /* end if */ 24 25 if ( ) { 26 printf( "%d is less than %d\n", num1, num2 ); 27 } /* end if */ 28

Fig. 2.13 | Using if statements, relational operators, and equality operators. (Part 1 of 2.)

if ( num1 == num2 ) { printf( "%d is equal to %d\n", num1, num2 ); } /* end if */

num1 != num2

num1 < num2

40 Chapter 2 Introduction to C Programming

The program uses scanf (line 15) to input two numbers. Each conversion specifier has a corresponding argument in which a value will be stored. The first %d converts a value to be stored in variable num1, and the second %d converts a value to be stored in variable num2. Indenting the body of each if statement and placing blank lines above and below each if statement enhances program readability.

29 if ( ) { 30 printf( "%d is greater than %d\n", num1, num2 ); 31 } /* end if */ 32 33 if ( ) { 34 printf( "%d is less than or equal to %d\n", num1, num2 ); 35 } /* end if */ 36 37 if ( ) { 38 printf( "%d is greater than or equal to %d\n", num1, num2 ); 39 } /* end if */ 40 41 return 0; /* indicate that program ended successfully */ 42 } /* end function main */

Enter two integers, and I will tell you the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7

Enter two integers, and I will tell you the relationships they satisfy: 12 12 22 is not equal to 12 22 is greater than 12 22 is greater than or equal to 12

Enter two integers, and I will tell you the relationships they satisfy: 7 7

7 is equal to 7 7 is less than or equal to 7 7 is greater than or equal to 7

Good Programming Practice 2.12 Indent the statement(s) in the body of an if statement.

Good Programming Practice 2.13 Place a blank line before and after every if statement in a program for readability.

Fig. 2.13 | Using if statements, relational operators, and equality operators. (Part 2 of 2.)

num1 > num2

num1 <= num2

num1 >= num2

2.6 Decision Making: Equality and Relational Operators 41

A left brace, {, begins the body of each if statement (e.g., line 17). A corresponding right brace, }, ends each if statement’s body (e.g., line 19). Any number of statements can be placed in the body of an if statement.2

The comment (lines 1–3) in Fig. 2.13 is split over three lines. In C programs, white space characters such as tabs, newlines and spaces are normally ignored. So, statements and comments may be split over several lines. It is not correct, however, to split identifiers.

Figure 2.14 lists the precedence of the operators introduced in this chapter. Operators are shown top to bottom in decreasing order of precedence. The equals sign is also an oper- ator. All these operators, with the exception of the assignment operator =, associate from left to right. The assignment operator (=) associates from right to left.

Good Programming Practice 2.14 Although it is allowed, there should be no more than one statement per line in a program.

Common Programming Error 2.21 Placing commas (when none are needed) between conversion specifiers in the format con- trol string of a scanf statement.

2. Using braces to delimit the body of an if statement is optional when the body contains only one statement. Many programmers consider it good practice to always use these braces. In Chapter 3, we’ll explain the issues.

Good Programming Practice 2.15 A lengthy statement may be spread over several lines. If a statement must be split across lines, choose breaking points that make sense (such as after a comma in a comma-separated list). If a statement is split across two or more lines, indent all subsequent lines.

Good Programming Practice 2.16 Refer to the operator precedence chart when writing expressions containing many opera- tors. Confirm that the operators in the expression are applied in the proper order. If you’re uncertain about the order of evaluation in a complex expression, use parentheses to group expressions or break the statement into several simpler statements. Be sure to observe that some of C’s operators such as the assignment operator (=) associate from right to left rather than from left to right.

Operators Associativity

() left to right

* / % left to right

+ - left to right

< <= > >= left to right

== != left to right

= right to left

Fig. 2.14 | Precedence and associativity of the operators discussed so far.

42 Chapter 2 Introduction to C Programming

Some of the words we have used in the C programs in this chapter—in particular int, return and if—are keywords or reserved words of the language. Figure 2.15 contains the C keywords. These words have special meaning to the C compiler, so you must be careful not to use these as identifiers such as variable names. In this book, we discuss all these key- words.

In this chapter, we have introduced many important features of the C programming language, including printing data on the screen, inputting data from the user, performing calculations and making decisions. In the next chapter, we build upon these techniques as we introduce structured programming. You’ll become more familiar with indentation techniques. We’ll study how to specify the order in which statements are executed—this is called flow of control.

Keywords

auto double int struct

break else long switch

case enum register typedef

char extern return union

const float short unsigned

continue for signed void

default goto sizeof volatile

do if static while

Keywords added in C99

_Bool _Complex _Imaginary inline restrict

Fig. 2.15 | C’s keywords.

Summary Section 2.1 Introduction • The C language facilitates a structured and disciplined approach to computer program design.

Section 2.2 A Simple C Program: Printing a Line of Text • Comments begin with /* and end with */. Comments document programs and improve pro-

gram readability. C99 also supports C++’s single-line comments that begin with //.

• Comments do not cause the computer to perform any action when the program is run. They’re ignored by the C compiler and do not cause any machine-language object code to be generated.

• Lines beginning with # are processed by the preprocessor before the program is compiled. The #include directive tells the preprocessor to include the contents of another file (typically a header file such as <stdio.h>).

• The <stdio.h> header contains information used by the compiler when compiling calls to stan- dard input/output library functions such as printf.

Summary 43

• The function main is a part of every C program. The parentheses after main indicate that main is a program building block called a function. C programs contain one or more functions, one of which must be main. Every program in C begins executing at the function main.

• Functions can return information. The keyword int to the left of main indicates that main “re- turns” an integer (whole number) value.

• Functions can receive information when they’re called upon to execute. The void in parentheses after main indicates that main does not receive any information.

• A left brace, {, begins the body of every function. A corresponding right brace, }, ends each func- tion. This pair of braces and the portion of the program between the braces is called a block.

• The printf function instructs the computer to display information on the screen.

• A string is sometimes called a character string, a message or a literal.

• Every statement must end with a semicolon (also known as the statement terminator).

• The characters \n do not display characters on the screen. The backslash (\) is called an escape character. When encountering a backslash in a string, the compiler looks ahead at the next char- acter and combines it with the backslash to form an escape sequence. The escape sequence \n means newline.

• When a newline appears in the string output by a printf, the newline causes the cursor to posi- tion to the beginning of the next line on the screen.

• The double backslash (\\) escape sequence can be used to place a single backslash in a string.

• The escape sequence \" represents a literal double-quote character.

• The keyword return is one of several means to exit a function. When the return statement is used at the end of main, the value 0 indicates that the program has terminated successfully.

Section 2.3 Another Simple C Program: Adding Two Integers • A variable is a location in memory where a value can be stored for use by a program.

• Variables of type int hold integer values, i.e., whole numbers such as 7, –11, 0, 31914.

• All variables must be defined with a name and a data type immediately after the left brace that begins the body of main before they can be used in a program.

• A variable name in C is any valid identifier. An identifier is a series of characters consisting of letters, digits and underscores ( _ ) that does not begin with a digit. An identifier can be any length, but only the first 31 characters are required to be recognized by C compilers according to the C standard.

• C is case sensitive—uppercase and lowercase letters are different in C.

• Definitions must be placed after the left brace of a function and before any executable statements.

• A syntax error is caused when the compiler cannot recognize a statement. The compiler normally issues an error message to help you locate and fix the incorrect statement. Syntax errors are vio- lations of the language. Syntax errors are also called compile errors, or compile-time errors.

• Standard Library function scanf can be used to obtain input from the standard input, which is usually the keyboard.

• The scanf format control string indicates the type(s) of data that should be input.

• The %d conversion specifier indicates that the data should be an integer (the letter d stands for “decimal integer”). The % in this context is treated by scanf (and printf) as a special character that begins a conversion specifier.

• The other arguments of scanf begin with an ampersand (&)—called the address operator in C— followed by a variable name. The ampersand, when combined with a variable name, tells scanf

44 Chapter 2 Introduction to C Programming

the location in memory at which the variable is located. The computer then stores the value for the variable at that location.

• Most calculations are performed in assignment statements.

• The = operator and the + operator are binary operators—each has two operands.

• Function printf also can use a format control string as its first argument. This string contains some literal characters to be displayed and the conversion specifiers that indicate place holders for data to output.

Section 2.4 Memory Concepts • Variable names correspond to locations in the computer’s memory. Every variable has a name, a

type and a value.

• Whenever a value is placed in a memory location, the value replaces the previous value in that location; thus, placing a new value into a memory location is said to be destructive.

• When a value is read out of a memory location, the process is said to be nondestructive.

Section 2.5 Arithmetic in C • In algebra, if we want to multiply a times b, we can simply place these single-letter variable names

side by side as in ab. In C, however, if we were to do this, ab would be interpreted as a single, two-letter name (or identifier). Therefore, C (like other programming languages, in general) re- quires that multiplication be explicitly denoted by using the * operator, as in a * b.

• The arithmetic operators are all binary operators.

• Integer division yields an integer result. For example, the expression 7 / 4 evaluates to 1 and the expression 17 / 5 evaluates to 3.

• C provides the remainder operator, %, which yields the remainder after integer division. The re- mainder operator is an integer operator that can be used only with integer operands. The expres- sion x % y yields the remainder after x is divided by y. Thus, 7 % 4 yields 3 and 17 % 5 yields 2.

• An attempt to divide by zero is normally undefined on computer systems and generally results in a fatal error that causes the program to terminate immediately. Nonfatal errors allow programs to run to completion, often producing incorrect results.

• Arithmetic expressions in C must be written in straight-line form to facilitate entering programs into the computer. Thus, expressions such as “a divided by b” must be written as a/b so that all operators and operands appear in a straight line.

• Parentheses are used to group terms in C expressions in much the same manner as in algebraic expressions.

• C evaluates arithmetic expressions in a precise sequence determined by the following rules of op- erator precedence, which are generally the same as those followed in algebra.

• Multiplication, division and remainder operations are applied first. If an expression contains sev- eral multiplication, division and remainder operations, evaluation proceeds from left to right. Multiplication, division and remainder are said to be on the same level of precedence.

• Addition and subtraction operations are evaluated next. If an expression contains several addition and subtraction operations, evaluation proceeds from left to right. Addition and subtraction also have the same level of precedence, which is lower than the precedence of the multiplication, di- vision and remainder operators.

• The rules of operator precedence specify the order C uses to evaluate expressions. When we say evaluation proceeds from left to right, we’re referring to the associativity of the operators. Some operators associate from right to left.

Terminology 45

Section 2.6 Decision Making: Equality and Relational Operators • Executable C statements either perform actions or make decisions.

• C’s if statement allows a program to make a decision based on the truth or falsity of a statement of fact called a condition. If the condition is met (i.e., the condition is true) the statement in the body of the if statement executes. If the condition is not met (i.e., the condition is false) the body statement does not execute. Whether the body statement is executed or not, after the if statement completes, execution proceeds with the next statement after the if statement.

• Conditions in if statements are formed by using the equality operators and relational operators.

• The relational operators all have the same level of precedence and associate left to right. The equality operators have a lower level of precedence than the relational operators and they also as- sociate left to right.

• To avoid confusing assignment (=) and equality (==), the assignment operator should be read “gets” and the equality operator should be read “double equals.”

• In C programs, white-space characters such as tabs, newlines and spaces are normally ignored. So, statements and comments may be split over several lines. It is not correct to split identifiers.

• Some of the words in C programs—such as int, return and if—are keywords or reserved words of the language. These words have special meaning to the C compiler, so you cannot use them as identifiers such as variable names.

Terminology * multiplication operator 34 % remainder operator 34 %d conversion specifier 30 action 26 action/decision model 27 address operator (&) 31 argument 26 arithmetic operators 34 assignment statement 31 associativity 35 body 25 C preprocessor 25 case sensitive 29 character string 26 comment (/* */) 24 compile error 30 compile-time error 30 condition 38 conversational computing 31 decision 38 definition 29 destructive 33 document a program 24 embedded parentheses 35 Enter key 31 equality operator 38 escape character 26 escape sequence 26

executable 27 exit a function 26 false 38 flow of control 42 format control string 30 function 25 identifier 29 if statement 38 integer 29 integer division 34 interactive computing 31 keyword 42 literal 26 message 26 nested parentheses 35 newline (\n) 26 nondestructive 34 operand 31 percent sign (%) 34 prompt 30 redundant parentheses 38 relational operator 38 right brace (}) 25 rules of operator precedence 35 scanf function 30 single-line comment (//) 25 standard input/output header 25 statement 26

46 Chapter 2 Introduction to C Programming

statement terminator (;) 26 <stdio.h> header 25 straight-line form 35 string 26 structured programming 24 syntax error 30

true 38 type 33 value 33 variable 29 white space 41

Self-Review Exercises 2.1 Fill in the blanks in each of the following.

a) Every C program begins execution at the function . b) The begins the body of every function and the ends the body

of every function. c) Every statement ends with a(n) . d) The standard library function displays information on the screen. e) The escape sequence \n represents the character, which causes the cursor

to position to the beginning of the next line on the screen. f) The Standard Library function is used to obtain data from the keyboard. g) The conversion specifier is used in a scanf format control string to indicate

that an integer will be input and in a printf format control string to indicate that an integer will be output.

h) Whenever a new value is placed in a memory location, that value overrides the previous value in that location. This process is said to be .

i) When a value is read out of a memory location, the value in that location is preserved; this process is said to be .

j) The statement is used to make decisions.

2.2 State whether each of the following is true or false. If false, explain why. a) Function printf always begins printing at the beginning of a new line. b) Comments cause the computer to print the text enclosed between /* and */ on the

screen when the program is executed. c) The escape sequence \n when used in a printf format control string causes the cursor

to position to the beginning of the next line on the screen. d) All variables must be defined before they’re used. e) All variables must be given a type when they’re defined. f) C considers the variables number and NuMbEr to be identical. g) Definitions can appear anywhere in the body of a function. h) All arguments following the format control string in a printf function must be preced-

ed by an ampersand (&). i) The remainder operator (%) can be used only with integer operands. j) The arithmetic operators *, /, %, + and - all have the same level of precedence. k) The following variable names are identical on all Standard C systems.

thisisasuperduperlongname1234567 thisisasuperduperlongname1234568

l) A program that prints three lines of output must contain three printf statements.

2.3 Write a single C statement to accomplish each of the following: a) Define the variables c, thisVariable, q76354 and number to be of type int. b) Prompt the user to enter an integer. End your prompting message with a colon (:) fol-

lowed by a space and leave the cursor positioned after the space. c) Read an integer from the keyboard and store the value entered in integer variable a. d) If number is not equal to 7, print "The variable number is not equal to 7."

Answers to Self-Review Exercises 47

e) Print the message "This is a C program." on one line. f) Print the message "This is a C program." on two lines so that the first line ends with C. g) Print the message "This is a C program." with each word on a separate line. h) Print the message "This is a C program." with the words separated by tabs.

2.4 Write a statement (or comment) to accomplish each of the following: a) State that a program will calculate the product of three integers. b) Define the variables x, y, z and result to be of type int. c) Prompt the user to enter three integers. d) Read three integers from the keyboard and store them in the variables x, y and z. e) Compute the product of the three integers contained in variables x, y and z, and assign

the result to the variable result. f) Print "The product is" followed by the value of the integer variable result.

2.5 Using the statements you wrote in Exercise 2.4, write a complete program that calculates the product of three integers.

2.6 Identify and correct the errors in each of the following statements: a) printf( "The value is %d\n", &number ); b) scanf( "%d%d", &number1, number2 ); c) if ( c < 7 );{

printf( "C is less than 7\n" );

} d) if ( c => 7 ) {

printf( "C is equal to or less than 7\n" );

}

Answers to Self-Review Exercises 2.1 a) main. b) left brace ({), right brace (}). c) semicolon. d) printf. e) newline. f) scanf. g) %d. h) destructive. i) nondestructive. j) if.

2.2 a) False. Function printf always begins printing where the cursor is positioned, and this may be anywhere on a line of the screen.

b) False. Comments do not cause any action to be performed when the program is exe- cuted. They’re used to document programs and improve their readability.

c) True. d) True. e) True. f) False. C is case sensitive, so these variables are unique. g) False. The definitions must appear after the left brace of the body of a function and be-

fore any executable statements. h) False. Arguments in a printf function ordinarily should not be preceded by an am-

persand. Arguments following the format control string in a scanf function ordinarily should be preceded by an ampersand. We’ll discuss exceptions to these rules in Chapter 6 and Chapter 7.

i) True. j) False. The operators *, / and % are on the same level of precedence, and the operators +

and - are on a lower level of precedence. k) False. Some systems may distinguish between identifiers longer than 31 characters. l) False. A printf statement with multiple \n escape sequences can print several lines.

2.3 a) int c, thisVariable, q76354, number; b) printf( "Enter an integer: " );

48 Chapter 2 Introduction to C Programming

c) scanf( "%d", &a ); d) if ( number != 7 )

{

printf( "The variable number is not equal to 7.\n" ); }

e) printf( "This is a C program.\n" ); f) printf( "This is a C\nprogram.\n" ); g) printf( "This\nis\na\nC\nprogram.\n" ); h) printf( "This\tis\ta\tC\tprogram.\n" );

2.4 a) /* Calculate the product of three integers */ b) int x, y, z, result; c) printf( "Enter three integers: " ); d) scanf( "%d%d%d", &x, &y, &z ); e) result = x * y * z; f) printf( "The product is %d\n", result );

2.5 See below.

2.6 a) Error: &number. Correction: Eliminate the &. We discuss exceptions to this later. b) Error: number2 does not have an ampersand. Correction: number2 should be &number2.

Later in the text we discuss exceptions to this. c) Error: Semicolon after the right parenthesis of the condition in the if statement. Cor-

rection: Remove the semicolon after the right parenthesis. [Note: The result of this error is that the printf statement will be executed whether or not the condition in the if statement is true. The semicolon after the right parenthesis is considered an empty state- ment—a statement that does nothing.]

d) Error: The relational operator => should be changed to >= (greater than or equal to).

Exercises 2.7 Identify and correct the errors in each of the following statements. (Note: There may be more than one error per statement.)

a) scanf( "d", value ); b) printf( "The product of %d and %d is %d"\n, x, y ); c) firstNumber + secondNumber = sumOfNumbers d) if ( number => largest )

largest == number; e) */ Program to determine the largest of three integers /* f) Scanf( "%d", anInteger );

1 /* Calculate the product of three integers */ 2 #include <stdio.h> 3 4 int main( void ) 5 { 6 int x, y, z, result; /* declare variables */ 7 8 printf( "Enter three integers: " ); /* prompt */ 9 scanf( "%d%d%d", &x, &y, &z ); /* read three integers */

10 result = x * y * z; /* multiply values */ 11 printf( "The product is %d\n", result ); /* display result */ 12 return 0; 13 } /* end function main */

Exercises 49

g) printf( "Remainder of %d divided by %d is\n", x, y, x % y ); h) if ( x = y );

printf( %d is equal to %d\n", x, y ); i) print( "The sum is %d\n," x + y ); j) Printf( "The value you entered is: %d\n, &value );

2.8 Fill in the blanks in each of the following: a) are used to document a program and improve its readability. b) The function used to display information on the screen is . c) A C statement that makes a decision is . d) Calculations are normally performed by statements. e) The function inputs values from the keyboard.

2.9 Write a single C statement or line that accomplishes each of the following: a) Print the message “Enter two numbers.” b) Assign the product of variables b and c to variable a. c) State that a program performs a sample payroll calculation (i.e., use text that helps to

document a program). d) Input three integer values from the keyboard and place these values in integer variables

a, b and c.

2.10 State which of the following are true and which are false. If false, explain your answer. a) C operators are evaluated from left to right. b) The following are all valid variable names: _under_bar_, m928134, t5, j7, her_sales,

his_account_total, a, b, c, z, z2. c) The statement printf("a = 5;"); is a typical example of an assignment statement. d) A valid arithmetic expression containing no parentheses is evaluated from left to right. e) The following are all invalid variable names: 3g, 87, 67h2, h22, 2h.

2.11 Fill in the blanks in each of the following: a) What arithmetic operations are on the same level of precedence as multiplication?

. b) When parentheses are nested, which set of parentheses is evaluated first in an arithmetic

expression? . c) A location in the computer's memory that may contain different values at various times

throughout the execution of a program is called a .

2.12 What, if anything, prints when each of the following statements is performed? If nothing prints, then answer “Nothing.” Assume x = 2 and y = 3.

a) printf( "%d", x ); b) printf( "%d", x + x ); c) printf( "x=" ); d) printf( "x=%d", x ); e) printf( "%d = %d", x + y, y + x ); f) z = x + y; g) scanf( "%d%d", &x, &y ); h) /* printf( "x + y = %d", x + y ); */ i) printf( "\n" );

2.13 Which, if any, of the following C statements contain variables whose values are replaced? a) scanf( "%d%d%d%d%d", &b, &c, &d, &e, &f ); b) p = i + j + k + 7; c) printf( "Values are replaced" ); d) printf( "a = 5" );

50 Chapter 2 Introduction to C Programming

2.14 Given the equation y = ax3 + 7, which of the following, if any, are correct C statements for this equation?

a) y = a * x * x * x + 7; b) y = a * x * x * ( x + 7 ); c) y = ( a * x ) * x * ( x + 7 ); d) y = ( a * x ) * x * x + 7; e) y = a * ( x * x * x ) + 7; f) y = a * x * ( x * x + 7 );

2.15 State the order of evaluation of the operators in each of the following C statements and show the value of x after each statement is performed.

a) x = 7 + 3 * 6 / 2 - 1; b) x = 2 % 2 + 2 * 2 - 2 / 2; c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );

2.16 (Arithmetic) Write a program that asks the user to enter two numbers, obtains them from the user and prints their sum, product, difference, quotient and remainder.

2.17 (Printing Values with printf) Write a program that prints the numbers 1 to 4 on the same line. Write the program using the following methods.

a) Using one printf statement with no conversion specifiers. b) Using one printf statement with four conversion specifiers. c) Using four printf statements.

2.18 (Comparing Integers) Write a program that asks the user to enter two integers, obtains the numbers from the user, then prints the larger number followed by the words “is larger.” If the numbers are equal, print the message “These numbers are equal.” Use only the single-selection form of the if statement you learned in this chapter.

2.19 (Arithmetic, Largest Value and Smallest Value) Write a program that inputs three different integers from the keyboard, then prints the sum, the average, the product, the smallest and the larg- est of these numbers. Use only the single-selection form of the if statement you learned in this chap- ter. The screen dialogue should appear as follows:

2.20 (Diameter, Circumference and Area of a Circle) Write a program that reads in the radius of a circle and prints the circle’s diameter, circumference and area. Use the constant value 3.14159 for π. Perform each of these calculations inside the printf statement(s) and use the conversion spec- ifier %f. [Note: In this chapter, we have discussed only integer constants and variables. In Chapter 3 we’ll discuss floating-point numbers, i.e., values that can have decimal points.]

2.21 (Shapes with Asterisks) Write a program that prints the following shapes with asterisks.

Input three different integers: 13 27 14 Sum is 54 Average is 18 Product is 4914 Smallest is 13 Largest is 27

********* *** * * * * * * *** * * * * * * ***** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ********* *** * *

Exercises 51

2.22 What does the following code print? printf( "*\n**\n***\n****\n*****\n" );

2.23 (Largest and Smallest Integers) Write a program that reads in five integers and then deter- mines and prints the largest and the smallest integers in the group. Use only the programming tech- niques you have learned in this chapter.

2.24 (Odd or Even) Write a program that reads an integer and determines and prints whether it is odd or even. [Hint: Use the remainder operator. An even number is a multiple of two. Any mul- tiple of two leaves a remainder of zero when divided by 2.]

2.25 Print your initials in block letters down the page. Construct each block letter out of the let- ter it represents as shown below.

2.26 (Multiples) Write a program that reads in two integers and determines and prints if the first is a multiple of the second. [Hint: Use the remainder operator.]

2.27 (Checkerboard Pattern of Asterisks) Display the following checkerboard pattern with eight printf statements and then display the same pattern with as few printf statements as possible.

2.28 Distinguish between the terms fatal error and nonfatal error. Why might you prefer to ex- perience a fatal error rather than a nonfatal error?

2.29 (Integer Value of a Character) Here’s a peek ahead. In this chapter you learned about inte- gers and the type int. C can also represent uppercase letters, lowercase letters and a considerable variety of special symbols. C uses small integers internally to represent each different character. The set of characters a computer uses together with the corresponding integer representations for those characters is called that computer’s character set. You can print the integer equivalent of uppercase A, for example, by executing the statement

printf( "%d", 'A' );

Write a C program that prints the integer equivalents of some uppercase letters, lowercase letters, digits and special symbols. As a minimum, determine the integer equivalents of the following: A B C a b c 0 1 2 $ * + / and the blank character.

PPPPPPPPP P P P P P P P P

JJ J J J JJJJJJJ

DDDDDDDDD D D D D D D DDDDD

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

52 Chapter 2 Introduction to C Programming

2.30 (Separating Digits in an Integer) Write a program that inputs one five-digit number, sep- arates the number into its individual digits and prints the digits separated from one another by three spaces each. [Hint: Use combinations of integer division and the remainder operation.] For exam- ple, if the user types in 42139, the program should print

2.31 (Table of Squares and Cubes) Using only the techniques you learned in this chapter, write a program that calculates the squares and cubes of the numbers from 0 to 10 and uses tabs to print the following table of values:

Making a Difference 2.32 (Body Mass Index Calculator) We introduced the body mass index (BMI) calculator in Exercise 1.11. The formulas for calculating BMI are

or

Create a BMI calculator application that reads the user’s weight in pounds and height in inches (or, if you prefer, the user’s weight in kilograms and height in meters), then calculates and displays the user’s body mass index. Also, the application should display the following information from the Department of Health and Human Services/National Institutes of Health so the user can eval- uate his/her BMI:

[Note: In this chapter, you learned to use the int type to represent whole numbers. The BMI calcu- lations when done with int values will both produce whole-number results. In Chapter 4 you’ll learn to use the double type to represent numbers with decimal points. When the BMI calculations are performed with doubles, they’ll both produce numbers with decimal points—these are called “floating-point” numbers.]

4 2 1 3 9

number square cube 0 0 0 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000

BMI VALUES Underweight: less than 18.5 Normal: between 18.5 and 24.9 Overweight: between 25 and 29.9 Obese: 30 or greater

BMI weightInPounds 703× heightInInches heightInInches× ------------------------------------------------------------------------------------=

BMI weightInKi ramslog heightInMeters heightInMeters× ---------------------------------------------------------------------------------------=

Making a Difference 53

2.33 (Car-Pool Savings Calculator) Research several car-pooling websites. Create an application that calculates your daily driving cost, so that you can estimate how much money could be saved by car pooling, which also has other advantages such as reducing carbon emissions and reducing traffic congestion. The application should input the following information and display the user’s cost per day of driving to work:

a) Total miles driven per day. b) Cost per gallon of gasoline. c) Average miles per gallon. d) Parking fees per day. e) Tolls per day.

3 Structured Program Development in C Let’s all move one place on. —Lewis Carroll

The wheel is come full circle. —William Shakespeare

How many apples fell on Newton’s head before he took the hint! —Robert Frost

All the evolution we know of proceeds from the vague to the definite. —Charles Sanders Peirce

O b j e c t i v e s In this chapter, you’ll learn:

■ Basic problem-solving techniques.

■ To develop algorithms through the process of top- down, stepwise refinement.

■ To use the if selection statement and the if…else selection statement to select actions.

■ To use the while repetition statement to execute statements in a program repeatedly.

■ Counter-controlled repetition and sentinel-controlled repetition.

■ Structured programming.

■ The increment, decrement and assignment operators.

3.1 Introduction 55

3.1 Introduction Before writing a program to solve a particular problem, it’s essential to have a thorough understanding of the problem and a carefully planned approach to solving the problem. The next two chapters discuss techniques that facilitate the development of structured computer programs. In Section 4.12, we present a summary of structured programming that ties together the techniques developed here and in Chapter 4.

3.2 Algorithms The solution to any computing problem involves executing a series of actions in a specific order. A procedure for solving a problem in terms of

1. the actions to be executed, and

2. the order in which these actions are to be executed

is called an algorithm. The following example demonstrates that correctly specifying the order in which the actions are to be executed is important.

Consider the “rise-and-shine algorithm” followed by one junior executive for getting out of bed and going to work: Consider the “rise-and-shine algorithm” followed by one junior executive for getting out of bed and going to work: (1) Get out of bed, (2) take off pajamas, (3) take a shower, (4) get dressed, (5) eat breakfast, (6) carpool to work. This rou- tine gets the executive to work well prepared to make critical decisions. Suppose that the same steps are performed in a slightly different order: (1) Get out of bed, (2) take off pajamas, (3) get dressed, (4) take a shower, (5) eat breakfast, (6) carpool to work. In this case, our junior executive shows up for work soaking wet. Specifying the order in which statements are to be executed in a computer program is called program control. In this and the next chapter, we investigate the program control capabilities of C.

3.3 Pseudocode Pseudocode is an artificial and informal language that helps you develop algorithms. The pseudocode we present here is particularly useful for developing algorithms that will be converted to structured C programs. Pseudocode is similar to everyday English; it’s con- venient and user friendly although it’s not an actual computer programming language.

3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4 Control Structures 3.5 The if Selection Statement 3.6 The if…else Selection Statement 3.7 The while Repetition Statement

3.8 Formulating Algorithms Case Study 1: Counter-Controlled Repetition

3.9 Formulating Algorithms with Top- Down, Stepwise Refinement Case Study 2: Sentinel-Controlled Repetition

3.10 Formulating Algorithms with Top- Down, Stepwise Refinement Case Study 3: Nested Control Structures

3.11 Assignment Operators 3.12 Increment and Decrement Operators

Summary |Terminology | Self-Review Exercises | Answers to Self-Review Exercises | Exercises | Making a Difference

56 Chapter 3 Structured Program Development in C

Pseudocode programs are not executed on computers. Rather, they merely help you “think out” a program before attempting to write it in a programming language such as C. In this chapter, we give several examples of how pseudocode may be used effectively in developing structured C programs.

Pseudocode consists purely of characters, so you may conveniently type pseudocode programs into a computer using an editor program. The computer can display or print a fresh copy of a pseudocode program on demand. A carefully prepared pseudocode pro- gram may be converted easily to a corresponding C program. This is done in many cases simply by replacing pseudocode statements with their C equivalents.

Pseudocode consists only of action statements—those that are executed when the pro- gram has been converted from pseudocode to C and is run in C. Definitions are not exe- cutable statements. They’re messages to the compiler. For example, the definition

simply tells the compiler the type of variable i and instructs the compiler to reserve space in memory for the variable. But this definition does not cause any action—such as input, output, or a calculation—to occur when the program is executed. Some programmers choose to list each variable and briefly mention the purpose of each at the beginning of a pseudocode program.

3.4 Control Structures Normally, statements in a program are executed one after the other in the order in which they’re written. This is called sequential execution. Various C statements we’ll soon dis- cuss enable you to specify that the next statement to be executed may be other than the next one in sequence. This is called transfer of control.

During the 1960s, it became clear that the indiscriminate use of transfers of control was the root of a great deal of difficulty experienced by software development groups. The finger of blame was pointed at the goto statement that allows programmers to specify a transfer of control to one of many possible destinations in a program. The notion of so- called structured programming became almost synonymous with “goto elimination.”

The research of Bohm and Jacopini1 had demonstrated that programs could be written without any goto statements. The challenge of the era was for programmers to shift their styles to “goto-less programming.” It was not until well into the 1970s that the programming profession started taking structured programming seriously. The results were impressive, as software development groups reported reduced development times, more frequent on-time delivery of systems and more frequent within-budget completion of software projects. Programs produced with structured techniques were clearer, easier to debug and modify and more likely to be bug free in the first place.

Bohm and Jacopini’s work demonstrated that all programs could be written in terms of only three control structures, namely the sequence structure, the selection structure and the repetition structure. The sequence structure is built into C. Unless directed oth- erwise, the computer executes C statements one after the other in the order in which they’re written. The flowchart segment of Fig. 3.1 illustrates C’s sequence structure.

int i;

1. Bohm, C., and G. Jacopini, “Flow Diagrams, Turing Machines, and Languages with Only Two For- mation Rules,” Communications of the ACM, Vol. 9, No. 5, May 1966, pp. 336–371.

3.4 Control Structures 57

A flowchart is a graphical representation of an algorithm or of a portion of an algo- rithm. Flowcharts are drawn using certain special-purpose symbols such as rectangles, diamonds, ovals, and small circles; these symbols are connected by arrows called flowlines.

Like pseudocode, flowcharts are useful for developing and representing algorithms, although pseudocode is preferred by most programmers. Flowcharts clearly show how control structures operate; that is all we use them for in this text.

Consider the flowchart for the sequence structure in Fig. 3.1. We use the rectangle symbol, also called the action symbol, to indicate any type of action including a calcula- tion or an input/output operation. The flowlines in the figure indicate the order in which the actions are performed—first, grade is added to total, then 1 is added to counter. C allows us to have as many actions as we want in a sequence structure. As we’ll soon see, anywhere a single action may be placed, we may place several actions in sequence.

When drawing a flowchart that represents a complete algorithm, an oval symbol con- taining the word “Begin” is the first symbol used in the flowchart; an oval symbol containing the word “End” is the last symbol used. When drawing only a portion of an algorithm as in Fig. 3.1, the oval symbols are omitted in favor of using small circle sym- bols, also called connector symbols.

Perhaps the most important flowcharting symbol is the diamond symbol, also called the decision symbol, which indicates that a decision is to be made. We’ll discuss the diamond symbol in the next section.

C provides three types of selection structures in the form of statements. The if selec- tion statement (Section 3.5) either performs (selects) an action if a condition is true or skips the action if the condition is false. The if…else selection statement (Section 3.6) performs an action if a condition is true and performs a different action if the condition is false. The switch selection statement (discussed in Chapter 4) performs one of many dif- ferent actions depending on the value of an expression. The if statement is called a single- selection statement because it selects or ignores a single action. The if…else statement is called a double-selection statement because it selects between two different actions. The switch statement is called a multiple-selection statement because it selects among many different actions.

C provides three types of repetition structures in the form of statements, namely while (Section 3.7), do…while, and for (both discussed in Chapter 4).

Fig. 3.1 | Flowcharting C’s sequence structure.

add 1 to counter

add grade to total total = total + grade;

counter = counter + 1;

58 Chapter 3 Structured Program Development in C

That is all there is. C has only seven control statements: sequence, three types of selection and three types of repetition. Each C program is formed by combining as many of each type of control statement as is appropriate for the algorithm the program imple- ments. As with the sequence structure of Fig. 3.1, we’ll see that the flowchart representa- tion of each control statement has two small circle symbols, one at the entry point to the control statement and one at the exit point. These single-entry/single-exit control state- ments make it easy to build programs. The control-statement flowchart segments can be attached to one another by connecting the exit point of one control statement to the entry point of the next. This is much like the way in which a child stacks building blocks, so we call this control-statement stacking. We’ll learn that there is only one other way control statements may be connected—a method called control-statement nesting. Thus, any C program we’ll ever need to build can be constructed from only seven different types of con- trol statements combined in only two ways. This is the essence of simplicity.

3.5 The if Selection Statement Selection structures are used to choose among alternative courses of action. For example, suppose the passing grade on an exam is 60. The pseudocode statement

determines if the condition “student’s grade is greater than or equal to 60” is true or false. If the condition is true, then “Passed” is printed, and the next pseudocode statement in order is “performed” (remember that pseudocode is not a real programming language). If the condition is false, the printing is ignored, and the next pseudocode statement in order is performed. The second line of this selection structure is indented. Such indentation is optional, but it’s highly recommended as it helps emphasize the inherent structure of structured programs. We’ll apply indentation conventions carefully throughout this text. The C compiler ignores white-space characters like blanks, tabs and newlines used for in- dentation and vertical spacing.

The preceding pseudocode If statement may be written in C as

Notice that the C code corresponds closely to the pseudocode. This is one of the prop- erties of pseudocode that makes it such a useful program development tool.

If student’s grade is greater than or equal to 60 Print “Passed”

Good Programming Practice 3.1 Consistently applying responsible indentation conventions greatly improves program read- ability. We suggest a fixed-size tab of about 1/4 inch or three blanks per indent. In this book, we use three blanks per indent.

if ( grade >= 60 ) { printf( "Passed\n" ); } /* end if */

Good Programming Practice 3.2 Pseudocode is often used to “think out” a program during the program design process. Then the pseudocode program is converted to C.

3.6 The if…else Selection Statement 59

The flowchart of Fig. 3.2 illustrates the single-selection if statement. This flowchart contains what is perhaps the most important flowcharting symbol—the diamond symbol, also called the decision symbol, which indicates that a decision is to be made. The decision symbol contains an expression, such as a condition, that can be either true or false. The decision symbol has two flowlines emerging from it. One indicates the direction to take when the expression in the symbol is true; the other indicates the direction to take when the expression is false. Decisions can be based on conditions containing relational or equality operators. In fact, a decision can be based on any expression—if the expression evaluates to zero, it’s treated as false, and if it evaluates to nonzero, it’s treated as true.

The if statement, too, is a single-entry/single-exit structure. We’ll soon learn that the flowcharts for the remaining control structures can also contain (besides small circle sym- bols and flowlines) only rectangle symbols to indicate the actions to be performed, and diamond symbols to indicate decisions to be made. This is the action/decision model of programming we’ve been emphasizing.

We can envision seven bins, each containing only control-statement flowcharts of one of the seven types. These flowchart segments are empty—nothing is written in the rectan- gles and nothing is written in the diamonds. Your task, then, is assembling a program from as many of each type of control statement as the algorithm demands, combining those control statements in only two possible ways (stacking or nesting), and then filling in the actions and decisions in a manner appropriate for the algorithm. We’ll discuss the variety of ways in which actions and decisions may be written.

3.6 The if…else Selection Statement The if selection statement performs an indicated action only when the condition is true; otherwise the action is skipped. The if…else selection statement allows you to specify that different actions are to be performed when the condition is true than when the con- dition is false. For example, the pseudocode statement

prints Passed if the student’s grade is greater than or equal to 60 and prints Failed if the student’s grade is less than 60. In either case, after printing occurs, the next pseudocode

Fig. 3.2 | Flowcharting the single-selection if statement.

If student’s grade is greater than or equal to 60 Print “Passed”

else Print “Failed”

grade >= 60 true

false

print “Passed”

60 Chapter 3 Structured Program Development in C

statement in sequence is “performed.” The body of the else is also indented. Whatever in- dentation convention you choose should be carefully applied throughout your programs. It’s difficult to read a program that does not obey uniform spacing conventions.

The preceding pseudocode If…else statement may be written in C as

The flowchart of Fig. 3.3 nicely illustrates the flow of control in the if…else state- ment. Once again, note that (besides small circles and arrows) the only symbols in the flowchart are rectangles (for actions) and a diamond (for a decision). We continue to emphasize this action/decision model of computing. Imagine again a deep bin containing as many empty double-selection statements (represented as flowchart segments) as might be needed to build any C program. Your job, again, is to assemble these selection state- ments (by stacking and nesting) with any other control statements required by the algo- rithm, and to fill in the empty rectangles and empty diamonds with actions and decisions appropriate to the algorithm being implemented.

C provides the conditional operator (?:) which is closely related to the if…else statement. The conditional operator is C’s only ternary operator—it takes three operands. The operands together with the conditional operator form a conditional expression. The first operand is a condition. The second operand is the value for the entire conditional expression if the condition is true and the third operand is the value for the entire condi- tional expression if the condition is false. For example, the printf statement

Good Programming Practice 3.3 Indent both body statements of an if…else statement.

Good Programming Practice 3.4 If there are several levels of indentation, each level should be indented the same additional amount of space.

if ( grade >= 60 ) { printf( "Passed\n" ); } /* end if */ else { printf( "Failed\n" ); } /* end else */

Fig. 3.3 | Flowcharting the double-selection if…else statement.

truefalse print “Failed” grade >= 60 print “Passed”

3.6 The if…else Selection Statement 61

contains a conditional expression that evaluates to the string literal "Passed" if the con- dition grade >= 60 is true and evaluates to the string literal "Failed" if the condition is false. The format control string of the printf contains the conversion specification %s for printing a character string. So the preceding printf statement performs in essentially the same way as the preceding if…else statement.

The second and third operands in a conditional expression can also be actions to be executed. For example, the conditional expression

is read, “If grade is greater than or equal to 60 then printf("Passed\n"), otherwise printf( "Failed\n" ).” This, too, is comparable to the preceding if…else statement. We’ll see that conditional operators can be used in some situations where if…else state- ments cannot.

Nested if…else statements test for multiple cases by placing if…else statements inside if…else statements. For example, the following pseudocode statement will print A for exam grades greater than or equal to 90, B for grades greater than or equal to 80, C for grades greater than or equal to 70, D for grades greater than or equal to 60, and F for all other grades.

This pseudocode may be written in C as

printf( "%s\n", grade >= 60 ? "Passed" : "Failed" );

grade >= 60 ? printf( "Passed\n" ) : printf( "Failed\n" );

If student’s grade is greater than or equal to 90 Print “A”

else If student’s grade is greater than or equal to 80

Print “B” else

If student’s grade is greater than or equal to 70 Print “C”

else If student’s grade is greater than or equal to 60

Print “D” else

Print “F”

if ( grade >= 90 ) printf( "A\n" ); else if ( grade >= 80 ) printf("B\n"); else if ( grade >= 70 ) printf("C\n"); else if ( grade >= 60 ) printf( "D\n" ); else printf( "F\n" );

62 Chapter 3 Structured Program Development in C

If the variable grade is greater than or equal to 90, the first four conditions will be true, but only the printf statement after the first test will be executed. After that printf is ex- ecuted, the else part of the “outer” if…else statement is skipped. Many C programmers prefer to write the preceding if statement as

As far as the C compiler is concerned, both forms are equivalent. The latter form is popular because it avoids the deep indentation of the code to the right. Such indentation often leaves little room on a line, forcing lines to be split and decreasing program readability.

The if selection statement expects only one statement in its body. To include several statements in the body of an if, enclose the set of statements in braces ({ and }). A set of statements contained within a pair of braces is called a compound statement or a block.

The following example includes a compound statement in the else part of an if…else statement.

In this case, if grade is less than 60, the program executes both printf statements in the body of the else and prints

Notice the braces surrounding the two statements in the else clause. These braces are im- portant. Without the braces, the statement

would be outside the body of the else part of the if, and would execute regardless of whether the grade was less than 60.

if ( grade >= 90 ) printf( "A\n" ); else if ( grade >= 80 ) printf( "B\n" ); else if ( grade >= 70 ) printf( "C\n" ); else if ( grade >= 60 ) printf( "D\n" ); else printf( "F\n" );

Software Engineering Observation 3.1 A compound statement can be placed anywhere in a program that a single statement can be placed.

if ( grade >= 60 ) { printf( "Passed.\n" ); } /* end if */ else { printf( "Failed.\n" ); printf( "You must take this course again.\n" ); } /* end else */

Failed. You must take this course again.

printf( "You must take this course again.\n" );

Common Programming Error 3.1 Forgetting one or both of the braces that delimit a compound statement.

3.7 The while Repetition Statement 63

A syntax error is caught by the compiler. A logic error has its effect at execution time. A fatal logic error causes a program to fail and terminate prematurely. A nonfatal logic error allows a program to continue executing but to produce incorrect results.

3.7 The while Repetition Statement A repetition statement allows you to specify that an action is to be repeated while some condition remains true. The pseudocode statement

describes the repetition that occurs during a shopping trip. The condition, “there are more items on my shopping list” may be true or false. If it’s true, then the action, “Purchase next item and cross it off my list” is performed. This action will be performed repeatedly while the condition remains true. The statement(s) contained in the while repetition statement constitute the body of the while. The while statement body may be a single statement or a compound statement.

Eventually, the condition will become false (when the last item on the shopping list has been purchased and crossed off the list). At this point, the repetition terminates, and the first pseudocode statement after the repetition structure is executed.

As an example of an actual while, consider a program segment designed to find the first power of 3 larger than 100. Suppose the integer variable product has been initialized

Software Engineering Observation 3.2 Just as a compound statement can be placed anywhere a single statement can be placed, it’s also possible to have no statement at all, i.e., the empty statement. The empty statement is represented by placing a semicolon (;) where a statement would normally be.

Common Programming Error 3.2 Placing a semicolon after the condition in an if statement as in if ( grade >= 60 ); leads to a logic error in single-selection if statements and a syntax error in double-selection if statements.

Error-Prevention Tip 3.1 Typing the beginning and ending braces of compound statements before typing the indi- vidual statements within the braces helps avoid omitting one or both of the braces, pre- venting syntax errors and logic errors (where both braces are indeed required).

While there are more items on my shopping list Purchase next item and cross it off my list

Common Programming Error 3.3 Not providing the body of a while statement with an action that eventually causes the condition in the while to become false. Normally, such a repetition structure will never terminate—an error called an “infinite loop.”

Common Programming Error 3.4 Spelling the keyword while with an uppercase W as in While (remember that C is a case- sensitive language). All of C’s reserved keywords such as while, if and else contain only lowercase letters.

64 Chapter 3 Structured Program Development in C

to 3. When the following while repetition statement finishes executing, product will con- tain the desired answer:

The flowchart of Fig. 3.4 nicely illustrates the flow of control in the while repetition statement. Once again, note that (besides small circles and arrows) the flowchart contains only a rectangle symbol and a diamond symbol. The flowchart clearly shows the repeti- tion. The flowline emerging from the rectangle wraps back to the decision, which is tested each time through the loop until the decision eventually becomes false. At this point, the while statement is exited and control passes to the next statement in the program.

When the while statement is entered, the value of product is 3. The variable product is repeatedly multiplied by 3, taking on the values 9, 27 and 81 successively. When product becomes 243, the condition in the while statement, product <= 100, becomes false. This terminates the repetition, and the final value of product is 243. Program exe- cution continues with the next statement after the while.

3.8 Formulating Algorithms Case Study 1: Counter- Controlled Repetition To illustrate how algorithms are developed, we solve several variations of a class averaging problem. Consider the following problem statement:

A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz.

The class average is equal to the sum of the grades divided by the number of students. The algorithm for solving this problem on a computer must input each of the grades, perform the averaging calculation, and print the result.

Let’s use pseudocode to list the actions to execute and specify the order in which these actions should execute. We use counter-controlled repetition to input the grades one at a time. This technique uses a variable called a counter to specify the number of times a set of statements should execute. In this example, repetition terminates when the counter exceeds 10. In this section we simply present the pseudocode algorithm (Fig. 3.5) and the corresponding C program (Fig. 3.6). In the next section we show how pseudocode algo-

product = 3;

while ( product <= 100 ) { product = 3 * product; } /* end while */

Fig. 3.4 | Flowcharting the while repetition statement.

product <= 1000 true

false

product = 2 * product

3.8 Counter-Controlled Repetition 65

rithms are developed. Counter-controlled repetition is often called definite repetition because the number of repetitions is known before the loop begins executing.

1 Set total to zero 2 Set grade counter to one 3 4 While grade counter is less than or equal to ten 5 Input the next grade 6 Add the grade into the total 7 Add one to the grade counter 8 9 Set the class average to the total divided by ten

10 Print the class average

Fig. 3.5 | Pseudocode algorithm that uses counter-controlled repetition to solve the class average problem.

1 /* Fig. 3.6: fig03_06.c 2 Class average program with */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main( void ) 7 { 8 9 int grade; /* grade value */

10 int total; /* sum of grades input by user */ 11 int average; /* average of grades */ 12 13 /* initialization phase */ 14 total = 0; /* initialize total */ 15 16 17 /* processing phase */ 18 while ( ) { /* loop 10 times */ 19 printf( "Enter grade: " ); /* prompt for input */ 20 scanf( "%d", &grade ); /* read grade from user */ 21 total = total + grade; /* add grade to total */ 22 23 } /* end while */ 24 25 /* termination phase */ 26 average = total / 10; /* integer division */ 27 28 printf( "Class average is %d\n", average ); /* display result */ 29 return 0; /* indicate program ended successfully */ 30 } /* end function main */

Fig. 3.6 | C program and sample execution for the class average problem with counter- controlled repetition. (Part 1 of 2.)

counter-controlled repetition

int counter; /* number of grade to be entered next */

counter = 1; /* initialize loop counter */

counter <= 10

counter = counter + 1; /* increment counter */

66 Chapter 3 Structured Program Development in C

Note the references in the algorithm to a total and a counter. A total is a variable used to accumulate the sum of a series of values. A counter is a variable used to count—in this case, to count the number of grades entered. Variables used to store totals should normally be initialized to zero before being used in a program; otherwise the sum would include the previous value stored in the total’s memory location. Counter variables are normally ini- tialized to zero or one, depending on their use (we’ll present examples showing each of these uses). An uninitialized variable contains a “garbage” value—the value last stored in the memory location reserved for that variable.

The averaging calculation in the program produced an integer result of 81. Actually, the sum of the grades in this example is 817, which when divided by 10 should yield 81.7, i.e., a number with a decimal point. We’ll see how to deal with such numbers (called floating-point numbers) in the next section.

3.9 Formulating Algorithms with Top-Down, Stepwise Refinement Case Study 2: Sentinel-Controlled Repetition Let’s generalize the class average problem. Consider the following problem:

Develop a class averaging program that will process an arbitrary number of grades each time the program is run.

In the first class average example, the number of grades (10) was known in advance. In this example, no indication is given of how many grades are to be entered. The program must process an arbitrary number of grades. How can the program determine when to stop the input of grades? How will it know when to calculate and print the class average?

Enter grade: 98 Enter grade: 76 Enter grade: 71 Enter grade: 87 Enter grade: 83 Enter grade: 90 Enter grade: 57 Enter grade: 79 Enter grade: 82 Enter grade: 94 Class average is 81

Common Programming Error 3.5 If a counter or total is not initialized, the results of your program will probably be incor- rect. This is an example of a logic error.

Error-Prevention Tip 3.2 Initialize all counters and totals.

Fig. 3.6 | C program and sample execution for the class average problem with counter- controlled repetition. (Part 2 of 2.)

3.9 Sentinel-Controlled Repetition 67

One way to solve this problem is to use a special value called a sentinel value (also called a signal value, a dummy value, or a flag value) to indicate “end of data entry.” The user types in grades until all legitimate grades have been entered. The user then types the sentinel value to indicate that the last grade has been entered. Sentinel-controlled repeti- tion is often called indefinite repetition because the number of repetitions is not known before the loop begins executing.

Clearly, the sentinel value must be chosen so that it cannot be confused with an acceptable input value. Since grades on a quiz are normally nonnegative integers, –1 is an acceptable sentinel value for this problem. Thus, a run of the class average program might process a stream of inputs such as 95, 96, 75, 74, 89 and –1. The program would then compute and print the class average for the grades 95, 96, 75, 74, and 89 (–1 is the sentinel value, so it should not enter into the averaging calculation).

We approach the class average program with a technique called top-down, stepwise refinement, a technique that is essential to the development of well-structured programs. We begin with a pseudocode representation of the top:

The top is a single statement that conveys the program’s overall function. As such, the top is, in effect, a complete representation of a program. Unfortunately, the top rarely conveys a sufficient amount of detail for writing the C program. So we now begin the refinement process. We divide the top into a series of smaller tasks and list these in the order in which they need to be performed. This results in the following first refinement.

Here, only the sequence structure has been used—the steps listed are to be executed in or- der, one after the other.

To proceed to the next level of refinement, i.e., the second refinement, we commit to specific variables. We need a running total of the numbers, a count of how many numbers have been processed, a variable to receive the value of each grade as it’s input and a variable to hold the calculated average. The pseudocode statement

may be refined as follows:

Common Programming Error 3.6 Choosing a sentinel value that is also a legitimate data value.

Determine the class average for the quiz

Initialize variables Input, sum, and count the quiz grades Calculate and print the class average

Software Engineering Observation 3.3 Each refinement, as well as the top itself, is a complete specification of the algorithm; only the level of detail varies.

Initialize variables

Initialize total to zero Initialize counter to zero

68 Chapter 3 Structured Program Development in C

Notice that only total and counter need to be initialized; the variables average and grade (for the calculated average and the user input, respectively) need not be initialized because their values will be written over by the process of destructive read-in discussed in Chapter 2. The pseudocode statement

requires a repetition structure (i.e., a loop) that successively inputs each grade. Since we do not know in advance how many grades are to be processed, we’ll use sentinel-controlled repetition. The user will type legitimate grades in one at a time. After the last legitimate grade is typed, the user will type the sentinel value. The program will test for this value after each grade is input and will terminate the loop when the sentinel is entered. The re- finement of the preceding pseudocode statement is then

Notice that in pseudocode, we do not use braces around the set of statements that form the body of the while statement. We simply indent all these statements under the while to show that they all belong to the while. Again, pseudocode is only an informal pro- gram development aid.

The pseudocode statement

may be refined as follows:

Notice that we’re being careful here to test for the possibility of division by zero—a fatal error that if undetected would cause the program to fail (often called “bombing” or “crashing”). The complete second refinement is shown in Fig. 3.7.

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. Actually, the blank lines separate these programs into their various phases.

Input, sum, and count the quiz grades

Input the first grade While the user has not as yet entered the sentinel

Add this grade into the running total Add one to the grade counter Input the next grade (possibly the sentinel)

Calculate and print the class average

If the counter is not equal to zero Set the average to the total divided by the counter Print the average

else Print “No grades were entered”

Common Programming Error 3.7 An attempt to divide by zero causes a fatal error.

Good Programming Practice 3.5 When performing division by an expression whose value could be zero, explicitly test for this case and handle it appropriately in your program (such as printing an error message) rather than allowing the fatal error to occur.

3.9 Sentinel-Controlled Repetition 69

The pseudocode algorithm in Fig. 3.7 solves the more general class averaging problem. This algorithm was developed after only two levels of refinement. Sometimes more levels are necessary.

The C program and a sample execution are shown in Fig. 3.8. Although only integer grades are entered, the averaging calculation is likely to produce a decimal number with a decimal point. The type int cannot represent such a number. The program introduces the data type float to handle numbers with decimal points (called floating-point numbers) and introduces a special operator called a cast operator to handle the averaging calculation. These features are explained in detail after the program is presented.

1 Initialize total to zero 2 Initialize counter to zero 3 4 Input the first grade 5 While the user has not as yet entered the sentinel 6 Add this grade into the running total 7 Add one to the grade counter 8 Input the next grade (possibly the sentinel) 9

10 If the counter is not equal to zero 11 Set the average to the total divided by the counter 12 Print the average 13 else 14 Print “No grades were entered”

Fig. 3.7 | Pseudocode algorithm that uses sentinel-controlled repetition to solve the class average problem.

Software Engineering Observation 3.4 Many programs can be divided logically into three phases: an initialization phase that initializes the program variables; a processing phase that inputs data values and adjusts program variables accordingly; and a termination phase that calculates and prints the final results.

Software Engineering Observation 3.5 You terminate the top-down, stepwise refinement process when the pseudocode algorithm is specified in sufficient detail for you to be able to convert the pseudocode to C. Implementing the C program is then normally straightforward.

1 /* Fig. 3.8: fig03_08.c 2 Class average program with sentinel-controlled repetition */ 3 #include <stdio.h> 4

Fig. 3.8 | C program and sample execution for the class average problem with sentinel- controlled repetition. (Part 1 of 3.)

70 Chapter 3 Structured Program Development in C

5 /* function main begins program execution */ 6 int main( void ) 7 { 8 int counter; /* number of grades entered */ 9 int grade; /* grade value */

10 int total; /* sum of grades */ 11 12 ; /* number with decimal point for average */ 13 14 /* initialization phase */ 15 total = 0; /* initialize total */ 16 ; /* initialize loop counter */ 17 18 /* processing phase */ 19 /* get first grade from user */ 20 printf( "Enter grade, -1 to end: " ); /* prompt for input */ 21 scanf( "%d", &grade ); /* read grade from user */ 22 23 /* loop while sentinel value not yet read from user */ 24 while ( ) { 25 total = total + grade; /* add grade to total */ 26 counter = counter + 1; /* increment counter */ 27 28 /* get next grade from user */ 29 30 31 } /* end while */ 32 33 /* termination phase */ 34 /* if user entered at least one grade */ 35 if ( ) { 36 37 /* calculate average of all grades entered */ 38 average = / counter; /* avoid truncation */ 39 40 /* display average with two digits of precision */ 41 printf( "Class average is \n", average ); 42 } /* end if */ 43 else { /* if no grades were entered, output message */ 44 printf( "No grades were entered\n" ); 45 } /* end else */ 46 47 return 0; /* indicate program ended successfully */ 48 } /* end function main */

Enter grade, -1 to end: 75 Enter grade, -1 to end: 94 Enter grade, -1 to end: 97 Enter grade, -1 to end: 88 Enter grade, -1 to end: 70 Enter grade, -1 to end: 64

Fig. 3.8 | C program and sample execution for the class average problem with sentinel- controlled repetition. (Part 2 of 3.)

float average

counter = 0

grade != -1

printf( "Enter grade, -1 to end: " ); /* prompt for input */ scanf("%d", &grade); /* read next grade */

counter != 0

( float ) total

%.2f

3.9 Sentinel-Controlled Repetition 71

Notice the compound statement in the while loop (line 24) in Fig. 3.8 Once again, the braces are necessary for all four statements to be executed within the loop. Without the braces, the last three statements in the body of the loop would fall outside the loop, causing the computer to interpret this code incorrectly as follows.

This would cause an infinite loop if the user did not input -1 for the first grade.

Averages do not always evaluate to integer values. Often, an average is a value such as 7.2 or –93.5 that contains a fractional part. These values are referred to as floating-point numbers and are represented by the data type float. The variable average is defined to be of type float (line 12) to capture the fractional result of our calculation. However, the result of the calculation total / counter is an integer because total and counter are both integer variables. Dividing two integers results in integer division in which any fractional part of the calculation is lost (i.e., truncated). Since the calculation is performed first, the fractional part is lost before the result is assigned to average. To produce a floating-point calculation with integer values, we must create temporary values that are floating-point numbers. C provides the unary cast operator to accomplish this task. Line 38

includes the cast operator (float), which creates a temporary floating-point copy of its operand, total. The value stored in total is still an integer. Using a cast operator in this manner is called explicit conversion. The calculation now consists of a floating-point val- ue (the temporary float version of total) divided by the integer value stored in counter. Most computers can evaluate arithmetic expressions only in which the data types of the operands are identical. To ensure that the operands are of the same type, the compiler per- forms an operation called promotion (also called implicit conversion) on selected oper-

Enter grade, -1 to end: 83 Enter grade, -1 to end: 89 Enter grade, -1 to end: -1 Class average is 82.50

Enter grade, -1 to end: -1 No grades were entered

while ( grade != -1 ) total = total + grade; /* add grade to total */ counter = counter + 1; /* increment counter */ printf( "Enter grade, -1 to end: " ); /* prompt for input */ scanf( "%d", &grade ); /* read next grade */

Good Programming Practice 3.6 In a sentinel-controlled loop, the prompts requesting data entry should explicitly remind the user what the sentinel value is.

average = ( float ) total / counter;

Fig. 3.8 | C program and sample execution for the class average problem with sentinel- controlled repetition. (Part 3 of 3.)

72 Chapter 3 Structured Program Development in C

ands. For example, in an expression containing the data types int and float, copies of int operands are made and promoted to float. In our example, after a copy of counter is made and promoted to float, the calculation is performed and the result of the floating- point division is assigned to average. C provides a set of rules for promotion of operands of different types. Chapter 5 presents a discussion of all the standard data types and their order of promotion.

Cast operators are available for most data types. The cast operator is formed by placing parentheses around a data type name. The cast operator is a unary operator, i.e., an operator that takes only one operand. In Chapter 2, we studied the binary arithmetic oper- ators. C also supports unary versions of the plus (+) and minus (-) operators, so you can write expressions like -7 or +5. Cast operators associate from right to left and have the same precedence as other unary operators such as unary + and unary -. This precedence is one level higher than that of the multiplicative operators *, / and %.

Figure 3.8 uses the printf conversion specifier %.2f (line 41) to print the value of average. The f specifies that a floating-point value will be printed. The .2 is the precision with which the value will be displayed—with 2 digits to the right of the decimal point. If the %f conversion specifier is used (without specifying the precision), the default precision of 6 is used—exactly as if the conversion specifier %.6f had been used. When floating- point values are printed with precision, the printed value is rounded to the indicated number of decimal positions. The value in memory is unaltered. When the following statements are executed, the values 3.45 and 3.4 are printed.

Despite the fact that floating-point numbers are not always “100% precise,” they have numerous applications. For example, when we speak of a “normal” body temperature of 98.6, we do not need to be precise to a large number of digits. When we view the temper- ature on a thermometer and read it as 98.6, it may actually be 98.5999473210643. The point here is that calling this number simply 98.6 is fine for most applications. We’ll say more about this issue later.

Another way floating-point numbers develop is through division. When we divide 10 by 3, the result is 3.3333333… with the sequence of 3s repeating infinitely. The computer allocates only a fixed amount of space to hold such a value, so clearly the stored floating- point value can be only an approximation.

printf( "%.2f\n", 3.446 ); /* prints 3.45 */ printf( "%.1f\n", 3.446 ); /* prints 3.4 */

Common Programming Error 3.8 Using precision in a conversion specification in the format control string of a scanf state- ment is wrong. Precisions are used only in printf conversion specifications.

Common Programming Error 3.9 Using floating-point numbers in a manner that assumes they’re represented precisely can lead to incorrect results. Floating-point numbers are represented only approximately by most computers.

Error-Prevention Tip 3.3 Do not compare floating-point values for equality.

3.10 Nested Control Structures 73

3.10 Formulating Algorithms with Top-Down, Stepwise Refinement Case Study 3: Nested Control Structures Let’s work another complete problem. We’ll once again formulate the algorithm using pseudocode and top-down, stepwise refinement, and write a corresponding C program. We’ve seen that control statements may be stacked on top of one another (in sequence) just as a child stacks building blocks. In this case study we’ll see the only other structured way control statements may be connected in C, namely through nesting of one control statement within another.

Consider the following problem statement:

A college offers a course that prepares students for the state licensing exam for real estate brokers. Last year, 10 of the students who completed this course took the licens- ing examination. Naturally, the college wants to know how well its students did on the exam. You have been asked to write a program to summarize the results. You have been given a list of these 10 students. Next to each name a 1 is written if the student passed the exam and a 2 if the student failed.

Your program should analyze the results of the exam as follows:

1. Input each test result (i.e., a 1 or a 2). Display the prompting message “Enter result” each time the program requests another test result.

2. Count the number of test results of each type.

3. Display a summary of the test results indicating the number of students who passed and the number who failed.

4. If more than eight students passed the exam, print the message “Bonus to instructor!”

After reading the problem statement carefully, we make the following observations:

1. The program must process 10 test results. A counter-controlled loop will be used.

2. Each test result is a number—either a 1 or a 2. Each time the program reads a test result, the program must determine if the number is a 1 or a 2. We test for a 1 in our algorithm. If the number is not a 1, we assume that it’s a 2. (An exercise at the end of the chapter considers the consequences of this assumption.)

3. Two counters are used—one to count the number of students who passed the exam and one to count the number of students who failed the exam.

4. After the program has processed all the results, it must decide if more than 8 stu- dents passed the exam.

Let’s proceed with top-down, stepwise refinement. We begin with a pseudocode rep- resentation of the top:

Once again, it’s important to emphasize that the top is a complete representation of the program, but several refinements are likely to be needed before the pseudocode can be nat- urally evolved into a C program. Our first refinement is

Analyze exam results and decide if instructor should receive a bonus

Initialize variables Input the ten quiz grades and count passes and failures Print a summary of the exam results and decide if instructor should receive a bonus

74 Chapter 3 Structured Program Development in C

Here, too, even though we have a complete representation of the entire program, further refinement is necessary. We now commit to specific variables. Counters are needed to re- cord the passes and failures, a counter will be used to control the looping process, and a variable is needed to store the user input. The pseudocode statement

may be refined as follows:

Notice that only the counters and totals are initialized. The pseudocode statement

requires a loop that successively inputs the result of each exam. Here it’s known in advance that there are precisely ten exam results, so counter-controlled looping is appropriate. In- side the loop (i.e., nested within the loop) a double-selection statement will determine whether each exam result is a pass or a failure, and will increment the appropriate counters accordingly. The refinement of the preceding pseudocode statement is then

Notice the use of blank lines to set off the If…else to improve program readability. The pseudocode statement

may be refined as follows:

The complete second refinement appears in Fig. 3.9. Notice that blank lines are also used to set off the while statement for program readability.

This pseudocode is now sufficiently refined for conversion to C. The C program and two sample executions are shown in Fig. 3.10. We’ve taken advantage of a feature of C that allows initialization to be incorporated into definitions. Such initialization occurs at compile time.

Initialize variables

Initialize passes to zero Initialize failures to zero Initialize student to one

Input the ten quiz grades and count passes and failures

While student counter is less than or equal to ten Input the next exam result

If the student passed Add one to passes

else Add one to failures

Add one to student counter

Print a summary of the exam results and decide if instructor should receive a bonus

Print the number of passes Print the number of failures If more than eight students passed

Print “Bonus to instructor!”

3.10 Nested Control Structures 75

1 Initialize passes to zero 2 Initialize failures to zero 3 Initialize student to one 4 5 While student counter is less than or equal to ten 6 Input the next exam result 7 8 If the student passed 9 Add one to passes

10 else 11 Add one to failures 12 13 Add one to student counter 14 15 Print the number of passes 16 Print the number of failures 17 If more than eight students passed 18 Print “Bonus to instructor!”

Fig. 3.9 | Pseudocode for examination results problem.

Performance Tip 3.1 Initializing variables when they’re defined can help reduce a program’s execution time.

Performance Tip 3.2 Many of the performance tips we mention in this text result in nominal improvements, so the reader may be tempted to ignore them. The cumulative effect of all these performance enhancements can make a program perform significantly faster. Also, significant improve- ment is realized when a supposedly nominal improvement is placed in a loop that may repeat a large number of times.

1 /* Fig. 3.10: fig03_10.c 2 Analysis of examination results */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main( void ) 7 { 8 /* initialize variables in definitions */ 9 int passes = 0; /* number of passes */

10 int failures = 0; /* number of failures */ 11 int student = 1; /* student counter */ 12 int result; /* one exam result */ 13

Fig. 3.10 | C program and sample executions for examination results problem. (Part 1 of 3.)

76 Chapter 3 Structured Program Development in C

14 /* process 10 students using counter-controlled loop */ 15 while ( student <= 10 ) { 16 17 /* prompt user for input and obtain value from user */ 18 printf( "Enter result ( 1=pass,2=fail ): " ); 19 scanf( "%d", &result ); 20 21 /* if result 1, increment passes */ 22 23 passes = passes + 1; 24 } /* end if */ 25 /* otherwise, increment failures */ 26 failures = failures + 1; 27 } /* end else */ 28 29 student = student + 1; /* increment student counter */ 30 } /* end while */ 31 32 /* termination phase; display number of passes and failures */ 33 printf( "Passed %d\n", passes ); 34 printf( "Failed %d\n", failures ); 35 36 /* if more than eight students passed, print "Bonus to instructor!" */ 37 if ( passes > 8 ) { 38 printf( "Bonus to instructor!\n" ); 39 } /* end if */ 40 41 return 0; /* indicate program ended successfully */ 42 } /* end function main */

Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 2 Enter Result (1=pass,2=fail): 2 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 2 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 2 Passed 6 Failed 4

Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 2 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 1

Fig. 3.10 | C program and sample executions for examination results problem. (Part 2 of 3.)

if ( result == 1 ) {

else {

3.11 Assignment Operators 77

3.11 Assignment Operators C provides several assignment operators for abbreviating assignment expressions. For ex- ample, the statement

can be abbreviated with the addition assignment operator += as

The += operator adds the value of the expression on the right of the operator to the value of the variable on the left of the operator and stores the result in the variable on the left of the operator. Any statement of the form

where operator is one of the binary operators +, -, *, / or % (or others we’ll discuss in Chapter 10), can be written in the form

Thus the assignment c += 3 adds 3 to c. Figure 3.11 shows the arithmetic assignment operators, sample expressions using these operators and explanations.

Passed 9 Failed 1 Bonus to instructor!

Software Engineering Observation 3.6 Experience has shown that the most difficult part of solving a problem on a computer is developing the algorithm for the solution. Once a correct algorithm has been specified, the process of producing a working C program is normally straightforward.

Software Engineering Observation 3.7 Many programmers write programs without ever using program development tools such as pseudocode. They feel that their ultimate goal is to solve the problem on a computer and that writing pseudocode merely delays the production of final outputs.

c = c + 3;

c += 3;

variable = variable operator expression;

variable operator= expression;

Assignment operator Sample expression Explanation Assigns

Assume: int c = 3, d = 5, e = 4, f = 6, g = 12; += c += 7 c = c + 7 10 to c

-= d -= 4 d = d - 4 1 to d *= e *= 5 e = e * 5 20 to e

/= f /= 3 f = f / 3 2 to f

%= g %= 9 g = g % 9 3 to g

Fig. 3.11 | Arithmetic assignment operators.

Fig. 3.10 | C program and sample executions for examination results problem. (Part 3 of 3.)

78 Chapter 3 Structured Program Development in C

3.12 Increment and Decrement Operators C also provides the unary increment operator, ++, and the unary decrement operator, --, which are summarized in Fig. 3.12. If a variable c is incremented by 1, the increment operator ++ can be used rather than the expressions c = c + 1 or c += 1. If increment or dec- rement operators are placed before a variable (i.e., prefixed), they’re referred to as the pre- increment or predecrement operators, respectively. If increment or decrement operators are placed after a variable (i.e., postfixed), they’re referred to as the postincrement or post- decrement operators, respectively. Preincrementing (predecrementing) a variable causes the variable to be incremented (decremented) by 1, then the new value of the variable is used in the expression in which it appears. Postincrementing (postdecrementing) the vari- able causes the current value of the variable to be used in the expression in which it appears, then the variable value is incremented (decremented) by 1.

Figure 3.13 demonstrates the difference between the preincrementing and the postin- crementing versions of the ++ operator. Postincrementing the variable c causes it to be incremented after it’s used in the printf statement. Preincrementing the variable c causes it to be incremented before it’s used in the printf statement.

Operator Sample expression Explanation

++ ++a Increment a by 1, then use the new value of a in the expression in which a resides.

++ a++ Use the current value of a in the expression in which a resides, then increment a by 1.

-- --b Decrement b by 1, then use the new value of b in the expression in which b resides.

-- b-- Use the current value of b in the expression in which b resides, then decrement b by 1.

Fig. 3.12 | Increment and decrement operators

1 /* Fig. 3.13: fig03_13.c 2 Preincrementing and postincrementing */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main( void ) 7 { 8 int c; /* define variable */ 9

10 /* demonstrate postincrement */ 11 c = 5; /* assign 5 to c */ 12 printf( "%d\n", c ); /* print 5 */ 13 14

Fig. 3.13 | Preincrementing vs. postincrementing. (Part 1 of 2.)

printf( "%d\n", c++ ); /* print 5 then postincrement */ printf( "%d\n\n", c ); /* print 6 */

3.12 Increment and Decrement Operators 79

The program displays the value of c before and after the ++ operator is used. The dec- rement operator (--) works similarly.

The three assignment statements in Fig. 3.10

can be written more concisely with assignment operators as

with preincrement operators as

or with postincrement operators as

It’s important to note here that when incrementing or decrementing a variable in a statement by itself, the preincrement and postincrement forms have the same effect. It’s only when a variable appears in the context of a larger expression that preincrementing and postincrementing have different effects (and similarly for predecrementing and post- decrementing). Of the expressions we’ve studied thus far, only a simple variable name may be used as the operand of an increment or decrement operator.

15 16 /* demonstrate preincrement */ 17 c = 5; /* assign 5 to c */ 18 printf( "%d\n", c ); /* print 5 */ 19 20 21 return 0; /* indicate program ended successfully */ 22 } /* end function main */

5 5 6 s 5 6 6

Good Programming Practice 3.7 Unary operators should be placed directly next to their operands with no intervening spaces.

passes = passes + 1; failures = failures + 1; student = student + 1;

passes += 1; failures += 1; student += 1;

++passes; ++failures; ++student;

passes++; failures++; student++;

Fig. 3.13 | Preincrementing vs. postincrementing. (Part 2 of 2.)

printf( "%d\n", ++c ); /* preincrement then print 6 */ printf( "%d\n", c ); /* print 6 */

80 Chapter 3 Structured Program Development in C

Figure 3.14 lists the precedence and associativity of the operators introduced to this point. The operators are shown top to bottom in decreasing order of precedence. The second column describes the associativity of the operators at each level of precedence. Notice that the conditional operator (?:), the unary operators increment (++), decrement (--), plus (+), minus (-) and casts, and the assignment operators =, +=, -=, *=, /= and %= associate from right to left. The third column names the various groups of operators. All other operators in Fig. 3.14 associate from left to right.

Common Programming Error 3.10 Attempting to use the increment or decrement operator on an expression other than a sim- ple variable name is a syntax error, e.g., writing ++(x + 1).

Error-Prevention Tip 3.4 C generally does not specify the order in which an operator’s operands will be evaluated (although we’ll see exceptions to this for a few operators in Chapter 4). Therefore you should avoid using statements with increment or decrement operators in which a partic- ular variable being incremented or decremented appears more than once.

Operators Associativity Type

++ (postfix) -- (postfix) right to left postfix

+ - (type) ++ (prefix) -- (prefix) right to left unary

* / % left to right multiplicative

+ - left to right additive

< <= > >= left to right relational

== != left to right equality

?: right to left conditional

= += -= *= /= %= right to left assignment

Fig. 3.14 | Precedence and associativity of the operators encountered so far in the text.

Summary Section 3.1 Introduction • Before writing a program to solve a particular problem, it’s essential to have a thorough under-

standing of the problem and a carefully planned approach to solving the problem.

Section 3.2 Algorithms • The solution to any computing problem involves executing a series of actions in a specific order.

• A procedure for solving a problem in terms of the actions to be executed, and the order in which these actions are to be executed, is called an algorithm.

• The order in which actions are to be executed is important.

Section 3.3 Pseudocode • Pseudocode is an artificial and informal language that helps you develop algorithms.

Summary 81

• Pseudocode is similar to everyday English; it’s not an actual computer programming language.

• Pseudocode programs help you “think out” a program before attempting to write it in a program- ming language such as C.

• Pseudocode consists purely of characters; you may type pseudocode using an editor.

• Carefully prepared pseudocode programs may be converted easily to corresponding C programs.

• Pseudocode consists only of action statements.

Section 3.4 Control Structures • Normally, statements in a program execute one after the other in the order in which they’re writ-

ten. This is called sequential execution.

• Various C statements enable you to specify that the next statement to execute may be other than the next one in sequence. This is called transfer of control.

• Structured programming has become almost synonymous with “goto elimination.”

• Structured programs are clearer, easier to debug and modify and more likely to be bug free.

• All programs can be written in terms of only three control structures—sequence, selection and repetition.

• Unless directed otherwise, the computer automatically executes C statements in sequence.

• A flowchart is a graphical representation of an algorithm. They’re drawn using rectangles, di- amonds, ovals and small circles, connected by arrows called flowlines.

• The rectangle (action) symbol indicates any type of action including a calculation or an input/ output operation.

• Flowlines indicate the order in which the actions are performed.

• When drawing a flowchart that represents a complete algorithm, an oval symbol containing the word “Begin” is the first symbol used in the flowchart; an oval symbol containing the word “End” is the last symbol used. When drawing only a portion of an algorithm, the oval symbols are omitted in favor of using small circle symbols also called connector symbols.

• The diamond (decision) symbol indicates that a decision is to be made.

• C provides three types of selection structures in the form of statements. The if selection state- ment either performs (selects) an action if a condition is true or skips the action if the condition is false. The if…else selection statement performs an action if a condition is true and performs a different action if the condition is false. The switch selection statement performs one of many different actions depending on the value of an expression.

• The if statement is called a single-selection statement because it selects or ignores a single action.

• The if…else statement is called a double-selection statement because it selects between two dif- ferent actions.

• The switch statement is called a multiple-selection statement because it selects among many dif- ferent actions.

• C provides three types of repetition structures in the form of statements, namely while, do…while and for.

• Control statement flowchart segments can be attached to one another with control-statement stacking—connecting the exit point of one control statement to the entry point of the next.

• There is only one other way control statements may be connected—control-statement nesting.

Section 3.5 The if Selection Statement • Selection structures are used to choose among alternative courses of action.

82 Chapter 3 Structured Program Development in C

• The decision symbol contains an expression, such as a condition, that can be either true or false. The decision symbol has two flowlines emerging from it. One indicates the direction to be taken when the expression is true; the other indicates the direction when the expression is false.

• A decision can be based on any expression—if the expression evaluates to zero, it’s treated as false, and if the expression evaluates to nonzero, it’s treated as true.

• The if statement is a single-entry/single-exit structure.

Section 3.6 The if…else Selection Statement • C provides the conditional operator (?:) which is closely related to the if…else statement.

• The conditional operator is C’s only ternary operator—it takes three operands. The operands to- gether with the conditional operator form a conditional expression. The first operand is a condi- tion. The second operand is the value for the conditional expression if the condition is true, and the third operand is the value for the conditional expression if the condition is false.

• The values in a conditional expression can also be actions to execute.

• Nested if…else statements test for multiple cases by placing if…else statements inside if…else statements.

• The if selection statement expects only one statement in its body. To include several statements in the body of an if, enclose the set of statements in braces ({ and }).

• A set of statements contained within a pair of braces is called a compound statement or a block.

• A syntax error is caught by the compiler. A logic error has its effect at execution time. A fatal logic error causes a program to fail and terminate prematurely. A nonfatal logic error allows a program to continue executing but to produce incorrect results.

Section 3.7 The while Repetition Statement • The while repetition statement specifies that an action is to be repeated while a condition is true.

Eventually, the condition will become false. At this point, the repetition terminates, and the first statement after the repetition statement executes.

Section 3.8 Formulating Algorithms Case Study 1: Counter-Controlled Repetition • Counter-controlled repetition uses a variable called a counter to specify the number of times a

set of statements should execute.

• Counter-controlled repetition is often called definite repetition because the number of repeti- tions is known before the loop begins executing.

• A total is a variable used to accumulate the sum of a series of values. Variables used to store totals should normally be initialized to zero before being used in a program; otherwise the sum would include the previous value stored in the total’s memory location.

• A counter is a variable used to count. Counter variables are normally initialized to zero or one, depending on their use.

• An uninitialized variable contains a “garbage” value—the value last stored in the memory loca- tion reserved for that variable.

Section 3.9 Formulating Algorithms with Top-Down, Stepwise Refinement Case Study 2: Sentinel-Controlled Repetition • A sentinel value (also called a signal value, a dummy value, or a flag value) is used in a sentinel-

controlled loop to indicate the “end of data entry.”

• Sentinel-controlled repetition is often called indefinite repetition because the number of repeti- tions is not known before the loop begins executing.

• The sentinel value must be chosen so that it cannot be confused with an acceptable input value.

Summary 83

• Top-down, stepwise refinement is essential to the development of well-structured programs.

• The top is a statement that conveys the program’s overall function. It’s a complete representation of a program. The top rarely conveys a sufficient amount of detail for writing a C program. In the refinement process, we divide the top into smaller tasks and list these in execution order.

• The type float represents numbers with decimal points (called floating-point numbers).

• When dividing two integers any fractional part of the result is truncated.

• To produce a floating-point calculation with integer values, you must cast the integers to float- ing-point numbers. C provides the unary cast operator (float) to accomplish this task.

• Cast operators perform explicit conversions.

• Most computers can evaluate arithmetic expressions only in which the operands’ data types are identical. To ensure this, the compiler performs an operation called promotion (also called im- plicit conversion) on selected operands. For example, in an expression containing the data types int and float, copies of int operands are made and promoted to float.

• Cast operators are available for most data types. A cast operator is formed by placing parentheses around a data type name. The cast operator is a unary operator, i.e., it takes only one operand.

• Cast operators associate from right to left and have the same precedence as other unary operators such as unary + and unary -. This precedence is one level higher than that of *, / and %.

• The printf conversion specifier %.2f specifies that a floating-point value will be displayed with two digits to the right of the decimal point. If the %f conversion specifier is used (without spec- ifying the precision), the default precision of 6 is used.

• When floating-point values are printed with precision, the printed value is rounded to the indi- cated number of decimal positions for display purposes.

Section 3.11 Assignment Operators • C provides several assignment operators for abbreviating assignment expressions.

• The += operator adds the value of the expression on the right of the operator to the value of the variable on the left of the operator and stores the result in the variable on the left of the operator.

• Any statement of the form

variable = variable operator expression;

where operator is one of the binary operators +, -, *, / or % (or others we’ll discuss in Chapter 10), can be written in the form

variable operator= expression;

Section 3.12 Increment and Decrement Operators • C provides the unary increment operator, ++, and the unary decrement operator, --.

• If increment or decrement operators are placed before a variable, they’re referred to as the prein- crement or predecrement operators, respectively. If increment or decrement operators are placed after a variable, they’re referred to as the postincrement or postdecrement operators, respectively.

• Preincrementing (predecrementing) a variable causes the variable to be incremented (decrement- ed) by 1, then the new value of the variable is used in the expression in which it appears.

• Postincrementing (postdecrementing) a variable uses the current value of the variable in the ex- pression in which it appears, then the variable value is incremented (decremented) by 1.

• When incrementing or decrementing a variable in a statement by itself, the preincrement and postincrement forms have the same effect. When a variable appears in the context of a larger ex- pression, preincrementing and postincrementing have different effects (and similarly for predec- rementing and postdecrementing).

84 Chapter 3 Structured Program Development in C

Terminology ?: conditional operator 60 * multiplication operator 72 *= multiplication assignment operator 77 / division operator 72 /= division assignment operator 77 % remainder operator 72 %= remainder assignment operator 77 -- decrement operator 78 ++ increment operator 78 += addition assignment operator 77 -= subtraction assignment operator 77 action 55 action symbol 57 addition assignment operator (+=) 77 algorithm 55 block 62 “bombing” 68 cast operator 71 compound statement 62 conditional expression 60 conditional operator (?:) 60 connector symbol 57 control statement stacking 58 control structure 56 counter 64 counter-controlled repetition 64 “crashing” 68 decision symbol 57 default precision 72 definite repetition 65 diamond symbol 57, 59 double-selection statement 57 dummy value 67 explicit conversion 71 fatal error 68 first refinement 67 flag value 67 float 69 floating-point number 69 flowchart 56 flowline 57 “garbage value” 66

goto elimination 56 goto statement 56 implicit conversion 71 indefinite repetition 67 integer division 71 multiple-selection statement 57 multiplicative operator 72 nested statements 74 nested if...else statement 61 nesting statements 73 order 55 oval symbol 57 postdecrement operator (--) 78 postincrement operator (++) 78 precision 72 predecrement operator (--) 78 preincrement operator(++) 78 procedure 55 program control 55 promotion 71 pseudocode 55 rectangle symbol 57 repetition statement 63 repetition structure 56 rounded 72 second refinement 67 selection structure 56 sentinel value 67 sequence structure 56 sequential execution 56 signal value 67 single-selection statement 57 single-entry/single-exit control statement 58 small circle symbols 57 top 67 top-down, stepwise refinement 67 total 66 transfer of control 56 truncated 71 unary operator 72 while repetition statement 63 white-space character 58

Self-Review Exercises 3.1 Fill in the blanks in each of the following questions.

a) A procedure for solving a problem in terms of the actions to be executed and the order in which the actions should be executed is called a(n) .

b) Specifying the execution order of statements by the computer is called .

Self-Review Exercises 85

c) All programs can be written in terms of three types of control statements: , and .

d) The selection statement is used to execute one action when a condition is true and another action when that condition is false.

e) Several statements grouped together in braces ({ and }) are called a(n) . f) The repetition statement specifies that a statement or group of statements is

to be executed repeatedly while some condition remains true. g) Repetition of a set of instructions a specific number of times is called repeti-

tion. h) When it’s not known in advance how many times a set of statements will be repeated,

a(n) value can be used to terminate the repetition.

3.2 Write four different C statements that each add 1 to integer variable x.

3.3 Write a single C statement to accomplish each of the following: a) Assign the sum of x and y to z and increment the value of x by 1 after the calculation. b) Multiply the variable product by 2 using the *= operator. c) Multiply the variable product by 2 using the = and * operators. d) Test if the value of the variable count is greater than 10. If it is, print “Count is greater

than 10.” e) Decrement the variable x by 1, then subtract it from the variable total. f) Add the variable x to the variable total, then decrement x by 1. g) Calculate the remainder after q is divided by divisor and assign the result to q. Write

this statement two different ways. h) Print the value 123.4567 with 2 digits of precision. What value is printed? i) Print the floating-point value 3.14159 with three digits to the right of the decimal point.

What value is printed?

3.4 Write a C statement to accomplish each of the following tasks. a) Define variables sum and x to be of type int. b) Initialize variable x to 1. c) Initialize variable sum to 0. d) Add variable x to variable sum and assign the result to variable sum. e) Print "The sum is: " followed by the value of variable sum.

3.5 Combine the statements that you wrote in Exercise 3.4 into a program that calculates the sum of the integers from 1 to 10. Use the while statement to loop through the calculation and in- crement statements. The loop should terminate when the value of x becomes 11.

3.6 Determine the values of variables product and x after the following calculation is per- formed. Assume that product and x each have the value 5 when the statement begins executing.

product *= x++;

3.7 Write single C statements that a) Input integer variable x with scanf. b) Input integer variable y with scanf. c) Initialize integer variable i to 1. d) Initialize integer variable power to 1. e) Multiply variable power by x and assign the result to power. f) Increment variable i by 1. g) Test i to see if it’s less than or equal to y in the condition of a while statement. h) Output integer variable power with printf.

3.8 Write a C program that uses the statements in Exercise 3.7 to calculate x raised to the y pow- er. The program should have a while repetition control statement.

86 Chapter 3 Structured Program Development in C

3.9 Identify and correct the errors in each of the following: a) while ( c <= 5 ) {

product *= c;

++c; b) scanf( "%.4f", &value ); c) if ( gender == 1 )

printf( "Woman\n" );

else;

printf( "Man\n" );

3.10 What is wrong with the following while repetition statement (assume z has value 100), which is supposed to calculate the sum of the integers from 100 down to 1:

while ( z >= 0 ) sum += z;

Answers to Self-Review Exercises 3.1 a) Algorithm. b) Program control. c) Sequence, selection, repetition. d) if…else. e) Com- pound statement. f) while. g) Counter-controlled. h) Sentinel.

3.2 x = x + 1; x += 1; ++x; x++;

3.3 a) z = x++ + y; b) product *= 2; c) product = product * 2; d) if ( count > 10 )

printf( "Count is greater than 10.\n" ); e) total -= --x; f) total += x--; g) q %= divisor;

q = q % divisor; h) printf( "%.2f", 123.4567 );

123.46 is displayed. i) printf( "%.3f\n", 3.14159 );

3.142 is displayed.

3.4 a) int sum, x; b) x = 1; c) sum = 0; d) sum += x; or sum = sum + x; e) printf( "The sum is: %d\n", sum );

3.5 See top of next page.

1 /* Calculate the sum of the integers from 1 to 10 */ 2 #include <stdio.h> 3 4 int main( void ) 5 { 6 int sum, x; /* define variables sum and x */ 7

Answers to Self-Review Exercises 87

3.6 product = 25, x = 6;

3.7 a) scanf( "%d", &x ); b) scanf( "%d", &y ); c) i = 1; d) power = 1; e) power *= x; f) i++; g) if ( i <= y ) h) printf( "%d", power );

3.8 See below.

3.9 a) Error: Missing the closing right brace of the while body. Correction: Add closing right brace after the statement ++c;.

b) Error: Precision used in a scanf conversion specification. Correction: Remove .4 from the conversion specification.

c) Error: Semicolon after the else part of the if…else statement results in a logic error. The second printf will always be executed. Correction: Remove the semicolon after else.

3.10 The value of the variable z is never changed in the while statement. Therefore, an infinite loop is created. To prevent the infinite loop, z must be decremented so that it eventually becomes 0.

8 x = 1; /* initialize x */ 9 sum = 0; /* initialize sum */

10 11 while ( x <= 10 ) { /* loop while x is less than or equal to 10 */ 12 sum += x; /* add x to sum */ 13 ++x; /* increment x */ 14 } /* end while */ 15 16 printf( "The sum is: %d\n", sum ); /* display sum */ 17 return 0; 18 } /* end main function */

1 /* raise x to the y power */ 2 #include <stdio.h> 3 4 int main( void ) 5 { 6 int x, y, i, power; /* define variables */ 7 8 i = 1; /* initialize i */ 9 power = 1; /* initialize power */

10 scanf( "%d", &x ); /* read value for x from user */ 11 scanf( "%d", &y ); /* read value for y from user */ 12 13 while ( i <= y ) { /* loop while i is less than or equal to y */ 14 power *= x; /* multiply power by x */ 15 ++i; /* increment i */ 16 } /* end while */ 17 18 printf( "%d", power ); /* display power */ 19 return 0; 20 } /* end main function */

88 Chapter 3 Structured Program Development in C

Exercises 3.11 Identify and correct the errors in each of the following. [Note: There may be more than one error in each piece of code.]

a) if ( age >= 65 ); printf( "Age is greater than or equal to 65\n" );

else

printf( "Age is less than 65\n" ); b) int x = 1, total;

while ( x <= 10 ) {

total += x;

++x;

} c) While ( x <= 100 )

total += x;

++x; d) while ( y > 0 ) {

printf( "%d\n", y );

++y;

}

3.12 Fill in the blanks in each of the following: a) The solution to any problem involves performing a series of actions in a specific

. b) A synonym for procedure is . c) A variable that accumulates the sum of several numbers is a(n) . d) Setting certain variables to specific values at the beginning of a program is called . e) A special value used to indicate “end of data entry” is called a(n) , a(n)

, a(n) or a(n) value. f) A(n) is a graphical representation of an algorithm. g) In a flowchart, the order in which the steps should be performed is indicated by

symbols. h) The termination symbol indicates the and of every algorithm. i) Rectangle symbols correspond to calculations that are normally performed by

statements and input/output operations that are normally performed by calls to the and Standard Library functions.

j) The item written inside a decision symbol is called a(n) .

3.13 What does the following program print?

1 #include <stdio.h> 2 3 int main( void ) 4 { 5 int x = 1, total = 0, y; 6 7 while ( x <= 10 ) { 8 y = x * x; 9 printf( "%d\n", y );

10 total += y; 11 ++x; 12 } /* end while */ 13

Exercises 89

3.14 Write a single pseudocode statement that indicates each of the following: a) Display the message "Enter two numbers". b) Assign the sum of variables x, y, and z to variable p. c) The following condition is to be tested in an if…else selection statement: The current

value of variable m is greater than twice the current value of variable v. d) Obtain values for variables s, r, and t from the keyboard.

3.15 Formulate a pseudocode algorithm for each of the following: a) Obtain two numbers from the keyboard, compute their sum and display the result. b) Obtain two numbers from the keyboard, and determine and display which (if either) is

the larger of the two numbers. c) Obtain a series of positive numbers from the keyboard, and determine and display their

sum. Assume that the user types the sentinel value -1 to indicate “end of data entry.”

3.16 State which of the following are true and which are false. If a statement is false, explain why. a) Experience has shown that the most difficult part of solving a problem on a computer

is producing a working C program. b) A sentinel value must be a value that cannot be confused with a legitimate data value. c) Flowlines indicate the actions to be performed. d) Conditions written inside decision symbols always contain arithmetic operators (i.e., +,

-, *, /, and %). e) In top-down, stepwise refinement, each refinement is a complete representation of the

algorithm.

For Exercises 3.17 to 3.21, perform each of these steps: 1. Read the problem statement. 2. Formulate the algorithm using pseudocode and top-down, stepwise refinement.

3. Write a C program.

4. Test, debug and execute the C program.

3.17 (Gas Mileage) Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. Develop a program that will input the miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful. After pro- cessing all input information, the program should calculate and print the combined miles per gallon obtained for all tankfuls. Here is a sample input/output dialog:

14 printf("Total is %d\n", total); 15 return 0; 16 } /* end main */

Enter the gallons used (-1 to end): 12.8 Enter the miles driven: 287 The miles / gallon for this tank was 22.421875

Enter the gallons used (-1 to end): 10.3 Enter the miles driven: 200 The miles / gallon for this tank was 19.417475

Enter the gallons used (-1 to end): 5 Enter the miles driven: 120 The miles / gallon for this tank was 24.000000

Enter the gallons used (-1 to end): -1

The overall average miles/gallon was 21.601423

90 Chapter 3 Structured Program Development in C

3.18 (Credit Limit Calculator) Develop a C program that will determine if a department store customer has exceeded the credit limit on a charge account. For each customer, the following facts are available:

a) Account number b) Balance at the beginning of the month c) Total of all items charged by this customer this month d) Total of all credits applied to this customer's account this month e) Allowed credit limit

The program should input each of these facts, calculate the new balance (= beginning balance + charges – credits), and determine if the new balance exceeds the customer's credit limit. For those customers whose credit limit is exceeded, the program should display the customer's account num- ber, credit limit, new balance and the message “Credit limit exceeded.” Here is a sample input/out- put dialog:

3.19 (Sales Commission Calculator) One large chemical company pays its salespeople on a com- mission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who sells $5000 worth of chemicals in a week receives $200 plus 9% of $5000, or a total of $650. Develop a program that will input each salesperson’s gross sales for last week and will calculate and display that salesperson's earnings. Process one salesperson's figures at a time. Here is a sample input/output dialog:

3.20 (Interest Calculator) The simple interest on a loan is calculated by the formula

interest = principal * rate * days / 365;

Enter account number (-1 to end): 100 Enter beginning balance: 5394.78 Enter total charges: 1000.00 Enter total credits: 500.00 Enter credit limit: 5500.00 Account: 100 Credit limit: 5500.00 Balance: 5894.78 Credit Limit Exceeded.

Enter account number (-1 to end): 200 Enter beginning balance: 1000.00 Enter total charges: 123.45 Enter total credits: 321.00 Enter credit limit: 1500.00

Enter account number (-1 to end): 300 Enter beginning balance: 500.00 Enter total charges: 274.73 Enter total credits: 100.00 Enter credit limit: 800.00

Enter account number (-1 to end): -1

Enter sales in dollars (-1 to end): 5000.00 Salary is: $650.00

Enter sales in dollars (-1 to end): 1234.56 Salary is: $311.11

Enter sales in dollars (-1 to end): 1088.89 Salary is: $298.00

Enter sales in dollars (-1 to end): -1

Exercises 91

The preceding formula assumes that rate is the annual interest rate, and therefore includes the division by 365 (days). Develop a program that will input principal, rate and days for several loans, and will calculate and display the simple interest for each loan, using the preceding formula. Here is a sample input/output dialog:

3.21 (Salary Calculator) Develop a program that will determine the gross pay for each of several employees. The company pays “straight time” for the first 40 hours worked by each employee and pays “time-and-a-half” for all hours worked in excess of 40 hours. You’re given a list of the employ- ees of the company, the number of hours each employee worked last week and the hourly rate of each employee. Your program should input this information for each employee, and should deter- mine and display the employee's gross pay. Here is a sample input/output dialog:

3.22 (Predecrementing vs. Postdecrementing) Write a program that demonstrates the difference between predecrementing and postdecrementing using the decrement operator --.

3.23 (Printing Numbers from a Loop) Write a program that utilizes looping to print the num- bers from 1 to 10 side by side on the same line with three spaces between numbers.

3.24 (Find the Largest Number) The process of finding the largest number (i.e., the maximum of a group of numbers) is used frequently in computer applications. For example, a program that determines the winner of a sales contest would input the number of units sold by each salesperson. The salesperson who sold the most units wins the contest. Write a pseudocode program and then a program that inputs a series of 10 numbers and determines and prints the largest of the numbers. [Hint: Your program should use three variables as follows]:

counter: A counter to count to 10 (i.e., to keep track of how many numbers have been input and to determine when all 10 numbers have been processed)

number: The current number input to the program largest: The largest number found so far

Enter loan principal (-1 to end): 1000.00 Enter interest rate: .1 Enter term of the loan in days: 365 The interest charge is $100.00

Enter loan principal (-1 to end): 1000.00 Enter interest rate: .08375 Enter term of the loan in days: 224 The interest charge is $51.40

Enter loan principal (-1 to end): 10000.00 Enter interest rate: .09 Enter term of the loan in days: 1460 The interest charge is $3600.00

Enter loan principal (-1 to end): -1

Enter # of hours worked (-1 to end): 39 Enter hourly rate of the worker ($00.00): 10.00 Salary is $390.00

Enter # of hours worked (-1 to end): 40 Enter hourly rate of the worker ($00.00): 10.00 Salary is $400.00

Enter # of hours worked (-1 to end): 41 Enter hourly rate of the worker ($00.00): 10.00 Salary is $415.00

Enter # of hours worked (-1 to end): -1

92 Chapter 3 Structured Program Development in C

3.25 (Tabular Output) Write a program that uses looping to print the following table of values. Use the tab escape sequence, \t, in the printf statement to separate the columns with tabs.

3.26 (Tabular Output) Write a program that utilizes looping to produce the following table of values:

3.27 (Find the Two Largest Numbers) Using an approach similar to Exercise 3.24, find the two largest values of the 10 numbers. [Note: You may input each number only once.]

3.28 (Validating User Input) Modify the program in Figure 3.10 to validate its inputs. On any input, if the value entered is other than 1 or 2, keep looping until the user enters a correct value.

3.29 What does the following program print?

3.30 What does the following program print?

N 10*N 100*N 1000*N

1 10 100 1000 2 20 200 2000 3 30 300 3000 4 40 400 4000 5 50 500 5000 6 60 600 6000 7 70 700 7000 8 80 800 8000 9 90 900 9000 10 100 1000 10000

A A+2 A+4 A+6

3 5 7 9 6 8 10 12 9 11 13 15 12 14 16 18 15 17 19 21

1 #include <stdio.h> 2 3 int main( void ) 4 { 5 int count = 1; /* initialize count */ 6 7 while ( count <= 10 ) { /* loop 10 times */ 8 9 /* output line of text */

10 printf( "%s\n", count % 2 ? "****" : "++++++++" ); 11 count++; /* increment count */ 12 } /* end while */ 13 14 return 0; /* indicate program ended successfully */ 15 } /* end function main */

1 #include <stdio.h> 2 3 int main( void ) 4 { 5 int row = 10; /* initialize row */ 6 int column; /* define column */

Exercises 93

3.31 (Dangling Else Problem) Determine the output for each of the following when x is 9 and y is 11, and when x is 11 and y is 9. The compiler ignores the indentation in a C program. Also, the compiler always associates an else with the previous if unless told to do otherwise by the placement of braces {}. Because, on first glance, you may not be sure which if an else matches, this is referred to as the “dangling else” problem. We eliminated the indentation from the following code to make the problem more challenging. [Hint: Apply indentation conventions you have learned.]

a) if ( x < 10 ) if ( y > 10 )

printf( "*****\n" );

else

printf( "#####\n" );

printf( "$$$$$\n" ); b) if ( x < 10 ) {

if ( y > 10 )

printf( "*****\n" );

}

else {

printf( "#####\n" );

printf( "$$$$$\n" );

}

3.32 (Another Dangling Else Problem) Modify the following code to produce the output shown. Use proper indentation techniques. You may not make any changes other than inserting braces. The compiler ignores the indentation in a program. We eliminated the indentation from the following code to make the problem more challenging. [Note: It’s possible that no modification is necessary.]

if ( y == 8 ) if ( x == 5 ) printf( "@@@@@\n" ); else printf( "#####\n" ); printf( "$$$$$\n" ); printf( "&&&&&\n" );

a) Assuming x = 5 and y = 8, the following output is produced.

7 8 while ( row >= 1 ) { /* loop until row < 1 */ 9 column = 1; /* set column to 1 as iteration begins */

10 11 while ( column <= 10 ) { /* loop 10 times */ 12 printf( "%s", row % 2 ? "<": ">" ); /* output */ 13 column++; /* increment column */ 14 } /* end inner while */ 15 16 row--; /* decrement row */ 17 printf( "\n" ); /* begin new output line */ 18 } /* end outer while */ 19 20 return 0; /* indicate program ended successfully */ 21 } /* end function main */

@@@@@ $$$$$ &&&&&

94 Chapter 3 Structured Program Development in C

b) Assuming x = 5 and y = 8, the following output is produced.

c) Assuming x = 5 and y = 8, the following output is produced.

d) Assuming x = 5 and y = 7, the following output is produced. [Note: The last three printf statements are all part of a compound statement.]

3.33 (Square of Asterisks) Write a program that reads in the side of a square and then prints that square out of asterisks. Your program should work for squares of all side sizes between 1 and 20. For example, if your program reads a size of 4, it should print

3.34 (Hollow Square of Asterisks) Modify the program you wrote in Exercise 3.33 so that it prints a hollow square. For example, if your program reads a size of 5, it should print

3.35 (Palindrome Tester) A palindrome is a number or a text phrase that reads the same back- ward as forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a program that reads in a five-digit integer and determines whether or not it’s a palindrome. [Hint: Use the division and remainder operators to separate the number into its individual digits.]

3.36 (Printing the Decimal Equivalent of a Binary Number) Input an integer containing only 0s and 1s (i.e., a “binary” integer) and print its decimal equivalent. [Hint: Use the remainder and division operators to pick off the “binary” number’s digits one at a time from right to left. Just as in the decimal number system, in which the rightmost digit has a positional value of 1, and the next digit left has a positional value of 10, then 100, then 1000, and so on, in the binary number system the rightmost digit has a positional value of 1, the next digit left has a positional value of 2, then 4, then 8, and so on. Thus the decimal number 234 can be interpreted as 4 * 1 + 3 * 10 + 2 * 100. The decimal equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8 or 1 + 0 + 4 + 8 or 13.]

3.37 (How Fast is Your Computer?) How can you determine how fast your own computer really operates? Write a program with a while loop that counts from 1 to 300,000,000 by 1s. Every time the count reaches a multiple of 100,000,000, print that number on the screen. Use your watch to time how long each 100 million repetitions of the loop takes.

@@@@@

@@@@@ &&&&&

##### $$$$$ &&&&&

**** **** **** ****

***** * * * * * * *****

Exercises 95

3.38 Write a program that prints 100 asterisks, one at a time. After every tenth asterisk, your pro- gram should print a newline character. [Hint: Count from 1 to 100. Use the remainder operator to recognize each time the counter reaches a multiple of 10.]

3.39 (Counting 7s) Write a program that reads an integer and determines and prints how many digits in the integer are 7s.

3.40 (Checkerboard Pattern of Asterisks) Write a program that displays the following checker- board pattern:

Your program must use only three output statements, one of each of the following forms:

printf( "* " ); printf( " " ); printf( "\n" );

3.41 (Multiples of 2 with an Infinite Loop) Write a program that keeps printing the multiples of the integer 2, namely 2, 4, 8, 16, 32, 64, and so on. Your loop should not terminate (i.e., you should create an infinite loop). What happens when you run this program?

3.42 (Diameter, Circumference and Area of a Cirle) Write a program that reads the radius of a circle (as a float value) and computes and prints the diameter, the circumference and the area. Use the value 3.14159 for π. 3.43 What is wrong with the following statement? Rewrite the statement to accomplish what the programmer was probably trying to do.

printf( "%d", ++( x + y ) );

3.44 (Sides of a Triangle) Write a program that reads three nonzero float values and determines and prints if they could represent the sides of a triangle.

3.45 (Sides of a Right Triangle) Write a program that reads three nonzero integers and deter- mines and prints if they could be the sides of a right triangle.

3.46 (Factorial) The factorial of a nonnegative integer n is written n! (pronounced “n factorial”) and is defined as follows:

n! = n · (n - 1) · (n - 2) · … · 1 (for values of n greater than or equal to 1) and

n! = 1 (for n = 0). For example, 5! = 5 · 4 · 3 · 2 · 1, which is 120.

a) Write a program that reads a nonnegative integer and computes and prints its factorial. b) Write a program that estimates the value of the mathematical constant e by using the

formula:

c) Write a program that computes the value of ex by using the formula

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

e 1 1 1! ----- 1

2! ----- 1

3! ----- …+ + + +=

ex 1 x 1! ----- x

2

2! ----- x

3

3! ----- …+ + + +=

96 Chapter 3 Structured Program Development in C

Making a Difference 3.47 (World Population Growth Calculator) Use the web to determine the current world pop- ulation and the annual world population growth rate. Write an application that inputs these values, then displays the estimated world population after one, two, three, four and five years.

3.48 (Target-Heart-Rate Calculator) While exercising, you can use a heart-rate monitor to see that your heart rate stays within a safe range suggested by your trainers and doctors. According to the American Heart Association (AHA) (www.americanheart.org/presenter.jhtml?identifier=4736), the formula for calculating your maximum heart rate in beats per minute is 220 minus your age in years. Your target heart rate is a range that is 50–85% of your maximum heart rate. [Note: These for- mulas are estimates provided by the AHA. Maximum and target heart rates may vary based on the health, fitness and gender of the individual. Always consult a physician or qualified health care pro- fessional before beginning or modifying an exercise program.] Create a program that reads the user’s birthday and the current day (each consisting of the month, day and year). Your program should cal- culate and display the person’s age (in years), the person’s maximum heart rate and the person’s target heart rate range.

3.49 (Enforcing Privacy with Cryptography) The explosive growth of Internet communications and data storage on Internet-connected computers has greatly increased privacy concerns. The field of cryptography is concerned with coding data to make it difficult (and hopefully—with the most advanced schemes—impossible) for unauthorized users to read. In this exercise you’ll investigate a simple scheme for encrypting and decrypting data. A company that wants to send data over the In- ternet has asked you to write a program that will encrypt it so that it may be transmitted more se- curely. All the data is transmitted as four-digit integers. Your application should read a four-digit integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap the first digit with the third, and swap the second digit with the fourth. Then print the encrypted integer. Write a separate application that inputs an encrypted four-digit integer and decrypts it (by reversing the encryption scheme) to form the original number. [Optional reading project: Research “public key cryptography” in general and the PGP (Pretty Good Privacy) specific public key scheme. You may also want to investigate the RSA scheme, which is widely used in industrial-strength applications.]

4C Program Control Not everything that can be counted counts, and not every thing that counts can be counted. —Albert Einstein

Who can control his fate? —William Shakespeare

The used key is always bright. —Benjamin Franklin

Every advantage in the past is judged in the light of the final issue. —Demosthenes

O b j e c t i v e s In this chapter, you’ll learn:

■ The essentials of counter- controlled repetition.

■ To use the for and do…while repetition statements to execute statements repeatedly.

■ To understand multiple selection using the switch selection statement.

■ To use the break and continue statements to alter the flow of control.

■ To use the logical operators to form complex conditional expressions in control statements.

■ To avoid the consequences of confusing the equality and assignment operators.

98 Chapter 4 C Program Control

4.1 Introduction You should now be comfortable with writing simple but complete C programs. In this chapter, repetition is considered in greater detail, and additional repetition control state- ments, namely the for and the do…while, are presented. The switch multiple-selection statement is introduced. We discuss the break statement for exiting immediately from cer- tain control statements, and the continue statement for skipping the remainder of the body of a repetition statement and proceeding with the next iteration of the loop. The chapter discusses logical operators used for combining conditions, and summarizes the principles of structured programming as presented in Chapter 3 and 4.

4.2 Repetition Essentials Most programs involve repetition, or looping. A loop is a group of instructions the com- puter executes repeatedly while some loop-continuation condition remains true. We have discussed two means of repetition:

1. Counter-controlled repetition

2. Sentinel-controlled repetition

Counter-controlled repetition is sometimes called definite repetition because we know in advance exactly how many times the loop will be executed. Sentinel-controlled repetition is sometimes called indefinite repetition because it’s not known in advance how many times the loop will be executed.

In counter-controlled repetition, a control variable is used to count the number of repetitions. The control variable is incremented (usually by 1) each time the group of instructions is performed. When the value of the control variable indicates that the correct number of repetitions has been performed, the loop terminates and the computer continues executing with the statement after the repetition statement.

Sentinel values are used to control repetition when:

1. The precise number of repetitions is not known in advance, and

2. The loop includes statements that obtain data each time the loop is performed.

The sentinel value indicates “end of data.” The sentinel is entered after all regular data items have been supplied to the program. Sentinels must be distinct from regular data items.

4.1 Introduction 4.2 Repetition Essentials 4.3 Counter-Controlled Repetition 4.4 for Repetition Statement 4.5 for Statement: Notes and

Observations 4.6 Examples Using the for Statement

4.7 switch Multiple-Selection Statement 4.8 do…while Repetition Statement 4.9 break and continue Statements

4.10 Logical Operators 4.11 Confusing Equality (==) and

Assignment (=) Operators 4.12 Structured Programming Summary

Summary | Terminology | Self-Review Exercises | Answers to Self-Review Exercises | Exercises | Making a Difference

4.3 Counter-Controlled Repetition 99

4.3 Counter-Controlled Repetition Counter-controlled repetition requires:

1. The name of a control variable (or loop counter).

2. The initial value of the control variable.

3. The increment (or decrement) by which the control variable is modified each time through the loop.

4. The condition that tests for the final value of the control variable (i.e., whether looping should continue).

Consider the simple program shown in Fig. 4.1, which prints the numbers from 1 to 10. The definition

names the control variable (counter), defines it to be an integer, reserves memory space for it, and sets it to an initial value of 1. This definition is not an executable statement.

The definition and initialization of counter could also have been written as

int counter = 1; /* initialization */

1 /* Fig. 4.1: fig04_01.c 2 Counter-controlled repetition */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main( void ) 7 { 8 9

10 while ( ) { /* repetition condition */ 11 printf ( "%d\n", counter ); /* display counter */ 12 13 } /* end while */ 14 15 return 0; /* indicate program ended successfully */ 16 } /* end function main */

1 2 3 4 5 6 7 8 9 10

Fig. 4.1 | Counter-controlled repetition.

int counter; counter = 1;

int counter = 1; /* initialization */

counter <= 10

++counter; /* increment */

100 Chapter 4 C Program Control

The definition is not executable, but the assignment is. We use both methods of initializ- ing variables.

The statement

increments the loop counter by 1 each time the loop is performed. The loop-continuation condition in the while statement tests if the value of the control variable is less than or equal to 10 (the last value for which the condition is true). The body of this while is per- formed even when the control variable is 10. The loop terminates when the control vari- able exceeds 10 (i.e., counter becomes 11).

You could make the program in Fig. 4.1 more concise by initializing counter to 0 and by replacing the while statement with

This code saves a statement because the incrementing is done directly in the while condi- tion before the condition is tested. Also, this code eliminates the need for the braces around the body of the while because the while now contains only one statement. Coding in such a condensed fashion takes some practice. Some programmers feel that this makes the code too cryptic and error prone,

4.4 for Repetition Statement The for repetition statement handles all the details of counter-controlled repetition. To illustrate its power, let’s rewrite the program of Fig. 4.1. The result is shown in Fig. 4.2.

++counter; /* increment */

while ( ++counter <= 10 ) printf( "%d\n", counter );

Common Programming Error 4.1 Floating-point values may be approximate, so controlling counting loops with floating- point variables may result in imprecise counter values and inaccurate termination tests.

Error-Prevention Tip 4.1 Control counting loops with integer values.

Good Programming Practice 4.1 Too many levels of nesting can make a program difficult to understand. As a rule, try to avoid using more than three levels of nesting.

Good Programming Practice 4.2 The combination of vertical spacing before and after control statements and indentation of the bodies of control statements within the control-statement headers gives programs a two-dimensional appearance that greatly improves program readability.

1 /* Fig. 4.2: fig04_02.c 2 Counter-controlled repetition with the for statement */ 3 #include <stdio.h>

Fig. 4.2 | Counter-controlled repetition with the for statement. (Part 1 of 2.)

4.4 for Repetition Statement 101

The program operates as follows. When the for statement begins executing, the con- trol variable counter is initialized to 1. Then, the loop-continuation condition counter <= 10 is checked. Because the initial value of counter is 1, the condition is satisfied, so the printf statement (line 13) prints the value of counter, namely 1. The control variable counter is then incremented by the expression counter++, and the loop begins again with the loop-continuation test. Since the control variable is now equal to 2, the final value is not exceeded, so the program performs the printf statement again. This process continues until the control variable counter is incremented to its final value of 11—this causes the loop-continuation test to fail, and repetition terminates. The program continues by per- forming the first statement after the for statement (in this case, the return statement at the end of the program).

Figure 4.3 takes a closer look at the for statement of Fig. 4.2. Notice that the for statement “does it all”—it specifies each of the items needed for counter-controlled repeti- tion with a control variable. If there is more than one statement in the body of the for, braces are required to define the body of the loop.

Notice that Fig. 4.2 uses the loop-continuation condition counter <= 10. If you incorrectly wrote counter < 10, then the loop would be executed only 9 times. This is a common logic error called an off-by-one error.

4 5 /* function main begins program execution */ 6 int main( void ) 7 { 8 int counter; /* define counter */ 9

10 /* initialization, repetition condition, and increment 11 are all included in the for statement header. */ 12 { 13 printf( "%d\n", counter ); 14 } /* end for */ 15 16 return 0; /* indicate program ended successfully */ 17 } /* end function main */

Fig. 4.3 | for statement header components.

Fig. 4.2 | Counter-controlled repetition with the for statement. (Part 2 of 2.)

for ( counter = 1; counter <= 10; counter++ )

Initial value of control variable Loop-continuation

condition

Increment of control variable

for keyword

Control variable name

Required semicolon separator

Required semicolon separator

Final value of control variable for which the condition is true

for ( counter = 1; counter <= 10; counter++ )

102 Chapter 4 C Program Control

The general format of the for statement is

where expression1 initializes the loop-control variable, expression2 is the loop-continuation condition, and expression3 increments the control variable. In most cases, the for state- ment can be represented with an equivalent while statement as follows:

There is an exception to this rule, which we discuss in Section 4.9. Often, expression1 and expression3 are comma-separated lists of expressions. The

commas as used here are actually comma operators that guarantee that lists of expressions evaluate from left to right. The value and type of a comma-separated list of expressions are the value and type of the right-most expression in the list. The comma operator is most often used in the for statement. Its primary use is to enable you to use multiple initializa- tion and/or multiple increment expressions. For example, there may be two control vari- ables in a single for statement that must be initialized and incremented.

The three expressions in the for statement are optional. If expression2 is omitted, C assumes that the condition is true, thus creating an infinite loop. One may omit expression1 if the control variable is initialized elsewhere in the program. expression3 may be omitted if the increment is calculated by statements in the body of the for statement or if no incre- ment is needed. The increment expression in the for statement acts like a stand-alone C statement at the end of the body of the for. Therefore, the expressions

Common Programming Error 4.2 Using an incorrect relational operator or using an incorrect initial or final value of a loop counter in the condition of a while or for statement can cause off-by-one errors.

Error-Prevention Tip 4.2 Using the final value in the condition of a while or for statement and using the <= re- lational operator will help avoid off-by-one errors. For a loop used to print the values 1 to 10, for example, the loop-continuation condition should be counter <= 10 rather than counter < 11 or counter < 10.

for ( expression1; expression2; expression3 ) statement

expression1;

while ( expression2 ) { statement expression3; }

Software Engineering Observation 4.1 Place only expressions involving the control variables in the initialization and increment sections of a for statement. Manipulations of other variables should appear either before the loop (if they execute only once, like initialization statements) or in the loop body (if they execute once per repetition, like incrementing or decrementing statements).

counter = counter + 1 counter += 1 ++counter counter++

4.5 for Statement: Notes and Observations 103

are all equivalent in the increment part of the for statement. Many C programmers prefer the form counter++ because the incrementing occurs after the loop body is executed, and the postincrementing form seems more natural. Because the variable being preincrement- ed or postincremented here does not appear in a larger expression, both forms of incre- menting have the same effect. The two semicolons in the for statement are required.

4.5 for Statement: Notes and Observations 1. The initialization, loop-continuation condition and increment can contain arith-

metic expressions. For example, if x = 2 and y = 10, the statement

is equivalent to the statement

2. The “increment” may be negative (in which case it’s really a decrement and the loop actually counts downward).

3. If the loop-continuation condition is initially false, the loop body does not exe- cute. Instead, execution proceeds with the statement following the for statement.

4. The control variable is frequently printed or used in calculations in the body of a loop, but it need not be. It’s common to use the control variable for controlling repetition while never mentioning it in the body of the loop.

5. The for statement is flowcharted much like the while statement. For example, Fig. 4.4 shows the flowchart of the for statement

This flowchart makes it clear that the initialization occurs only once and that in- crementing occurs after the body statement is performed.

4.6 Examples Using the for Statement The following examples show methods of varying the control variable in a for statement.

1. Vary the control variable from 1 to 100 in increments of 1.

Common Programming Error 4.3 Using commas instead of semicolons in a for header is a syntax error.

Common Programming Error 4.4 Placing a semicolon immediately to the right of a for header makes the body of that for statement an empty statement. This is normally a logic error.

for ( j = x; j <= 4 * x * y; j += y / x )

for ( j = 2; j <= 80; j += 5 )

for ( counter = 1; counter <= 10; counter++ ) printf( "%d", counter );

Error-Prevention Tip 4.3 Although the value of the control variable can be changed in the body of a for loop, this can lead to subtle errors. It’s best not to change it.

for ( i = 1; i <= 100; i++ )

104 Chapter 4 C Program Control

2. Vary the control variable from 100 to 1 in increments of -1 (decrements of 1).

3. Vary the control variable from 7 to 77 in steps of 7.

4. Vary the control variable from 20 to 2 in steps of -2.

5. Vary the control variable over the following sequence of values: 2, 5, 8, 11, 14, 17.

6. Vary the control variable over the following sequence of values: 44, 33, 22, 11, 0.

The next two examples provide simple applications of the for statement. Figure 4.5 uses the for statement to sum all the even integers from 2 to 100.

Fig. 4.4 | Flowcharting a typical for repetition statement.

for ( i = 100; i >= 1; i-- )

for ( i = 7; i <= 77; i += 7 )

for ( i = 20; i >= 2; i -= 2 )

for ( j = 2; j <= 17; j += 3 )

for ( j = 44; j >= 0; j -= 11 )

1 /* Fig. 4.5: fig04_05.c 2 Summation with for */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main( void ) 7 { 8 9 int number; /* number to be added to sum */

10 11 12 13 } /* end for */

Fig. 4.5 | Using for to sum numbers. (Part 1 of 2.)

Determine if final value of control variable has been reached

Increment the control variable

counter <= 10 true

false

printf( "%d", counter ); counter++

counter = 1

Body of loop (this may be many statements)

Establish initial value of control variable

int sum = 0; /* initialize sum */

for ( number = 2; number <= 100; number += 2 ) { sum += number; /* add number to sum */

4.6 Examples Using the for Statement 105

The body of the for statement in Fig. 4.5 could actually be merged into the rightmost portion of the for header by using the comma operator as follows:

The initialization sum = 0 could also be merged into the initialization section of the for.

The next example computes compound interest using the for statement. Consider the following problem statement:

A person invests $1000.00 in a savings account yielding 5% interest. Assuming that all interest is left on deposit in the account, calculate and print the amount of money in the account at the end of each year for 10 years. Use the following formula for determining these amounts:

a = p(1 + r)n

where

p is the original amount invested (i.e., the principal) r is the annual interest rate n is the number of years a is the amount on deposit at the end of the nth year.

This problem involves a loop that performs the indicated calculation for each of the 10 years the money remains on deposit. The solution is shown in Fig. 4.6.

14 15 printf( "Sum is %d\n", sum ); /* output sum */ 16 return 0; /* indicate program ended successfully */ 17 } /* end function main */

Sum is 2550

for ( number = 2; number <= 100; sum += number, number += 2 ) ; /* empty statement */

Good Programming Practice 4.3 Although statements preceding a for and statements in the body of a for can often be merged into the for header, avoid doing so because it makes the program more difficult to read.

Good Programming Practice 4.4 Limit the size of control-statement headers to a single line if possible.

Fig. 4.5 | Using for to sum numbers. (Part 2 of 2.)

1 /* Fig. 4.6: fig04_06.c 2 Calculating compound interest */ 3 #include <stdio.h> 4

Fig. 4.6 | Calculating compound interest with for. (Part 1 of 2.)

#include <math.h>

106 Chapter 4 C Program Control

The for statement executes the body of the loop 10 times, varying a control variable from 1 to 10 in increments of 1. Although C does not include an exponentiation operator, we can use the Standard Library function pow for this purpose. The function pow(x, y) calculates the value of x raised to the yth power. It takes two arguments of type double and returns a double value. Type double is a floating-point type much like float, but typically a variable of type double can store a value of much greater magnitude with greater preci- sion than float. The header <math.h> (line 4) should be included whenever a math func- tion such as pow is used. Actually, this program would malfunction without the inclusion of math.h, as the linker would be unable to find the pow function.1 Function pow requires

5 6 /* function main begins program execution */ 7 int main( void ) 8 { 9 double amount; /* amount on deposit */

10 double principal = 1000.0; /* starting principal */ 11 double rate = .05; /* annual interest rate */ 12 int year; /* year counter */ 13 14 /* output table column head */ 15 printf( "%4s%21s\n", "Year", "Amount on deposit" ); 16 17 /* calculate amount on deposit for each of ten years */ 18 for ( year = 1; year <= 10; year++ ) { 19 20 /* calculate new amount for specified year */ 21 22 23 /* output one table row */ 24 printf( "%4d%21.2f\n", year, amount ); 25 } /* end for */ 26 27 return 0; /* indicate program ended successfully */ 28 } /* end function main */

Year Amount on deposit 1 1050.00 2 1102.50 3 1157.63 4 1215.51 5 1276.28 6 1340.10 7 1407.10 8 1477.46 9 1551.33 10 1628.89

1. On many Linux/UNIX C compilers, you must include the -lm option (e.g., cc -lm fig04_06.c) when compiling Fig. 4.6. This links the math library to the program.

Fig. 4.6 | Calculating compound interest with for. (Part 2 of 2.)

amount = principal * pow( 1.0 + rate, year );

4.7 switch Multiple-Selection Statement 107

two double arguments, but variable year is an integer. The math.h file includes informa- tion that tells the compiler to convert the value of year to a temporary double represen- tation before calling the function. This information is contained in something called pow’s function prototype. Function prototypes are explained in Chapter 5. We also provide a summary of the pow function and other math library functions in Chapter 5.

Notice that we defined the variables amount, principal and rate to be of type double. We did this for simplicity because we’re dealing with fractional parts of dollars.

Here is a simple explanation of what can go wrong when using float or double to represent dollar amounts. Two float dollar amounts stored in the machine could be 14.234 (which with %.2f prints as 14.23) and 18.673 (which with %.2f prints as 18.67). When these amounts are added, they produce the sum 32.907, which with %.2f prints as 32.91. Thus your printout could appear as

Clearly the sum of the individual numbers as printed should be 32.90! You’ve been warned! The conversion specifier %21.2f is used to print the value of the variable amount in

the program. The 21 in the conversion specifier denotes the field width in which the value will be printed. A field width of 21 specifies that the value printed will appear in 21 print positions. The 2 specifies the precision (i.e., the number of decimal positions). If the number of characters displayed is less than the field width, then the value will auto- matically be right justified in the field. This is particularly useful for aligning floating- point values with the same precision (so that their decimal points align vertically). To left justify a value in a field, place a - (minus sign) between the % and the field width. The minus sign may also be used to left justify integers (such as in %-6d) and character strings (such as in %-8s). We’ll discuss the powerful formatting capabilities of printf and scanf in detail in Chapter 9.

4.7 switch Multiple-Selection Statement In Chapter 3, we discussed the if single-selection statement and the if…else double-se- lection statement. Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each of the constant integral values it may as- sume, and different actions are taken. This is called multiple selection. C provides the switch multiple-selection statement to handle such decision making.

The switch statement consists of a series of case labels, an optional default case and statements to execute for each case. Figure 4.7 uses switch to count the number of each different letter grade students earned on an exam.

Error-Prevention Tip 4.4 Do not use variables of type float or double to perform monetary calculations. The im- preciseness of floating-point numbers can cause errors that will result in incorrect mone- tary values. [In this chapter’s exercises, we explore the use of integers to perform monetary calculations.]

14.23 + 18.67 ------- 32.91

108 Chapter 4 C Program Control

1 /* Fig. 4.7: fig04_07.c 2 Counting letter grades */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main( void ) 7 { 8 int grade; /* one grade */ 9 int aCount = 0; /* number of As */

10 int bCount = 0; /* number of Bs */ 11 int cCount = 0; /* number of Cs */ 12 int dCount = 0; /* number of Ds */ 13 int fCount = 0; /* number of Fs */ 14 15 printf( "Enter the letter grades.\n" ); 16 printf( "Enter the EOF character to end input.\n" ); 17 18 /* loop until user types end-of-file key sequence */ 19 while ( ( ) != ) { 20 21 /* determine which grade was input */ 22 23 24 /* grade was uppercase A */ 25 /* or lowercase a */ 26 ++aCount; /* increment aCount */ 27 /* necessary to exit switch */ 28 29 /* grade was uppercase B */ 30 /* or lowercase b */ 31 ++bCount; /* increment bCount */ 32 /* exit switch */ 33 34 /* grade was uppercase C */ 35 /* or lowercase c */ 36 ++cCount; /* increment cCount */ 37 /* exit switch */ 38 39 /* grade was uppercase D */ 40 /* or lowercase d */ 41 ++dCount; /* increment dCount */ 42 /* exit switch */ 43 44 /* grade was uppercase F */ 45 /* or lowercase f */ 46 ++fCount; /* increment fCount */ 47 /* exit switch */ 48 49 /* ignore newlines, */ 50 /* tabs, */ 51 /* and spaces in input */ 52 /* exit switch */ 53

Fig. 4.7 | switch example. (Part 1 of 2.)

grade = getchar() EOF

switch ( grade ) { /* switch nested in while */

case 'A': case 'a':

break;

case 'B': case 'b':

break;

case 'C': case 'c':

break;

case 'D': case 'd':

break;

case 'F': case 'f':

break;

case '\n': case '\t': case ' ':

break;

4.7 switch Multiple-Selection Statement 109

In the program, the user enters letter grades for a class. In the while header (line 19),

the parenthesized assignment (grade = getchar()) executes first. The getchar function (from <stdio.h>) reads one character from the keyboard and stores that character in the integer variable grade. Characters are normally stored in variables of type char. However, an important feature of C is that characters can be stored in any integer data type because they’re usually represented as one-byte integers in the computer. Thus, we can treat a char- acter as either an integer or a character, depending on its use. For example, the statement

54 /* catch all other characters */ 55 printf( "Incorrect letter grade entered." ); 56 printf( " Enter a new grade.\n" ); 57 /* optional; will exit switch anyway */ 58 } /* end switch */ 59 } /* end while */ 60 61 /* output summary of results */ 62 printf( "\nTotals for each letter grade are:\n" ); 63 printf( "A: %d\n", aCount ); /* display number of A grades */ 64 printf( "B: %d\n", bCount ); /* display number of B grades */ 65 printf( "C: %d\n", cCount ); /* display number of C grades */ 66 printf( "D: %d\n", dCount ); /* display number of D grades */ 67 printf( "F: %d\n", fCount ); /* display number of F grades */c 68 return 0; /* indicate program ended successfully */ 69 } /* end function main */

Enter the letter grades. Enter the EOF character to end input. a b c C A d f C E Incorrect letter grade entered. Enter a new grade. D A b ^Z

Totals for each letter grade are: A: 3 B: 2 C: 3 D: 2 F: 1

while ( ( grade = getchar() ) != EOF )

printf( "The character (%c) has the value %d.\n", 'a', 'a' );

Fig. 4.7 | switch example. (Part 2 of 2.)

default:

break;

Not all systems display a representation of the EOF character

110 Chapter 4 C Program Control

uses the conversion specifiers %c and %d to print the character a and its integer value, re- spectively. The result is

The integer 97 is the character’s numerical representation in the computer. Many computers today use the ASCII (American Standard Code for Information Interchange) character set in which 97 represents the lowercase letter 'a'. A list of the ASCII characters and their decimal values is presented in Appendix B. Characters can be read with scanf by using the conversion specifier %c.

Assignments as a whole actually have a value. This value is assigned to the variable on the left side of the =. The value of the assignment expression grade = getchar() is the char- acter that is returned by getchar and assigned to the variable grade.

The fact that assignments have values can be useful for setting several variables to the same value. For example,

first evaluates the assignment c = 0 (because the = operator associates from right to left). The variable b is then assigned the value of the assignment c = 0 (which is 0). Then, the variable a is assigned the value of the assignment b = (c = 0) (which is also 0). In the pro- gram, the value of the assignment grade = getchar() is compared with the value of EOF (a symbol whose acronym stands for “end of file”). We use EOF (which normally has the value -1) as the sentinel value. The user types a system-dependent keystroke combination to mean “end of file”—i.e., “I have no more data to enter.” EOF is a symbolic integer con- stant defined in the <stdio.h> header (we’ll see how symbolic constants are defined in Chapter 6). If the value assigned to grade is equal to EOF, the program terminates. We have chosen to represent characters in this program as ints because EOF has an integer val- ue (again, normally -1).

On Linux/UNIX/Mac OS X systems, the EOF indicator is entered by typing

on a line by itself. This notation <Ctrl> d means to press the Enter key and then simulta- neously press both the Ctrl key and the d key. On other systems, such as Microsoft Win- dows, the EOF indicator can be entered by typing

You may also need to press Enter on Windows. The user enters grades at the keyboard. When the Enter key is pressed, the characters

are read by function getchar one character at a time. If the character entered is not equal

The character (a) has the value 97.

a = b = c = 0;

Portability Tip 4.1 The keystroke combinations for entering EOF (end of file) are system dependent.

Portability Tip 4.2 Testing for the symbolic constant EOF rather than –1 makes programs more portable. The C standard states that EOF is a negative integral value (but not necessarily –1). Thus, EOF could have different values on different systems.

<Ctrl> d

<Ctrl> z

4.7 switch Multiple-Selection Statement 111

to EOF, the switch statement (line 22) is entered. Keyword switch is followed by the vari- able name grade in parentheses. This is called the controlling expression. The value of this expression is compared with each of the case labels. Assume the user has entered the letter C as a grade. C is automatically compared to each case in the switch. If a match occurs (case 'C':), the statements for that case are executed. In the case of the letter C, cCount is incremented by 1 (line 36), and the switch statement is exited immediately with the break statement.

The break statement causes program control to continue with the first statement after the switch statement. The break statement is used because the cases in a switch state- ment would otherwise run together. If break is not used anywhere in a switch statement, then each time a match occurs in the statement, the statements for all the remaining cases will be executed. (This feature is rarely useful, although it’s perfect for programming the iterative song The Twelve Days of Christmas!) If no match occurs, the default case is exe- cuted, and an error message is printed.

Each case can have one or more actions. The switch statement is different from all other control statements in that braces are not required around multiple actions in a case of a switch. The general switch multiple-selection statement (using a break in each case) is flowcharted in Fig. 4.8. The flowchart makes it clear that each break statement at the end of a case causes control to immediately exit the switch statement.

Fig. 4.8 | switch multiple-selection statement with breaks.

. . .

case a true

false

case a actions(s) break

case b true

false

case b actions(s) break

case z true

false

case z actions(s) break

default actions(s)

112 Chapter 4 C Program Control

In the switch statement of Fig. 4.7, the lines

cause the program to skip newline, tab and blank characters. Reading characters one at a time can cause some problems. To have the program read the characters, they must be sent to the computer by pressing the Enter key. This causes the newline character to be placed in the input after the character we wish to process. Often, this newline character must be specially processed to make the program work correctly. By including the preceding cases in our switch statement, we prevent the error message in the default case from being printed each time a newline, tab or space is encountered in the input.

Listing several case labels together (such as case 'D': case 'd': in Fig. 4.7) simply means that the same set of actions is to occur for either of these cases.

When using the switch statement, remember that each individual case can test only a constant integral expression—i.e., any combination of character constants and integer constants that evaluates to a constant integer value. A character constant is represented as the specific character in single quotes, such as 'A'. Characters must be enclosed within single quotes to be recognized as character constants—characters in double quotes are rec- ognized as strings. Integer constants are simply integer values. In our example, we have used character constants. Remember that characters are represented as small integer values.

Common Programming Error 4.5 Forgetting a break statement when one is needed in a switch statement is a logic error.

Good Programming Practice 4.5 Provide a default case in switch statements. Cases not explicitly tested in a switch are ignored. The default case helps prevent this by focusing the programmer on the need to process exceptional conditions. Sometimes no default processing is needed.

Good Programming Practice 4.6 Although the case clauses and the default case clause in a switch statement can occur in any order, it’s considered good programming practice to place the default clause last.

Good Programming Practice 4.7 In a switch statement when the default clause is last, the break statement is not re- quired. Some programmers include this break for clarity and symmetry with other cases.

case '\n': /* ignore newlines, */ case '\t': /* tabs, */ case ' ': /* and spaces in input */ break; /* exit switch */

Common Programming Error 4.6 Not processing newline characters in the input when reading characters one at a time can cause logic errors.

Error-Prevention Tip 4.5 Remember to provide processing capabilities for newline (and possibly other white-space) characters in the input when processing characters one at a time.

4.8 do…while Repetition Statement 113

Notes on Integral Types Portable languages like C must have flexible data type sizes. Different applications may need integers of different sizes. C provides several data types to represent integers. The range of values for each type depends on the particular computer’s hardware. In addition to int and char, C provides types short (an abbreviation of short int) and long (an ab- breviation of long int). C specifies that the minimum range of values for short integers is –32768 to +32767. For the vast majority of integer calculations, long integers are suffi- cient. The standard specifies that the minimum range of values for long integers is – 2147483648 to +2147483647. The standard states that the range of values for an int is at least the same as the range for short integers and no larger than the range for long in- tegers. The data type signed char can be used to represent integers in the range –128 to +127 or any of the characters in the computer’s character set.

4.8 do…while Repetition Statement The do…while repetition statement is similar to the while statement. In the while state- ment, the loop-continuation condition is tested at the beginning of the loop before the body of the loop is performed. The do…while statement tests the loop-continuation con- dition after the loop body is performed. Therefore, the loop body will be executed at least once. When a do…while terminates, execution continues with the statement after the while clause. It’s not necessary to use braces in the do…while statement if there is only one statement in the body. However, the braces are usually included to avoid confusion between the while and do…while statements. For example,

is normally regarded as the header to a while statement. A do…while with no braces around the single-statement body appears as

which can be confusing. The last line—while( condition );—may be misinterpreted by as a while statement containing an empty statement. Thus, to avoid confusion, the do…while with one statement is often written as follows:

while ( condition )

do statement while ( condition );

do { statement } while ( condition );

Good Programming Practice 4.8 To eliminate the potential for ambiguity, some programmers always include braces in a do…while statement, even if the braces are not necessary.

Common Programming Error 4.7 Infinite loops are caused when the loop-continuation condition in a while, for or do…while statement never becomes false. To prevent this, make sure there is not a semi- colon immediately after the header of a while or for statement. In a counter-controlled loop, make sure the control variable is incremented (or decremented) in the loop. In a sen- tinel-controlled loop, make sure the sentinel value is eventually input.

114 Chapter 4 C Program Control

Figure 4.9 uses a do…while statement to print the numbers from 1 to 10. The con- trol variable counter is preincremented in the loop-continuation test. Note also the use of the braces to enclose the single-statement body of the do…while.

Figure 4.10 shows the do…while statement flowchart, which makes it clear that the loop-continuation condition does not execute until after the action is performed at least once.

4.9 break and continue Statements The break and continue statements are used to alter the flow of control. The break state- ment, when executed in a while, for, do…while or switch statement, causes an imme- diate exit from that statement. Program execution continues with the next statement. Common uses of the break statement are to escape early from a loop or to skip the remain- der of a switch statement (as in Fig. 4.7). Figure 4.11 demonstrates the break statement

1 /* Fig. 4.9: fig04_09.c 2 Using the do/while repetition statement */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main( void ) 7 { 8 int counter = 1; /* initialize counter */ 9

10 11 12 13 14 return 0; /* indicate program ended successfully */ 15 } /* end function main */

1 2 3 4 5 6 7 8 9 10

Fig. 4.9 | do…while statement example.

Fig. 4.10 | Flowcharting the do…while repetition statement.

do { printf( "%d ", counter ); /* display counter */ } while ( ++counter <= 10 ); /* end do...while */

condition true

false

action(s)

4.9 break and continue Statements 115

in a for repetition statement. When the if statement detects that x has become 5, break is executed. This terminates the for statement, and the program continues with the printf after the for. The loop fully executes only four times.

The continue statement, when executed in a while, for or do…while statement, skips the remaining statements in the body of that control statement and performs the next iteration of the loop. In while and do…while statements, the loop-continuation test is evaluated immediately after the continue statement is executed. In the for statement, the increment expression is executed, then the loop-continuation test is evaluated. Earlier, we said that the while statement could be used in most cases to represent the for statement. The one exception occurs when the increment expression in the while statement follows the continue statement. In this case, the increment is not executed before the repetition- continuation condition is tested, and the while does not execute in the same manner as the for. Figure 4.12 uses the continue statement in a for statement to skip the printf statement and begin the next iteration of the loop.

1 /* Fig. 4.11: fig04_11.c 2 Using the break statement in a for statement */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main( void ) 7 { 8 int x; /* counter */ 9

10 /* loop 10 times */ 11 for ( x = 1; x <= 10; x++ ) { 12 13 /* if x is 5, terminate loop */ 14 if ( x == 5 ) { 15 /* break loop only if x is 5 */ 16 } /* end if */ 17 18 printf( "%d ", x ); /* display value of x */ 19 } /* end for */ 20 21 printf( "\nBroke out of loop at x == %d\n", x ); 22 return 0; /* indicate program ended successfully */ 23 } /* end function main */

1 2 3 4 Broke out of loop at x == 5

Fig. 4.11 | Using the break statement in a for statement.

1 /* Fig. 4.12: fig04_12.c 2 Using the continue statement in a for statement */ 3 #include <stdio.h> 4

Fig. 4.12 | Using the continue statement in a for statement. (Part 1 of 2.)

break;

116 Chapter 4 C Program Control

4.10 Logical Operators So far we have studied only simple conditions, such as counter <= 10, total > 1000, and number != sentinelValue. We’ve expressed these conditions in terms of the relational op- erators, >, <, >= and <=, and the equality operators, == and !=. Each decision tested pre- cisely one condition. To test multiple conditions in the process of making a decision, we had to perform these tests in separate statements or in nested if or if…else statements.

C provides logical operators that may be used to form more complex conditions by combining simple conditions. The logical operators are && (logical AND), || (logical

5 /* function main begins program execution */ 6 int main( void ) 7 { 8 int x; /* counter */ 9

10 /* loop 10 times */ 11 for ( x = 1; x <= 10; x++ ) { 12 13 /* if x is 5, continue with next iteration of loop */ 14 if ( x == 5 ) { 15 /* skip remaining code in loop body */ 16 } /* end if */ 17 18 printf( "%d ", x ); /* display value of x */ 19 } /* end for */ 20 21 printf( "\nUsed continue to skip printing the value 5\n" ); 22 return 0; /* indicate program ended successfully */ 23 } /* end function main */

1 2 3 4 6 7 8 9 10 Used continue to skip printing the value 5

Software Engineering Observation 4.2 Some programmers feel that break and continue violate the norms of structured programming. The effects of these statements can be achieved by structured programming techniques we’ll soon learn, so these programmers do not use break and continue.

Performance Tip 4.1 The break and continue statements, when used properly, perform faster than the cor- responding structured techniques that we’ll soon learn.

Software Engineering Observation 4.3 There is a tension between achieving quality software engineering and achieving the best- performing software. Often one of these goals is achieved at the expense of the other.

Fig. 4.12 | Using the continue statement in a for statement. (Part 2 of 2.)

continue;

4.10 Logical Operators 117

OR) and ! (logical NOT also called logical negation). We’ll consider examples of each of these operators.

Suppose we wish to ensure that two conditions are both true before we choose a cer- tain path of execution. In this case, we can use the logical operator && as follows:

This if statement contains two simple conditions. The condition gender == 1 might be evaluated, for example, to determine if a person is a female. The condition age >= 65 is evaluated to determine if a person is a senior citizen. The two simple conditions are eval- uated first because the precedences of == and >= are both higher than the precedence of &&. The if statement then considers the combined condition

This condition is true if and only if both of the simple conditions are true. Finally, if this combined condition is indeed true, then the count of seniorFemales is incremented by 1. If either or both of the simple conditions are false, then the program skips the in- crementing and proceeds to the statement following the if.

Figure 4.13 summarizes the && operator. The table shows all four possible combi- nations of zero (false) and nonzero (true) values for expression1 and expression2. Such tables are often called truth tables. C evaluates all expressions that include relational oper- ators, equality operators, and/or logical operators to 0 or 1. Although C sets a true value to 1, it accepts any nonzero value as true.

Now let’s consider the || (logical OR) operator. Suppose we wish to ensure at some point in a program that either or both of two conditions are true before we choose a certain path of execution. In this case, we use the || operator as in the following program segment:

This statement also contains two simple conditions. The condition semesterAverage >= 90 is evaluated to determine if the student deserves an “A” in the course because of a solid performance throughout the semester. The condition finalExam >= 90 is evaluated to de- termine if the student deserves an “A” in the course because of an outstanding performance on the final exam. The if statement then considers the combined condition

if ( gender == 1 && age >= 65 ) ++seniorFemales;

gender == 1 && age >= 65

expression1 expression2 expression1 && expression2

0 0 0

0 nonzero 0

nonzero 0 0

nonzero nonzero 1

Fig. 4.13 | Truth table for the logical AND (&&) operator.

if ( semesterAverage >= 90 || finalExam >= 90 ) printf( "Student grade is A\n" );

semesterAverage >= 90 || finalExam >= 90

118 Chapter 4 C Program Control

and awards the student an “A” if either or both of the simple conditions are true. The mes- sage “Student grade is A” is not printed only when both of the simple conditions are false (zero). Figure 4.14 is a truth table for the logical OR operator (||).

The && operator has a higher precedence than ||. Both operators associate from left to right. An expression containing && or || operators is evaluated only until truth or false- hood is known. Thus, evaluation of the condition

will stop if gender is not equal to 1 (i.e., the entire expression is false), and continue if gen- der is equal to 1 (i.e., the entire expression could still be true if age >= 65). This perfor- mance feature for the evaluation of logical AND and logical OR expressions is called short-circuit evaluation.

C provides ! (logical negation) to enable a programmer to “reverse” the meaning of a condition. Unlike operators && and ||, which combine two conditions (and are therefore binary operators), the logical negation operator has only a single condition as an operand (and is therefore a unary operator). The logical negation operator is placed before a con- dition when we’re interested in choosing a path of execution if the original condition (without the logical negation operator) is false, such as in the following program segment:

The parentheses around the condition grade == sentinelValue are needed because the logical negation operator has a higher precedence than the equality operator. Figure 4.15 is a truth table for the logical negation operator.

expression1 expression2 expression1 || expression2

0 0 0 0 nonzero 1 nonzero 0 1 nonzero nonzero 1

Fig. 4.14 | Truth table for the logical OR (||) operator.

gender == 1 && age >= 65

Performance Tip 4.2 In expressions using operator &&, make the condition that is most likely to be false the left- most condition. In expressions using operator ||, make the condition that is most likely to be true the leftmost condition. This can reduce a program’s execution time.

if ( !( grade == sentinelValue ) ) printf( "The next grade is %f\n", grade );

expression !expression

0 1 nonzero 0

Fig. 4.15 | Truth table for operator ! (logical negation).

4.11 Confusing Equality (==) and Assignment (=) Operators 119

In most cases, you can avoid using logical negation by expressing the condition dif- ferently with an appropriate relational operator. For example, the preceding statement may also be written as follows:

Figure 4.16 shows the precedence and associativity of the operators introduced to this point. The operators are shown from top to bottom in decreasing order of precedence.

4.11 Confusing Equality (==) and Assignment (=) Operators There is one type of error that C programmers, no matter how experienced, tend to make so frequently that we felt it was worth a separate section. That error is accidentally swap- ping the operators == (equality) and = (assignment). What makes these swaps so damaging is the fact that they do not ordinarily cause compilation errors. Rather, statements with these errors ordinarily compile correctly, allowing programs to run to completion while likely generating incorrect results through runtime logic errors.

Two aspects of C cause these problems. One is that any expression in C that produces a value can be used in the decision portion of any control statement. If the value is 0, it’s treated as false, and if the value is nonzero, it’s treated as true. The second is that assign- ments in C produce a value, namely the value that is assigned to the variable on the left side of the assignment operator. For example, suppose we intend to write

but we accidentally write

if ( grade != sentinelValue ) printf( "The next grade is %f\n", grade );

Operators Associativity Type

++ (postfix) -- (postfix) right to left postfix

+ - ! ++ (prefix) -- (prefix) (type) right to left unary

* / % left to right multiplicative

+ - left to right additive

< <= > >= left to right relational

== != left to right equality

&& left to right logical AND

|| left to right logical OR

?: right to left conditional

= += -= *= /= %= right to left assignment

, left to right comma

Fig. 4.16 | Operator precedence and associativity,

if ( payCode == 4 ) printf( "You get a bonus!" );

if ( payCode = 4 ) printf( "You get a bonus!" );

120 Chapter 4 C Program Control

The first if statement properly awards a bonus to the person whose paycode is equal to 4. The second if statement—the one with the error—evaluates the assignment expression in the if condition. This expression is a simple assignment whose value is the constant 4. Be- cause any nonzero value is interpreted as “true,” the condition in this if statement is al- ways true, and not only is the value of payCode inadvertantly set to 4, but the person always receives a bonus regardless of what the actual paycode is!

Programmers normally write conditions such as x == 7 with the variable name on the left and the constant on the right. By reversing these terms so that the constant is on the left and the variable name is on the right, as in 7 == x, the programmer who accidentally replaces the == operator with = is protected by the compiler. The compiler will treat this as a syntax error, because only a variable name can be placed on the left-hand side of an assignment expression. At least this will prevent the potential devastation of a runtime logic error.

Variable names are said to be lvalues (for “left values”) because they can be used on the left side of an assignment operator. Constants are said to be rvalues (for “right values”) because they can be used on only the right side of an assignment operator. Lvalues can also be used as rvalues, but not vice versa.

The other side of the coin can be equally unpleasant. Suppose you want to assign a value to a variable with a simple statement like

but instead write

Here, too, this is not a syntax error. Rather the compiler simply evaluates the conditional expression. If x is equal to 1, the condition is true and the expression returns the value 1. If x is not equal to 1, the condition is false and the expression returns the value 0. Regard- less of what value is returned, there is no assignment operator, so the value is simply lost, and the value of x remains unaltered, probably causing an execution-time logic error. Un- fortunately, we do not have a handy trick available to help you with this problem! Many compilers, however, will issue a warning on such a statement.

Common Programming Error 4.8 Using operator == for assignment or using operator = for equality is a logic error.

Good Programming Practice 4.9 When an equality expression has a variable and a constant, as in x == 1, some pro- grammers prefer to write the expression with the constant on the left and the variable name on the right (e.g. 1 == x as protection against the logic error that occurs when you accidentally replace operator == with =).

x = 1;

x == 1;

Error-Prevention Tip 4.6 After you write a program, text search it for every = and check that it’s used properly.

4.12 Structured Programming Summary 121

4.12 Structured Programming Summary Just as architects design buildings by employing the collective wisdom of their profession, so should programmers design programs. Our field is younger than architecture is, and our collective wisdom is considerably sparser. We have learned a great deal in a mere six de- cades. Perhaps most important, we have learned that structured programming produces programs that are easier (than unstructured programs) to understand and hence are easier to test, debug, modify, and even prove correct in a mathematical sense.

Chapters 3 and 4 have concentrated on C’s control statements. Each statement has been presented, flowcharted and discussed separately with examples. Now, we summarize the results of Chapters 3 and 4 and introduce a simple set of rules for the formation and properties of structured programs.

Figure 4.17 summarizes the control statements discussed in Chapters 3 and 4. Small circles are used in the figure to indicate the single entry point and the single exit point of each statement. Connecting individual flowchart symbols arbitrarily can lead to unstruc- tured programs. Therefore, the programming profession has chosen to combine flowchart symbols to form a limited set of control statements, and to build only structured programs by properly combining control statements in two simple ways. For simplicity, only single-entry/single-exit control statements are used—there is only one way to enter and only one way to exit each control statement. Connecting control statements in sequence to form structured programs is simple—the exit point of one control statement is con- nected directly to the entry point of the next, i.e., the control statements are simply placed one after another in a program—we have called this “control-statement stacking.” The rules for forming structured programs also allow for control statements to be nested.

Figure 4.18 shows the rules for forming structured programs. The rules assume that the rectangle flowchart symbol may be used to indicate any action including input/output. Figure 4.19 shows the simplest flowchart.

Applying the rules of Fig. 4.18 always results in a structured flowchart with a neat, building-block appearance. Repeatedly applying Rule 2 to the simplest flowchart (Fig. 4.19) results in a structured flowchart containing many rectangles in sequence (Fig. 4.20). Notice that Rule 2 generates a stack of control statements; so we call Rule 2 the stacking rule.

Rule 3 is called the nesting rule. Repeatedly applying Rule 3 to the simplest flowchart results in a flowchart with neatly nested control statements. For example, in Fig. 4.21, the rectangle in the simplest flowchart is first replaced with a double-selection (if…else) statement. Then Rule 3 is applied again to both of the rectangles in the double-selection statement, replacing each of these rectangles with double-selection statements. The dashed box around each of the double-selection statements represents the rectangle that was replaced in the original flowchart.

Rule 4 generates larger, more involved, and more deeply nested structures. The flow- charts that emerge from applying the rules in Fig. 4.18 constitute the set of all possible structured flowcharts and hence the set of all possible structured programs.

It’s because of the elimination of the goto statement that these building blocks never overlap one another. The beauty of the structured approach is that we use only a small number of simple single-entry/single-exit pieces, and we assemble them in only two simple ways. Figure 4.22 shows the kinds of stacked building blocks that emerge from applying Rule 2 and the kinds of nested building blocks that emerge from applying Rule 3. The

122 Chapter 4 C Program Control

Fig. 4.17 | C’s single-entry/single-exit sequence, selection and repetition statements.

. . .

. . .

break

break

break

while statement

if statement (single selection)

if...else statement (double selection)

switch statement (multiple selection)

do...while statement for statement

Repetition

Sequence Selection

T TF

F

T

F

T

F

T

F

T

F T

F

T

F

4.12 Structured Programming Summary 123

figure also shows the kind of overlapped building blocks that cannot appear in structured flowcharts (because of the elimination of the goto statement).

Rules for Forming Structured Programs

1) Begin with the “simplest flowchart” (Fig. 4.19).

2) Any rectangle (action) can be replaced by two rectangles (actions) in sequence.

3) Any rectangle (action) can be rep’laced by any control statement (sequence, if, if…else, switch, while, do…while or for).

4) Rules 2 and 3 may be applied as often as you like and in any order.

Fig. 4.18 | Rules for forming structured programs.

Fig. 4.19 | Simplest flowchart.

Fig. 4.20 | Repeatedly applying Rule 2 of Fig. 4.18 to the simplest flowchart.

. . .

Rule 2 Rule 2 Rule 2

124 Chapter 4 C Program Control

If the rules in Fig. 4.18 are followed, an unstructured flowchart (such as that in Fig. 4.23) cannot be created. If you’re uncertain whether a particular flowchart is struc- tured, apply the rules of Fig. 4.18 in reverse to try to reduce the flowchart to the simplest flowchart. If you succeed, the original flowchart is structured; otherwise, it’s not.

Structured programming promotes simplicity. Bohm and Jacopini showed that only three forms of control are needed:

• Sequence

• Selection

• Repetition

Fig. 4.21 | Applying Rule 3 of Fig. 4.18 to the simplest flowchart.

Rule 3

Rule 3

Rule 3

4.12 Structured Programming Summary 125

Sequence is straighforward. Selection is implemented in one of three ways:

• if statement (single selection)

• if…else statement (double selection)

• switch statement (multiple selection)

In fact, it’s straightforward to prove that the simple if statement is sufficient to provide any form of selection—everything that can be done with the if…else statement and the switch statement can be implemented with one or more if statements.

Repetition is implemented in one of three ways:

• while statement

Fig. 4.22 | Stacked, nested and overlapped building blocks.

Fig. 4.23 | An unstructured flowchart.

Stacked building blocks Nested building blocks

Overlapping building blocks (Illegal in structured programs)

126 Chapter 4 C Program Control

• do…while statement

• for statement

It’s straightforward to prove that the while statement is sufficient to provide any form of repetition. Everything that can be done with the do…while statement and the for statement can be done with the while statement.

Combining these results illustrates that any form of control ever needed in a C pro- gram can be expressed in terms of only three forms of control:

• sequence

• if statement (selection)

• while statement (repetition)

And these control statements can be combined in only two ways—stacking and nesting. Indeed, structured programming promotes simplicity.

In Chapters 3 and 4, we discussed how to compose programs from control statements containing actions and decisions. In Chapter 5, we introduce another program structuring unit called the function. We’ll learn to compose large programs by combining functions, which, in turn, are composed of control statements. We’ll also discuss how using functions promotes software reusability.

Summary Section 4.2 Repetition Essentials • Most programs involve repetition, or looping. A loop is a group of instructions the computer ex-

ecutes repeatedly while some loop-continuation condition remains true.

• Counter-controlled repetition is sometimes called definite repetition because we know in ad- vance exactly how many times the loop will execute.

• Sentinel-controlled repetition is sometimes called indefinite repetition because it’s not known in advance how many times the loop will execute.

• In counter-controlled repetition, a control variable is used to count the number of repetitions. The control variable is incremented (usually by 1) each time the group of instructions is per- formed. When the correct number of repetitions has been performed, the loop terminates, and the program resumes execution with the statement after the repetition statement.

• Sentinel values are used to control repetition when the number of repetitions is not known in advance, and the loop includes statements that obtain data each time the loop is performed.

• The sentinel value indicates “end of data.” The sentinel is entered after all regular data items have been supplied to the program. Sentinels must be distinct from regular data items.

Section 4.3 Counter-Controlled Repetition • Counter-controlled repetition requires the name of a control variable (or loop counter), the ini-

tial value of the control variable, the increment (or decrement) by which the control variable is modified each time through the loop, and the condition that tests for the final value of the con- trol variable (i.e., whether looping should continue).

Section 4.4 for Repetition Statement • The for repetition statement handles all the details of counter-controlled repetition.

Summary 127

• When the for statement begins executing, its control variable is initialized. Then, the loop-con- tinuation condition is checked. If the condition is true, the loop’s body executes. The control variable is then incremented, and the loop begins again with the loop-continuation condition. This process continues until the loop-continuation condition fails.

• The general format of the for statement is

for ( expression1; expression2; expression3 ) statement

where expression1 initializes the loop-control variable, expression2 is the loop-continuation con- dition, and expression3 increments the control variable.

• In most cases, the for statement can be represented with an equivalent while statement as in:

expression1; while ( expression2 ) { statement expression3; }

• The comma operator guarantees that lists of expressions evaluate from left to right. The value of the entire expression is that of the rightmost expression.

• The three expressions in the for statement are optional. If expression2 is omitted, C assumes that the condition is true, thus creating an infinite loop. One might omit expression1 if the control variable is initialized elsewhere in the program. expression3 might be omitted if the increment is calculated by statements in the body of the for statement or if no increment is needed.

• The increment expression in the for statement acts like a stand-alone C statement at the end of the body of the for.

• The two semicolons in the for statement are required.

Section 4.5 for Statement: Notes and Observations • The initialization, loop-continuation condition and increment can contain arithmetic expressions.

• The “increment” may be negative (in which case it’s really a decrement and the loop actually counts downward).

• If the loop-continuation condition is initially false, the body portion of the loop is not per- formed. Instead, execution proceeds with the statement following the for statement.

Section 4.6 Examples Using the for Statement • Function pow performs exponentiation. The function pow(x, y) calculates the value of x raised

to the yth power. It takes two arguments of type double and returns a double value.

• Type double is a floating-point type much like float, but typically a variable of type double can store a value of much greater magnitude with greater precision than float.

• The header <math.h> should be included whenever a math function such as pow is used.

• The conversion specifier %21.2f denotes that a floating-point value will be displayed right justi- fied in a field of 21 characters with two digits to the right of the decimal point.

• To left justify a value in a field, place a - (minus sign) between the % and the field width.

Section 4.7 switch Multiple-Selection Statement • Occasionally, an algorithm will contain a series of decisions in which a variable or expression is

tested separately for each of the constant integral values it may assume, and different actions are taken. This is called multiple selection. C provides the switch statement to handle this.

• The switch statement consists of a series of case labels, an optional default case and statements to execute for each case.

128 Chapter 4 C Program Control

• The getchar function (from the standard input/output library) reads and returns one character from the keyboard.

• Characters are normally stored in variables of type char. Characters can be stored in any integer data type because they’re usually represented as one-byte integers in the computer. Thus, we can treat a character as either an integer or a character, depending on its use.

• Many computers today use the ASCII (American Standard Code for Information Interchange) character set in which 97 represents the lowercase letter 'a'.

• Characters can be read with scanf by using the conversion specifier %c.

• Assignment expressions as a whole actually have a value. This value is assigned to the variable on the left side of the =.

• The fact that assignment statements have values can be useful for setting several variables to the same value, as in a = b = c = 0;.

• EOF is often used as a sentinel value. EOF is a symbolic integer constant defined in <stdio.h>.

• On Linux/UNIX systems and many others, the EOF indicator is entered by typing <Ctrl> d . On other systems, such as Microsoft Windows, the EOF indicator can be entered by typing <Ctrl> z.

• Keyword switch is followed by the controlling expression in parentheses. The value of this ex- pression is compared with each of the case labels. If a match occurs, the statements for that case execute. If no match occurs, the default case executes.

• The break statement causes program control to continue with the statement after the switch. The break statement prevents the cases in a switch statement from running together.

• Each case can have one or more actions. The switch statement is different from all other control statements in that braces are not required around multiple actions in a case of a switch.

• Listing several case labels together simply means that the same set of actions is to occur for any of these cases.

• Remember that the switch statement can be used only for testing a constant integral expres- sion—i.e., any combination of character constants and integer constants that evaluates to a con- stant integer value. A character constant is represented as the specific character in single quotes, such as 'A'. Characters must be enclosed within single quotes to be recognized as character con- stants. Integer constants are simply integer values.

• C provides several data types to represent integers. The range of integer values for each type de- pends on the particular computer’s hardware. In addition to the types int and char, C provides types short (an abbreviation of short int) and long (an abbreviation of long int). The mini- mum range of values for short integers is –32768 to +32767. For the vast majority of integer calculations, long integers are sufficient. The standard specifies that the minimum range of val- ues for long integers is –2147483648 to +2147483647. The standard states that the range of val- ues for an int is at least the same as the range for short integers and no larger than the range for long integers. The data type signed char can be used to represent integers in the range –128 to +127 or any of the characters in the computer’s character set.

Section 4.8 do…while Repetition Statement • The do…while statement tests the loop-continuation condition after the loop body is performed.

Therefore, the loop body will be executed at least once. When a do…while terminates, execution continues with the statement after the while clause.

Section 4.9 break and continue Statements • The break statement, when executed in a while, for, do…while or switch statement, causes im-

mediate exit from that statement. Program execution continues with the next statement.

Terminology 129

• The continue statement, when executed in a while, for or do…while statement, skips the re- maining statements in the body of that control statement and performs the next iteration of the loop. In while and do…while statements, the loop-continuation test is evaluated immediately after the continue statement is executed. In the for statement, the increment expression is exe- cuted, then the loop-continuation test is evaluated.

Section 4.10 Logical Operators • Logical operators may be used to form complex conditions by combining simple conditions. The

logical operators are && (logical AND), || (logical OR) and ! (logical NOT, or logical negation).

• A condition containing the && (logical AND) operator is true if and only if both of the simple conditions are true.

• C evaluates all expressions that include relational operators, equality operators, and/or logical op- erators to 0 or 1. Although C sets a true value to 1, it accepts any nonzero value as true.

• A condition containing the || (logical OR) operator is true if either or both of the simple con- ditions are true.

• The && operator has a higher precedence than ||. Both operators associate from left to right.

• An expression containing && or || operators is evaluated only until truth or falsehood is known.

• C provides ! (logical negation) to enable a programmer to “reverse” the meaning of a condition. Unlike the binary operators && and ||, which combine two conditions, the unary logical negation operator has only a single condition as an operand.

• The logical negation operator is placed before a condition when we’re interested in choosing a path of execution if the original condition (without the logical negation operator) is false.

• In most cases, you can avoid using logical negation by expressing the condition differently with an appropriate relational operator.

Section 4.11 Confusing Equality (==) and Assignment (=) Operators • Programmers often accidentally swap the operators == (equality) and = (assignment). What

makes these swaps so damaging is that they do not ordinarily cause syntax errors. Rather, state- ments with these errors ordinarily compile correctly, allowing programs to run to completion while likely generating incorrect results through runtime logic errors.

• Programmers normally write conditions such as x == 7 with the variable name on the left and the constant on the right. By reversing these terms so that the constant is on the left and the variable name is on the right, as in 7 == x, the programmer who accidentally replaces the == operator with = will be protected by the compiler. The compiler will treat this as a syntax error, because only a variable name can be placed on the left-hand side of an assignment statement.

• Variable names are said to be lvalues (for “left values”) because they can be used on the left side of an assignment operator.

• Constants are said to be rvalues (for “right values”) because they can be used only on the right side of an assignment operator. Lvalues can also be used as rvalues, but not vice versa.

Terminology ASCII (American Standard Code for Informa-

tion Interchange) character set 110 case label 111 char primitive type 109 comma operator 102 constant integral expression 112

control variable 98 controlling expression in a switch 111 decrement a control variable 99 definite repetition 98 final value of a control variable 99 function prototype 107

130 Chapter 4 C Program Control

increment a control variable 99 indefinite repetition 98 initial value of a control variable 99 logical AND operator (&&) 116 logical negation operator (!) 117 logical OR operator (||) 117 logical NOT operator (!) 117 loop-continuation condition 98 lvalue (“left value”) 120

name of a control variable 99 nesting rule 121 off-by-one error 101 pow (power) function 107 rvalue (“right value”) 120 short-circuit evaluation 118 stacking rule 121 truth table 117

Self-Review Exercises 4.1 Fill in the blanks in each of the following statements.

a) Counter-controlled repetition is also known as repetition because it’s known in advance how many times the loop will be executed.

b) Sentinel-controlled repetition is also known as repetition because it’s not known in advance how many times the loop will be executed.

c) In counter-controlled repetition, a(n) is used to count the number of times a group of instructions should be repeated.

d) The statement, when executed in a repetition statement, causes the next it- eration of the loop to be performed immediately.

e) The statement, when executed in a repetition statement or a switch, causes an immediate exit from the statement.

f) The is used to test a particular variable or expression for each of the constant integral values it may assume.

4.2 State whether the following are true or false. If the answer is false, explain why. a) The default case is required in the switch selection statement. b) The break statement is required in the default case of a switch selection statement. c) The expression (x > y && a < b) is true if either x > y is true or a < b is true. d) An expression containing the || operator is true if either or both of its operands is true.

4.3 Write a statement or a set of statements to accomplish each of the following tasks: a) Sum the odd integers between 1 and 99 using a for statement. Assume the integer vari-

ables sum and count have been defined. b) Print the value 333.546372 in a field width of 15 characters with precisions of 1, 2, 3, 4

and 5. Left justify the output. What are the five values that print? c) Calculate the value of 2.5 raised to the power of 3 using the pow function. Print the re-

sult with a precision of 2 in a field width of 10 positions. What is the value that prints? d) Print the integers from 1 to 20 using a while loop and the counter variable x. Assume

that the variable x has been defined, but not initialized. Print only five integers per line. [Hint: Use the calculation x % 5. When the value of this is 0, print a newline character, otherwise print a tab character.]

e) Repeat Exercise 4.3 (d) using a for statement.

4.4 Find the error in each of the following code segments and explain how to correct it. a) x = 1;

while ( x <= 10 );

x++;

} b) for ( y = .1; y != 1.0; y += .1 )

printf( "%f\n", y );

Answers to Self-Review Exercises 131

c) switch ( n ) { case 1: printf( "The number is 1\n" );

case 2: printf( "The number is 2\n" );

break;

default:

printf( "The number is not 1 or 2\n" );

break;

} d) The following code should print the values 1 to 10.

n = 1;

while ( n < 10 ) printf( "%d ", n++ );

Answers to Self-Review Exercises 4.1 a) definite. b) indefinite. c) control variable or counter. d) continue. e) break. f) switch selection statement.

4.2 a) False. The default case is optional. If no default action is needed, then there is no need for a default case.

b) False. The break statement is used to exit the switch statement. The break statement is not required when the default case is the last case.

c) False. Both of the relational expressions must be true in order for the entire expression to be true when using the && operator.

d) True.

4.3 a) sum = 0; for ( count = 1; count <= 99; count += 2 ) {

sum += count; }

b) printf( "%-15.1f\n", 333.546372 ); /* prints 333.5 */ printf( "%-15.2f\n", 333.546372 ); /* prints 333.55 */

printf( "%-15.3f\n", 333.546372 ); /* prints 333.546 */

printf( "%-15.4f\n", 333.546372 ); /* prints 333.5464 */

printf( "%-15.5f\n", 333.546372 ); /* prints 333.54637 */ c) printf( "%10.2f\n", pow( 2.5, 3 ) ); /* prints 15.63 */ d) x = 1;

while ( x <= 20 ) {

printf( "%d", x );

if ( x % 5 == 0 ) {

printf( "\n" );

}

else {

printf( "\t" );

}

x++;

}

or

132 Chapter 4 C Program Control

x = 1;

while ( x <= 20 ) {

if ( x % 5 == 0 ) {

printf( "%d\n", x++ );

}

else {

printf( "%d\t", x++ );

}

}

or

x = 0;

while ( ++x <= 20 ) {

if ( x % 5 == 0 ) {

printf( "%d\n", x );

}

else {

printf( "%d\t", x );

}

}

e) for ( x = 1; x <= 20; x++ ) { printf( "%d", x ); if ( x % 5 == 0 ) {

printf( "\n" );

}

else {

printf( "\t" );

}

}

or

for ( x = 1; x <= 20; x++ ) {

if ( x % 5 == 0 ) {

printf( "%d\n", x );

}

else {

printf( "%d\t", x );

}

}

4.4 a) Error: The semicolon after the while header causes an infinite loop. Correction: Replace the semicolon with a { or remove both the ; and the }.

b) Error: Using a floating-point number to control a for repetition statement. Correction: Use an integer, and perform the proper calculation to get the values you de- sire.

for ( y = 1; y != 10; y++ ) printf( "%f\n", ( float ) y / 10 );

c) Error: Missing break statement in the statements for the first case.

Exercises 133

Correction: Add a break statement at the end of the statements for the first case. This is not necessarily an error if you want the statement of case 2: to execute every time the case 1: statement executes.

d) Error: Improper relational operator used in the while repetition-continuation condi- tion. Correction: Use <= rather than <.

Exercises 4.5 Find the error in each of the following. (Note: There may be more than one error.)

a) For ( x = 100, x >= 1, x++ ) printf( "%d\n", x );

b) The following code should print whether a given integer is odd or even:

switch ( value % 2 ) { case 0: printf( "Even integer\n" ); case 1: printf( "Odd integer\n" );

}

c) The following code should input an integer and a character and print them. Assume the user types as input 100 A.

scanf( "%d", &intVal ); charVal = getchar(); printf( "Integer: %d\nCharacter: %c\n", intVal, charVal );

d) for ( x = .000001; x == .0001; x += .000001 ) { printf( "%.7f\n", x );

} e) The following code should output the odd integers from 999 to 1:

for ( x = 999; x >= 1; x += 2 ) { printf( "%d\n", x ); }

f) The following code should output the even integers from 2 to 100:

counter = 2;

Do { if ( counter % 2 == 0 ) { printf( "%d\n", counter ); }

counter += 2; } While ( counter < 100 );

g) The following code should sum the integers from 100 to 150 (assume total is initial- ized to 0):

for ( x = 100; x <= 150; x++ ); { total += x; }

4.6 State which values of the control variable x are printed by each of the following for state- ments:

a) for ( x = 2; x <= 13; x += 2 ) { printf( "%d\n", x ); }

134 Chapter 4 C Program Control

b) for ( x = 5; x <= 22; x += 7 ) { printf( "%d\n", x );

} c) for ( x = 3; x <= 15; x += 3 ) {

printf( "%d\n", x );

} d) for ( x = 1; x <= 5; x += 7 ) {

printf( "%d\n", x );

} e) for ( x = 12; x >= 2; x -= 3 ) {

printf( "%d\n", x );

}

4.7 Write for statements that print the following sequences of values: a) 1, 2, 3, 4, 5, 6, 7 b) 3, 8, 13, 18, 23 c) 20, 14, 8, 2, –4, –10 d) 19, 27, 35, 43, 51

4.8 What does the following program do?

4.9 (Sum a Sequence of Integers) Write a program that sums a sequence of integers. Assume that the first integer read with scanf specifies the number of values remaining to be entered. Your pro- gram should read only one value each time scanf is executed. A typical input sequence might be

5 100 200 300 400 500

where the 5 indicates that the subsequent five values are to be summed.

4.10 (Average a Sequence of Integers) Write a program that calculates and prints the average of several integers. Assume the last value read with scanf is the sentinel 9999. A typical input sequence might be

1 #include <stdio.h> 2 3 /* function main begins program execution */ 4 int main( void ) 5 { 6 int x; 7 int y; 8 int i; 9 int j;

10 11 /* prompt user for input */ 12 printf( "Enter two integers in the range 1-20: " ); 13 scanf( "%d%d", &x, &y ); /* read values for x and y */ 14 15 for ( i = 1; i <= y; i++ ) { /* count from 1 to y */ 16 17 for ( j = 1; j <= x; j++ ) { /* count from 1 to x */ 18 printf( "@" ); /* output @ */ 19 } /* end inner for */ 20 21 printf( "\n" ); /* begin new line */ 22 } /* end outer for */ 23 24 return 0; /* indicate program ended successfully */ 25 } /* end function main */

Exercises 135

10 8 11 7 9 9999

indicating that the average of all the values preceding 9999 is to be calculated.

4.11 (Find the Smallest) Write a program that finds the smallest of several integers. Assume that the first value read specifies the number of values remaining.

4.12 (Calculating the Sum of Even Integers) Write a program that calculates and prints the sum of the even integers from 2 to 30.

4.13 (Calculating the Product of Odd Integers) Write a program that calculates and prints the product of the odd integers from 1 to 15.

4.14 (Factorials) The factorial function is used frequently in probability problems. The factorial of a positive integer n (written n! and pronounced “n factorial”) is equal to the product of the posi- tive integers from 1 to n. Write a program that evaluates the factorials of the integers from 1 to 5. Print the results in tabular format. What difficulty might prevent you from calculating the factorial of 20?

4.15 (Modified Compound Interest Program) Modify the compound-interest program of Section 4.6 to repeat its steps for interest rates of 5%, 6%, 7%, 8%, 9%, and 10%. Use a for loop to vary the interest rate.

4.16 (Triangle Printing Program) Write a program that prints the following patterns separately, one below the other. Use for loops to generate the patterns. All asterisks (*) should be printed by a single printf statement of the form printf( "*" ); (this causes the asterisks to print side by side). [Hint: The last two patterns require that each line begin with an appropriate number of blanks.]

4.17 (Calculating Credit Limits) Collecting money becomes increasingly difficult during peri- ods of recession, so companies may tighten their credit limits to prevent their accounts receivable (money owed to them) from becoming too large. In response to a prolonged recession, one company has cut its customers’ credit limits in half. Thus, if a particular customer had a credit limit of $2000, it’s now $1000. If a customer had a credit limit of $5000, it’s now $2500. Write a program that analyzes the credit status of three customers of this company. For each customer you’re given:

a) The customer’s account number b) The customer’s credit limit before the recession c) The customer’s current balance (i.e., the amount the customer owes the company).

Your program should calculate and print the new credit limit for each customer and should determine (and print) which customers have current balances that exceed their new credit limits.

4.18 (Bar Chart Printing Program) One interesting application of computers is drawing graphs and bar charts (sometimes called “histograms”). Write a program that reads five numbers (each be- tween 1 and 30). For each number read, your program should print a line containing that number of adjacent asterisks. For example, if your program reads the number seven, it should print *******.

4.19 (Calculating Sales) An online retailer sells five different products whose retail prices are shown in the following table:

(A) (B) (C) (D) * ********** ********** * ** ********* ********* ** *** ******** ******** *** **** ******* ******* **** ***** ****** ****** ***** ****** ***** ***** ****** ******* **** **** ******* ******** *** *** ******** ********* ** ** ********* ********** * * **********

136 Chapter 4 C Program Control

Write a program that reads a series of pairs of numbers as follows: a) Product number b) Quantity sold for one day

Your program should use a switch statement to help determine the retail price for each product. Your program should calculate and display the total retail value of all products sold last week.

4.20 (Truth Tables) Complete the following truth tables by filling in each blank with 0 or 1.

4.21 Rewrite the program of Fig. 4.2 so that the initialization of the variable counter is done in the definition rather than in the for statement.

4.22 (Average Grade) Modify the program of Fig. 4.7 so that it calculates the average grade for the class.

4.23 (Calculating the Compound Interest with Integers) Modify the program of Fig. 4.6 so that it uses only integers to calculate the compound interest. [Hint: Treat all monetary amounts as inte-

Product number Retail price

1 $ 2.98

2 $ 4.50

3 $ 9.98

4 $ 4.49

5 $ 6.87

Condition1 Condition2 Condition1 && Condition2

0 0 0

0 nonzero 0

nonzero 0 _____

nonzero nonzero _____

Condition1 Condition2 Condition1 || Condition2

0 0 0

0 nonzero 1

nonzero 0 _____

nonzero nonzero _____

Condition1 !Condition1

0 1

nonzero _____

Exercises 137

gral numbers of pennies. Then “break” the result into its dollar portion and cents portion by using the division and remainder operations, respectively. Insert a period.]

4.24 Assume i = 1, j = 2, k = 3 and m = 2. What does each of the following statements print? a) printf( "%d", i == 1 ); b) printf( "%d", j == 3 ); c) printf( "%d", i >= 1 && j < 4 ); d) printf( "%d", m < = 99 && k < m ); e) printf( "%d", j >= i || k == m ); f) printf( "%d", k + m < j || 3 - j >= k ); g) printf( "%d", !m ); h) printf( "%d", !( j - m ) ); i) printf( "%d", !( k > m ) ); j) printf( "%d", !( j > k ) );

4.25 (Table of Decimal, Binary, Octal and Hexadecimal Equivalents) Write a program that prints a table of the binary, octal and hexadecimal equivalents of the decimal numbers in the range 1 through 256. If you’re not familiar with these number systems, read Appendix C before you at- tempt this exercise.

4.26 (Calculating the Value of π) Calculate the value of π from the infinite series

Print a table that shows the value of π approximated by one term of this series, by two terms, by three terms, and so on. How many terms of this series do you have to use before you first get 3.14? 3.141? 3.1415? 3.14159?

4.27 (Pythagorean Triples) A right triangle can have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal to the square of the hypotenuse. Find all Pythagorean triples for side1, side2, and the hypotenuse all no larger than 500. Use a triple-nested for loop that simply tries all possibilities. This is an example of “brute-force” computing. It’s not aesthetically pleasing to many people. But there are many reasons why these techniques are important. First, with computing power increasing at such a phenomenal pace, so- lutions that would have taken years or even centuries of computer time to produce with the tech- nology of just a few years ago can now be produced in hours, minutes or even seconds. Recent microprocessor chips can process a billion instructions per second! Second, as you’ll learn in more advanced computer science courses, there are large numbers of interesting problems for which there is no known algorithmic approach other than sheer brute force. We investigate many kinds of prob- lem-solving methodologies in this book. We’ll consider many brute-force approaches to various in- teresting problems.

4.28 (Calculating Weekly Pay) A company pays its employees as managers (who receive a fixed weekly salary), hourly workers (who receive a fixed hourly wage for up to the first 40 hours they work and “time-and-a-half”—i.e., 1.5 times their hourly wage—for overtime hours worked), com- mission workers (who receive $250 plus 5.7% of their gross weekly sales), or pieceworkers (who re- ceive a fixed amount of money for each of the items they produce—each pieceworker in this company works on only one type of item). Write a program to compute the weekly pay for each employee. You do not know the number of employees in advance. Each type of employee has its own pay code: Managers have paycode 1, hourly workers have code 2, commission workers have code 3 and pieceworkers have code 4. Use a switch to compute each employee’s pay based on that employee’s paycode. Within the switch, prompt the user (i.e., the payroll clerk) to enter the appro- priate facts your program needs to calculate each employee’s pay based on that employee’s paycode.

π 4 4 3 ---– 4

5 --- 4

7 ---– 4

9 --- 4

11 ------– …+ + +=

138 Chapter 4 C Program Control

4.29 (De Morgan’s Laws) In this chapter, we discussed the logical operators &&, ||, and !. De Morgan’s Laws can sometimes make it more convenient for us to express a logical expression. These laws state that the expression !(condition1 && condition2) is logically equivalent to the expression (!condition1 || !condition2). Also, the expression !(condition1 || condition2) is logically equivalent to the expression (!condition1 && !condition2). Use De Morgan’s Laws to write equivalent expres- sions for each of the following, and then write a program to show that both the original expression and the new expression in each case are equivalent.

a) !( x < 5 ) && !( y >= 7 ) b) !( a == b ) || !( g != 5 ) c) !( ( x <= 8 ) && ( y > 4 ) ) d) !( ( i > 4 ) || ( j <= 6 ) )

4.30 (Replacing switch with if…else) Rewrite the program of Fig. 4.7 by replacing the switch statement with a nested if…else statement; be careful to deal with the default case properly. Then rewrite this new version by replacing the nested if…else statement with a series of if statements; here, too, be careful to deal with the default case properly (this is more difficult than in the nested if…else version). This exercise demonstrates that switch is a convenience and that any switch statement can be written with only single-selection statements.

4.31 (Diamond Printing Program) Write a program that prints the following diamond shape. You may use printf statements that print either a single asterisk (*) or a single blank. Maximize your use of repetition (with nested for statements) and minimize the number of printf statements.

4.32 (Modified Diamond Printing Program) Modify the program you wrote in Exercise 4.31 to read an odd number in the range 1 to 19 to specify the number of rows in the diamond. Your pro- gram should then display a diamond of the appropriate size.

4.33 (Roman Numeral Equivalent of Decimal Values) Write a program that prints a table of all the Roman numeral equivalents of the decimal numbers in the range 1 to 100.

4.34 Describe the process you would use to replace a do…while loop with an equivalent while loop. What problem occurs when you try to replace a while loop with an equivalent do…while loop? Suppose you have been told that you must remove a while loop and replace it with a do…while. What additional control statement would you need to use and how would you use it to ensure that the resulting program behaves exactly as the original?

4.35 A criticism of the break statement and the continue statement is that each is unstructured. Actually, break statements and continue statements can always be replaced by structured state- ments, although doing so can be awkward. Describe in general how you would remove any break statement from a loop in a program and replace that statement with some structured equivalent. [Hint: The break statement leaves a loop from within the body of the loop. The other way to leave is by failing the loop-continuation test. Consider using in the loop-continuation test a second test that indicates “early exit because of a ‘break’ condition.”] Use the technique you developed here to remove the break statement from the program of Fig. 4.11.

* *** ***** ******* ********* ******* ***** *** *

Making a Difference 139

4.36 What does the following program segment do?

4.37 Describe in general how you would remove any continue statement from a loop in a pro- gram and replace that statement with some structured equivalent. Use the technique you developed here to remove the continue statement from the program of Fig. 4.12.

Making a Difference 4.38 (World Population Growth) World population has grown considerably over the centuries. Continued growth could eventually challenge the limits of breathable air, drinkable water, arable cropland and other limited resources. There is evidence that growth has been slowing in recent years and that world population could peak some time this century, then start to decline.

For this exercise, research world population growth issues online. Be sure to investigate various viewpoints. Get estimates for the current world population and its growth rate (the percentage by which it’s likely to increase this year). Write a program that calculates world population growth each year for the next 75 years, using the simplifying assumption that the current growth rate will stay constant. Print the results in a table. The first column should display the year from year 1 to year 75. The second column should display the anticipated world population at the end of that year. The third column should display the numerical increase in the world population that would occur that year. Using your results, determine the year in which the population would be double what it is today, if this year’s growth rate were to persist.

4.39 (Tax Plan Alternatives; The “FairTax”) There are many proposals to make taxation fairer. Check out the FairTax initiative in the United States at

www.fairtax.org/site/PageServer?pagename=calculator

Research how the proposed FairTax works. One suggestion is to eliminate income taxes and most other taxes in favor of a 23% consumption tax on all products and services that you buy. Some FairTax opponents question the 23% figure and say that because of the way the tax is calculated, it would be more accurate to say the rate is 30%—check this carefully. Write a program that prompts the user to enter expenses in various expense categories they have (e.g., housing, food, clothing, transportation, education, health care, vacations), then prints the estimated FairTax that person would pay.

1 for ( i = 1; i <= 5; i++ ) { 2 for ( j = 1; j <= 3; j++ ) { 3 for ( k = 1; k <= 4; k++ ) 4 printf( "*" ); 5 printf( "\n" ); 6 } 7 printf( "\n" ); 8 }

5 C Functions Form ever follows function. —Louis Henri Sullivan

E pluribus unum. (One composed of many.) —Virgil

O! call back yesterday, bid time return. —William Shakespeare

Answer me in one word. —William Shakespeare

There is a point at which methods devour themselves. —Frantz Fanon

O b j e c t i v e s In this chapter, you’ll learn:

■ To construct programs modularly from small pieces called functions.

■ Common math functions in the C Standard Library.

■ To create new functions.

■ The mechanisms used to pass information between functions.

■ How the function call/return mechanism is supported by the function call stack and activation records.

■ Simulation techniques using random number generation.

■ How to write and use functions that call themselves.

5.1 Introduction 141

5.1 Introduction Most computer programs that solve real-world problems are much larger than the pro- grams presented in the first few chapters. Experience has shown that the best way to de- velop and maintain a large program is to construct it from smaller pieces or modules, each of which is more manageable than the original program. This technique is called divide and conquer. This chapter describes the features of the C language that facilitate the de- sign, implementation, operation and maintenance of large programs.

5.2 Program Modules in C Modules in C are called functions. C programs are typically written by combining new functions you write with “prepackaged” functions available in the C Standard Library. We discuss both kinds of functions in this chapter. The C Standard Library provides a rich collection of functions for performing common mathematical calculations, string manip- ulations, character manipulations, input/output, and many other useful operations. This makes your job easier, because these functions provide many of the capabilities you need.

Although the Standard Library functions are technically not a part of the C language, they’re provided with standard C systems. The functions printf, scanf and pow that we’ve used in previous chapters are Standard Library functions.

5.1 Introduction

5.2 Program Modules in C

5.3 Math Library Functions

5.4 Functions

5.5 Function Definitions

5.6 Function Prototypes

5.7 Function Call Stack and Activation Records

5.8 Headers

5.9 Calling Functions By Value and By Reference

5.10 Random Number Generation 5.11 Example: A Game of Chance 5.12 Storage Classes 5.13 Scope Rules 5.14 Recursion 5.15 Example Using Recursion: Fibonacci

Series 5.16 Recursion vs. Iteration

Summary | Terminology | Self-Review Exercises | Answers to Self-Review Exercises | Exercises | Making a Difference

Good Programming Practice 5.1 Familiarize yourself with the rich collection of functions in the C Standard Library.

Software Engineering Observation 5.1 Avoid reinventing the wheel. When possible, use C Standard Library functions instead of writing new functions. This can reduce program development time.

Portability Tip 5.1 Using the functions in the C Standard Library helps make programs more portable.

142 Chapter 5 C Functions

You can write functions to define specific tasks that may be used at many points in a program. These are sometimes referred to as programmer-defined functions. The actual statements defining the function are written only once, and the statements are hidden from other functions.

Functions are invoked by a function call, which specifies the function name and pro- vides information (as arguments) that the called function needs to perform its designated task. A common analogy for this is the hierarchical form of management. A boss (the calling function or caller) asks a worker (the called function) to perform a task and report back when the task is done (Fig. 5.1). For example, a function needing to display infor- mation on the screen calls the worker function printf to perform that task, then printf displays the information and reports back—or returns—to the calling function when its task is completed. The boss function does not know how the worker function performs its designated tasks. The worker may call other worker functions, and the boss will be unaware of this. We’ll soon see how this “hiding” of implementation details promotes good software engineering. Figure 5.1 shows the main function communicating with sev- eral worker functions in a hierarchical manner. Note that worker1 acts as a boss function to worker4 and worker5. Relationships among functions may differ from the hierarchical structure shown in this figure.

5.3 Math Library Functions Math library functions allow you to perform certain common mathematical calculations. We use various math library functions here to introduce the concept of functions. Later in the book, we’ll discuss many of the other functions in the C Standard Library.

Functions are normally used in a program by writing the name of the function followed by a left parenthesis followed by the argument (or a comma-separated list of arguments) of the function followed by a right parenthesis. For example, a programmer desiring to calculate and print the square root of 900.0 might write

When this statement executes, the math library function sqrt is called to calculate the square root of the number contained in the parentheses (900.0). The number 900.0 is the

Fig. 5.1 | Hierarchical boss function/worker function relationship.

printf( "%.2f", sqrt( 900.0 ) );

Boss

Worker2 Worker3Worker1

Worker5Worker4

5.3 Math Library Functions 143

argument of the sqrt function. The preceding statement would print 30.00. The sqrt function takes an argument of type double and returns a result of type double. All func- tions in the math library that return floating point values return the data type double. Note that double values, like float values, can be output using the %f conversion specifi- cation.

Function arguments may be constants, variables, or expressions. If c1 = 13.0, d = 3.0 and f = 4.0, then the statement

calculates and prints the square root of 13.0 + 3.0 * 4.0 = 25.0, namely 5.00. Some C math library functions are summarized in Fig. 5.2. In the figure, the variables

x and y are of type double.

Error-Prevention Tip 5.1 Include the math header by using the preprocessor directive #include <math.h> when using functions in the math library.

printf( "%.2f", sqrt( c1 + d * f ) );

Function Description Example

sqrt( x ) square root of x sqrt( 900.0 ) is 30.0 sqrt( 9.0 ) is 3.0

exp( x ) exponential function ex exp( 1.0 ) is 2.718282

exp( 2.0 ) is 7.389056

log( x ) natural logarithm of x (base e) log( 2.718282 ) is 1.0 log( 7.389056 ) is 2.0

log10( x ) logarithm of x (base 10) log10( 1.0 ) is 0.0 log10( 10.0 ) is 1.0 log10( 100.0 ) is 2.0

fabs( x ) absolute value of x fabs( 13.5 ) is 13.5 fabs( 0.0 ) is 0.0 fabs( -13.5 ) is 13.5

ceil( x ) rounds x to the smallest integer not less than x

ceil( 9.2 ) is 10.0 ceil( -9.8 ) is -9.0

floor( x ) rounds x to the largest integer not greater than x

floor( 9.2 ) is 9.0 floor( -9.8 ) is -10.0

pow( x, y ) x raised to power y (x y) pow( 2, 7 ) is 128.0 pow( 9, .5 ) is 3.0

fmod( x, y ) remainder of x/y as a floating-point number

fmod( 13.657, 2.333 ) is 1.992

sin( x ) trigonometric sine of x (x in radians) sin( 0.0 ) is 0.0

cos( x ) trigonometric cosine of x (x in radians) cos( 0.0 ) is 1.0 tan( x ) trigonometric tangent of x (x in radians) tan( 0.0 ) is 0.0

Fig. 5.2 | Commonly used math library functions.

144 Chapter 5 C Functions

5.4 Functions Functions allow you to modularize a program. All variables defined in function definitions are local variables—they’re known only in the function in which they’re defined. Most functions have a list of parameters that provide the means for communicating information between functions. A function’s parameters are also local variables of that function.

There are several motivations for “functionalizing” a program. The divide-and-con- quer approach makes program development more manageable. Another motivation is software reusability—using existing functions as building-blocks to create new programs. Software reusability is a major factor in the object-oriented programming movement that you’ll learn more about when you study languages derived from C, such as C++, Java and C# (pronounced “C sharp”). With good function naming and definition, programs can be created from standardized functions that accomplish specific tasks, rather than being built by using customized code. This is known as abstraction. We use abstraction each time we use standard library functions like printf, scanf and pow. A third motivation is to avoid repeating code in a program. Packaging code as a function allows the code to be executed from several locations in a program simply by calling the function.

5.5 Function Definitions Each program we’ve presented has consisted of a function called main that called standard library functions to accomplish its tasks. We now consider how to write custom functions. Consider a program that uses a function square to calculate and print the squares of the integers from 1 to 10 (Fig. 5.3).

Software Engineering Observation 5.2 In programs containing many functions, main is often implemented as a group of calls to functions that perform the bulk of the program’s work.

Software Engineering Observation 5.3 Each function should be limited to performing a single, well-defined task, and the function name should express that task. This facilitates abstraction and promotes software reusability.

Software Engineering Observation 5.4 If you cannot choose a concise name that expresses what the function does, it’s possible that your function is attempting to perform too many diverse tasks. It’s usually best to break such a function into several smaller functions—sometimes called decomposition.

Good Programming Practice 5.2 Place a blank line between function definitions to separate the functions and enhance pro- gram readability.

1 /* Fig. 5.3: fig05_03.c 2 Creating and using a programmer-defined function */ 3 #include <stdio.h>

Fig. 5.3 | Using a programmer-defined function. (Part 1 of 2.)

5.5 Function Definitions 145

Function square is invoked or called in main within the printf statement (line 14)

Function square receives a copy of the value of x in the parameter y (line 22). Then square calculates y * y (line 24). The result is passed back to function printf in main where square was invoked (line 14), and printf displays the result. This process is repeat- ed 10 times using the for repetition statement.

The definition of function square shows that square expects an integer parameter y. The keyword int preceding the function name (line 22) indicates that square returns an integer result. The return statement in square passes the result of the calculation back to the calling function.

Line 5

is a function prototype. The int in parentheses informs the compiler that square expects to receive an integer value from the caller. The int to the left of the function name square informs the compiler that square returns an integer result to the caller. The compiler re- fers to the function prototype to check that calls to square (line 14) contain the correct return type, the correct number of arguments, the correct argument types, and that the arguments are in the correct order. Function prototypes are discussed in detail in Section 5.6.

4 5 6 7 /* function main begins program execution */ 8 int main( void ) 9 {

10 int x; /* counter */ 11 12 /* loop 10 times and calculate and output square of x each time */ 13 for ( x = 1; x <= 10; x++ ) { 14 printf( "%d ", ); /* function call */ 15 } /* end for */ 16 17 printf( "\n" ); 18 return 0; /* indicates successful termination */ 19 } /* end main */ 20 21 22 23 24 25

1 4 9 16 25 36 49 64 81 100

printf( "%d ", square( x ) ); /* function call */

int square( int y ); /* function prototype */

Fig. 5.3 | Using a programmer-defined function. (Part 2 of 2.)

int square( int y ); /* function prototype */

square( x )

/* square function definition returns square of parameter */ int square( int y ) /* y is a copy of argument to function */ { return y * y; /* returns square of y as an int */ } /* end function square */

146 Chapter 5 C Functions

The format of a function definition is

The function-name is any valid identifier. The return-value-type is the data type of the re- sult returned to the caller. The return-value-type void indicates that a function does not return a value. Together, the return-value-type, function-name and parameter-list are some- times referred to as the function header.

The parameter-list is a comma-separated list that specifies the parameters received by the function when it’s called. If a function does not receive any values, parameter-list is void. A type must be listed explicitly for each parameter.

The definitions and statements within braces form the function body. The function body is also referred to as a block. Variables can be declared in any block, and blocks can be nested. A function cannot be defined inside another function.

return-value-type function-name( parameter-list ) {

definitions statements

}

Common Programming Error 5.1 Forgetting to return a value from a function that is supposed to return a value can lead to unexpected errors. The C standard states that the result of this omission is undefined.

Common Programming Error 5.2 Returning a value from a function with a void return type is a compilation error.

Common Programming Error 5.3 Specifying function parameters of the same type as double x, y instead of double x, dou- ble y results in a compilation error.

Common Programming Error 5.4 Placing a semicolon after the right parenthesis enclosing the parameter list of a function definition is a syntax error.

Common Programming Error 5.5 Defining a parameter again as a local variable in a function is a compilation error.

Good Programming Practice 5.3 Although it’s not incorrect to do so, do not use the same names for a function’s arguments and the corresponding parameters in the function definition. This helps avoid ambiguity.

Common Programming Error 5.6 Defining a function inside another function is a syntax error.

Good Programming Practice 5.4 Choosing meaningful function names and meaningful parameter names makes programs more readable and helps avoid excessive use of comments.

5.5 Function Definitions 147

There are three ways to return control from a called function to the point at which a function was invoked. If the function does not return a result, control is returned simply when the function-ending right brace is reached, or by executing the statement

If the function does return a result, the statement

returns the value of expression to the caller.

Function maximum Our second example uses a programmer-defined function maximum to determine and re- turn the largest of three integers (Fig. 5.4). The three integers are input with scanf1 (line 15). Next, the integers are passed to maximum (line 19), which determines the largest inte- ger. This value is returned to main by the return statement in maximum (line 37). The val- ue returned is then printed in the printf statement (line 19).

Software Engineering Observation 5.5 A function should generally be no longer than one page. Better yet, functions should generally be no longer than half a page. Small functions promote software reusability.

Software Engineering Observation 5.6 Programs should be written as collections of small functions. This makes programs easier to write, debug, maintain and modify.

Software Engineering Observation 5.7 A function requiring a large number of parameters may be performing too many tasks. Consider dividing the function into smaller functions that perform the separate tasks. The function header should fit on one line if possible.

Software Engineering Observation 5.8 The function prototype, function header and function calls should all agree in the number, type, and order of arguments and parameters, and in the type of return value.

return;

return expression;

1. Many C library functions, like scanf, return values indicating whether they performed their task suc- cessfuly. In production code, you should test these return values to ensure that your program is op- erating properly. Read the documentation for each library function you use to learn about its return values. The site wpollock.com/CPlus/PrintfRef.htm#scanfRetCode discusses how to process re- turn values from function scanf.

1 /* Fig. 5.4: fig05_04.c 2 Finding the maximum of three integers */ 3 #include <stdio.h> 4 5 6

Fig. 5.4 | Finding the maximum of three integers. (Part 1 of 2.)

int maximum( int x, int y, int z ); /* function prototype */

148 Chapter 5 C Functions

5.6 Function Prototypes One of the most important features of C is the function prototype. This feature was bor- rowed by the C standard committee from the developers of C++. A function prototype tells the compiler the type of data returned by the function, the number of parameters the function expects to receive, the types of the parameters, and the order in which these pa- rameters are expected. The compiler uses function prototypes to validate function calls.

7 /* function main begins program execution */ 8 int main( void ) 9 {

10 int number1; /* first integer */ 11 int number2; /* second integer */ 12 int number3; /* third integer */ 13 14 printf( "Enter three integers: " ); 15 scanf( "%d%d%d", &number1, &number2, &number3 ); 16 17 /* number1, number2 and number3 are arguments 18 to the maximum function call */ 19 printf( "Maximum is: %d\n", ); 20 return 0; /* indicates successful termination */ 21 } /* end main */ 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

Enter three integers: 22 85 17 Maximum is: 85

Enter three integers: 85 22 17 Maximum is: 85

Enter three integers: 22 17 85 Maximum is: 85

Fig. 5.4 | Finding the maximum of three integers. (Part 2 of 2.)

maximum( number1, number2, number3 )

/* Function maximum definition */ /* x, y and z are parameters */ int maximum( int x, int y, int z ) { int max = x; /* assume x is largest */ if ( y > max ) { /* if y is larger than max, assign y to max */ max = y; } /* end if */ if ( z > max ) { /* if z is larger than max, assign z to max */ max = z; } /* end if */ return max; /* max is largest value */ } /* end function maximum */

5.6 Function Prototypes 149

Previous versions of C did not perform this kind of checking, so it was possible to call functions improperly without the compiler detecting the errors. Such calls could result in fatal execution-time errors or nonfatal errors that caused subtle, difficult-to-detect logic er- rors. Function prototypes correct this deficiency.

The function prototype for maximum in Fig. 5.4 (line 5) is

This function prototype states that maximum takes three arguments of type int and returns a result of type int. Notice that the function prototype is the same as the first line of the function definition of maximum.

A function call that does not match the function prototype is a compilation error. An error is also generated if the function prototype and the function definition disagree. For example, in Fig. 5.4, if the function prototype had been written

the compiler would generate an error because the void return type in the function proto- type would differ from the int return type in the function header.

Another important feature of function prototypes is the coercion of arguments, i.e., the forcing of arguments to the appropriate type. For example, the math library function sqrt can be called with an integer argument even though the function prototype in <math.h> specifies a double argument, and the function will still work correctly. The statement

correctly evaluates sqrt( 4 ), and prints the value 2.000. The function prototype causes the compiler to convert the integer value 4 to the double value 4.0 before the value is passed to sqrt. In general, argument values that do not correspond precisely to the param- eter types in the function prototype are converted to the proper type before the function is called. These conversions can lead to incorrect results if C’s promotion rules are not fol- lowed. The promotion rules specify how types can be converted to other types without los- ing data. In our sqrt example above, an int is automatically converted to a double

Good Programming Practice 5.5 Include function prototypes for all functions to take advantage of C’s type-checking ca- pabilities. Use #include preprocessor directives to obtain function prototypes for the stan- dard library functions from the headers for the appropriate libraries, or to obtain headers containing function prototypes for functions developed by you and/or your group members.

int maximum( int x, int y, int z ); /* function prototype */

Good Programming Practice 5.6 Parameter names are sometimes included in function prototypes (our preference) for doc- umentation purposes. The compiler ignores these names.

Common Programming Error 5.7 Forgetting the semicolon at the end of a function prototype is a syntax error.

void maximum( int x, int y, int z );

printf( "%.3f\n", sqrt( 4 ) );

150 Chapter 5 C Functions

without changing its value. However, a double converted to an int truncates the fraction- al part of the double value. Converting large integer types to small integer types (e.g., long to short) may also result in changed values.

The promotion rules automatically apply to expressions containing values of two or more data types (also referred to as mixed-type expressions). The type of each value in a mixed-type expression is automatically promoted to the “highest” type in the expression (actually a temporary version of each value is created and used for the expression—the original values remain unchanged). Figure 5.5 lists the data types in order from highest type to lowest type with each type’s printf and scanf conversion specifications.

Converting values to lower types normally results in an incorrect value. Therefore, a value can be converted to a lower type only by explicitly assigning the value to a variable of lower type, or by using a cast operator. Function argument values are converted to the parameter types in a function prototype as if they were being assigned directly to variables of those types. If our square function that uses an integer parameter (Fig. 5.3) is called with a floating-point argument, the argument is converted to int (a lower type), and square usually returns an incorrect value. For example, square( 4.5 ) returns 16, not 20.25.

If there is no function prototype for a function, the compiler forms its own function prototype using the first occurrence of the function—either the function definition or a call to the function. This typically leads to warnings or errors, depending on the compiler.

Data type printf conversion specification

scanf conversion specification

long double %Lf %Lf

double %f %lf

float %f %f

unsigned long int %lu %lu

long int %ld %ld

unsigned int %u %u

int %d %d

unsigned short %hu %hu

short %hd %hd

char %c %c

Fig. 5.5 | Promotion hierarchy for data types.

Common Programming Error 5.8 Converting from a higher data type in the promotion hierarchy to a lower type can change the data value. Many compilers issue warnings in such cases.

Error-Prevention Tip 5.2 Always include function prototypes for the functions you define or use in your program to help prevent compilation errors and warnings.

5.7 Function Call Stack and Activation Records 151

5.7 Function Call Stack and Activation Records To understand how C performs function calls, we first need to consider a data structure (i.e., collection of related data items) known as a stack. Students can think of a stack as analogous to a pile of dishes. When a dish is placed on the pile, it’s normally placed at the top (referred to as pushing the dish onto the stack). Similarly, when a dish is removed from the pile, it’s always removed from the top (referred to as popping the dish off the stack). Stacks are known as last-in, first-out (LIFO) data structures—the last item pushed (insert- ed) on the stack is the first item popped (removed) from the stack.

When a program calls a function, the called function must know how to return to its caller, so the return address of the calling function is pushed onto the program execution stack (sometimes referred to as the function call stack). If a series of function calls occurs, the successive return addresses are pushed onto the stack in last-in, first-out order so that each function can return to its caller.

The program execution stack also contains the memory for the local variables used in each invocation of a function during a program’s execution. This data, stored as a portion of the program execution stack, is known as the activation record or stack frame of the function call. When a function call is made, the activation record for that function call is pushed onto the program execution stack. When the function returns to its caller, the acti- vation record for this function call is popped off the stack and those local variables are no longer known to the program.

Of course, the amount of memory in a computer is finite, so only a certain amount of memory can be used to store activation records on the program execution stack. If more function calls occur than can have their activation records stored on the program execution stack, an error known as a stack overflow occurs.

5.8 Headers Each standard library has a corresponding header containing the function prototypes for all the functions in that library and definitions of various data types and constants needed by those functions. Figure 5.6 lists alphabetically some of the standard library headers that may be included in programs. The term “macros” that is used several times in Fig. 5.6 is discussed in detail in Chapter 13, C Preprocessor.

You can create custom headers. Programmer-defined headers should also use the .h filename extension. A programmer-defined header can be included by using the #include preprocessor directive. For example, if the prototype for our square function was located in the header square.h, we’d include that header in our program by using the following directive at the top of the program:

Section 13.2 presents additional information on including headers.

Software Engineering Observation 5.9 A function prototype placed outside any function definition applies to all calls to the function appearing after the function prototype in the file. A function prototype placed in a function applies only to calls made in that function.

#include "square.h"

152 Chapter 5 C Functions

5.9 Calling Functions By Value and By Reference There are two ways to invoke functions in many programming languages—call-by-value and call-by-reference. When arguments are passed by value, a copy of the argument’s value is made and passed to the called function. Changes to the copy do not affect an original variable’s value in the caller. When an argument is passed by reference, the caller allows the called function to modify the original variable’s value.

Call-by-value should be used whenever the called function does not need to modify the value of the caller’s original variable. This prevents the accidental side effects (variable modifications) that so greatly hinder the development of correct and reliable software sys- tems. Call-by-reference should be used only with trusted called functions that need to modify the original variable.

In C, all calls are by value. As we’ll see in Chapter 7, it’s possible to simulate call-by- reference by using address operators and indirection operators. In Chapter 6, we’ll see that

Header Explanation

<assert.h> Contains macros and information for adding diagnostics that aid program debugging.

<ctype.h> Contains function prototypes for functions that test characters for certain properties, and function prototypes for functions that can be used to convert lowercase letters to uppercase letters and vice versa.

<errno.h> Defines macros that are useful for reporting error conditions.

<float.h> Contains the floating-point size limits of the system.

<limits.h> Contains the integral size limits of the system.

<locale.h> Contains function prototypes and other information that enables a program to be modified for the current locale on which it’s running. The notion of locale enables the computer system to handle different conventions for expressing data like dates, times, dollar amounts and large numbers throughout the world.

<math.h> Contains function prototypes for math library functions.

<setjmp.h> Contains function prototypes for functions that allow bypassing of the usual function call and return sequence.

<signal.h> Contains function prototypes and macros to handle various conditions that may arise during program execution.

<stdarg.h> Defines macros for dealing with a list of arguments to a function whose num- ber and types are unknown.

<stddef.h> Contains common type definitions used by C for performing calculations.

<stdio.h> Contains function prototypes for the standard input/output library functions, and information used by them.

<stdlib.h> Contains function prototypes for conversions of numbers to text and text to numbers, memory allocation, random numbers, and other utility functions.

<string.h> Contains function prototypes for string-processing functions.

<time.h> Contains function prototypes and types for manipulating the time and date.

Fig. 5.6 | Some of the standard library headers.

5.10 Random Number Generation 153

arrays are automatically passed by reference. We’ll have to wait until Chapter 7 for a full understanding of this complex issue. For now, we concentrate on call-by-value.

5.10 Random Number Generation We now take a brief and, hopefully, entertaining diversion into a popular programming application, namely simulation and game playing. In this and the next section, we’ll de- velop a nicely structured game-playing program that includes multiple functions. The pro- gram uses most of the control structures we’ve studied. The element of chance can be introduced into computer applications by using the C Standard Library function rand from the <stdlib.h> header.

Consider the following statement:

The rand function generates an integer between 0 and RAND_MAX (a symbolic constant de- fined in the <stdlib.h> header). Standard C states that the value of RAND_MAX must be at least 32767, which is the maximum value for a two-byte (i.e., 16-bit) integer. The pro- grams in this section were tested on a C system with a maximum value of 32767 for RAND_MAX. If rand truly produces integers at random, every number between 0 and RAND_MAX has an equal chance (or probability) of being chosen each time rand is called.

The range of values produced directly by rand is often different from what is needed in a specific application. For example, a program that simulates coin tossing might require only 0 for “heads” and 1 for “tails.” A dice-rolling program that simulates a six-sided die would require random integers from 1 to 6.

Rolling a Six-Sided Die To demonstrate rand, let’s develop a program to simulate 20 rolls of a six-sided die and print the value of each roll. The function prototype for function rand is in <stdlib.h>. We use the remainder operator (%) in conjunction with rand as follows

to produce integers in the range 0 to 5. This is called scaling. The number 6 is called the scaling factor. We then shift the range of numbers produced by adding 1 to our previous result. The output of Fig. 5.7 confirms that the results are in the range 1 to 6—the output might vary by compiler.

i = rand();

rand() % 6

1 /* Fig. 5.7: fig05_07.c 2 Shifted, scaled integers produced by 1 + rand() % 6 */ 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 /* function main begins program execution */ 7 int main( void ) 8 { 9 int i; /* counter */

10

Fig. 5.7 | Shifted, scaled random integers produced by 1 + rand() % 6. (Part 1 of 2.)

154 Chapter 5 C Functions

Rolling a Six-Sided Die 6000 Times To show that these numbers occur approximately with equal likelihood, let’s simulate 6000 rolls of a die with the program of Fig. 5.8. Each integer from 1 to 6 should appear approximately 1000 times.

As the program output shows, by scaling and shifting we’ve used the rand function to realistically simulate the rolling of a six-sided die. No default case is provided in the switch statement. Also note the use of the %s conversion specifier to print the character strings "Face" and "Frequency" as column headers (line 53). After we study arrays in Chapter 6, we’ll show how to replace this entire switch statement elegantly with a single- line statement.

11 /* loop 20 times */ 12 for ( i = 1; i <= 20; i++ ) { 13 14 /* pick random number from 1 to 6 and output it */ 15 printf( "%10d", ); 16 17 /* if counter is divisible by 5, begin new line of output */ 18 if ( i % 5 == 0 ) { 19 printf( "\n" ); 20 } /* end if */ 21 } /* end for */ 22 23 return 0; /* indicates successful termination */ 24 } /* end main */

6 6 5 5 6 5 1 1 5 3 6 6 2 4 2 6 2 3 4 1

1 /* Fig. 5.8: fig05_08.c 2 Roll a six-sided die 6000 times */ 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 /* function main begins program execution */ 7 int main( void ) 8 { 9 int frequency1 = 0; /* rolled 1 counter */

10 int frequency2 = 0; /* rolled 2 counter */ 11 int frequency3 = 0; /* rolled 3 counter */ 12 int frequency4 = 0; /* rolled 4 counter */ 13 int frequency5 = 0; /* rolled 5 counter */ 14 int frequency6 = 0; /* rolled 6 counter */ 15 16 int roll; /* roll counter, value 1 to 6000 */

Fig. 5.8 | Rolling a six-sided die 6000 times. (Part 1 of 2.)

Fig. 5.7 | Shifted, scaled random integers produced by 1 + rand() % 6. (Part 2 of 2.)

1 + ( rand() % 6 )

5.10 Random Number Generation 155

17 int face; /* represents one roll of the die, value 1 to 6 */ 18 19 /* loop 6000 times and summarize results */ 20 for ( roll = 1; roll <= 6000; roll++ ) { 21 face = 1 + rand() % 6; /* random number from 1 to 6 */ 22 23 /* determine face value and increment appropriate counter */ 24 switch ( face ) { 25 26 case 1: /* rolled 1 */ 27 ++frequency1; 28 break; 29 30 case 2: /* rolled 2 */ 31 ++frequency2; 32 break; 33 34 case 3: /* rolled 3 */ 35 ++frequency3; 36 break; 37 38 case 4: /* rolled 4 */ 39 ++frequency4; 40 break; 41 42 case 5: /* rolled 5 */ 43 ++frequency5; 44 break; 45 46 case 6: /* rolled 6 */ 47 ++frequency6; 48 break; /* optional */ 49 } /* end switch */ 50 } /* end for */ 51 52 /* display results in tabular format */ 53 printf( " \n", "Face", "Frequency" ); 54 printf( " 1%13d\n", frequency1 ); 55 printf( " 2%13d\n", frequency2 ); 56 printf( " 3%13d\n", frequency3 ); 57 printf( " 4%13d\n", frequency4 ); 58 printf( " 5%13d\n", frequency5 ); 59 printf( " 6%13d\n", frequency6 ); 60 return 0; /* indicates successful termination */ 61 } /* end main */

Face Frequency 1 1003 2 1017 3 983 4 994 5 1004 6 999

Fig. 5.8 | Rolling a six-sided die 6000 times. (Part 2 of 2.)

%s%13s

156 Chapter 5 C Functions

Randomizing the Random Number Generator Executing the program of Fig. 5.7 again produces

Notice that exactly the same sequence of values was printed. How can these be random numbers? Ironically, this repeatability is an important characteristic of function rand. When debugging a program, this repeatability is essential for proving that corrections to a program work properly.

Function rand actually generates pseudorandom numbers. Calling rand repeatedly produces a sequence of numbers that appears to be random. However, the sequence repeats itself each time the program is executed. Once a program has been thoroughly debugged, it can be conditioned to produce a different sequence of random numbers for each execution. This is called randomizing and is accomplished with the standard library function srand. Function srand takes an unsigned integer argument and seeds function rand to produce a different sequence of random numbers for each execution of the program.

We demonstrate srand in Fig. 5.9. In the program, we use the data type unsigned, which is short for unsigned int. An int is stored in at least two bytes of memory and can have positive and negative values. A variable of type unsigned is also stored in at least two bytes of memory. A two-byte unsigned int can have only positive values in the range 0 to 65535. A four-byte unsigned int can have only positive values in the range 0 to 4294967295. Function srand takes an unsigned value as an argument. The conversion specifier %u is used to read an unsigned value with scanf. The function prototype for srand is found in <stdlib.h>.

6 6 5 5 6 5 1 1 5 3 6 6 2 4 2 6 2 3 4 1

1 /* Fig. 5.9: fig05_09.c 2 Randomizing die-rolling program */ 3 #include <stdlib.h> 4 #include <stdio.h> 5 6 /* function main begins program execution */ 7 int main( void ) 8 { 9 int i; /* counter */

10 11 12 printf( "Enter seed: " ); 13 scanf( "%u", &seed ); /* note %u for unsigned */ 14 15 /* seed random number generator */ 16

Fig. 5.9 | Randomizing the die-rolling program. (Part 1 of 2.)

unsigned seed; /* number used to seed random number generator */

srand( seed );

5.10 Random Number Generation 157

Let’s run the program several times and observe the results. Notice that a different sequence of random numbers is obtained each time the program is run, provided that a different seed is supplied.

To randomize without entering a seed each time, use a statement like

This causes the computer to read its clock to obtain the value for the seed automatically. Function time returns the number of seconds that have passed since midnight on January 1, 1970. This value is converted to an unsigned integer and used as the seed to the random number generator. Function time takes NULL as an argument (time is capable of providing you with a string representing the value it returns; NULL disables this capability for a spe- cific call to time). The function prototype for time is in <time.h>.

Generalized Scaling and Shifting of Random Numbers The values produced directly by rand are always in the range:

17 /* loop 10 times */ 18 for ( i = 1; i <= 10; i++ ) { 19 20 /* pick a random number from 1 to 6 and output it */ 21 printf( "%10d", 1 + ( rand() % 6 ) ); 22 23 /* if counter is divisible by 5, begin a new line of output */ 24 if ( i % 5 == 0 ) { 25 printf( "\n" ); 26 } /* end if */ 27 } /* end for */ 28 29 return 0; /* indicates successful termination */ 30 } /* end main */

Enter seed: 67 6 1 4 6 2 1 6 1 6 4 Enter seed: 867 2 4 6 1 6 1 1 3 6 2 Enter seed: 67 6 1 4 6 2 1 6 1 6 4

srand( time( NULL ) );

0 ≤ rand() ≤ RAND_MAX

Fig. 5.9 | Randomizing the die-rolling program. (Part 2 of 2.)

158 Chapter 5 C Functions

As you know, the following statement simulates rolling a six-sided die:

This statement always assigns an integer value (at random) to the variable face in the range 1 ≤face ≤6. The width of this range (i.e., the number of consecutive integers in the range) is 6 and the starting number in the range is 1. Referring to the preceding statement, we see that the width of the range is determined by the number used to scale rand with the remainder operator (i.e., 6), and the starting number of the range is equal to the num- ber (i.e., 1) that is added to rand % 6. We can generalize this result as follows

where a is the shifting value (which is equal to the first number in the desired range of consecutive integers) and b is the scaling factor (which is equal to the width of the desired range of consecutive integers). In the exercises, we’ll see that it’s possible to choose integers at random from sets of values other than ranges of consecutive integers.

5.11 Example: A Game of Chance One of the most popular games of chance is a dice game known as “craps,” which is played in casinos and back alleys throughout the world. The rules of the game are straightforward:

A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5, and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on the first throw, the player wins. If the sum is 2, 3, or 12 on the first throw (called “craps”), the player loses (i.e., the “house” wins). If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then that sum becomes the player’s “point.” To win, you must continue rolling the dice until you “make your point.” The player loses by rolling a 7 before making the point.

Figure 5.10 simulates the game of craps and Fig. 5.11 shows several sample executions.

face = 1 + rand() % 6;

n = a + rand() % b;

Common Programming Error 5.9 Using srand in place of rand to generate random numbers.

1 /* Fig. 5.10: fig05_10.c 2 Craps */ 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <time.h> /* contains prototype for function time */ 6 7 /* enumeration constants represent game status */ 8 9

10 11 12 /* function main begins program execution */ 13 int main( void ) 14 {

Fig. 5.10 | Program to simulate the game of craps. (Part 1 of 3.)

enum Status { CONTINUE, WON, LOST };

int rollDice( void ); /* function prototype */

5.11 Example: A Game of Chance 159

15 int sum; /* sum of rolled dice */ 16 int myPoint; /* point earned */ 17 18 /* can contain CONTINUE, WON, or LOST */ 19 20 /* randomize random number generator using current time */ 21 22 23 24 25 /* determine game status based on sum of dice */ 26 switch( sum ) { 27 28 /* win on first roll */ 29 case 7: 30 case 11: 31 gameStatus = WON; 32 break; 33 34 /* lose on first roll */ 35 case 2: 36 case 3: 37 case 12: 38 gameStatus = LOST; 39 break; 40 41 /* remember point */ 42 default: 43 gameStatus = CONTINUE; 44 myPoint = sum; 45 printf( "Point is %d\n", myPoint ); 46 break; /* optional */ 47 } /* end switch */ 48 49 /* while game not complete */ 50 while ( gameStatus == CONTINUE ) { 51 52 53 /* determine game status */ 54 if ( sum == myPoint ) { /* win by making point */ 55 gameStatus = WON; /* game over, player won */ 56 } /* end if */ 57 else { 58 if ( sum == 7 ) { /* lose by rolling 7 */ 59 gameStatus = LOST; /* game over, player lost */ 60 } /* end if */ 61 } /* end else */ 62 } /* end while */ 63 64 /* display won or lost message */ 65 if ( gameStatus == WON ) { /* did player win? */ 66 printf( "Player wins\n" ); 67 } /* end if */

Fig. 5.10 | Program to simulate the game of craps. (Part 2 of 3.)

enum Status gameStatus;

srand( time( NULL ) );

sum = rollDice(); /* first roll of the dice */

sum = rollDice(); /* roll dice again */

160 Chapter 5 C Functions

In the rules of the game, notice that the player must roll two dice on the first roll, and must do so later on all subsequent rolls. We define a function rollDice to roll the dice and compute and print their sum. Function rollDice is defined once, but it’s called from

68 else { /* player lost */ 69 printf( "Player loses\n" ); 70 } /* end else */ 71 72 return 0; /* indicates successful termination */ 73 } /* end main */ 74 75 /* roll dice, calculate sum and display results */ 76 77 { 78 int die1; /* first die */ 79 int die2; /* second die */ 80 int workSum; /* sum of dice */ 81 82 die1 = 1 + ( rand() % 6 ); /* pick random die1 value */ 83 die2 = 1 + ( rand() % 6 ); /* pick random die2 value */ 84 workSum = die1 + die2; /* sum die1 and die2 */ 85 86 /* display results of this roll */ 87 printf( "Player rolled %d + %d = %d\n", die1, die2, workSum ); 88 return workSum; /* return sum of dice */ 89 } /* end function rollRice */

Player rolled 5 + 6 = 11 Player wins

Player rolled 4 + 1 = 5 Point is 5 Player rolled 6 + 2 = 8 Player rolled 2 + 1 = 3 Player rolled 3 + 2 = 5 Player wins

Player rolled 1 + 1 = 2 Player loses

Player rolled 6 + 4 = 10 Point is 10 Player rolled 3 + 4 = 7 Player loses

Fig. 5.11 | Sample runs for the game of craps.

Fig. 5.10 | Program to simulate the game of craps. (Part 3 of 3.)

int rollDice( void )

5.12 Storage Classes 161

two places in the program (lines 23 and 51). Interestingly, rollDice takes no arguments, so we’ve indicated void in the parameter list (line 76). Function rollDice does return the sum of the two dice, so a return type of int is indicated in the function header.

The game is reasonably involved. The player may win or lose on the first roll, or may win or lose on any subsequent roll. Variable gameStatus, defined to be of a new type— enum Status—stores the current status. Line 8 creates a programmer-defined type called an enumeration. An enumeration, introduced by the keyword enum, is a set of integer con- stants represented by identifiers. Enumeration constants are sometimes called symbolic constants. Values in an enum start with 0 and are incremented by 1. In line 8, the constant CONTINUE has the value 0, WON has the value 1 and LOST has the value 2. It’s also possible to assign an integer value to each identifier in an enum (see Chapter 10). The identifiers in an enumeration must be unique, but the values may be duplicated.

When the game is won, either on the first roll or on a subsequent roll, gameStatus is set to WON. When the game is lost, either on the first roll or on a subsequent roll, game- Status is set to LOST. Otherwise gameStatus is set to CONTINUE and the game continues.

After the first roll, if the game is over, the while statement (line 50) is skipped because gameStatus is not CONTINUE. The program proceeds to the if…else statement at line 65, which prints "Player wins" if gameStatus is WON and "Player loses" otherwise.

After the first roll, if the game is not over, then sum is saved in myPoint. Execution proceeds with the while statement (line 50) because gameStatus is CONTINUE. Each time through the while, rollDice is called to produce a new sum. If sum matches myPoint, gameStatus is set to WON to indicate that the player won, the while-test fails, the if…else statement (line 65) prints "Player wins" and execution terminates. If sum is equal to 7 (line 58), gameStatus is set to LOST to indicate that the player lost, the while-test fails, the if…else statement (line 65) prints "Player loses" and execution terminates.

Note the program’s interesting control architecture. We’ve used two functions—main and rollDice—and the switch, while, nested if…else and nested if statements. In the exercises, we’ll investigate various interesting characteristics of the game of craps.

5.12 Storage Classes In Chapters 2–4, we used identifiers for variable names. The attributes of variables include name, type, size and value. In this chapter, we also use identifiers as names for user-defined functions. Actually, each identifier in a program has other attributes, including storage class, storage duration, scope and linkage.

C provides four storage classes, indicated by the storage class specifiers: auto, reg- ister, extern and static. An identifier’s storage class determines its storage duration, scope and linkage. An identifier’s storage duration is the period during which the identi- fier exists in memory. Some exist briefly, some are repeatedly created and destroyed, and

Common Programming Error 5.10 Assigning a value to an enumeration constant after it has been defined is a syntax error.

Common Programming Error 5.11 Use only uppercase letters in the names of enumeration constants to make these constants stand out in a program and to indicate that enumeration constants are not variables.

162 Chapter 5 C Functions

others exist for the entire execution of a program. An identifier’s scope is where the iden- tifier can be referenced in a program. Some can be referenced throughout a program, others from only portions of a program. An identifier’s linkage determines for a multiple- source-file program (a topic we’ll investigate in Chapter 14) whether the identifier is known only in the current source file or in any source file with proper declarations. This section discusses storage classes and storage duration. Section 5.13 discusses scope. Chapter 14 discusses identifier linkage and programming with multiple source files.

The four storage-class specifiers can be split into two storage durations: automatic storage duration and static storage duration. Keywords auto and register are used to declare variables of automatic storage duration. Variables with automatic storage duration are created when the block in which they’re defined is entered; they exist while the block is active, and they’re destroyed when the block is exited.

Local Variables Only variables can have automatic storage duration. A function’s local variables (those de- clared in the parameter list or function body) normally have automatic storage duration. Keyword auto explicitly declares variables of automatic storage duration. For example, the following declaration indicates that double variables x and y are automatic local variables and they exist only in the body of the function in which the declaration appears:

Local variables have automatic storage duration by default, so keyword auto is rarely used. For the remainder of the text, we’ll refer to variables with automatic storage duration simply as automatic variables.

Register Variables Data in the machine-language version of a program is normally loaded into registers for calculations and other processing.

The compiler may ignore register declarations. For example, there may not be a suf- ficient number of registers available for the compiler to use. The following declaration sug-

auto double x, y;

Performance Tip 5.1 Automatic storage is a means of conserving memory, because automatic variables exist only when they’re needed. They’re created when a function is entered and destroyed when the function is exited.

Software Engineering Observation 5.10 Automatic storage is an example of the principle of least privilege—allowing access to data only when it’s absolutely needed. Why have variables stored in memory and accessible when in fact they’re not needed?

Performance Tip 5.2 The storage-class specifier register can be placed before an automatic variable dec- laration to suggest that the compiler maintain the variable in one of the computer’s high- speed hardware registers. If intensely used variables such as counters or totals can be main- tained in hardware registers, the overhead of repeatedly loading the variables from mem- ory into the registers and storing the results back into memory can be eliminated.

5.12 Storage Classes 163

gests that the integer variable counter be placed in one of the computer’s registers and initialized to 1:

Keyword register can be used only with variables of automatic storage duration.

Static Storage Class Keywords extern and static are used in the declarations of identifiers for variables and functions of static storage duration. Identifiers of static storage duration exist from the time at which the program begins execution. For static variables, storage is allocated and initialized once, when the program begins execution. For functions, the name of the func- tion exists when the program begins execution. However, even though the variables and the function names exist from the start of program execution, this does not mean that these identifiers can be accessed throughout the program. Storage duration and scope (where a name can be used) are separate issues, as we’ll see in Section 5.13.

There are two types of identifiers with static storage duration: external identifiers (such as global variables and function names) and local variables declared with the storage- class specifier static. Global variables and function names are of storage class extern by default. Global variables are created by placing variable declarations outside any function definition, and they retain their values throughout the execution of the program. Global variables and functions can be referenced by any function that follows their declarations or definitions in the file. This is one reason for using function prototypes—when we include stdio.h in a program that calls printf, the function prototype is placed at the start of our file to make the name printf known to the rest of the file.

Local variables declared with the keyword static are still known only in the function in which they’re defined, but unlike automatic variables, static local variables retain their value when the function is exited. The next time the function is called, the static local variable contains the value it had when the function last exited. The following statement declares local variable count to be static and to be initialized to 1.

register int counter = 1;

Performance Tip 5.3 Often, register declarations are unnecessary. Today’s optimizing compilers are capable of recognizing frequently used variables and can decide to place them in registers without the need for a register declaration.

Software Engineering Observation 5.11 Defining a variable as global rather than local allows unintended side effects to occur when a function that does not need access to the variable accidentally or maliciously modifies it. In general, use of global variables should be avoided except in certain situations with unique performance requirements (as discussed in Chapter 14).

Software Engineering Observation 5.12 Variables used only in a particular function should be defined as local variables in that function rather than as external variables.

static int count = 1;

164 Chapter 5 C Functions

All numeric variables of static storage duration are initialized to zero if you do not ex- plicitly initialize them.

Keywords extern and static have special meaning when explicitly applied to external identifiers. In Chapter 14 we discuss the explicit use of extern and static with external identifiers and multiple-source-file programs.

5.13 Scope Rules The scope of an identifier is the portion of the program in which the identifier can be ref- erenced. For example, when we define a local variable in a block, it can be referenced only following its definition in that block or in blocks nested within that block. The four iden- tifier scopes are function scope, file scope, block scope, and function-prototype scope.

Labels (an identifier followed by a colon such as start:) are the only identifiers with function scope. Labels can be used anywhere in the function in which they appear, but cannot be referenced outside the function body. Labels are used in switch statements (as case labels) and in goto statements (see Chapter 14). Labels are implementation details that functions hide from one another. This hiding—more formally called information hiding—is a means of implementing the principle of least privilege, one of the most fun- damental principles of good software engineering.

An identifier declared outside any function has file scope. Such an identifier is “known” (i.e., accessible) in all functions from the point at which the identifier is declared until the end of the file. Global variables, function definitions, and function prototypes placed outside a function all have file scope.

Identifiers defined inside a block have block scope. Block scope ends at the termi- nating right brace (}) of the block. Local variables defined at the beginning of a function have block scope as do function parameters, which are considered local variables by the function. Any block may contain variable definitions. When blocks are nested, and an identifier in an outer block has the same name as an identifier in an inner block, the iden- tifier in the outer block is “hidden” until the inner block terminates. This means that while executing in the inner block, the inner block sees the value of its own local identifier and not the value of the identically named identifier in the enclosing block. Local variables declared static still have block scope, even though they exist from the time the program begins execution. Thus, storage duration does not affect the scope of an identifier.

The only identifiers with function-prototype scope are those used in the parameter list of a function prototype. As mentioned previously, function prototypes do not require names in the parameter list—only types are required. If a name is used in the parameter list of a function prototype, the compiler ignores the name. Identifiers used in a function prototype can be reused elsewhere in the program without ambiguity.

Common Programming Error 5.12 Accidentally using the same name for an identifier in an inner block as is used for an iden- tifier in an outer block, when in fact you want the identifier in the outer block to be active for the duration of the inner block.

Error-Prevention Tip 5.3 Avoid variable names that hide names in outer scopes. This can be accomplished simply by avoiding the use of duplicate identifiers in a program.

5.13 Scope Rules 165

Figure 5.12 demonstrates scoping issues with global variables, automatic local vari- ables, and static local variables. A global variable x is defined and initialized to 1 (line 9). This global variable is hidden in any block (or function) in which a variable named x is defined. In main, a local variable x is defined and initialized to 5 (line 14). This variable is then printed to show that the global x is hidden in main. Next, a new block is defined in main with another local variable x initialized to 7 (line 19). This variable is printed to show that it hides x in the outer block of main. The variable x with value 7 is automatically destroyed when the block is exited, and the local variable x in the outer block of main is printed again to show that it’s no longer hidden. The program defines three functions that each take no arguments and return nothing. Function useLocal defines an automatic vari- able x and initializes it to 25 (line 40). When useLocal is called, the variable is printed, incremented, and printed again before exiting the function. Each time this function is called, automatic variable x is reinitialized to 25. Function useStaticLocal defines a static variable x and initializes it to 50 (line 53). Local variables declared as static retain their values even when they’re out of scope. When useStaticLocal is called, x is printed, incremented, and printed again before exiting the function. In the next call to this func- tion, static local variable x will contain the value 51. Function useGlobal does not define any variables. Therefore, when it refers to variable x, the global x (line 9) is used. When useGlobal is called, the global variable is printed, multiplied by 10, and printed again before exiting the function. The next time function useGlobal is called, the global variable still has its modified value, 10. Finally, the program prints the local variable x in main again (line 33) to show that none of the function calls modified the value of x because the func- tions all referred to variables in other scopes.

1 /* Fig. 5.12: fig05_12.c 2 A scoping example */ 3 #include <stdio.h> 4 5 void useLocal( void ); /* function prototype */ 6 void useStaticLocal( void ); /* function prototype */ 7 void useGlobal( void ); /* function prototype */ 8 9

10 11 /* function main begins program execution */ 12 int main( void ) 13 { 14 int x = 5; /* local variable to main */ 15 16 printf("local x in outer scope of main is %d\n", x ); 17 18 19 20 21 22 23 24 printf( "local x in outer scope of main is %d\n", x );

Fig. 5.12 | Scoping example. (Part 1 of 3.)

int x = 1; /* global variable */

{ /* start new scope */ int x = 7; /* local variable to new scope */ printf( "local x in inner scope of main is %d\n", x ); } /* end new scope */

166 Chapter 5 C Functions

25 26 useLocal(); /* useLocal has automatic local x */ 27 useStaticLocal(); /* useStaticLocal has static local x */ 28 useGlobal(); /* useGlobal uses global x */ 29 useLocal(); /* useLocal reinitializes automatic local x */ 30 useStaticLocal(); /* static local x retains its prior value */ 31 useGlobal(); /* global x also retains its value */ 32 33 printf( "\nlocal x in main is %d\n", x ); 34 return 0; /* indicates successful termination */ 35 } /* end main */ 36 37 /* useLocal reinitializes local variable x during each call */ 38 void useLocal( void ) 39 { 40 41 42 printf( "\nlocal x in useLocal is %d after entering useLocal\n", x ); 43 x++; 44 printf( "local x in useLocal is %d before exiting useLocal\n", x ); 45 } /* end function useLocal */ 46 47 /* useStaticLocal initializes static local variable x only the first time 48 the function is called; value of x is saved between calls to this 49 function */ 50 void useStaticLocal( void ) 51 { 52 53 54 55 printf( "\nlocal static x is %d on entering useStaticLocal\n", x ); 56 x++; 57 printf( "local static x is %d on exiting useStaticLocal\n", x ); 58 } /* end function useStaticLocal */ 59 60 /* function useGlobal modifies global variable x during each call */ 61 void useGlobal( void ) 62 { 63 printf( "\nglobal x is %d on entering useGlobal\n", x ); 64 x *= 10; 65 printf( "global x is %d on exiting useGlobal\n", x ); 66 } /* end function useGlobal */

local x in outer scope of main is 5 local x in inner scope of main is 7 local x in outer scope of main is 5

local x in useLocal is 25 after entering useLocal local x in useLocal is 26 before exiting useLocal

local static x is 50 on entering useStaticLocal local static x is 51 on exiting useStaticLocal

Fig. 5.12 | Scoping example. (Part 2 of 3.)

int x = 25; /* initialized each time useLocal is called */

/* initialized only first time useStaticLocal is called */ static int x = 50;

5.14 Recursion 167

5.14 Recursion The programs we’ve discussed are generally structured as functions that call one another in a disciplined, hierarchical manner. For some types of problems, it’s useful to have func- tions call themselves. A recursive function is a function that calls itself either directly or indirectly through another function. Recursion is a complex topic discussed at length in upper-level computer science courses. In this section and the next, simple examples of re- cursion are presented. This book contains an extensive treatment of recursion, which is spread throughout Chapters 5–8, 12 and Appendix F. Figure 5.17, in Section 5.16, sum- marizes the 31 recursion examples and exercises in the book.

We consider recursion conceptually first, and then examine several programs con- taining recursive functions. Recursive problem-solving approaches have a number of ele- ments in common. A recursive function is called to solve a problem. The function actually knows how to solve only the simplest case(s), or so-called base case(s). If the function is called with a base case, the function simply returns a result. If the function is called with a more complex problem, the function divides the problem into two conceptual pieces: a piece that the function knows how to do and a piece that it does not know how to do. To make recursion feasible, the latter piece must resemble the original problem, but be a slightly simpler or slightly smaller version. Because this new problem looks like the original problem, the function launches (calls) a fresh copy of itself to go to work on the smaller problem—this is referred to as a recursive call and is also called the recursion step. The recursion step also includes the keyword return, because its result will be combined with the portion of the problem the function knew how to solve to form a result that will be passed back to the original caller, possibly main.

The recursion step executes while the original call to the function is still open, i.e., it has not yet finished executing. The recursion step can result in many more such recursive calls, as the function keeps dividing each problem it’s called with into two conceptual pieces. In order for the recursion to terminate, each time the function calls itself with a slightly simpler version of the original problem, this sequence of smaller problems must eventually converge on the base case. At that point, the function recognizes the base case, returns a result to the previous copy of the function, and a sequence of returns ensues all the way up the line until the original call of the function eventually returns the final result

global x is 1 on entering useGlobal global x is 10 on exiting useGlobal

local x in useLocal is 25 after entering useLocal local x in useLocal is 26 before exiting useLocal

local static x is 51 on entering useStaticLocal local static x is 52 on exiting useStaticLocal

global x is 10 on entering useGlobal global x is 100 on exiting useGlobal

local x in main is 5

Fig. 5.12 | Scoping example. (Part 3 of 3.)

168 Chapter 5 C Functions

to main. All of this sounds quite exotic compared to the kind of problem solving we’ve been using with conventional function calls to this point. Indeed, it takes a great deal of practice writing recursive programs before the process will appear natural. As an example of these concepts at work, let’s write a recursive program to perform a popular mathemat- ical calculation.

Recursively Calculating Factorials The factorial of a nonnegative integer n, written n! (and pronounced “n factorial”), is the product

with 1! equal to 1, and 0! defined to be 1. For example, 5! is the product 5 * 4 * 3 * 2 * 1, which is equal to 120.

The factorial of an integer, number, greater than or equal to 0 can be calculated iteratively (nonrecursively) using a for statement as follows:

A recursive definition of the factorial function is arrived at by observing the following relationship:

For example, 5! is clearly equal to 5 * 4! as is shown by the following:

The evaluation of 5! would proceed as shown in Fig. 5.13. Figure 5.13(a) shows how the succession of recursive calls proceeds until 1! is evaluated to be 1, which terminates the recursion. Figure 5.13(b) shows the values returned from each recursive call to its caller until the final value is calculated and returned.

Figure 5.14 uses recursion to calculate and print the factorials of the integers 0–10 (the choice of the type long will be explained momentarily). The recursive factorial function first tests whether a terminating condition is true, i.e., whether number is less than or equal to 1. If number is indeed less than or equal to 1, factorial returns 1, no further recursion is necessary, and the program terminates. If number is greater than 1, the state- ment

expresses the problem as the product of number and a recursive call to factorial evaluat- ing the factorial of number - 1. The call factorial( number - 1 ) is a slightly simpler prob- lem than the original calculation factorial( number ).

Function factorial (line 22) has been declared to receive a parameter of type long and return a result of type long. This is shorthand notation for long int. The C standard specifies that a variable of type long int is stored in at least 4 bytes, and thus may hold a value as large as +2147483647. As can be seen in Fig. 5.14, factorial values become large quickly. We’ve chosen the data type long so the program can calculate factorials greater

n · (n –1) · (n – 2) · … · 1

factorial = 1;

for ( counter = number; counter >= 1; counter-- ) factorial *= counter;

n! = n · (n – 1)!

5! = 5 · 4 · 3 · 2 · 1 5! = 5 · (4 · 3 · 2 · 1) 5! = 5 · (4!)

return number * factorial( number - 1 );

5.14 Recursion 169

Fig. 5.13 | Recursive evaluation of 5!.

1 /* Fig. 5.14: fig05_14.c 2 Recursive factorial function */ 3 #include <stdio.h> 4 5 6 7 /* function main begins program execution */ 8 int main( void ) 9 {

10 int i; /* counter */ 11 12 /* loop 11 times; during each iteration, calculate 13 factorial( i ) and display result */ 14 for ( i = 0; i <= 10; i++ ) { 15 printf( "%2d! = %ld\n", i, ); 16 } /* end for */ 17 18 return 0; /* indicates successful termination */ 19 } /* end main */ 20 21 22 23 24 25 26 27

Fig. 5.14 | Calculating factorials with a recursive function. (Part 1 of 2.)

(a) Sequence of recursive calls.

5 * 4!

4 * 3!

3 * 2!

2 * 1!

5!

1

(b) Values returned from each recursive call.

Final value = 120

5! = 5 * 24 = 120 is returned

4! = 4 * 6 = 24 is returned

3! = 3 * 2 = 6 is returned

2! = 2 * 1 = 2 is returned

1 returned

5 * 4!

4 * 3!

3 * 2!

2 * 1!

5!

1

long factorial( long number ); /* function prototype */

factorial( i )

/* recursive definition of function factorial */ long factorial( long number ) { /* base case */ if ( number <= 1 ) { return 1; } /* end if */

170 Chapter 5 C Functions

than 7! on computers with small (such as 2-byte) integers. The conversion specifier %ld is used to print long values. Unfortunately, the factorial function produces large values so quickly that even long int does not help us print many factorial values before the size of a long int variable is exceeded.

As we’ll explore in the exercises, double may ultimately be needed by the user desiring to calculate factorials of larger numbers. This points to a weakness in C (and most other procedural programming languages), namely that the language is not easily extended to handle the unique requirements of various applications. As we’ll see later in the book, C++ is an extensible language that, through “classes,” allows us to create arbitrarily large inte- gers if we wish.

5.15 Example Using Recursion: Fibonacci Series The Fibonacci series

begins with 0 and 1 and has the property that each subsequent Fibonacci number is the sum of the previous two Fibonacci numbers.

The series occurs in nature and, in particular, describes a form of spiral. The ratio of successive Fibonacci numbers converges to a constant value of 1.618…. This number, too,

28 29 30 31

0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 10! = 3628800

Common Programming Error 5.13 Forgetting to return a value from a recursive function when one is needed.

Common Programming Error 5.14 Either omitting the base case, or writing the recursion step incorrectly so that it does not converge on the base case, will cause infinite recursion, eventually exhausting memory. This is analogous to the problem of an infinite loop in an iterative (nonrecursive) solution. Infinite recursion can also be caused by providing an unexpected input.

0, 1, 1, 2, 3, 5, 8, 13, 21, …

Fig. 5.14 | Calculating factorials with a recursive function. (Part 2 of 2.)

else { /* recursive step */ return ( number * factorial( number - 1 ) ); } /* end else */ } /* end function factorial */

5.15 Example Using Recursion: Fibonacci Series 171

repeatedly occurs in nature and has been called the golden ratio or the golden mean. Humans tend to find the golden mean aesthetically pleasing. Architects often design win- dows, rooms, and buildings whose length and width are in the ratio of the golden mean. Postcards are often designed with a golden mean length/width ratio.

The Fibonacci series may be defined recursively as follows:

Figure 5.15 calculates the nth Fibonacci number recursively using function fibonacci. Notice that Fibonacci numbers tend to become large quickly. Therefore, we’ve chosen the data type long for the parameter type and the return type in function fibonacci. In Fig. 5.15, each pair of output lines shows a separate run of the program.

fibonacci(0) = 0 fibonacci(1) = 1 fibonacci(n) = fibonacci(n – 1) + fibonacci(n – 2)

1 /* Fig. 5.15: fig05_15.c 2 Recursive fibonacci function */ 3 #include <stdio.h> 4 5 6 7 /* function main begins program execution */ 8 int main( void ) 9 {

10 long result; /* fibonacci value */ 11 long number; /* number input by user */ 12 13 /* obtain integer from user */ 14 printf( "Enter an integer: " ); 15 scanf( "%ld", &number ); 16 17 /* calculate fibonacci value for number input by user */ 18 19 20 /* display result */ 21 printf( "Fibonacci( %ld ) = %ld\n", number, result ); 22 return 0; /* indicates successful termination */ 23 } /* end main */ 24 25 26 27 28 29 30 31 32 33 34 35

Fig. 5.15 | Recursively generating Fibonacci numbers. (Part 1 of 2.)

long fibonacci( long n ); /* function prototype */

result = fibonacci( number );

/* Recursive definition of function fibonacci */ long fibonacci( long n ) { /* base case */ if ( n == 0 || n == 1 ) { return n; } /* end if */ else { /* recursive step */ return fibonacci( n - 1 ) + fibonacci( n - 2 ); } /* end else */ } /* end function fibonacci */

172 Chapter 5 C Functions

The call to fibonacci from main is not a recursive call (line 18), but all subsequent calls to fibonacci are recursive (line 33). Each time fibonacci is invoked, it immediately tests for the base case—n is equal to 0 or 1. If this is true, n is returned. Interestingly, if n is greater than 1, the recursion step generates two recursive calls, each of which is for a slightly simpler problem than the original call to fibonacci. Figure 5.16 shows how func- tion fibonacci would evaluate fibonacci(3).

Enter an integer: 0 Fibonacci( 0 ) = 0

Enter an integer: 1 Fibonacci( 1 ) = 1

Enter an integer: 2 Fibonacci( 2 ) = 1

Enter an integer: 3 Fibonacci( 3 ) = 2

Enter an integer: 4 Fibonacci( 4 ) = 3

Enter an integer: 5 Fibonacci( 5 ) = 5

Enter an integer: 6 Fibonacci( 6 ) = 8

Enter an integer: 10 Fibonacci( 10 ) = 55

Enter an integer: 20 Fibonacci( 20 ) = 6765

Enter an integer: 30 Fibonacci( 30 ) = 832040

Enter an integer: 35 Fibonacci( 35 ) = 9227465

Fig. 5.15 | Recursively generating Fibonacci numbers. (Part 2 of 2.)

5.15 Example Using Recursion: Fibonacci Series 173

Order of Evaluation of Operands This figure raises some interesting issues about the order in which C compilers will evalu- ate the operands of operators. This is a different issue from the order in which operators are applied to their operands, namely the order dictated by the rules of operator prece- dence. From Fig. 5.16 it appears that while evaluating fibonacci(3), two recursive calls will be made, namely fibonacci(2) and fibonacci(1). But in what order will these calls be made? Most programmers simply assume the operands will be evaluated left to right. Strangely, Standard C does not specify the order in which the operands of most operators (including +) are to be evaluated. Therefore, you may make no assumption about the order in which these calls will execute. The calls could in fact execute fibonacci(2) first and then fibonacci(1), or the calls could execute in the reverse order, fibonacci(1) then fi- bonacci(2). In this program and in most other programs, it turns out the final result would be the same. But in some programs the evaluation of an operand may have side ef- fects that could affect the final result of the expression. Of C’s many operators, Standard C specifies the order of evaluation of the operands of only four operators—namely &&, ||, the comma (,) operator and ?:. The first three of these are binary operators whose two operands are guaranteed to be evaluated left to right. [Note: The commas used to separate the arguments in a function call are not comma operators.] The last operator is C’s only ternary operator. Its leftmost operand is always evaluated first; if the leftmost operand eval- uates to nonzero, the middle operand is evaluated next and the last operand is ignored; if the leftmost operand evaluates to zero, the third operand is evaluated next and the middle operand is ignored.

Common Programming Error 5.15 Writing programs that depend on the order of evaluation of the operands of operators oth- er than &&, ||, ?:, and the comma (,) operator can lead to errors because compilers may not necessarily evaluate the operands in the order you expect.

Fig. 5.16 | Set of recursive calls for fibonacci( 3 ).

+

fibonacci( 3 )

fibonacci( 2 ) fibonacci( 1 )return +

fibonacci( 1 ) fibonacci( 0 ) return 1

return 0return 1

return

174 Chapter 5 C Functions

Exponential Complexity A word of caution is in order about recursive programs like the one we use here to generate Fibonacci numbers. Each level of recursion in the fibonacci function has a doubling ef- fect on the number of calls; i.e., the number of recursive calls that will be executed to cal- culate the nth Fibonacci number is on the order of 2n. This rapidly gets out of hand. Calculating only the 20th Fibonacci number would require on the order of 220 or about a million calls, calculating the 30th Fibonacci number would require on the order of 230 or about a billion calls, and so on. Computer scientists refer to this as exponential complexity. Problems of this nature humble even the world’s most powerful computers! Complexity issues in general, and exponential complexity in particular, are discussed in detail in the upper-level computer science curriculum course generally called “Algorithms.”

The example we showed in this section used an intuitively appealing solution to cal- culate Fibonacci numbers, but there are better approaches. Exercise 5.48 asks you to inves- tigate recursion in more depth and propose alternate approaches to implementing the recursive Fibonacci algorithm.

5.16 Recursion vs. Iteration In the previous sections, we studied two functions that can easily be implemented either recursively or iteratively. In this section, we compare the two approaches and discuss why you might choose one approach over the other in a particular situation.

Both iteration and recursion are based on a control structure: Iteration uses a repeti- tion structure; recursion uses a selection structure. Both iteration and recursion involve repetition: Iteration explicitly uses a repetition structure; recursion achieves repetition through repeated function calls. Iteration and recursion each involve a termination test: Iteration terminates when the loop-continuation condition fails; recursion terminates when a base case is recognized. Iteration with counter-controlled repetition and recursion each gradually approach termination: Iteration keeps modifying a counter until the counter assumes a value that makes the loop-continuation condition fail; recursion keeps producing simpler versions of the original problem until the base case is reached. Both iteration and recursion can occur infinitely: An infinite loop occurs with iteration if the loop-continuation test never becomes false; infinite recursion occurs if the recursion step does not reduce the problem each time in a manner that converges on the base case.

Recursion has many negatives. It repeatedly invokes the mechanism, and conse- quently the overhead, of function calls. This can be expensive in both processor time and memory space. Each recursive call causes another copy of the function (actually only the function’s variables) to be created; this can consume considerable memory. Iteration nor-

Portability Tip 5.2 Programs that depend on the order of evaluation of the operands of operators other than &&, ||, ?:, and the comma (,) operator can function differently on systems with different compilers.

Performance Tip 5.4 Avoid Fibonacci-style recursive programs which result in an exponential “explosion” of calls.

5.16 Recursion vs. Iteration 175

mally occurs within a function, so the overhead of repeated function calls and extra memory assignment is omitted. So why choose recursion?

Most programming textbooks introduce recursion much later than we’ve done here. We feel that recursion is a sufficiently rich and complex topic that it’s better to introduce it earlier and spread the examples over the remainder of the text. Figure 5.17 summarizes by chapter the 31 recursion examples and exercises in the text.

Software Engineering Observation 5.13 Any problem that can be solved recursively can also be solved iteratively (nonrecursively). A recursive approach is normally chosen in preference to an iterative approach when the recursive approach more naturally mirrors the problem and results in a program that is easier to understand and debug. Another reason to choose a recursive solution is that an iterative solution may not be apparent.

Performance Tip 5.5 Avoid using recursion in performance situations. Recursive calls take time and consume additional memory.

Common Programming Error 5.16 Accidentally having a nonrecursive function call itself either directly, or indirectly through another function.

Chapter Recursion examples and exercises

Chapter 5 Factorial function Fibonacci function Greatest common divisor Sum of two integers Multiply two integers Raising an integer to an integer power Towers of Hanoi Recursive main Printing keyboard inputs in reverse Visualizing recursion

Chapter 6 Sum the elements of an array Print an array Print an array backward Print a string backward Check if a string is a palindrome Minimum value in an array Linear search Binary search

Chapter 7 Eight Queens Maze traversal

Chapter 8 Printing a string input at the keyboard backward

Fig. 5.17 | Recursion examples and exercises in the text. (Part 1 of 2.)

176 Chapter 5 C Functions

Let’s close this chapter with some observations that we make repeatedly throughout the book. Good software engineering is important. High performance is important. Unfor- tunately, these goals are often at odds with one another. Good software engineering is key to making more manageable the task of developing the larger and more complex software systems we need. High performance is key to realizing the systems of the future that will place ever greater computing demands on hardware. Where do functions fit in here?

Chapter 12 Linked list insert Linked list delete Search a linked list Print a linked list backward Binary tree insert Preorder traversal of a binary tree Inorder traversal of a binary tree Postorder traversal of a binary tree

Appendix F Selection sort Quicksort

Performance Tip 5.6 Functionalizing programs in a neat, hierarchical manner promotes good software engi- neering. But it has a price. A heavily functionalized program—as compared to a mono- lithic (i.e., one-piece) program without functions—makes potentially large numbers of function calls, and these consume execution time on a computer’s processor(s). So, although monolithic programs may perform better, they’re more difficult to program, test, debug, maintain, and evolve.

Chapter Recursion examples and exercises

Fig. 5.17 | Recursion examples and exercises in the text. (Part 2 of 2.)

Summary Section 5.1 Introduction • The best way to develop and maintain a large program is to divide it into several smaller program

modules, each of which is more manageable than the original program. Modules are written as functions in C.

Section 5.2 Program Modules in C • A function is invoked by a function call. The function call mentions the function by name and

provides information (as arguments) that the called function needs to perform its task.

• The purpose of information hiding is for functions to have access only to the information they need to complete their tasks. This is a means of implementing the principle of least privilege, one of the most important principles of good software engineering.

Section 5.3 Math Library Functions • Functions are normally invoked in a program by writing the name of the function followed by a

left parenthesis followed by the argument (or a comma-separated list of arguments) of the func- tion followed by a right parenthesis.

Summary 177

• Data type double is a floating-point type like float. A variable of type double can store a value of much greater magnitude and precision than float can store.

• Each argument of a function may be a constant, a variable, or an expression.

Section 5.4 Functions • A local variable is known only in a function definition. Other functions are not allowed to know

the names of a function’s local variables, nor is any function allowed to know the implementation details of any other function.

Section 5.5 Function Definitions • The general format for a function definition is

return-value-type function-name( parameter-list ) {

definitions statements

}

The return-value-type states the type of the value returned to the calling function. If a function does not return a value, the return-value-type is declared as void. The function-name is any valid identifier. The parameter-list is a comma-separated list containing the definitions of the variables that will be passed to the function. If a function does not receive any values, parameter-list is de- clared as void. The function-body is the set of definitions and statements that constitute the func- tion.

• The arguments passed to a function should match in number, type and order with the parameters in the function definition.

• When a program encounters a function call, control is transferred from the point of invocation to the called function, the statements of the called function are executed and control returns to the caller.

• A called function can return control to the caller in one of three ways. If the function does not return a value, control is returned when the function-ending right brace is reached, or by execut- ing the statement

return;

If the function does return a value, the statement

return expression;

returns the value of expression.

Section 5.6 Function Prototypes • A function prototype declares the return type of the function and declares the number, the types,

and order of the parameters the function expects to receive.

• Function prototypes enable the compiler to verify that functions are called correctly.

• The compiler ignores variable names mentioned in the function prototype.

Section 5.7 Function Call Stack and Activation Records • Stacks are known as last-in, first-out (LIFO) data structures—the last item pushed (inserted) on

the stack is the first item popped (removed) from the stack.

• A called function must know how to return to its caller, so the return address of the calling func- tion is pushed onto the program execution stack when the function is called. If a series of func- tion calls occurs, the successive return addresses are pushed onto the stack in last-in, first-out order so that the last function to execute will be the first to return to its caller.

178 Chapter 5 C Functions

• The program execution stack contains the memory for the local variables used in each invocation of a function during a program’s execution. This data is known as the activation record or stack frame of the function call. When a function call is made, the activation record for that function call is pushed onto the program execution stack. When the function returns to its caller, the ac- tivation record for this function call is popped off the stack and those local variables are no longer known to the program.

• The amount of memory in a computer is finite, so only a certain amount of memory can be used to store activation records on the program execution stack. If there are more function calls than can have their activation records stored on the program execution stack, an error known as a stack overflow occurs. The application will compile correctly, but its execution causes a stack overflow.

Section 5.8 Headers • Each standard library has a corresponding header containing the function prototypes for all the

functions in that library, as well as definitions of various symbolic constants needed by those functions.

• You can create and include your own headers.

Section 5.9 Calling Functions By Value and By Reference • When an argument is passed by value, a copy of the variable’s value is made and the copy is

passed to the called function. Changes to the copy in the called function do not affect the original variable’s value.

• All calls in C are call-by-value.

• It’s possible to simulate call-by-reference by using address operators and indirection operators.

Section 5.10 Random Number Generation • Function rand generates an integer between 0 and RAND_MAX which is defined by the C standard

to be at least 32767.

• The function prototypes for rand and srand are contained in <stdlib.h>.

• Values produced by rand can be scaled and shifted to produce values in a specific range.

• To randomize a program, use the C Standard Library function srand.

• The srand function call is ordinarily inserted in a program only after it has been thoroughly de- bugged. While debugging, it’s better to omit srand. This ensures repeatability, which is essential to proving that corrections to a random number generation program work properly.

• To randomize without the need for entering a seed each time, we use srand(time(NULL)).

• The general equation for scaling and shifting a random number is

n = a + rand() % b;

where a is the shifting value (i.e., the first number in the desired range of consecutive integers) and b is the scaling factor (i.e,. the width of the desired range of consecutive integers).

Section 5.11 Example: A Game of Chance • An enumeration, introduced by the keyword enum, is a set of integer constants represented by

identifiers. Values in an enum start with 0 and are incremented by 1. It’s also possible to assign an integer value to each identifier in an enum. The identifiers in an enumeration must be unique, but the values may be duplicated.

Section 5.12 Storage Classes • Each identifier in a program has the attributes storage class, storage duration, scope and linkage.

Summary 179

• C provides four storage classes indicated by the storage class specifiers: auto, register, extern and static; only one storage class specifier can be used for a given declaration.

• An identifier’s storage duration is when that identifier exists in memory.

Section 5.13 Scope Rules • An identifier’s scope is where the identifier can be referenced in a program.

• An identifier’s linkage determines for a multiple-source-file program whether an identifier is known only in the current source file or in any source file with proper declarations.

• Variables with automatic storage duration are created when the block in which they’re defined is entered, exist while the block is active and are destroyed when the block is exited. A function’s local variables normally have automatic storage duration.

• The storage class specifier register can be placed before an automatic variable declaration to suggest that the compiler maintain the variable in one of the computer’s high-speed hardware registers. The compiler may ignore register declarations. Keyword register can be used only with variables of automatic storage duration.

• Keywords extern and static are used to declare identifiers for variables and functions of static storage duration.

• Variables with static storage duration are allocated and initialized once, when the program begins execution.

• There are two types of identifiers with static storage duration: external identifiers (such as global variables and function names) and local variables declared with the storage-class specifier static.

• Global variables are created by placing variable definitions outside any function definition. Glob- al variables retain their values throughout the execution of the program.

• Local variables declared static retain their value between calls to the function in which they’re defined.

• All numeric variables of static storage duration are initialized to zero if you do not explicitly ini- tialize them.

• The four scopes for an identifier are function scope, file scope, block scope and function-proto- type scope.

• Labels are the only identifiers with function scope. Labels can be used anywhere in the function in which they appear but cannot be referenced outside the function body.

• An identifier declared outside any function has file scope. Such an identifier is “known” in all functions from the point at which the identifier is declared until the end of the file.

• Identifiers defined inside a block have block scope. Block scope ends at the terminating right brace (}) of the block.

• Local variables defined at the beginning of a function have block scope, as do function parame- ters, which are considered local variables by the function.

• Any block may contain variable definitions. When blocks are nested, and an identifier in an outer block has the same name as an identifier in an inner block, the identifier in the outer block is “hidden” until the inner block terminates.

• The only identifiers with function-prototype scope are those used in the parameter list of a func- tion prototype. Identifiers used in a function prototype can be reused elsewhere in the program without ambiguity.

Section 5.14 Recursion • A recursive function is a function that calls itself either directly or indirectly.

180 Chapter 5 C Functions

• If a recursive function is called with a base case, the function simply returns a result. If the func- tion is called with a more complex problem, the function divides the problem into two concep- tual pieces: a piece that the function knows how to do and a slightly smaller version of the original problem. Because this new problem looks like the original problem, the function launches a re- cursive call to work on the smaller problem.

• For recursion to terminate, each time the recursive function calls itself with a slightly simpler ver- sion of the original problem, the sequence of smaller and smaller problems must converge on the base case. When the function recognizes the base case, the result is returned to the previous func- tion call, and a sequence of returns ensues all the way up the line until the original call of the function eventually returns the final result.

• Standard C does not specify the order in which the operands of most operators (including +) are to be evaluated. Of C’s many operators, the standard specifies the order of evaluation of the op- erands of only the operators &&, ||, the comma (,) operator and ?:. The first three of these are binary operators whose two operands are evaluated left to right. The last operator is C’s only ter- nary operator. Its leftmost operand is evaluated first; if the leftmost operand evaluates to nonzero, the middle operand is evaluated next and the last operand is ignored; if the leftmost operand eval- uates to zero, the third operand is evaluated next and the middle operand is ignored.

Section 5.16 Recursion vs. Iteration • Both iteration and recursion are based on a control structure: Iteration uses a repetition structure;

recursion uses a selection structure.

• Both iteration and recursion involve repetition: Iteration explicitly uses a repetition structure; re- cursion achieves repetition through repeated function calls.

• Iteration and recursion each involve a termination test: Iteration terminates when the loop-con- tinuation condition fails; recursion terminates when a base case is recognized.

• Iteration and recursion can occur infinitely: An infinite loop occurs with iteration if the loop- continuation test never becomes false; infinite recursion occurs if the recursion step does not re- duce the problem in a manner that converges on the base case.

• Recursion repeatedly invokes the mechanism, and consequently the overhead, of function calls. This can be expensive in both processor time and memory space.

Terminology abstraction 144 activation record 151 argument (of a function) 142 auto 161 automatic storage duration 162 automatic variable 162 base case 167 block 146 block scope 164 C Standard Library 141 call a function 142 call-by-reference 152 call-by-value 152 called 145 called function 142 caller 142 calling function 142

coercion of arguments 149 divide and conquer 141 enum 161 enumeration 161 enumeration constant 161 file scope 164 function 141 function body 146 function call 142 function call stack 151 function prototype 145 function-prototype scope 164 function scope 164 header 146 information hiding 164 invoke a function 142 invoked 142

Self-Review Exercises 181

last-in-first-out (LIFO) 151 linkage 161 linkage of an identifier 162 local variable 144 mixed-type expression 150 module 141 parameter 144 parameter-list 146 pop off a stack 151 principle of least privilege 164 program execution stack 151 programmer-defined function 142 promotion rule 149 pseudorandom numbers 156 push onto a stack 151 randomizing 156 recursion step 167 recursive call 167 recursive function 167 return from a function 142 return value type 146

scaling 153 scaling factor 153 scope 162 scope of an identifier 164 seed the rand function 156 shift 153 shifting value 158 side effect 152 simulate 152 software reusability 144 stack 151 stack frame 151 stack overflow 151 standard library header 151 static 161 static storage duration 162 storage class 161 storage class of an identifier 161 storage class specifier 161 storage duration 161

Self-Review Exercises 5.1 Answer each of the following:

a) A program module in C is called a(n) . b) A function is invoked with a(n) . c) A variable that is known only within the function in which it’s defined is called a(n)

. d) The statement in a called function is used to pass the value of an expression

back to the calling function. e) Keyword is used in a function header to indicate that a function does not re-

turn a value or to indicate that a function contains no parameters. f) The of an identifier is the portion of the program in which the identifier can

be used. g) The three ways to return control from a called function to a caller are ,

and . h) A(n) allows the compiler to check the number, types, and order of the argu-

ments passed to a function. i) The function is used to produce random numbers. j) The function is used to set the random number seed to randomize a program. k) The storage-class specifiers are , , and . l) Variables declared in a block or in the parameter list of a function are assumed to be of

storage class unless specified otherwise. m) The storage-class specifier is a recommendation to the compiler to store a vari-

able in one of the computer’s registers. n) A non-static variable defined outside any block or function is a(n) variable. o) For a local variable in a function to retain its value between calls to the function, it must

be declared with the storage-class specifier. p) The four possible scopes of an identifier are , , and

.

182 Chapter 5 C Functions

q) A function that calls itself either directly or indirectly is a(n) function. r) A recursive function typically has two components: one that provides a means for the

recursion to terminate by testing for a(n) case, and one that expresses the problem as a recursive call for a slightly simpler problem than the original call.

5.2 For the following program, state the scope (either function scope, file scope, block scope or function prototype scope) of each of the following elements.

a) The variable x in main. b) The variable y in cube. c) The function cube. d) The function main. e) The function prototype for cube. f) The identifier y in the function prototype for cube.

5.3 Write a program that tests whether the examples of the math library function calls shown in Fig. 5.2 actually produce the indicated results.

5.4 Give the function header for each of the following functions. a) Function hypotenuse that takes two double-precision floating-point arguments, side1

and side2, and returns a double-precision floating-point result. b) Function smallest that takes three integers, x, y, z, and returns an integer. c) Function instructions that does not receive any arguments and does not return a val-

ue. [Note: Such functions are commonly used to display instructions to a user.] d) Function intToFloat that takes an integer argument, number, and returns a floating-

point result.

5.5 Give the function prototype for each of the following: a) The function described in Exercise 5.4(a). b) The function described in Exercise 5.4(b). c) The function described in Exercise 5.4(c). d) The function described in Exercise 5.4(d).

5.6 Write a declaration for each of the following: a) Integer count that should be maintained in a register. Initialize count to 0. b) Floating-point variable lastVal that is to retain its value between calls to the function

in which it’s defined. c) External integer number whose scope should be restricted to the remainder of the file in

which it’s defined.

1 #include <stdio.h> 2 int cube( int y ); 3 4 int main( void ) 5 { 6 int x; 7 8 for ( x = 1; x <= 10; x++ ) 9 printf( "%d\n", cube( x ) );

10 return 0; 11 } 12 13 int cube( int y ) 14 { 15 return y * y * y; 16 }

Answers to Self-Review Exercises 183

5.7 Find the error in each of the following program segments and explain how the error can be corrected (see also Exercise 5.46):

a) int g( void ) {

printf( "Inside function g\n" );

int h( void )

{

printf( "Inside function h\n" );

}

} b) int sum( int x, int y )

{

int result;

result = x + y;

} c) int sum( int n )

{

if ( n == 0 ) {

return 0;

}

else {

n + sum( n - 1 );

}

} d) void f( float a );

{

float a;

printf( "%f", a );

} e) void product( void )

{

int a, b, c, result;

printf( "Enter three integers: " )

scanf( "%d%d%d", &a, &b, &c );

result = a * b * c;

printf( "Result is %d", result );

return result;

}

Answers to Self-Review Exercises 5.1 a) Function. b) Function call. c) Local variable. d) return. e) void. f) Scope. g) return; or return expression; or encountering the closing right brace of a function. h) Function proto- type. i) rand. j) srand. k) auto, register, extern, static. l) auto. m) register. n) External, global. o) static. p) Function scope, file scope, block scope, function prototype scope. q) Recursive. r) Base.

5.2 a) Block scope. b) Block Scope. c) File scope. d) File scope. e) File scope. f) Function-pro- totype scope.

184 Chapter 5 C Functions

5.3 See below. [Note: On most Linux systems, you must use the -lm option when compiling this program.]

1 /* ex05_03.c */ 2 /* Testing the math library functions */ 3 #include <stdio.h> 4 #include <math.h> 5 6 /* function main begins program execution */ 7 int main( void ) 8 { 9 /* calculates and outputs the square root */

10 printf( "sqrt(%.1f) = %.1f\n", 900.0, sqrt( 900.0 ) ); 11 printf( "sqrt(%.1f) = %.1f\n", 9.0, sqrt( 9.0 ) ); 12 13 /* calculates and outputs the exponential function e to the x */ 14 printf( "exp(%.1f) = %f\n", 1.0, exp( 1.0 ) ); 15 printf( "exp(%.1f) = %f\n", 2.0, exp( 2.0 ) ); 16 17 /* calculates and outputs the logarithm (base e) */ 18 printf( "log(%f) = %.1f\n", 2.718282, log( 2.718282 ) ); 19 printf( "log(%f) = %.1f\n", 7.389056, log( 7.389056 ) ); 20 21 /* calculates and outputs the logarithm (base 10) */ 22 printf( "log10(%.1f) = %.1f\n", 1.0, log10( 1.0 ) ); 23 printf( "log10(%.1f) = %.1f\n", 10.0, log10( 10.0 ) ); 24 printf( "log10(%.1f) = %.1f\n", 100.0, log10( 100.0 ) ); 25 26 /* calculates and outputs the absolute value */ 27 printf( "fabs(%.1f) = %.1f\n", 13.5, fabs( 13.5 ) ); 28 printf( "fabs(%.1f) = %.1f\n", 0.0, fabs( 0.0 ) ); 29 printf( "fabs(%.1f) = %.1f\n", -13.5, fabs( -13.5 ) ); 30 31 /* calculates and outputs ceil( x ) */ 32 printf( "ceil(%.1f) = %.1f\n", 9.2, ceil( 9.2 ) ); 33 printf( "ceil(%.1f) = %.1f\n", -9.8, ceil( -9.8 ) ); 34 35 /* calculates and outputs floor( x ) */ 36 printf( "floor(%.1f) = %.1f\n", 9.2, floor( 9.2 ) ); 37 printf( "floor(%.1f) = %.1f\n", -9.8, floor( -9.8 ) ); 38 39 /* calculates and outputs pow( x, y ) */ 40 printf( "pow(%.1f, %.1f) = %.1f\n", 2.0, 7.0, pow( 2.0, 7.0 ) ); 41 printf( "pow(%.1f, %.1f) = %.1f\n", 9.0, 0.5, pow( 9.0, 0.5 ) ); 42 43 /* calculates and outputs fmod( x, y ) */ 44 printf( "fmod(%.3f/%.3f) = %.3f\n", 13.675, 2.333, 45 fmod( 13.675, 2.333 ) ); 46 47 /* calculates and outputs sin( x ) */ 48 printf( "sin(%.1f) = %.1f\n", 0.0, sin( 0.0 ) ); 49 50 /* calculates and outputs cos( x ) */ 51 printf( "cos(%.1f) = %.1f\n", 0.0, cos( 0.0 ) ); 52 53 /* calculates and outputs tan( x ) */ 54 printf( "tan(%.1f) = %.1f\n", 0.0, tan( 0.0 ) ); 55 return 0; /* indicates successful termination */ 56 } /* end main */

Answers to Self-Review Exercises 185

5.4 a) double hypotenuse( double side1, double side2 ) b) int smallest( int x, int y, int z ) c) void instructions( void ) d) float intToFloat( int number )

5.5 a) double hypotenuse( double side1, double side2 ); b) int smallest( int x, int y, int z ); c) void instructions( void ); d) float intToFloat( int number );

5.6 a) register int count = 0; b) static float lastVal; c) static int number;

[Note: This would appear outside any function definition.]

5.7 a) Error: Function h is defined in function g. Correction: Move the definition of h out of the definition of g.

b) Error: The body of the function is supposed to return an integer, but does not. Correction: Delete variable result and place the following statement in the function:

return x + y;

c) Error: The result of n + sum( n - 1 ) is not returned; sum returns an improper result. Correction: Rewrite the statement in the else clause as

return n + sum( n - 1 );

d) Error: Semicolon after the right parenthesis that encloses the parameter list, and re- defining the parameter a in the function definition. Correction: Delete the semicolon after the right parenthesis of the parameter list, and delete the declaration float a; in the function body.

e) Error: The function returns a value when it’s not supposed to. Correction: Eliminate the return statement.

sqrt(900.0) = 30.0 sqrt(9.0) = 3.0 exp(1.0) = 2.718282 exp(2.0) = 7.389056 log(2.718282) = 1.0 log(7.389056) = 2.0 log10(1.0) = 0.0 log10(10.0) = 1.0 log10(100.0) = 2.0 fabs(13.5) = 13.5 fabs(0.0) = 0.0 fabs(-13.5) = 13.5 ceil(9.2) = 10.0 ceil(-9.8) = -9.0 floor(9.2) = 9.0 floor(-9.8) = -10.0 pow(2.0, 7.0) = 128.0 pow(9.0, 0.5) = 3.0 fmod(13.675/2.333) = 2.010 sin(0.0) = 0.0 cos(0.0) = 1.0 tan(0.0) = 0.0

186 Chapter 5 C Functions

Exercises 5.8 Show the value of x after each of the following statements is performed:

a) x = fabs( 7.5 ); b) x = floor( 7.5 ); c) x = fabs( 0.0 ); d) x = ceil( 0.0 ); e) x = fabs( -6.4 ); f) x = ceil( -6.4 ); g) x = ceil( -fabs( -8 + floor( -5.5 ) ) );

5.9 (Parking Charges) A parking garage charges a $2.00 minimum fee to park for up to three hours and an additional $0.50 per hour for each hour or part thereof over three hours. The maximum charge for any given 24-hour period is $10.00. Assume that no car parks for longer than 24 hours at a time. Write a program that will calculate and print the parking charges for each of three cus- tomers who parked their cars in this garage yesterday. You should enter the hours parked for each customer. Your program should print the results in a neat tabular format, and should calculate and print the total of yesterday's receipts. The program should use the function calculateCharges to determine the charge for each customer. Your outputs should appear in the following format:

5.10 (Rounding Numbers) An application of function floor is rounding a value to the nearest integer. The statement

y = floor( x + .5 );

will round the number x to the nearest integer and assign the result to y. Write a program that reads several numbers and uses the preceding statement to round each of these numbers to the nearest integer. For each number processed, print both the original number and the rounded number.

5.11 (Rounding Numbers) Function floor may be used to round a number to a specific decimal place. The statement

y = floor( x * 10 + .5 ) / 10;

rounds x to the tenths position (the first position to the right of the decimal point). The statement

y = floor( x * 100 + .5 ) / 100;

rounds x to the hundredths position (the second position to the right of the decimal point). Write a program that defines four functions to round a number x in various ways

a) roundToInteger( number ) b) roundToTenths( number ) c) roundToHundreths( number ) d) roundToThousandths( number )

For each value read, your program should print the original value, the number rounded to the nearest integer, the number rounded to the nearest tenth, the number rounded to the nearest hun- dredth, and the number rounded to the nearest thousandth.

5.12 Answer each of the following questions. a) What does it mean to choose numbers “at random”? b) Why is the rand function useful for simulating games of chance? c) Why would you randomize a program by using srand? Under what circumstances is it

desirable not to randomize?

Car Hours Charge 1 1.5 2.00 2 4.0 2.50 3 24.0 10.00 TOTAL 29.5 14.50

Exercises 187

d) Why is it often necessary to scale and/or shift the values produced by rand? e) Why is computerized simulation of real-world situations a useful technique?

5.13 Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤ n ≤ 1112 e) –1 ≤ n ≤ 1 f) –3 ≤ n ≤ 11

5.14 For each of the following sets of integers, write a single statement that will print a number at random from the set.

a) 2, 4, 6, 8, 10. b) 3, 5, 7, 9, 11. c) 6, 10, 14, 18, 22.

5.15 (Hypotenuse Calculations) Define a function called hypotenuse that calculates the length of the hypotenuse of a right triangle when the other two sides are given. Use this function in a pro- gram to determine the length of the hypotenuse for each of the following triangles. The function should take two arguments of type double and return the hypotenuse as a double. Test your pro- gram with the side values specified in Fig. 5.18.

5.16 (Exponentiation) Write a function integerPower(base, exponent) that returns the value of baseexponent

For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is a positive, nonzero integer, and base is an integer. Function integerPower should use for to control the calculation. Do not use any math library functions.

5.17 (Multiples) Write a function multiple that determines for a pair of integers whether the sec- ond integer is a multiple of the first. The function should take two integer arguments and return 1 (true) if the second is a multiple of the first, and 0 (false) otherwise. Use this function in a program that inputs a series of pairs of integers.

5.18 (Even or Odd) Write a program that inputs a series of integers and passes them one at a time to function even, which uses the remainder operator to determine if an integer is even. The function should take an integer argument and return 1 if the integer is even and 0 otherwise.

5.19 (Parking Charges) Write a function that displays a solid square of asterisks whose side is specified in integer parameter side. For example, if side is 4, the function displays:

5.20 (Displaying a Square of Any Character) Modify the function created in Exercise 5.19 to form the square out of whatever character is contained in character parameter fillCharacter. Thus if side is 5 and fillCharacter is “#” then this function should print:

**** **** **** ****

##### ##### ##### ##### #####

188 Chapter 5 C Functions

5.21 (Project: Drawing Shapes with Characters) Use techniques similar to those developed in Exercises 5.19–5.20 to produce a program that graphs a wide range of shapes.

5.22 (Separating Digits) Write program segments that accomplish each of the following: a) Calculate the integer part of the quotient when integer a is divided by integer b. b) Calculate the integer remainder when integer a is divided by integer b. c) Use the program pieces developed in a) and b) to write a function that inputs an integer

between 1 and 32767 and prints it as a series of digits,with two spaces between each digit. For example, the integer 4562 should be printed as:

5.23 (Time in Seconds) Write a function that takes the time as three integer arguments (for hours, minutes, and seconds) and returns the number of seconds since the last time the clock “struck 12.” Use this function to calculate the amount of time in seconds between two times, both of which are within one 12-hour cycle of the clock.

5.24 (Temperature Conversions) Implement the following integer functions: a) Function celsius returns the Celsius equivalent of a Fahrenheit temperature. b) Function fahrenheit returns the Fahrenheit equivalent of a Celsius temperature. c) Use these functions to write a program that prints charts showing the Fahrenheit equiv-

alents of all Celsius temperatures from 0 to 100 degrees, and the Celsius equivalents of all Fahrenheit temperatures from 32 to 212 degrees. Print the outputs in a neat tabular format that minimizes the number of lines of output while remaining readable.

5.25 (Find the Minimum) Write a function that returns the smallest of three floating-point numbers.

5.26 (Perfect Numbers) An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6 = 1 + 2 + 3. Write a function perfect that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000. Print the factors of each perfect number to confirm that the number is indeed perfect. Challenge the power of your computer by testing numbers much larger than 1000.

5.27 (Prime Numbers) An integer is said to be prime if it’s divisible by only 1 and itself. For ex- ample, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not.

a) Write a function that determines if a number is prime. b) Use this function in a program that determines and prints all the prime numbers be-

tween 1 and 10,000. How many of these 10,000 numbers do you really have to test be- fore being sure that you have found all the primes?

c) Initially you might think that n/2 is the upper limit for which you must test to see if a number is prime, but you need go only as high as the square root of n. Why? Rewrite the program, and run it both ways. Estimate the performance improvement.

Triangle Side 1 Side 2

1 3.0 4.0

2 5.0 12.0

3 8.0 15.0

Fig. 5.18 | Sample triangle side values for Exercise 5.15.

4 5 6 2

Exercises 189

5.28 (Reversing Digits) Write a function that takes an integer value and returns the number with its digits reversed. For example, given the number 7631, the function should return 1367.

5.29 (Greatest Common Divisor) The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the two numbers. Write function gcd that returns the greatest common divisor of two integers.

5.30 Write a function qualityPoints that inputs a student’s average and returns 4 if a student's average is 90–100, 3 if the average is 80–89, 2 if the average is 70–79, 1 if the average is 60–69, and 0 if the average is lower than 60.

5.31 (Coin Tossing) Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the coin 100 times, and count the num- ber of times each side of the coin appears. Print the results. The program should call a separate func- tion flip that takes no arguments and returns 0 for tails and 1 for heads. [Note: If the program realistically simulates the coin tossing, then each side of the coin should appear approximately half the time for a total of approximately 50 heads and 50 tails.]

5.32 (Guess the Number) Write a C program that plays the game of “guess the number” as fol- lows: Your program chooses the number to be guessed by selecting an integer at random in the range 1 to 1000. The program then types:

The player then types a first guess. The program responds with one of the following:

If the player’s guess is incorrect, your program should loop until the player finally gets the number right. Your program should keep telling the player Too high or Too low to help the player “zero in” on the correct answer. [Note: The searching technique employed in this problem is called binary search. We’ll say more about this in the next problem.]

5.33 (Guess the Number Modification) Modify the program of Exercise 5.32 to count the num- ber of guesses the player makes. If the number is 10 or fewer, print Either you know the secret or you got lucky! If the player guesses the number in 10 tries, then print Ahah! You know the secret! If the player makes more than 10 guesses, then print You should be able to do better! Why should it take no more than 10 guesses? Well, with each “good guess” the player should be able to eliminate half of the numbers. Now show why any number 1 to 1000 can be guessed in 10 or fewer tries.

5.34 (Recursive Exponentiation) Write a recursive function power( base, exponent ) that when invoked returns

baseexponent

For example, power( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater than or equal to 1. Hint: The recursion step would use the relationship

baseexponent = base * baseexponent–1

and the terminating condition occurs when exponent is equal to 1 because

base1 = base

I have a number between 1 and 1000. Can you guess my number? Please type your first guess.

1. Excellent! You guessed the number! Would you like to play again (y or n)? 2. Too low. Try again. 3. Too high. Try again.

190 Chapter 5 C Functions

5.35 (Fibonacci) The Fibonacci series

0, 1, 1, 2, 3, 5, 8, 13, 21, …

begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms. a) Write a nonrecursive function fibonacci(n) that calculates the nth Fibonacci number. b) Determine the largest Fibonacci number that can be printed on your system. Modify the program of part a) to use double instead of int to calculate and return Fibonacci numbers. Let the program loop until it fails because of an excessively high value.

5.36 (Towers of Hanoi) Every budding computer scientist must grapple with certain classic prob- lems, and the Towers of Hanoi (see Fig. 5.19) is one of the most famous of these. Legend has it that in a temple in the Far East, priests are attempting to move a stack of disks from one peg to another. The initial stack had 64 disks threaded onto one peg and arranged from bottom to top by decreasing size. The priests are attempting to move the stack from this peg to a second peg under the constraints that exactly one disk is moved at a time, and at no time may a larger disk be placed above a smaller disk. A third peg is available for temporarily holding the disks. Supposedly the world will end when the priests complete their task, so there is little incentive for us to facilitate their efforts.

Let’s assume that the priests are attempting to move the disks from peg 1 to peg 3. We wish to develop an algorithm that will print the precise sequence of disk-to-disk peg transfers.

If we were to approach this problem with conventional methods, we’d rapidly find ourselves hopelessly knotted up in managing the disks. Instead, if we attack the problem with recursion in mind, it immediately becomes tractable. Moving n disks can be viewed in terms of moving only n – 1 disks (and hence the recursion) as follows:

a) Move n – 1 disks from peg 1 to peg 2, using peg 3 as a temporary holding area. b) Move the last disk (the largest) from peg 1 to peg 3. c) Move the n – 1 disks from peg 2 to peg 3, using peg 1 as a temporary holding area.

The process ends when the last task involves moving n = 1 disk, i.e., the base case. This is accomplished by trivially moving the disk without the need for a temporary holding area.

Write a program to solve the Towers of Hanoi problem. Use a recursive function with four parameters:

a) The number of disks to be moved b) The peg on which these disks are initially threaded c) The peg to which this stack of disks is to be moved d) The peg to be used as a temporary holding area

Fig. 5.19 | Towers of Hanoi for the case with four disks.

Exercises 191

Your program should print the precise instructions it will take to move the disks from the starting peg to the destination peg. For example, to move a stack of three disks from peg 1 to peg 3, your program should print the following series of moves:

1 → 3 (This means move one disk from peg 1 to peg 3.) 1 → 2 3 → 2 1 → 3 2 → 1 2 → 3 1 → 3

5.37 (Towers of Hanoi: Iterative Solution) Any program that can be implemented recursively can be implemented iteratively, although sometimes with considerably more difficulty and consid- erably less clarity. Try writing an iterative version of the Towers of Hanoi. If you succeed, compare your iterative version with the recursive version you developed in Exercise 5.36. Investigate issues of performance, clarity, and your ability to demonstrate the correctness of the programs.

5.38 (Visualizing Recursion) It’s interesting to watch recursion “in action.” Modify the factorial function of Fig. 5.14 to print its local variable and recursive call parameter. For each recursive call, display the outputs on a separate line and add a level of indentation. Do your utmost to make the outputs clear, interesting and meaningful. Your goal here is to design and implement an output for- mat that helps a person understand recursion better. You may want to add such display capabilities to the many other recursion examples and exercises throughout the text.

5.39 (Recursive Greatest Common Divisor) The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y. Write a recursive function gcd that returns the greatest common divisor of x and y. The gcd of x and y is defined recursively as follows: If y is equal to 0, then gcd(x, y) is x; otherwise gcd(x, y) is gcd(y, x % y) where % is the remainder operator. 5.40 (Recursive main) Can main be called recursively? Write a program containing a function main. Include static local variable count initialized to 1. Postincrement and print the value of count each time main is called. Run your program. What happens?

5.41 (Distance Between Points) Write function distance that calculates the distance between two points (x1, y1) and (x2, y2). All numbers and return values should be of type double.

5.42 What does the following program do?

5.43 What does the following program do?

1 #include <stdio.h> 2 3 /* function main begins program execution */ 4 int main( void ) 5 { 6 int c; /* variable to hold character input by user */ 7 8 if ( ( c = getchar() ) != EOF ) { 9 main();

10 printf( "%c", c ); 11 } /* end if */ 12 13 return 0; /* indicates successful termination */ 14 } /* end main */

1 #include <stdio.h> 2 3 int mystery( int a, int b ); /* function prototype */

192 Chapter 5 C Functions

5.44 After you determine what the program of Exercise 5.43 does, modify the program to func- tion properly after removing the restriction of the second argument being nonnegative.

5.45 (Testing Math Library Functions) Write a program that tests as many of the math library functions in Fig. 5.2 as you can. Exercise each of these functions by having your program print out tables of return values for a diversity of argument values.

5.46 Find the error in each of the following program segments and explain how to correct it: a) double cube( float ); /* function prototype */

cube( float number ) /* function definition */ {

return number * number * number; }

b) register auto int x = 7; c) int randomNumber = srand(); d) double y = 123.45678;

int x; x = y;

printf( "%f\n", (double) x ); e) double square( double number )

{

double number; return number * number; }

f) int sum( int n ) {

if ( n == 0 ) { return 0; }

4 5 /* function main begins program execution */ 6 int main( void ) 7 { 8 int x; /* first integer */ 9 int y; /* second integer */

10 11 printf( "Enter two integers: " ); 12 scanf( "%d%d", &x, &y ); 13 14 printf( "The result is %d\n", mystery( x, y ) ); 15 return 0; /* indicates successful termination */ 16 } /* end main */ 17 18 /* Parameter b must be a positive integer 19 to prevent infinite recursion */ 20 int mystery( int a, int b ) 21 { 22 /* base case */ 23 if ( b == 1 ) { 24 return a; 25 } /* end if */ 26 else { /* recursive step */ 27 return a + mystery( a, b - 1 ); 28 } /* end else */ 29 } /* end function mystery */

Making a Difference 193

else { return n + sum( n ); }

}

5.47 (Craps Game Modification) Modify the craps program of Fig. 5.10 to allow wagering. Package as a function the portion of the program that runs one game of craps. Initialize variable bankBalance to 1000 dollars. Prompt the player to enter a wager. Use a while loop to check that wager is less than or equal to bankBalance, and if not, prompt the user to reenter wager until a valid wager is entered. After a correct wager is entered, run one game of craps. If the player wins, increase bankBalance by wager and print the new bankBalance. If the player loses, decrease bankBalance by wager, print the new bankBalance, check if bankBalance has become zero, and if so print the mes- sage, "Sorry. You busted!" As the game progresses, print various messages to create some “chatter” such as, "Oh, you're going for broke, huh?" or "Aw cmon, take a chance!" or "You're up big. Now's the time to cash in your chips!"

5.48 (Research Project: Improving the Recursive Fibonacci Implementation) In Section 5.15, the recursive algorithm we used to calculate Fibonacci numbers was intuitively appealing. However, re- call that the algorithm resulted in the exponential explosion of recursive function calls. Research the recursive Fibonacci implementation online. Study the various approaches, including the iterative version in Exercise 5.35 and versions that use only so-called “tail recursion.” Discuss the relative merits of each.

Making a Difference 5.49 (Global Warming Facts Quiz) The controversial issue of global warming has been widely publicized by the film An Inconvenient Truth, featuring former Vice President Al Gore. Mr. Gore and a U.N. network of scientists, the Intergovernmental Panel on Climate Change, shared the 2007 Nobel Peace Prize in recognition of “their efforts to build up and disseminate greater knowledge about man-made climate change.” Research both sides of the global warming issue online (you might want to search for phrases like “global warming skeptics”). Create a five-question multiple- choice quiz on global warming, each question having four possible answers (numbered 1–4). Be ob- jective and try to fairly represent both sides of the issue. Next, write an application that administers the quiz, calculates the number of correct answers (zero through five) and returns a message to the user. If the user correctly answers five questions, print “Excellent”; if four, print “Very good”; if three or fewer, print “Time to brush up on your knowledge of global warming,” and include a list of some of the websites where you found your facts.

Computer Assisted Instruction As computer costs decline, it becomes feasible for every student, regardless of economic circum- stance, to have a computer and use it in school. This creates exciting possibilities for improving the educational experience of all students worldwide as suggested by the next five exercises. [Note: Check out initiatives such as the One Laptop Per Child Project (www.laptop.org). Also, research “green” laptops—what are some key “going green” characteristics of these devices? Look into the Electronic Product Environmental Assessment Tool (www.epeat.net) which can help you assess the “greenness” of desktops, notebooks and monitors to help you decide which products to purchase.]

5.50 (Computer-Assisted Instruction) The use of computers in education is referred to as com- puter-assisted instruction (CAI). Write a program that will help an elementary school student learn multiplication. Use the rand function to produce two positive one-digit integers. The program should then prompt the user with a question, such as

How much is 6 times 7?

194 Chapter 5 C Functions

The student then inputs the answer. Next, the program checks the student’s answer. If it’s correct, display the message "Very good!" and ask another multiplication question. If the answer is wrong, display the message "No. Please try again." and let the student try the same question repeatedly until the student finally gets it right. A separate function should be used to generate each new ques- tion. This function should be called once when the application begins execution and each time the user answers the question correctly.

5.51 (Computer-Assisted Instruction: Reducing Student Fatigue) One problem in CAI environ- ments is student fatigue. This can be reduced by varying the computer’s responses to hold the stu- dent’s attention. Modify the program of Exercise 5.50 so that various comments are displayed for each answer as follows:

Possible responses to a correct answer:

Very good! Excellent! Nice work! Keep up the good work!

Possible responses to an incorrect answer:

No. Please try again. Wrong. Try once more. Don't give up! No. Keep trying.

Use random-number generation to choose a number from 1 to 4 that will be used to select one of the four appropriate responses to each correct or incorrect answer. Use a switch statement to issue the responses.

5.52 (Computer-Assisted Instruction: Monitoring Student Performance) More sophisticated computer-assisted instruction systems monitor the student’s performance over a period of time. The decision to begin a new topic is often based on the student’s success with previous topics. Modify the program of Exercise 5.51 to count the number of correct and incorrect responses typed by the student. After the student types 10 answers, your program should calculate the percentage that are correct. If the percentage is lower than 75%, display "Please ask your teacher for extra help.", then reset the program so another student can try it. If the percentage is 75% or higher, display "Congratulations, you are ready to go to the next level!", then reset the program so another student can try it.

5.53 (Computer-Assisted Instruction: Difficulty Levels) Exercise 5.50 through Exercise 5.52 de- veloped a computer-assisted instruction program to help teach an elementary school student multi- plication. Modify the program to allow the user to enter a difficulty level. At a difficulty level of 1, the program should use only single-digit numbers in the problems; at a difficulty level of 2, numbers as large as two digits, and so on.

5.54 (Computer-Assisted Instruction: Varying the Types of Problems) Modify the program of Exercise 5.53 to allow the user to pick a type of arithmetic problem to study. An option of 1 means addition problems only, 2 means subtraction problems only, 3 means multiplication problems only and 4 means a random mixture of all these types.

6C Arrays Now go, write it before them in a table, and note it in a book. —Isaiah 30:8

To go beyond is as wrong as to fall short. —Confucius

Begin at the beginning, … and go on till you come to the end: then stop. —Lewis Carroll

O b j e c t i v e s In this chapter, you’ll learn:

■ To use the array data structure to represent lists and tables of values.

■ To define an array, initialize an array and refer to individual elements of an array.

■ To define symbolic constants.

■ To pass arrays to functions.

■ To use arrays to store, sort and search lists and tables of values.

■ To define and manipulate multiple-subscripted arrays.

196 Chapter 6 C Arrays

6.1 Introduction This chapter serves as an introduction to the important topic of data structures. Arrays are data structures consisting of related data items of the same type. In Chapter 10, we discuss C’s notion of struct (structure)—a data structure consisting of related data items of pos- sibly different types. Arrays and structures are “static” entities in that they remain the same size throughout program execution (they may, of course, be of automatic storage class and hence created and destroyed each time the blocks in which they are defined are entered and exited). In Chapter 12, we introduce dynamic data structures such as lists, queues, stacks and trees that may grow and shrink as programs execute.

6.2 Arrays An array is a group of memory locations related by the fact that they all have the same name and the same type. To refer to a particular location or element in the array, we spec- ify the name of the array and the position number of the particular element in the array.

Figure 6.1 shows an integer array called c. This array contains 12 elements. Any one of these elements may be referred to by giving the name of the array followed by the posi- tion number of the particular element in square brackets ([]). The first element in every array is the zeroth element. Thus, the first element of array c is referred to as c[0], the second element of array c is referred to as c[1], the seventh element of array c is referred to as c[6], and, in general, the ith element of array c is referred to as c[i - 1]. Array names, like other variable names, can contain only letters, digits and underscores. Array names cannot begin with a digit.

The position number contained within square brackets is more formally called a sub- script (or index). A subscript must be an integer or an integer expression. If a program uses an expression as a subscript, then the expression is evaluated to determine the subscript. For example, if a = 5 and b = 6, then the statement

adds 2 to array element c[11]. A subscripted array name is an lvalue—it can be used on the left side of an assignment.

Let’s examine array c (Fig. 6.1) more closely. The array’s name is c. Its 12 elements are referred to as c[0], c[1], c[2], …, c[11]. The value stored in c[0] is –45, the value of c[1] is 6, the value of c[2] is 0, the value of c[7] is 62 and the value of c[11] is 78. To print the sum of the values contained in the first three elements of array c, we’d write

6.1 Introduction 6.2 Arrays 6.3 Defining Arrays 6.4 Array Examples 6.5 Passing Arrays to Functions

6.6 Sorting Arrays 6.7 Case Study: Computing Mean,

Median and Mode Using Arrays 6.8 Searching Arrays 6.9 Multiple-Subscripted Arrays

Summary | Terminology | Self-Review Exercises | Answers to Self-Review Exercises | Exercises Recursion Exercises | Special Section: Sudoku

c[ a + b ] += 2;

printf( "%d", c[ 0 ] + c[ 1 ] + c[ 2 ] );

6.2 Arrays 197

To divide the value of the seventh element of array c by 2 and assign the result to the vari- able x, we’d write

The brackets used to enclose the subscript of an array are actually considered to be an operator in C. They have the same level of precedence as the function call operator (i.e., the parentheses that are placed following a function name to call that function). Figure 6.2 shows the precedence and associativity of the operators introduced to this point in the text.

Fig. 6.1 | 12-element array.

x = c[ 6 ] / 2;

Common Programming Error 6.1 It’s important to note the difference between the “seventh element of the array” and “array element seven.” Because array subscripts begin at 0, the “seventh element of the array” has a subscript of 6, while “array element seven” has a subscript of 7 and is actually the eighth element of the array. This is a source of “off-by-one” errors.

Operators Associativity Type

[] () left to right highest ++ -- ! (type) right to left unary * / % left to right multiplicative

Fig. 6.2 | Operator precedence and associativity. (Part 1 of 2.)

62

-3

1

6453

78

0

-89

1543

72

0

6

c[ 7 ]

c[ 8 ]

c[ 9 ]

c[ 10 ]

c[ 11 ]

c[ 6 ]

c[ 5 ]

c[ 4 ]

c[ 3 ]

c[ 2 ]

c[ 1 ]

-45c[ 0 ]

Position number of the element within array c

Name of array (note that all elements of this array have the same name, c)

198 Chapter 6 C Arrays

6.3 Defining Arrays Arrays occupy space in memory. You specify the type of each element and the number of elements required by each array so that the computer may reserve the appropriate amount of memory. To tell the computer to reserve 12 elements for integer array c, the definition

is used. The following definition

reserves 100 elements for integer array b and 27 elements for integer array x. Arrays may contain other data types. For example, an array of type char can be used

to store a character string. Character strings and their similarity to arrays are discussed in Chapter 8. The relationship between pointers and arrays is discussed in Chapter 7.

6.4 Array Examples This section presents several examples that demonstrate how to define arrays, how to ini- tialize arrays and how to perform many common array manipulations.

Defining an Array and Using a Loop to Initialize the Array’s Elements Figure 6.3 uses for statements to initialize the elements of a 10-element integer array n to zeros and print the array in tabular format. The first printf statement (line 16) displays the column heads for the two columns printed in the subsequent for statement.

+ - left to right additive < <= > >= left to right relational == != left to right equality && left to right logical AND || left to right logical OR ?: right to left conditional = += -= *= /= %= right to left assignment , left to right comma

int c[ 12 ];

int b[ 100 ], x[ 27 ];

1 /* Fig. 6.3: fig06_03.c 2 initializing an array */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main( void ) 7 {

Fig. 6.3 | Initializing the elements of an array to zeros. (Part 1 of 2.)

Operators Associativity Type

Fig. 6.2 | Operator precedence and associativity. (Part 2 of 2.)

6.4 Array Examples 199

Initializing an Array in a Definition with an Initializer List The elements of an array can also be initialized when the array is defined by following the definition with an equals sign and braces, {}, containing a comma-separated list of initial- izers. Figure 6.4 initializes an integer array with 10 values (line 9) and prints the array in tabular format.

8 9 int i; /* counter */

10 11 12 13 14 15 16 printf( "%s%13s\n", "Element", "Value" ); 17 18 19 20 21 22 23 return 0; /* indicates successful termination */ 24 } /* end main */

Element Value 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0

1 /* Fig. 6.4: fig06_04.c 2 Initializing an array with an initializer list */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main( void ) 7 { 8 9

10 int i; /* counter */ 11 12 printf( "%s%13s\n", "Element", "Value" ); 13

Fig. 6.4 | Initializing the elements of an array with an initializer list. (Part 1 of 2.)

Fig. 6.3 | Initializing the elements of an array to zeros. (Part 2 of 2.)

int n[ 10 ]; /* n is an array of 10 integers */

/* initialize elements of array n to 0 */ for ( i = 0; i < 10; i++ ) { n[ i ] = 0; /* set element at location i to 0 */ } /* end for */

/* output contents of array n in tabular format */ for ( i = 0; i < 10; i++ ) { printf( "%7d%13d\n", i, n[ i ] ); } /* end for */

/* use initializer list to initialize array n */ int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };

200 Chapter 6 C Arrays

If there are fewer initializers than elements in the array, the remaining elements are initialized to zero. For example, the elements of the array n in Fig. 6.3 could have been initialized to zero as follows:

int n[ 10 ] = { 0 };

This explicitly initializes the first element to zero and initializes the remaining nine ele- ments to zero because there are fewer initializers than there are elements in the array. It’s important to remember that arrays are not automatically initialized to zero. You must at least initialize the first element to zero for the remaining elements to be automatically ze- roed. This method of initializing the array elements to 0 is performed at compile time for static arrays and at runtime for automatic arrays.

The array definition

causes a syntax error because there are six initializers and only five array elements.

If the array size is omitted from a definition with an initializer list, the number of ele- ments in the array will be the number of elements in the initializer list. For example,

would create a five-element array.

14 /* output contents of array in tabular format */ 15 for ( i = 0; i < 10; i++ ) { 16 printf( "%7d%13d\n", i, n[ i ] ); 17 } /* end for */ 18 19 return 0; /* indicates successful termination */ 20 } /* end main */

Element Value 0 32 1 27 2 64 3 18 4 95 5 14 6 90 7 70 8 60 9 37

Fig. 6.4 | Initializing the elements of an array with an initializer list. (Part 2 of 2.)

Common Programming Error 6.2 Forgetting to initialize the elements of an array whose elements should be initialized.

int n[ 5 ] = { 32, 27, 64, 18, 95, 14 };

Common Programming Error 6.3 Providing more initializers in an array initializer list than there are elements in the array is a syntax error.

int n[] = { 1, 2, 3, 4, 5 };

6.4 Array Examples 201

Specifying an Array’s Size with a Symbolic Constant and Initializing Array Elements with Calculations Figure 6.5 initializes the elements of a 10-element array s to the values 2, 4, 6, …, 20 and prints the array in tabular format. The values are generated by multiplying the loop coun- ter by 2 and adding 2.

The #define preprocessor directive is introduced in this program. Line 4

defines a symbolic constant SIZE whose value is 10. A symbolic constant is an identifier that is replaced with replacement text by the C preprocessor before the program is com- piled. When the program is preprocessed, all occurrences of the symbolic constant SIZE

1 /* Fig. 6.5: fig06_05.c 2 Initialize the elements of array s to the even integers from 2 to 20 */ 3 #include <stdio.h> 4 /* maximum size of array */ 5 6 /* function main begins program execution */ 7 int main( void ) 8 { 9 /* symbolic constant SIZE can be used to specify array size */

10 11 int j; /* counter */ 12 13 14 15 16 17 printf( "%s%13s\n", "Element", "Value" ); 18 19 /* output contents of array s in tabular format */ 20 for ( j = 0; j < SIZE; j++ ) { 21 printf( "%7d%13d\n", j, s[ j ] ); 22 } /* end for */ 23 24 return 0; /* indicates successful termination */ 25 } /* end main */

Element Value 0 2 1 4 2 6 3 8 4 10 5 12 6 14 7 16 8 18 9 20

Fig. 6.5 | Initialize the elements of array s to the even integers from 2 to 20.

#define SIZE 10

#define SIZE 10

int s[ SIZE ]; /* array s has SIZE elements */

for ( j = 0; j < SIZE; j++ ) { /* set the values */ s[ j ] = 2 + 2 * j; } /* end for */

202 Chapter 6 C Arrays

are replaced with the replacement text 10. Using symbolic constants to specify array sizes makes programs more scalable. In Fig. 6.5, we could have the first for loop (line 13) fill a 1000-element array by simply changing the value of SIZE in the #define directive from 10 to 1000. If the symbolic constant SIZE had not been used, we’d have to change the pro- gram in three separate places to scale the program to handle 1000 array elements. As pro- grams get larger, this technique becomes more useful for writing clear programs.

If the #define preprocessor directive in line 4 is terminated with a semicolon, all occurrences of the symbolic constant SIZE in the program are replaced with the text 10; by the preprocessor. This may lead to syntax errors at compile time, or logic errors at execution time. Remember that the preprocessor is not C—it’s only a text manipulator.

Summing the Elements of an Array Figure 6.6 sums the values contained in the 12-element integer array a. The for state- ment’s body (line 16) does the totaling.

Common Programming Error 6.4 Ending a #define or #include preprocessor directive with a semicolon. Remember that preprocessor directives are not C statements.

Common Programming Error 6.5 Assigning a value to a symbolic constant in an executable statement is a syntax error. A symbolic constant is not a variable. No space is reserved for it by the compiler as with vari- ables that hold values at execution time.

Software Engineering Observation 6.1 Defining the size of each array as a symbolic constant makes programs more scalable.

Good Programming Practice 6.1 Use only uppercase letters for symbolic constant names. This makes these constants stand out in a program and reminds you that symbolic constants are not variables.

Good Programming Practice 6.2 In multiword symbolic constant names, use underscores to separate the words for readabil- ity.

1 /* Fig. 6.6: fig06_06.c 2 Compute the sum of the elements of the array */ 3 #include <stdio.h> 4 #define SIZE 12 5 6 /* function main begins program execution */ 7 int main( void ) 8 { 9 /* use initializer list to initialize array */

10 int a[ SIZE ] = { 1, 3, 5, 4, 7, 2, 99, 16, 45, 67, 89, 45 };

Fig. 6.6 | Computing the sum of the elements of an array. (Part 1 of 2.)

6.4 Array Examples 203

Using Arrays to Summarize Survey Results Our next example uses arrays to summarize the results of data collected in a survey. Con- sider the problem statement.

Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 means awful and 10 means excellent). Place the 40 responses in an integer array and summarize the results of the poll.

This is a typical array application (see Fig. 6.7). We wish to summarize the number of responses of each type (i.e., 1 through 10). The array responses (line 17) is a 40-ele- ment array of the students’ responses. We use an 11-element array frequency (line 14) to count the number of occurrences of each response. We ignore frequency[0] because it’s logical to have response 1 increment frequency[1] rather than frequency[0]. This allows us to use each response directly as the subscript in the frequency array.

11 int i; /* counter */ 12 int total = 0; /* sum of array */ 13 14 15 16 17 18 19 printf( "Total of array element values is %d\n", total ); 20 return 0; /* indicates successful termination */ 21 } /* end main */

Total of array element values is 383

Fig. 6.6 | Computing the sum of the elements of an array. (Part 2 of 2.)

/* sum contents of array a */ for ( i = 0; i < SIZE; i++ ) { total += a[ i ]; } /* end for */

1 /* Fig. 6.7: fig06_07.c 2 Student poll program */ 3 #include <stdio.h> 4 #define RESPONSE_SIZE 40 /* define array sizes */ 5 #define FREQUENCY_SIZE 11 6 7 /* function main begins program execution */ 8 int main( void ) 9 {

10 int answer; /* counter to loop through 40 responses */ 11 int rating; /* counter to loop through frequencies 1-10 */ 12 13 /* initialize frequency counters to 0 */ 14 15 16 /* place the survey responses in the responses array */ 17 int responses[ RESPONSE_SIZE ] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 18 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 19 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };

Fig. 6.7 | Student poll analysis program. (Part 1 of 2.)

int frequency[ FREQUENCY_SIZE ] = { 0 };

204 Chapter 6 C Arrays

The for loop (line 24) takes the responses one at a time from the array responses and increments one of the 10 counters (frequency[1] to frequency[10]) in the frequency array. The key statement in the loop is line 25

which increments the appropriate frequency counter depending on the value of respons- es[answer]. When the counter variable answer is 0, responses[answer] is 1, so ++frequeoncy[ responses[answer]]; is interpreted as

20 21 /* for each answer, select value of an element of array responses 22 and use that value as subscript in array frequency to 23 determine element to increment */ 24 for ( answer = 0; answer < RESPONSE_SIZE; answer++ ) { 25 26 } /* end for */ 27 28 /* display results */ 29 printf( "%s%17s\n", "Rating", "Frequency" ); 30 31 /* output the frequencies in a tabular format */ 32 for ( rating = 1; rating < FREQUENCY_SIZE; rating++ ) { 33 printf( "%6d%17d\n", rating, frequency[ rating ] ); 34 } /* end for */ 35 36 return 0; /* indicates successful termination */ 37 } /* end main */

Rating Frequency 1 2 2 2 3 2 4 2 5 5 6 11 7 5 8 7 9 1 10 3

Good Programming Practice 6.3 Strive for program clarity. Sometimes it may be worthwhile to trade off the most efficient use of memory or processor time in favor of writing clearer programs.

Performance Tip 6.1 Sometimes performance considerations far outweigh clarity considerations.

++frequency[ responses[ answer ] ];

++frequency[ 1 ];

Fig. 6.7 | Student poll analysis program. (Part 2 of 2.)

++frequency[ responses [ answer ] ];

6.4 Array Examples 205

which increments array element one. When answer is 1, responses[answer] is 2, so ++frequency[responses[answer]]; is interpreted as

which increments array element two. When answer is 2, responses[answer] is 6, so ++frequency[responses[answer]]; is actually interpreted as

which increments array element six, and so on. Regardless of the number of responses pro- cessed in the survey, only an 11-element array is required (ignoring element zero) to sum- marize the results. If the data contained invalid values such as 13, the program would attempt to add 1 to frequency[13]. This would be outside the bounds of the array. C has no array bounds checking to prevent the program from referring to an element that does not ex- ist. Thus, an executing program can “walk off” the end of an array without warning. You should ensure that all array references remain within the bounds of the array.

Graphing Array Element Values with Histograms Our next example (Fig. 6.8) reads numbers from an array and graphs the information in the form of a bar chart or histogram—each number is printed, then a bar consisting of that many asterisks is printed beside the number. The nested for statement (line 20) draws the bars. Note the use of printf( "\n" ) to end a histogram bar (line 24).

++frequency[ 2 ];

++frequency[ 6 ];

Common Programming Error 6.6 Referring to an element outside the array bounds.

Error-Prevention Tip 6.1 When looping through an array, the array subscript should never go below 0 and should always be less than the total number of elements in the array (size – 1). Make sure the loop-terminating condition prevents accessing elements outside this range.

Error-Prevention Tip 6.2 Programs should validate the correctness of all input values to prevent erroneous infor- mation from affecting a program’s calculations.

1 /* Fig. 6.8: fig06_08.c 2 Histogram printing program */ 3 #include <stdio.h> 4 #define SIZE 10 5 6 /* function main begins program execution */ 7 int main( void ) 8 { 9 /* use initializer list to initialize array n */

10 int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; 11 int i; /* outer for counter for array elements */ 12 int j; /* inner for counter counts *s in each histogram bar */

Fig. 6.8 | Histogram printing. (Part 1 of 2.)

206 Chapter 6 C Arrays

Rolling a Die 6000 Times and Summarizing the Results in an Array In Chapter 5, we stated that we’d show a more elegant method of writing the dice-rolling program of Fig. 5.8. The problem was to roll a single six-sided die 6000 times to test whether the random number generator actually produces random numbers. An array ver- sion of this program is shown in Fig. 6.9.

13 14 printf( "%s%13s%17s\n", "Element", "Value", "Histogram" ); 15 16 /* for each element of array n, output a bar of the histogram */ 17 for ( i = 0; i < SIZE; i++ ) { 18 19 20 21 22 23 24 printf( "\n" ); /* end a histogram bar */ 25 } /* end outer for */ 26 27 return 0; /* indicates successful termination */ 28 } /* end main */

Element Value Histogram 0 19 ******************* 1 3 *** 2 15 *************** 3 7 ******* 4 11 *********** 5 9 ********* 6 13 ************* 7 5 ***** 8 17 ***************** 9 1 *

1 /* Fig. 6.9: fig06_09.c 2 Roll a six-sided die 6000 times */ 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <time.h> 6 #define SIZE 7 7 8 /* function main begins program execution */ 9 int main( void )

10 { 11 int face; /* random die value 1 - 6 */ 12 int roll; /* roll counter 1-6000 */ 13 int frequency[ SIZE ] = { 0 }; /* clear counts */ 14

Fig. 6.9 | Dice-rolling program using an array instead of switch. (Part 1 of 2.)

Fig. 6.8 | Histogram printing. (Part 2 of 2.)

printf( "%7d%13d ", i, n[ i ]) ;

for ( j = 1; j <= n[ i ]; j++ ) { /* print one bar */ printf( "%c", '*' ); } /* end inner for */

6.4 Array Examples 207

Using Character Arrays to Store and Manipulate Strings We have discussed only integer arrays. However, arrays are capable of holding data of any type. We now discuss storing strings in character arrays. So far, the only string-processing capability we have is outputting a string with printf. A string such as "hello" is really a static array of individual characters in C.

Character arrays have several unique features. A character array can be initialized using a string literal. For example,

initializes the elements of array string1 to the individual characters in the string literal "first". In this case, the size of array string1 is determined by the compiler based on the length of the string. The string "first" contains five characters plus a special string-termi- nation character called the null character. Thus, array string1 actually contains six ele- ments. The character constant representing the null character is '\0'. All strings in C end with this character. A character array representing a string should always be defined large enough to hold the number of characters in the string and the terminating null character.

Character arrays also can be initialized with individual character constants in an ini- tializer list. The preceding definition is equivalent to

Because a string is really an array of characters, we can access individual characters in a string directly using array subscript notation. For example, string1[0] is the character 'f' and string1[3] is the character 's'.

15 srand( time( NULL ) ); /* seed random-number generator */ 16 17 /* roll die 6000 times */ 18 for ( roll = 1; roll <= 6000; roll++ ) { 19 face = 1 + rand() % 6; 20 / 21 } /* end for */ 22 23 printf( "%s%17s\n", "Face", "Frequency" ); 24 25 /* output frequency elements 1-6 in tabular format */ 26 for ( face = 1; face < SIZE; face++ ) { 27 printf( "%4d%17d\n", face, frequency[ face ] ); 28 } /* end for */ 29 30 return 0; /* indicates successful termination */ 31 } /* end main */

Face Frequency 1 1029 2 951 3 987 4 1033 5 1010 6 990

char string1[] = "first";

char string1[] = { 'f', 'i', 'r', 's', 't', '\0' };

Fig. 6.9 | Dice-rolling program using an array instead of switch. (Part 2 of 2.)

++frequency[ face ]; /* replaces 26-line switch of Fig. 5.8 *

208 Chapter 6 C Arrays

We also can input a string directly into a character array from the keyboard using scanf and the conversion specifier %s. For example,

creates a character array capable of storing a string of at most 19 characters and a termi- nating null character. The statement

reads a string from the keyboard into string2. The name of the array is passed to scanf without the preceding & used with nonstring variables. The & is normally used to provide scanf with a variable’s location in memory so that a value can be stored there. In Section 6.5, when we discuss passing arrays to functions, we’ll see that the value of an array name is the address of the start of the array; therefore, the & is not necessary. Function scanf will read characters until a space, tab, newline or end-of-file indicator is encoun- tered. The string should be no longer than 19 characters to leave room for the terminating null character. If the user types 20 or more characters, your program may crash! For this reason, use the conversion specifier %19s so that scanf does not write characters into mem- ory beyond the end of the array s.

It’s your responsibility to ensure that the array into which the string is read is capable of holding any string that the user types at the keyboard. Function scanf reads characters from the keyboard until the first white-space character is encountered—it does not check how large the array is. Thus, scanf can write beyond the end of the array.

A character array representing a string can be output with printf and the %s con- version specifier. The array string2 is printed with the statement

printf( "%s\n", string2 );

Function printf, like scanf, does not check how large the character array is. The char- acters of the string are printed until a terminating null character is encountered.

Figure 6.10 demonstrates initializing a character array with a string literal, reading a string into a character array, printing a character array as a string and accessing individual characters of a string.

char string2[ 20 ];

scanf( "%s", string2 );

Common Programming Error 6.7 Not providing scanf with a character array large enough to store a string typed at the key- board can result in destruction of data in a program and other runtime errors. This can also make a system susceptible to worm and virus attacks.

1 /* Fig. 6.10: fig06_10.c 2 Treating character arrays as strings */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main( void ) 7 { 8 char string1[ 20 ]; /* reserves 20 characters */ 9

Fig. 6.10 | Treating character arrays as strings. (Part 1 of 2.)

char string2[] = "string literal"; /* reserves 15 characters */

6.4 Array Examples 209

Figure 6.10 uses a for statement (line 22) to loop through the string1 array and print the individual characters separated by spaces, using the %c conversion specifier. The condition in the for statement, string1[i] != '\0', is true while the terminating null character has not been encountered in the string.

Static Local Arrays and Automatic Local Arrays Chapter 5 discussed the storage-class specifier static. A static local variable exists for the duration of the program, but is visible only in the function body. We can apply static to a local array definition so the array is not created and initialized each time the function is called and the array is not destroyed each time the function is exited in the program. This reduces program execution time, particularly for programs with frequently called func- tions that contain large arrays.

Arrays that are static are initialized once at compile time. If you do not explicitly initialize a static array, that array’s elements are initialized to zero by the compiler.

Figure 6.11 demonstrates function staticArrayInit (line 22) with a local static array (line 25) and function automaticArrayInit (line 44) with a local automatic array

10 int i; /* counter */ 11 12 /* read string from user into array string1 */ 13 printf("Enter a string: "); 14 15 16 /* output strings */ 17 18 19 20 21 /* output characters until null character is reached */ 22 23 24 25 26 printf( "\n" ); 27 return 0; /* indicates successful termination */ 28 } /* end main */

Enter a string: Hello there string1 is: Hello string2 is: string literal string1 with spaces between characters is: H e l l o

Performance Tip 6.2 In functions that contain automatic arrays where the function is in and out of scope fre- quently, make the array static so it’s not created each time the function is called.

Fig. 6.10 | Treating character arrays as strings. (Part 2 of 2.)

scanf( "%s", string1 ); /* input ended by whitespace character */

printf( "string1 is: %s\nstring2 is: %s\n" "string1 with spaces between characters is:\n", string1, string2 );

for ( i = 0; string1[ i ] != '\0'; i++ ) { printf( "%c ", string1[ i ] ); } /* end for */

210 Chapter 6 C Arrays

(line 47). Function staticArrayInit is called twice (lines 12 and 16). The local static array in the function is initialized to zero by the compiler (line 25). The function prints the array, adds 5 to each element and prints the array again. The second time the function is called, the static array contains the values stored during the first function call. Func- tion automaticArrayInit is also called twice (lines 13 and 17). The elements of the auto- matic local array in the function are initialized with the values 1, 2 and 3 (line 47). The function prints the array, adds 5 to each element and prints the array again. The second time the function is called, the array elements are initialized to 1, 2 and 3 again because the array has automatic storage duration.

Common Programming Error 6.8 Assuming that elements of a local static array are initialized to zero every time the func- tion in which the array is defined is called.

1 /* Fig. 6.11: fig06_11.c 2 Static arrays are initialized to zero */ 3 #include <stdio.h> 4 5 void staticArrayInit( void ); /* function prototype */ 6 void automaticArrayInit( void ); /* function prototype */ 7 8 /* function main begins program execution */ 9 int main( void )

10 { 11 printf( "First call to each function:\n" ); 12 13 14 15 printf( "\n\nSecond call to each function:\n" ); 16 17 18 return 0; /* indicates successful termination */ 19 } /* end main */ 20 21 /* function to demonstrate a static local array */ 22 void staticArrayInit( void ) 23 { 24 /* initializes elements to 0 first time function is called */ 25 26 int i; /* counter */ 27 28 printf( "\nValues on entering staticArrayInit:\n" ); 29 30 /* output contents of array1 */ 31 for ( i = 0; i <= 2; i++ ) { 32 printf( "array1[ %d ] = %d ", i, array1[ i ] ); 33 } /* end for */ 34 35 printf( "\nValues on exiting staticArrayInit:\n" );

Fig. 6.11 | Static arrays are initialized to zero if not explicitly initialized. (Part 1 of 2.)

staticArrayInit(); automaticArrayInit();

staticArrayInit(); automaticArrayInit();

static int array1[ 3 ];

6.4 Array Examples 211

36 37 /* modify and output contents of array1 */ 38 for ( i = 0; i <= 2; i++ ) { 39 printf( "array1[ %d ] = %d ", i, array1[ i ] += 5 ); 40 } /* end for */ 41 } /* end function staticArrayInit */ 42 43 /* function to demonstrate an automatic local array */ 44 void automaticArrayInit( void ) 45 { 46 /* initializes elements each time function is called */ 47 48 int i; /* counter */ 49 50 printf( "\n\nValues on entering automaticArrayInit:\n" ); 51 52 /* output contents of array2 */ 53 for ( i = 0; i <= 2; i++ ) { 54 printf("array2[ %d ] = %d ", i, array2[ i ] ); 55 } /* end for */ 56 57 printf( "\nValues on exiting automaticArrayInit:\n" ); 58 59 /* modify and output contents of array2 */ 60 for ( i = 0; i <= 2; i++ ) { 61 printf( "array2[ %d ] = %d ", i, array2[ i ] += 5 ); 62 } /* end for */ 63 } /* end function automaticArrayInit */

First call to each function: Values on entering staticArrayInit: array1[ 0 ] = 0 array1[ 1 ] = 0 array1[ 2 ] = 0 Values on exiting staticArrayInit: array1[ 0 ] = 5 array1[ 1 ] = 5 array1[ 2 ] = 5 Values on entering automaticArrayInit: array2[ 0 ] = 1 array2[ 1 ] = 2 array2[ 2 ] = 3 Values on exiting automaticArrayInit: array2[ 0 ] = 6 array2[ 1 ] = 7 array2[ 2 ] = 8 Second call to each function: Values on entering staticArrayInit: array1[ 0 ] = 5 array1[ 1 ] = 5 array1[ 2 ] = 5 Values on exiting staticArrayInit: array1[ 0 ] = 10 array1[ 1 ] = 10 array1[ 2 ] = 10 Values on entering automaticArrayInit: array2[ 0 ] = 1 array2[ 1 ] = 2 array2[ 2 ] = 3 Values on exiting automaticArrayInit: array2[ 0 ] = 6 array2[ 1 ] = 7 array2[ 2 ] = 8

Fig. 6.11 | Static arrays are initialized to zero if not explicitly initialized. (Part 2 of 2.)

int array2[ 3 ] = { 1, 2, 3 };

212 Chapter 6 C Arrays

6.5 Passing Arrays to Functions To pass an array argument to a function, specify the name of the array without any brack- ets. For example, if array hourlyTemperatures has been defined as

the function call

passes array hourlyTemperatures and its size to function modifyArray. Unlike char arrays that contain strings, other array types do not have a special terminator. For this reason, the size of an array is passed to the function, so that the function can process the proper num- ber of elements.

C automatically passes arrays to functions by reference—the called functions can modify the element values in the callers’ original arrays. The name of the array evaluates to the address of the first element of the array. Because the starting address of the array is passed, the called function knows precisely where the array is stored. Therefore, when the called function modifies array elements in its function body, it’s modifying the actual ele- ments of the array in their original memory locations.

Figure 6.12 demonstrates that an array name is really the address of the first element of an array by printing array, &array[0] and &array using the %p conversion specifier— a special conversion specifier for printing addresses. The %p conversion specifier normally outputs addresses as hexadecimal numbers. Hexadecimal (base 16) numbers consist of the digits 0 through 9 and the letters A through F (these letters are the hexadecimal equivalents of the numbers 10–15). They are often used as shorthand notation for large integer values. Appendix C, Number Systems, provides an in-depth discussion of the relationships among binary (base 2), octal (base 8), decimal (base 10; standard integers) and hexadec- imal integers. The output shows that both array and &array[0] have the same value, namely 0012FF78. The output of this program is system dependent, but the addresses are always identical for a particular execution of this program on a particular computer.

Although entire arrays are passed by reference, individual array elements are passed by value exactly as simple variables are. Such simple single pieces of data (such as individual ints, floats and chars) are called scalars. To pass an element of an array to a function, use the subscripted name of the array element as an argument in the function call. In Chapter 7, we show how to pass scalars (i.e., individual variables and array elements) to functions by reference.

int hourlyTemperatures[ 24 ];

modifyArray( hourlyTemperatures, 24 )

Performance Tip 6.3 Passing arrays by reference makes sense for performance reasons. If arrays were passed by value, a copy of each element would be passed. For large, frequently passed arrays, this would be time consuming and would consume storage for the copies of the arrays.

Software Engineering Observation 6.2 It’s possible to pass an array by value (by using a simple trick we explain in Chapter 10).

6.5 Passing Arrays to Functions 213

For a function to receive an array through a function call, the function’s parameter list must specify that an array will be received. For example, the function header for function modifyArray (that we called earlier in this section) might be written as

indicating that modifyArray expects to receive an array of integers in parameter b and the number of array elements in parameter size. The size of the array is not required between the array brackets. If it’s included, the compiler checks that it’s greater than zero, then ignores it. Specifying a negative size is a compilation error. Because arrays are automatically passed by reference, when the called function uses the array name b, it will be referring to the array in the caller (array hourlyTemperatures in the preceding call). In Chapter 7, we introduce other notations for indicating that an array is being received by a function. As we’ll see, these notations are based on the intimate relationship between arrays and pointers in C.

Figure 6.13 demonstrates the difference between passing an entire array and passing an array element. The program first prints the five elements of integer array a (lines 20– 22). Next, a and its size are passed to function modifyArray (line 27), where each of a’s elements is multiplied by 2 (lines 54–55). Then a is reprinted in main (lines 32–34). As the output shows, the elements of a are indeed modified by modifyArray. Now the pro- gram prints the value of a[3] (line 38) and passes it to function modifyElement (line 40). Function modifyElement multiplies its argument by 2 (line 64) and prints the new value. When a[3] is reprinted in main (line 43), it has not been modified, because individual array elements are passed by value.

1 /* Fig. 6.12: fig06_12.c 2 The name of an array is the same as &array[ 0 ] */ 3 #include <stdio.h> 4 5 /* function main begins program execution */ 6 int main( void ) 7 { 8 char array[ 5 ]; /* define an array of size 5 */ 9

10 11 12 return 0; /* indicates successful termination */ 13 } /* end main */

array = 0012FF78 &array[0] = 0012FF78 &array = 0012FF78

Fig. 6.12 | Array name is the same as the address of the array’s first element.

void modifyArray( int b[], int size )

1 /* Fig. 6.13: fig06_13.c 2 Passing arrays and individual array elements to functions */ 3 #include <stdio.h> 4 #define SIZE 5

Fig. 6.13 | Passing arrays and individual array elements to functions. (Part 1 of 3.)

printf( " array = %p\n&array[0] = %p\n &array = %p\n", array, &array[ 0 ], &array );

214 Chapter 6 C Arrays

5 6 /* function prototypes */ 7 8 9

10 /* function main begins program execution */ 11 int main( void ) 12 { 13 int a[ SIZE ] = { 0, 1, 2, 3, 4 }; /* initialize a */ 14 int i; /* counter */ 15 16 printf( "Effects of passing entire array by reference:\n\nThe " 17 "values of the original array are:\n" ); 18 19 /* output original array */ 20 for ( i = 0; i < SIZE; i++ ) { 21 printf( "%3d", a[ i ] ); 22 } /* end for */ 23 24 printf( "\n" ); 25 26 27 28 29 printf( "The values of the modified array are:\n" ); 30 31 /* output modified array */ 32 for ( i = 0; i < SIZE; i++ ) { 33 printf( "%3d", a[ i ] ); 34 } /* end for */ 35 36 /* output value of a[ 3 ] */ 37 printf( "\n\n\nEffects of passing array element " 38 "by value:\n\nThe value of a[3] is %d\n", a[ 3 ] ); 39 40 41 42 /* output value of a[ 3 ] */ 43 printf( "The value of a[ 3 ] is %d\n", a[ 3 ] ); 44 return 0; /* indicates successful termination */ 45 } /* end main */ 46 47 48 49 50 51 52 53 54 55 56 57

Fig. 6.13 | Passing arrays and individual array elements to functions. (Part 2 of 3.)

void modifyArray( int b[], int size ); void modifyElement( int e );

/* pass array a to modifyArray by reference */ modifyArray( a, SIZE );

modifyElement( a[ 3 ] ); /* pass array element a[ 3 ] by value */

/* in function modifyArray, "b" points to the original array "a" in memory */ void modifyArray( int b[], int size ) { int j; /* counter */ /* multiply each array element by 2 */ for ( j = 0; j < size; j++ ) { b[ j ] *= 2; } /* end for */ } /* end function modifyArray */

6.5 Passing Arrays to Functions 215

There may be situations in your programs in which a function should not be allowed to modify array elements. Because arrays are always passed by reference, modification of values in an array is difficult to control. C provides the type qualifier const to prevent modification of array values in a function. When an array parameter is preceded by the const qualifier, the array elements become constant in the function body, and any attempt to modify an element of the array in the function body results in a compile-time error. This enables you to correct a program so it does not attempt to modify array elements.

Figure 6.14 demonstrates the const qualifier. Function tryToModifyArray (line 20) is defined with parameter const int b[], which specifies that array b is constant and cannot be modified. The output shows the error messages produced by the compiler—the errors may be different on your system. Each of the three attempts by the function to modify array elements results in the compiler error “l-value specifies a const object.” The const qualifier is discussed again in Chapter 7.

58 59 60 61 62 63 64 65

Effects of passing entire array by reference:

The values of the original array are: 0 1 2 3 4 The values of the modified array are: 0 2 4 6 8

Effects of passing array element by value:

The value of a[3] is 6 Value in modifyElement is 12 The value of a[ 3 ] is 6

1 /* Fig. 6.14: fig06_14.c 2 Demonstrating the const type qualifier with arrays */ 3 #include <stdio.h> 4 5 /* function prototype */ 6 7 /* function main begins program execution */ 8 int main( void ) 9 {

10 int a[] = { 10, 20, 30 }; /* initialize a */

Fig. 6.14 | const type qualifier. (Part 1 of 2.)

Fig. 6.13 | Passing arrays and individual array elements to functions. (Part 3 of 3.)

/* in function modifyElement, "e" is a local copy of array element a[ 3 ] passed from main */ void modifyElement( int e ) { /* multiply parameter by 2 */ printf( "Value in modifyElement is %d\n", e *= 2 ); } /* end function modifyElement */

void tryToModifyArray( const int b[] );

216 Chapter 6 C Arrays

6.6 Sorting Arrays Sorting data (i.e., placing the data into a particular order such as ascending or descending) is one of the most important computing applications. A bank sorts all checks by account number so that it can prepare individual bank statements at the end of each month. Tele- phone companies sort their lists of accounts by last name and, within that, by first name to make it easy to find phone numbers. Virtually every organization must sort some data and in many cases massive amounts of data. Sorting data is an intriguing problem which has attracted some of the most intense research efforts in the field of computer science. In this chapter we discuss what is perhaps the simplest known sorting scheme. In Chapter 12 and Appendix F, we investigate more complex schemes that yield superior performance.

Figure 6.15 sorts the values in the elements of the 10-element array a (line 10) into ascending order. The technique we use is called the bubble sort or the sinking sort because the smaller values gradually “bubble” their way upward to the top of the array like air bub- bles rising in water, while the larger values sink to the bottom of the array. The technique

11 12 13 14 printf("%d %d %d\n", a[ 0 ], a[ 1 ], a[ 2 ] ); 15 return 0; /* indicates successful termination */ 16 } /* end main */ 17 18 19 20 21 22 23 24 25

Compiling... FIG06_14.C fig06_14.c(22) : error C2166: l-value specifies const object fig06_14.c(23) : error C2166: l-value specifies const object fig06_14.c(24) : error C2166: l-value specifies const object

Software Engineering Observation 6.3 The const type qualifier can be applied to an array parameter in a function definition to prevent the original array from being modified in the function body. This is another example of the principle of least privilege. Functions should not be given the capability to modify an array unless it’s absolutely necessary.

Performance Tip 6.4 Often, the simplest algorithms perform poorly. Their virtue is that they are easy to write, test and debug. More complex algorithms are often needed to realize maximum performance.

Fig. 6.14 | const type qualifier. (Part 2 of 2.)

tryToModifyArray( a );

/* in function tryToModifyArray, array b is const, so it cannot be used to modify the original array a in main. */ void tryToModifyArray( const int b[] ) { b[ 0 ] /= 2; /* error */ b[ 1 ] /= 2; /* error */ b[ 2 ] /= 2; /* error */ } /* end function tryToModifyArray */

6.6 Sorting Arrays 217

is to make several passes through the array. On each pass, successive pairs of elements are compared. If a pair is in increasing order (or if the values are identical), we leave the values as they are. If a pair is in decreasing order, their values are swapped in the array.

1 /* Fig. 6.15: fig06_15.c 2 This program sorts an array's values into ascending order */ 3 #include <stdio.h> 4 #define SIZE 10 5 6 /* function main begins program execution */ 7 int main( void ) 8 { 9 /* initialize a */

10 int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; 11 int pass; /* passes counter */ 12 int i; /* comparisons counter */ 13 int hold; /* temporary location used to swap array elements */ 14 15 printf( "Data items in original order\n" ); 16 17 /* output original array */ 18 for ( i = 0; i < SIZE; i++ ) { 19 printf( "%4d", a[ i ] ); 20 } /* end for */ 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 printf( "\nData items in ascending order\n" ); 40 41 /* output sorted array */ 42 for ( i = 0; i < SIZE; i++ ) { 43 printf( "%4d", a[ i ] ); 44 } /* end for */ 45 46 printf( "\n" ); 47 return 0; /* indicates successful termination */ 48 } /* end main */

Fig. 6.15 | Sorting an array with bubble sort. (Part 1 of 2.)

/* bubble sort */ /* loop to control number of passes */ for ( pass = 1; pass < SIZE; pass++ ) { /* loop to control number of comparisons per pass */ for ( i = 0; i < SIZE - 1; i++ ) { /* compare adjacent elements and swap them if first element is greater than second element */ if ( a[ i ] > a[ i + 1 ] ) { hold = a[ i ]; a[ i ] = a[ i + 1 ]; a[ i + 1 ] = hold; } /* end if */ } /* end inner for */ } /* end outer for */

218 Chapter 6 C Arrays

First the program compares a[0] to a[1], then a[1] to a[2], then a[2] to a[3], and so on until it completes the pass by comparing a[8] to a[9]. Although there are 10 ele- ments, only nine comparisons are performed. Because of the way the successive compari- sons are made, a large value may move down the array many positions on a single pass, but a small value may move up only one position. On the first pass, the largest value is guar- anteed to sink to the bottom element of the array, a[9]. On the second pass, the second- largest value is guaranteed to sink to a[8]. On the ninth pass, the ninth-largest value sinks to a[1]. This leaves the smallest value in a[0], so only nine passes of the array are needed to sort the array, even though there are ten elements.

The sorting is performed by the nested for loop (lines 24–37). If a swap is necessary, it’s performed by the three assignments

where the extra variable hold temporarily stores one of the two values being swapped. The swap cannot be performed with only the two assignments

If, for example, a[i] is 7 and a[i + 1] is 5, after the first assignment both values will be 5 and the value 7 will be lost. Hence the need for the extra variable hold.

The chief virtue of the bubble sort is that it’s easy to program. However, the bubble sort runs slowly because every exchange moves an element only one position closer to its final destination. This becomes apparent when sorting large arrays. In the exercises, we’ll develop more efficient versions of the bubble sort. Far more efficient sorts than the bubble sort have been developed. We’ll investigate a few of these in the exercises. More advanced courses investigate sorting and searching in greater depth.

6.7 Case Study: Computing Mean, Median and Mode Using Arrays We now consider a larger example. Computers are commonly used for survey data analysis to compile and analyze the results of surveys and opinion polls. Figure 6.16 uses array re- sponse initialized with 99 responses to a survey. Each response is a number from 1 to 9. The program computes the mean, median and mode of the 99 values.

The mean is the arithmetic average of the 99 values. Function mean (line 40) com- putes the mean by totaling the 99 elements and dividing the result by 99.

The median is the “middle value.” Function median (line 61) determines the median by calling function bubbleSort (defined in line 133) to sort the array of responses into

Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items in ascending order 2 4 6 8 10 12 37 45 68 89

hold = a[ i ]; a[ i ] = a[ i + 1 ]; a[ i + 1 ] = hold;

a[ i ] = a[ i + 1 ]; a[ i + 1 ] = a[ i ];

Fig. 6.15 | Sorting an array with bubble sort. (Part 2 of 2.)

6.7 Case Study: Computing Mean, Median and Mode Using Arrays 219

ascending order, then picking the middle element, answer[SIZE / 2], of the sorted array. When there is an even number of elements, the median should be calculated as the mean of the two middle elements. Function median does not currently provide this capability. Function printArray (line 156) is called to output the response array.

The mode is the value that occurs most frequently among the 99 responses. Function mode (line 82) determines the mode by counting the number of responses of each type, then selecting the value with the greatest count. This version of function mode does not handle a tie (see Exercise 6.14). Function mode also produces a histogram to aid in deter- mining the mode graphically. Figure 6.17 contains a sample run of this program. This example includes most of the common manipulations usually required in array problems, including passing arrays to functions.

1 /* Fig. 6.16: fig06_16.c 2 This program introduces the topic of survey data analysis. 3 It computes the mean, median and mode of the data */ 4 #include <stdio.h> 5 #define SIZE 99 6 7 /* function prototypes */ 8 void mean( const int answer[] ); 9 void median( int answer[] );

10 void mode( int freq[], const int answer[] ) ; 11 void bubbleSort( int a[] ); 12 void printArray( const int a[] ); 13 14 /* function main begins program execution */ 15 int main( void ) 16 { 17 int frequency[ 10 ] = { 0 }; /* initialize array frequency */ 18 19 /* initialize array response */ 20 int response[ SIZE ] = 21 { 6, 7, 8, 9, 8, 7, 8, 9, 8, 9, 22 7, 8, 9, 5, 9, 8, 7, 8, 7, 8, 23 6, 7, 8, 9, 3, 9, 8, 7, 8, 7, 24 7, 8, 9, 8, 9, 8, 9, 7, 8, 9, 25 6, 7, 8, 7, 8, 7, 9, 8, 9, 2, 26 7, 8, 9, 8, 9, 8, 9, 7, 5, 3, 27 5, 6, 7, 2, 5, 3, 9, 4, 6, 4, 28 7, 8, 9, 6, 8, 7, 8, 9, 7, 8, 29 7, 4, 4, 2, 5, 3, 8, 7, 5, 6, 30 4, 5, 6, 1, 6, 5, 7, 8, 7 }; 31 32 /* process responses */ 33 mean( response ); 34 median( response ); 35 mode( frequency, response ); 36 return 0; /* indicates successful termination */ 37 } /* end main */ 38

Fig. 6.16 | Survey data analysis program. (Part 1 of 4.)

220 Chapter 6 C Arrays

39 /* calculate average of all response values */ 40 void mean( const int answer[] ) 41 { 42 int j; /* counter for totaling array elements */ 43 int total = 0; /* variable to hold sum of array elements */ 44 45 printf( "%s\n%s\n%s\n", "********", " Mean", "********" ); 46 47 /* total response values */ 48 for ( j = 0; j < SIZE; j++ ) { 49 total += answer[ j ]; 50 } /* end for */ 51 52 printf( "The mean is the average value of the data\n" 53 "items. The mean is equal to the total of\n" 54 "all the data items divided by the number\n" 55 "of data items ( %d ). The mean value for\n" 56 "this run is: %d / %d = %.4f\n\n", 57 SIZE, total, SIZE, ( double ) total / SIZE ); 58 } /* end function mean */ 59 60 /* sort array and determine median element's value */ 61 void median( int answer[] ) 62 { 63 printf( "\n%s\n%s\n%s\n%s", 64 "********", " Median", "********", 65 "The unsorted array of responses is" ); 66 67 printArray( answer ); /* output unsorted array */ 68 69 70 71 printf( "\n\nThe sorted array is" ); 72 printArray( answer ); /* output sorted array */ 73 74 /* display median element */ 75 printf( "\n\nThe median is element %d of\n" 76 "the sorted %d element array.\n" 77 "For this run the median is %d\n\n", 78 SIZE / 2, SIZE, ); 79 } /* end function median */ 80 81 /* determine most frequent response */ 82 void mode( int freq[], const int answer[] ) 83 { 84 int rating; /* counter for accessing elements 1-9 of array freq */ 85 int j; /* counter for summarizing elements 0-98 of array answer */ 86 int h; /* counter for diplaying histograms of elements in array freq */ 87 int largest = 0; /* represents largest frequency */ 88 int modeValue = 0; /* represents most frequent response */ 89 90 printf( "\n%s\n%s\n%s\n", 91 "********", " Mode", "********" );

Fig. 6.16 | Survey data analysis program. (Part 2 of 4.)

bubbleSort( answer ); /* sort array */

answer[ SIZE / 2 ]

6.7 Case Study: Computing Mean, Median and Mode Using Arrays 221

92 93 /* initialize frequencies to 0 */ 94 for ( rating = 1; rating <= 9; rating++ ) { 95 freq[ rating ] = 0; 96 } /* end for */ 97 98 /* summarize frequencies */ 99 for ( j = 0; j < SIZE; j++ ) { 100 ++freq[ answer[ j ] ]; 101 } /* end for */ 102 103 /* output headers for result columns */ 104 printf( "%s%11s%19s\n\n%54s\n%54s\n\n", 105 "Response", "Frequency", "Histogram", 106 "1 1 2 2", "5 0 5 0 5" ); 107 108 /* output results */ 109 for ( rating = 1; rating <= 9; rating++ ) { 110 printf( "%8d%11d ", rating, freq[ rating ] ); 111 112 113 114 115 116 117 118 /* output histogram bar representing frequency value */ 119 for ( h = 1; h <= freq[ rating ]; h++ ) { 120 printf( "*" ); 121 } /* end inner for */ 122 123 printf( "\n" ); /* being new line of output */ 124 } /* end outer for */ 125 126 /* display the mode value */ 127 printf( "The mode is the most frequent value.\n" 128 "For this run the mode is %d which occurred" 129 " %d times.\n", modeValue, largest ); 130 } /* end function mode */ 131 132 /* function that sorts an array with bubble sort algorithm */ 133 void bubbleSort( int a[] ) 134 { 135 int pass; /* pass counter */ 136 int j; /* comparison counter */ 137 int hold; /* temporary location used to swap elements */ 138 139 /* loop to control number of passes */ 140 for ( pass = 1; pass < SIZE; pass++ ) { 141 142 /* loop to control number of comparisons per pass */ 143 for ( j = 0; j < SIZE - 1; j++ ) { 144

Fig. 6.16 | Survey data analysis program. (Part 3 of 4.)

/* keep track of mode value and largest frequency value */ if ( freq[ rating ] > largest ) { largest = freq[ rating ]; modeValue = rating; } /* end if */

222 Chapter 6 C Arrays

145 /* swap elements if out of order */ 146 if ( a[ j ] > a[ j + 1 ] ) { 147 hold = a[ j ]; 148 a[ j ] = a[ j + 1 ]; 149 a[ j + 1 ] = hold; 150 } /* end if */ 151 } /* end inner for */ 152 } /* end outer for */ 153 } /* end function bubbleSort */ 154 155 /* output array contents (20 values per row) */ 156 void printArray( const int a[] ) 157 { 158 int j; /* counter */ 159 160 /* output array contents */ 161 for ( j = 0; j < SIZE; j++ ) { 162 163 if ( j % 20 == 0 ) { /* begin new line every 20 values */ 164 printf( "\n" ); 165 } /* end if */ 166 167 printf( "%2d", a[ j ] ); 168 } /* end for */ 169 } /* end function printArray */

******** Mean ******** The mean is the average value of the data items. The mean is equal to the total of all the data items divided by the number of data items ( 99 ). The mean value for this run is: 681 / 99 = 6.8788

******** Median ******** The unsorted array of responses is 6 7 8 9 8 7 8 9 8 9 7 8 9 5 9 8 7 8 7 8 6 7 8 9 3 9 8 7 8 7 7 8 9 8 9 8 9 7 8 9 6 7 8 7 8 7 9 8 9 2 7 8 9 8 9 8 9 7 5 3 5 6 7 2 5 3 9 4 6 4 7 8 9 6 8 7 8 9 7 8 7 4 4 2 5 3 8 7 5 6 4 5 6 1 6 5 7 8 7

The sorted array is 1 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8

Fig. 6.17 | Sample run for the survey data analysis program. (Part 1 of 2.)

Fig. 6.16 | Survey data analysis program. (Part 4 of 4.)

6.8 Searching Arrays 223

6.8 Searching Arrays You’ll often work with large amounts of data stored in arrays. It may be necessary to de- termine whether an array contains a value that matches a certain key value. The process of finding a particular element of an array is called searching. In this section we discuss two searching techniques—the simple linear search technique and the more efficient (but more complex) binary search technique. Exercise 6.32 and Exercise 6.33 at the end of this chapter ask you to implement recursive versions of the linear search and the binary search.

Searching an Array with Linear Search The linear search (Fig. 6.18) compares each element of the array with the search key. Since the array is not in any particular order, it’s just as likely that the value will be found in the first element as in the last. On average, therefore, the program will have to compare the search key with half the elements of the array.

8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9

The median is element 49 of the sorted 99 element array. For this run the median is 7

******** Mode ******** Response Frequency Histogram

1 1 2 2 5 0 5 0 5

1 1 * 2 3 *** 3 4 **** 4 5 ***** 5 8 ******** 6 9 ********* 7 23 *********************** 8 27 *************************** 9 19 ******************* The mode is the most frequent value. For this run the mode is 8 which occurred 27 times.

1 /* Fig. 6.18: fig06_18.c 2 Linear search of an array */ 3 #include <stdio.h> 4 #define SIZE 100

Fig. 6.18 | Linear search of an array. (Part 1 of 3.)

Fig. 6.17 | Sample run for the survey data analysis program. (Part 2 of 2.)

224 Chapter 6 C Arrays

5 6 /* function prototype */ 7 8 9 /* function main begins program execution */

10 int main( void ) 11 { 12 int a[ SIZE ]; /* create array a */ 13 int x; /* counter for initializing elements 0-99 of array a */ 14 int searchKey; /* value to locate in array a */ 15 int element; /* variable to hold location of searchKey or -1 */ 16 17 /* create data */ 18 for ( x = 0; x < SIZE; x++ ) { 19 a[ x ] = 2 * x; 20 } /* end for */ 21 22 printf( "Enter integer search key:\n" ); 23 scanf( "%d", &searchKey ); 24 25 26 27 28 /* display results */ 29 if ( element != -1 ) { 30 printf( "Found value in element %d\n", element ); 31 } /* end if */ 32 else { 33 printf( "Value not found\n" ); 34 } /* end else */ 35 36 return 0; /* indicates successful termination */ 37 } /* end main */ 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

Fig. 6.18 | Linear search of an array. (Part 2 of 3.)

int linearSearch( const int array[], int key, int size );

/* attempt to locate searchKey in array a */ element = linearSearch( a, searchKey, SIZE );

/* compare key to every element of array until the location is found or until the end of array is reached; return subscript of element if key or -1 if key is not found */ int linearSearch( const int array[], int key, int size ) { int n; /* counter */ /* loop through array */ for ( n = 0; n < size; ++n ) { if ( array[ n ] == key ) { return n; /* return location of key */ } /* end if */ } /* end for */ return -1; /* key not found */ } /* end function linearSearch */

6.8 Searching Arrays 225

Searching an Array with Binary Search The linear searching method works well for small or unsorted arrays. However, for large arrays linear searching is inefficient. If the array is sorted, the high-speed binary search technique can be used.

The binary search algorithm eliminates from consideration one-half of the elements in a sorted array after each comparison. The algorithm locates the middle element of the array and compares it to the search key. If they are equal, the search key is found and the array subscript of that element is returned. If they are not equal, the problem is reduced to searching one-half of the array. If the search key is less than the middle element of the array, the first half of the array is searched, otherwise the second half of the array is searched. If the search key is not found in the specified subarray (piece of the original array), the algorithm is repeated on one-quarter of the original array. The search continues until the search key is equal to the middle element of a subarray, or until the subarray con- sists of one element that is not equal to the search key (i.e., the search key is not found).

In a worst case-scenario, searching an array of 1023 elements takes only 10 compari- sons using a binary search. Repeatedly dividing 1024 by 2 yields the values 512, 256, 128, 64, 32, 16, 8, 4, 2 and 1. The number 1024 (210) is divided by 2 only 10 times to get the value 1. Dividing by 2 is equivalent to one comparison in the binary search algorithm. An array of 1048576 (220) elements takes a maximum of 20 comparisons to find the search key. An array of one billion elements takes a maximum of 30 comparisons to find the search key. This is a tremendous increase in performance over the linear search that required comparing the search key to an average of half of the array elements. For a one- billion-element array, this is a difference between an average of 500 million comparisons and a maximum of 30 comparisons! The maximum comparisons for any array can be determined by finding the first power of 2 greater than the number of array elements.

Figure 6.19 presents the iterative version of function binarySearch (lines 44–74). The function receives four arguments—an integer array b to be searched, an integer searchKey, the low array subscript and the high array subscript (these define the portion of the array to be searched). If the search key does not match the middle element of a sub- array, the low subscript or high subscript is modified so that a smaller subarray can be searched. If the search key is less than the middle element, the high subscript is set to middle - 1 and the search is continued on the elements from low to middle - 1. If the search key is greater than the middle element, the low subscript is set to middle + 1 and the search is continued on the elements from middle + 1 to high. The program uses an array of 15 elements. The first power of 2 greater than the number of elements in this array

Enter integer search key: 36 Found value in element 18 Enter integer search key: 37 Value not found

Fig. 6.18 | Linear search of an array. (Part 3 of 3.)

226 Chapter 6 C Arrays

is 16 (24), so a maximum of 4 comparisons are required to find the search key. The pro- gram uses function printHeader (lines 77–96) to output the array subscripts and function printRow (lines 100–120) to output each subarray during the binary search process. The middle element in each subarray is marked with an asterisk (*) to indicate the element to which the search key is compared.

1 /* Fig. 6.19: fig06_19.c 2 Binary search of a sorted array */ 3 #include <stdio.h> 4 #define SIZE 15 5 6 /* function prototypes */ 7 int binarySearch( const int b[], int searchKey, int low, int high ); 8 void printHeader( void ); 9 void printRow( const int b[], int low, int mid, int high );

10 11 /* function main begins program execution */ 12 int main( void ) 13 { 14 int a[ SIZE ]; /* create array a */ 15 int i; /* counter for initializing elements 0-14 of array a */ 16 int key; /* value to locate in array a */ 17 int result; /* variable to hold location of key or -1 */ 18 19 /* create data */ 20 for ( i = 0; i < SIZE; i++ ) { 21 a[ i ] = 2 * i; 22 } /* end for */ 23 24 printf( "Enter a number between 0 and 28: " ); 25 scanf( "%d", &key ); 26 27 printHeader(); 28 29 /* search for key in array a */ 30 result = binarySearch( a, key, 0, SIZE - 1 ); 31 32 /* display results */ 33 if ( result != -1 ) { 34 printf( "\n%d found in array element %d\n", key, result ); 35 } /* end if */ 36 else { 37 printf( "\n%d not found\n", key ); 38 } /* end else */ 39 40 return 0; /* indicates successful termination */ 41 } /* end main */ 42 43 /* function to perform binary search of an array */ 44 45 {

Fig. 6.19 | Binary search of a sorted array. (Part 1 of 4.)

int binarySearch( const int b[], int searchKey, int low, int high )

6.8 Searching Arrays 227

46 int middle; /* variable to hold middle element of array */ 47 48 /* loop until low subscript is greater than high subscript */ 49 50 51 /* determine middle element of subarray being searched */ 52 53 54 /* display subarray used in this loop iteration */ 55 printRow( b, low, middle, high ); 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 return -1; /* searchKey not found */ 74 } /* end function binarySearch */ 75 76 /* Print a header for the output */ 77 void printHeader( void ) 78 { 79 int i; /* counter */ 80 81 printf( "\nSubscripts:\n" ); 82 83 /* output column head */ 84 for ( i = 0; i < SIZE; i++ ) { 85 printf( "%3d ", i ); 86 } /* end for */ 87 88 printf( "\n" ); /* start new line of output */ 89 90 /* output line of - characters */ 91 for ( i = 1; i <= 4 * SIZE; i++ ) { 92 printf( "-" ); 93 } /* end for */ 94 95 printf( "\n" ); /* start new line of output */ 96 } /* end function printHeader */ 97

Fig. 6.19 | Binary search of a sorted array. (Part 2 of 4.)

while ( low <= high ) {

middle = ( low + high ) / 2;

/* if searchKey matched middle element, return middle */ if ( searchKey == b[ middle ] ) { return middle; } /* end if */

/* if searchKey less than middle element, set new high */ else if ( searchKey < b[ middle ] ) { high = middle - 1; /* search low end of array */ } /* end else if */

/* if searchKey greater than middle element, set new low */ else { low = middle + 1; /* search high end of array */ } /* end else */

} /* end while */

228 Chapter 6 C Arrays

98 /* Print one row of output showing the current 99 part of the array being processed. */ 100 void printRow( const int b[], int low, int mid, int high ) 101 { 102 int i; /* counter for iterating through array b */ 103 104 /* loop through entire array */ 105 for ( i = 0; i < SIZE; i++ ) { 106 107 /* display spaces if outside current subarray range */ 108 if ( i < low || i > high ) { 109 printf( " " ); 110 } /* end if */ 111 else if ( i == mid ) { /* display middle element */ 112 printf( "%3d*", b[ i ] ); /* mark middle value */ 113 } /* end else if */ 114 else { /* display other elements in subarray */ 115 printf( "%3d ", b[ i ] ); 116 } /* end else */ 117 } /* end for */ 118 119 printf( "\n" ); /* start new line of output */ 120 } /* end function printRow */

Enter a number between 0 and 28: 25

Subscripts: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ------------------------------------------------------------ 0 2 4 6 8 10 12 14* 16 18 20 22 24 26 28 16 18 20 22* 24 26 28 24 26* 28 24*

25 not found

Enter a number between 0 and 28: 8

Subscripts: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ------------------------------------------------------------ 0 2 4 6 8 10 12 14* 16 18 20 22 24 26 28 0 2 4 6* 8 10 12 8 10* 12 8*

8 found in array element 4

Fig. 6.19 | Binary search of a sorted array. (Part 3 of 4.)

6.9 Multiple-Subscripted Arrays 229

6.9 Multiple-Subscripted Arrays Arrays in C can have multiple subscripts. A common use of multiple-subscripted arrays (also called multidimensional arrays) is to represent tables of values consisting of infor- mation arranged in rows and columns. To identify a particular table element, we must specify two subscripts: The first (by convention) identifies the element’s row and the sec- ond (by convention) identifies the element’s column. Tables or arrays that require two subscripts to identify a particular element are called double-subscripted arrays. Multiple- subscripted arrays can have more than two subscripts.

Figure 6.20 illustrates a double-subscripted array, a. The array contains three rows and four columns, so it’s said to be a 3-by-4 array. In general, an array with m rows and n columns is called an m-by-n array.

Every element in array a is identified in Fig. 6.20 by an element name of the form a[i][j]; a is the name of the array, and i and j are the subscripts that uniquely identify each element in a. The names of the elements in the first row all have a first subscript of 0; the names of the elements in the fourth column all have a second subscript of 3.

Enter a number between 0 and 28: 6

Subscripts: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ------------------------------------------------------------ 0 2 4 6 8 10 12 14* 16 18 20 22 24 26 28 0 2 4 6* 8 10 12

6 found in array element 3

Fig. 6.20 | Double-subscripted array with three rows and four columns.

Common Programming Error 6.9 Referencing a double-subscripted array element as a[ x, y ] instead of a[ x ][ y ]. C interprets a[ x, y ] as a[ y ], and as such it does not cause a compilation error.

Fig. 6.19 | Binary search of a sorted array. (Part 4 of 4.)

Row 0

Row 1

Row 2

Column index Row index Array name

a[ 0 ][ 0 ]

a[ 1 ][ 0 ]

a[ 2 ][ 0 ]

a[ 0 ][ 1 ]

a[ 1 ][ 1 ]

a[ 2 ][ 1 ]

a[ 0 ][ 2 ]

a[ 1 ][ 2 ]

a[ 2 ][ 2 ]

a[ 0 ][ 3 ]

Column 0 Column 1 Column 2 Column 3

a[ 1 ][ 3 ]

a[ 2 ][ 3 ]

230 Chapter 6 C Arrays

A multiple-subscripted array can be initialized when it’s defined, much like a single- subscripted array. For example, a double-subscripted array int b[2][2] could be defined and initialized with

The values are grouped by row in braces. The values in the first set of braces initialize row 0 and the values in the second set of braces initialize row 1. So, the values 1 and 2 initialize elements b[0][0] and b[0][1], respectively, and the values 3 and 4 initialize elements b[1][0] and b[1][1], respectively. If there are not enough initializers for a given row, the remaining elements of that row are initialized to 0. Thus,

would initialize b[0][0] to 1, b[0][1] to 0, b[1][0] to 3 and b[1][1] to 4. Figure 6.21 demonstrates defining and initializing double-subscripted arrays.

The program defines three arrays of two rows and three columns (six elements each). The definition of array1 (line 11) provides six initializers in two sublists. The first sublist initializes the first row (i.e., row 0) of the array to the values 1, 2 and 3; and the second sublist initializes the second row (i.e., row 1) of the array to the values 4, 5 and 6.

int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };

int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };

1 /* Fig. 6.21: fig06_21.c 2 Initializing multidimensional arrays */ 3 #include <stdio.h> 4 5 void printArray( const int a[][ 3 ] ); /* function prototype */ 6 7 /* function main begins program execution */ 8 int main( void ) 9 {

10 /* initialize array1, array2, array3 */ 11 12 13 14 15 printf( "Values in array1 by row are:\n" ); 16 printArray( array1 ); 17 18 printf( "Values in array2 by row are:\n" ); 19 printArray( array2 ); 20 21 printf( "Values in array3 by row are:\n" ); 22 printArray( array3 ); 23 return 0; /* indicates successful termination */ 24 } /* end main */ 25 26 27 28 29 30 31

Fig. 6.21 | Initializing multidimensional arrays. (Part 1 of 2.)

int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } }; int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 }; int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };

/* function to output array with two rows and three columns */ void printArray( const int a[][ 3 ] ) { int i; /* row counter */ int j; /* column counter */

6.9 Multiple-Subscripted Arrays 231

If the braces around each sublist are removed from the array1 initializer list, the com- piler initializes the elements of the first row followed by the elements of the second row. The definition of array2 (line 12) provides five initializers. The initializers are assigned to the first row, then the second row. Any elements that do not have an explicit initializer are initialized to zero automatically, so array2[1][2] is initialized to 0.

The definition of array3 (line 13) provides three initializers in two sublists. The sub- list for the first row explicitly initializes the first two elements of the first row to 1 and 2. The third element is initialized to zero. The sublist for the second row explicitly initializes the first element to 4. The last two elements are initialized to zero.

The program calls printArray (lines 27–43) to output each array’s elements. The function definition specifies the array parameter as const int a[][3]. When we receive a single-subscripted array as a parameter, the array brackets are empty in the function’s parameter list. The first subscript of a multiple-subscripted array is not required either, but all subsequent subscripts are required. The compiler uses these subscripts to determine the locations in memory of elements in multiple-subscripted arrays. All array elements are stored consecutively in memory regardless of the number of subscripts. In a double-sub- scripted array, the first row is stored in memory followed by the second row.

Providing the subscript values in a parameter declaration enables the compiler to tell the function how to locate an element in the array. In a double-subscripted array, each row is basically a single-subscripted array. To locate an element in a particular row, the com- piler must know how many elements are in each row so that it can skip the proper number of memory locations when accessing the array. Thus, when accessing a[1][2] in our example, the compiler knows to skip the three elements of the first row to get to the second row (row 1). Then, the compiler accesses the third element of that row (element 2).

Many common array manipulations use for repetition statements. For example, the following statement sets all the elements in the third row of array a in Fig. 6.20 to zero:

32 33 34 35 36 37 38 39 40 41 42

Values in array1 by row are: 1 2 3 4 5 6 Values in array2 by row are: 1 2 3 4 5 0 Values in array3 by row are: 1 2 0 4 0 0

Fig. 6.21 | Initializing multidimensional arrays. (Part 2 of 2.)

/* loop through rows */ for ( i = 0; i <= 1; i++ ) { /* output column values */ for ( j = 0; j <= 2; j++ ) { printf( "%d ", a[ i ][ j ] ); } /* end inner for */ printf( "\n" ); /* start new line of output */ } /* end outer for */ } /* end function printArray */

232 Chapter 6 C Arrays

We specified the third row, therefore we know that the first subscript is always 2 (again, 0 is the first row and 1 is the second). The loop varies only the second subscript (i.e., the column). The preceding for statement is equivalent to the assignment statements:

The following nested for statement determines the total of all the elements in array a.

The for statement totals the elements of the array one row at a time. The outer for state- ment begins by setting row (i.e., the row subscript) to 0 so that the elements of the first row may be totaled by the inner for statement. The outer for statement then increments row to 1, so the elements of the second row can be totaled. Then, the outer for statement increments row to 2, so the elements of the third row can be totaled. The result is printed when the nested for statement terminates.

Two-Dimensonal Array Manipulations Figure 6.22 performs several other common array manipulations on 3-by-4 array stu- dentGrades using for statements. Each row of the array represents a student and each col- umn represents a grade on one of the four exams the students took during the semester. The array manipulations are performed by four functions. Function minimum (lines 43– 62) determines the lowest grade of any student for the semester. Function maximum (lines 65–84) determines the highest grade of any student for the semester. Function average (lines 87–98) determines a particular student’s semester average. Function printArray (lines 101–120) outputs the double-subscripted array in a neat, tabular format.

for ( column = 0; column <= 3; column++ ) { a[ 2 ][ column ] = 0; }

a[ 2 ][ 0 ] = 0; a[ 2 ][ 1 ] = 0; a[ 2 ][ 2 ] = 0; a[ 2 ][ 3 ] = 0;

total = 0;

for ( row = 0; row <= 2; row++ ) { for ( column = 0; column <= 3; column++ ) { total += a[ row ][ column ]; } }

1 /* Fig. 6.22: fig06_22.c 2 Double-subscripted array example */ 3 #include <stdio.h> 4 #define STUDENTS 3 5 #define EXAMS 4 6 7 /* function prototypes */ 8 int minimum( const int grades[][ EXAMS ], int pupils, int tests ); 9 int maximum( const int grades[][ EXAMS ], int pupils, int tests );

10 double average( const int setOfGrades[], int tests ); 11 void printArray( const int grades[][ EXAMS ], int pupils, int tests );

Fig. 6.22 | Double-subscripted arrays example. (Part 1 of 4.)

6.9 Multiple-Subscripted Arrays 233

12 13 /* function main begins program execution */ 14 int main( void ) 15 { 16 int student; /* student counter */ 17 18 /* initialize student grades for three students (rows) */ 19 const int studentGrades[ STUDENTS ][ EXAMS ] = 20 { { 77, 68, 86, 73 }, 21 { 96, 87, 89, 78 }, 22 { 70, 90, 86, 81 } }; 23 24 /* output array studentGrades */ 25 printf( "The array is:\n" ); 26 printArray( studentGrades, STUDENTS, EXAMS ); 27 28 /* determine smallest and largest grade values */ 29 printf( "\n\nLowest grade: %d\nHighest grade: %d\n", 30 minimum( studentGrades, STUDENTS, EXAMS ), 31 maximum( studentGrades, STUDENTS, EXAMS ) ); 32 33 /* calculate average grade for each student */ 34 for ( student = 0; student < STUDENTS; student++ ) { 35 printf( "The average grade for student %d is %.2f\n", 36 37 } /* end for */ 38 39 return 0; /* indicates successful termination */ 40 } /* end main */ 41 42 /* Find the minimum grade */ 43 int minimum( const int grades[][ EXAMS ], int pupils, int tests ) 44 { 45 int i; /* student counter */ 46 int j; /* exam counter */ 47 int lowGrade = 100; /* initialize to highest possible grade */ 48 49 /* loop through rows of grades */ 50 for ( i = 0; i < pupils; i++ ) { 51 52 /* loop through columns of grades */ 53 for ( j = 0; j < tests; j++ ) { 54 55 if ( grades[ i ][ j ] < lowGrade ) { 56 lowGrade = grades[ i ][ j ]; 57 } /* end if */ 58 } /* end inner for */ 59 } /* end outer for */ 60 61 return lowGrade; /* return minimum grade */ 62 } /* end function minimum */ 63

Fig. 6.22 | Double-subscripted arrays example. (Part 2 of 4.)

student, average( studentGrades[ student ], EXAMS ) );

234 Chapter 6 C Arrays

64 /* Find the maximum grade */ 65 int maximum( const int grades[][ EXAMS ], int pupils, int tests ) 66 { 67 int i; /* student counter */ 68 int j; /* exam counter */ 69 int highGrade = 0; /* initialize to lowest possible grade */ 70 71 /* loop through rows of grades */ 72 for ( i = 0; i < pupils; i++ ) { 73 74 /* loop through columns of grades */ 75 for ( j = 0; j < tests; j++ ) { 76 77 if ( grades[ i ][ j ] > highGrade ) { 78 highGrade = grades[ i ][ j ]; 79 } /* end if */ 80 } /* end inner for */ 81 } /* end outer for */ 82 83 return highGrade; /* return maximum grade */ 84 } /* end function maximum */ 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 /* Print the array */ 101 void printArray( const int grades[][ EXAMS ], int pupils, int tests ) 102 { 103 int i; /* student counter */ 104 int j; /* exam counter */ 105 106 /* output column heads */ 107 printf( " [0] [1] [2] [3]" ); 108 109 /* output grades in tabular format */ 110 for ( i = 0; i < pupils; i++ ) { 111 112 /* output label for row */ 113 printf( "\nstudentGrades[%d] ", i ); 114

Fig. 6.22 | Double-subscripted arrays example. (Part 3 of 4.)

/* Determine the average grade for a particular student */ double average( const int setOfGrades[], int tests ) { int i; /* exam counter */ int total = 0; /* sum of test grades */ /* total all grades for one student */ for ( i = 0; i < tests; i++ ) { total += setOfGrades[ i ]; } /* end for */ return ( double ) total / tests; /* average */ } /* end function average */

6.9 Multiple-Subscripted Arrays 235

Functions minimum, maximum and printArray each receive three arguments—the studentGrades array (called grades in each function), the number of students (rows of the array) and the number of exams (columns of the array). Each function loops through array grades using nested for statements. The following nested for statement is from the function minimum definition:

The outer for statement begins by setting i (i.e., the row subscript) to 0 so that the ele- ments of the first row (i.e., the grades of the first student) can be compared to variable lowGrade in the body of the inner for statement. The inner for statement loops through the four grades of a particular row and compares each grade to lowGrade. If a grade is less than lowGrade, lowGrade is set to that grade. The outer for statement then increments the row subscript to 1. The elements of the second row are compared to variable lowGrade. The outer for statement then increments the row subscript to 2. The elements of the third row are compared to variable lowGrade. When execution of the nested structure is com- plete, lowGrade contains the smallest grade in the double-subscripted array. Function max- imum works similarly to function minimum.

Function average (line 87) takes two arguments—a single-subscripted array of test results for a particular student called setOfGrades and the number of test results in the array. When average is called, the first argument studentGrades[student] is passed. This causes the address of one row of the double-subscripted array to be passed to average.

115 /* output grades for one student */ 116 for ( j = 0; j < tests; j++ ) { 117 printf( "%-5d", grades[ i ][ j ] ); 118 } /* end inner for */ 119 } /* end outer for */ 120 } /* end function printArray */

The array is: [0] [1] [2] [3] studentGrades[0] 77 68 86 73 studentGrades[1] 96 87 89 78 studentGrades[2] 70 90 86 81

Lowest grade: 68 Highest grade: 96 The average grade for student 0 is 76.00 The average grade for student 1 is 87.50 The average grade for student 2 is 81.75

/* loop through rows of grades */ for ( i = 0; i < pupils; i++ ) { /* loop through columns of grades */ for ( j = 0; j < tests; j++ ) { if ( grades[ i ][ j ] < lowGrade ) { lowGrade = grades[ i ][ j ]; } /* end if */ } /* end inner for */ } /* end outer for */

Fig. 6.22 | Double-subscripted arrays example. (Part 4 of 4.)

236 Chapter 6 C Arrays

The argument studentGrades[1] is the starting address of the second row of the array. Remember that a double-subscripted array is basically an array of single-subscripted arrays and that the name of a single-subscripted array is the address of the array in memory. Function average calculates the sum of the array elements, divides the total by the number of test results and returns the floating-point result.

Summary Section 6.1 Introduction • Arrays are data structures consisting of related data items of the same type.

• Arrays are “static” entities in that they remain the same size throughout program execution.

Section 6.2 Arrays • An array is a group of memory locations related by the fact that they all have the same name and

the same type.

• To refer to a particular location or element in the array, specify the name of the array and the position number of the particular element in the array.

• The first element in every array is the zeroth element. Thus, the first element of array c is referred to as c[0], the second element of array c is referred to as c[1], the seventh element of array c is referred to as c[6], and, in general, the ith element of array c is referred to as c[i - 1].

• Array names, like other variable names, can contain only letters, digits and underscores. Array names cannot begin with a digit.

• The position number contained within square brackets is more formally called a subscript. A sub- script must be an integer or an integer expression.

• The brackets used to enclose the subscript of an array are actually considered to be an operator in C. They have the same level of precedence as the function call operator.

Section 6.3 Defining Arrays • Arrays occupy space in memory. You specify the type of each element and the number of ele-

ments in the array so that the computer may reserve the appropriate amount of memory.

• An array of type char can be used to store a character string.

Section 6.4 Array Examples • The elements of an array can be initialized when the array is defined by following the definition

with an equals sign and braces, {}, containing a comma-separated list of initializers. If there are fewer initializers than elements in the array, the remaining elements are initialized to zero.

• The statement int n[10] = {0}; explicitly initializes the first element to zero and initializes the remaining nine elements to zero because there are fewer initializers than there are elements in the array. It’s important to remember that automatic arrays are not automatically initialized to zero. You must at least initialize the first element to zero for the remaining elements to be automati- cally zeroed. This method of initializing the array elements to 0 is performed at compile time for static arrays and at runtime for automatic arrays.

• If the array size is omitted from a definition with an initializer list, the number of elements in the array will be the number of elements in the initializer list.

• The #define preprocessor directive can be used to define a symbolic constant—an identifier that is replaced with replacement text by the C preprocessor before the program is compiled. When the program is preprocessed, all occurrences of the symbolic constant are replaced with the re- placement text. Using symbolic constants to specify array sizes makes programs more scalable.

Summary 237

• C has no array bounds checking to prevent a program from referring to an element that does not exist. Thus, an executing program can “walk off” the end of an array without warning. You should ensure that all array references remain within the bounds of the array.

• A string such as "hello" is really a static array of individual characters in C.

• A character array can be initialized using a string literal. In this case, the size of the array is deter- mined by the compiler based on the length of the string.

• Every string contains a special string-termination character called the null character. The charac- ter constant representing the null character is '\0'.

• A character array representing a string should always be defined large enough to hold the number of characters in the string and the terminating null character.

• Character arrays also can be initialized with individual character constants in an initializer list.

• Because a string is really an array of characters, we can access individual characters in a string di- rectly using array subscript notation.

• You can input a string directly into a character array from the keyboard using scanf and the con- version specifier %s. The name of the character array is passed to scanf without the preceding & used with nonstring variables. The & is normally used to provide scanf with a variable’s location in memory so that a value can be stored there. An array name is the address of the start of the array; therefore, the & is not necessary.

• Function scanf reads characters from the keyboard until the first white-space character is en- countered—it does not check the array size. Thus, scanf can write beyond the end of the array.

• A character array representing a string can be output with printf and the %s conversion specifier. The characters of the string are printed until a terminating null character is encountered.

• A static local variable exists for the duration of the program but is only visible in the function body. We can apply static to a local array definition so that the array is not created and initial- ized each time the function is called and the array is not destroyed each time the function is exited in the program. This reduces program execution time, particularly for programs with frequently called functions that contain large arrays.

• Arrays that are static are automatically initialized once at compile time. If you do not explicitly initialize a static array, that array’s elements are initialized to zero by the compiler.

Section 6.5 Passing Arrays to Functions • To pass an array argument to a function, specify the name of the array without any brackets.

• Unlike char arrays that contain strings, other array types do not have a special terminator. For this reason, the size of an array is passed to a function, so that the function can process the proper number of elements.

• C automatically passes arrays to functions by reference—the called functions can modify the el- ement values in the callers’ original arrays. The name of the array evaluates to the address of the first element of the array. Because the starting address of the array is passed, the called function knows precisely where the array is stored. Therefore, when the called function modifies array el- ements in its function body, it’s modifying the actual elements of the array in their original mem- ory locations.

• Although entire arrays are passed by reference, individual array elements are passed by value ex- actly as simple variables are.

• Such simple single pieces of data (such as individual ints, floats and chars) are called scalars.

• To pass an element of an array to a function, use the subscripted name of the array element as an argument in the function call.

238 Chapter 6 C Arrays

• For a function to receive an array through a function call, the function’s parameter list must spec- ify that an array will be received. The size of the array is not required between the array brackets. If it’s included, the compiler checks that it’s greater than zero, then ignores it.

• When an array parameter is preceded by the const qualifier, the elements of the array become constant in the function body, and any attempt to modify an element of the array in the function body results in a compile-time error.

Section 6.6 Sorting Arrays • Sorting data (i.e., placing the data into a particular order such as ascending or descending) is one

of the most important computing applications.

• One sorting technique is called the bubble sort or the sinking sort, because the smaller values gradually “bubble” their way upward to the top of the array like air bubbles rising in water, while the larger values sink to the bottom of the array. The technique is to make several passes through the array. On each pass, successive pairs of elements are compared. If a pair is in increasing order (or if the values are identical), we leave the values as they are. If a pair is in decreasing order, their values are swapped in the array.

• Because of the way the successive comparisons are made, a large value may move down the array many positions on a single pass, but a small value may move up only one position.

• The chief virtue of the bubble sort is that it’s easy to program. However, the bubble sort runs slowly. This becomes apparent when sorting large arrays.

Section 6.7 Case Study: Computing Mean, Median and Mode Using Arrays • The mean is the arithmetic average of a set of values.

• The median is the “middle value” in a sorted set of values.

• The mode is the value that occurs most frequently in a set of values.

Section 6.8 Searching Arrays • The process of finding a particular element of an array is called searching.

• The linear search compares each element of the array with the search key. Since the array is not in any particular order, it’s just as likely that the value will be found in the first element as in the last. On average, therefore, the search key will be compared with half the elements of the array.

• The linear searching method works well for small or unsorted arrays. For sorted arrays, the high- speed binary search technique can be used.

• The binary search algorithm eliminates from consideration one-half of the elements in a sorted array after each comparison. The algorithm locates the middle element of the array and compares it to the search key. If they are equal, the search key is found and the array subscript of that el- ement is returned. If they are not equal, the problem is reduced to searching one-half of the array. If the search key is less than the middle element of the array, the first half of the array is searched, otherwise the second half of the array is searched. If the search key is not found in the specified subarray (piece of the original array), the algorithm is repeated on one-quarter of the original array. The search continues until the search key is equal to the middle element of a subarray, or until the subarray consists of one element that is not equal to the search key (i.e., the search key is not found).

• When using a binary search, the maximum number of comparisons required for any array can be determined by finding the first power of 2 greater than the number of array elements.

Section 6.9 Multiple-Subscripted Arrays • A common use of multiple-subscripted arrays (also called multidimensional arrays) is to repre-

sent tables of values consisting of information arranged in rows and columns. To identify a par-

Terminology 239

ticular table element, we must specify two subscripts: The first (by convention) identifies the element’s row and the second (by convention) identifies the element’s column.

• Tables or arrays that require two subscripts to identify a particular element are called double-sub- scripted arrays.

• Multiple-subscripted arrays can have more than two subscripts.

• A multiple-subscripted array can be initialized when it’s defined, much like a single-subscripted array. The values are grouped by row in braces. If there are not enough initializers for a given row, the remaining elements of that row are initialized to 0.

• The first subscript of a multiple-subscripted array parameter declaration is not required, but all subsequent subscripts are required. The compiler uses these subscripts to determine the locations in memory of elements in multiple-subscripted arrays. All array elements are stored consecutively in memory regardless of the number of subscripts. In a double-subscripted array, the first row is stored in memory followed by the second row.

• Providing the subscript values in a parameter declaration enables the compiler to tell the function how to locate an element in the array. In a double-subscripted array, each row is basically a single- subscripted array. To locate an element in a particular row, the compiler must know how many elements are in each row so that it can skip the proper number of memory locations when access- ing the array.

Terminology array 196 binary search 223 bubble sort 216 const keyword 215 double-subscripted array 229 element 196 index (or subscript) 196 initializer 199 key value 223 linear search 223 m-by-n array 229 multidimensional array 229 multiple-subscripted array 229 name 196

null character 207 position number 196 replacement text 201 scalable 202 scalar 212 search key 223 searching 223 sinking sort 216 subscript 196 survey data analysis 218 symbolic constant 201 table 229 value 196 zeroth element 196

Self-Review Exercises 6.1 Answer each of the following:

a) Lists and tables of values are stored in . b) An array’s elements are related by the fact that they have the same and . c) The number used to refer to a particular element of an array is called its . d) A(n) should be used to specify the size of an array because it makes the pro-

gram more scalable. e) The process of placing the elements of an array in order is called the array. f) Determining whethet an array contains a certain key value is called the array. g) An array that uses two subscripts is referred to as a(n) array.

6.2 State whether the following are true or false. If the answer is false, explain why. a) An array can store many different types of values. b) An array subscript can be of data type double.

240 Chapter 6 C Arrays

c) If there are fewer initializers in an initializer list than the number of elements in the ar- ray, C automatically initializes the remaining elements to the last value in the list of ini- tializers.

d) It’s an error if an initializer list contains more initializers than there are elements in the array.

e) An individual array element that is passed to a function as an argument of the form a[i] and modified in the called function will contain the modified value in the calling function.

6.3 Answer the following questions regarding an array called fractions. a) Define a symbolic constant SIZE to be replaced with the replacement text 10. b) Define an array with SIZE elements of type double and initialize the elements to 0. c) Name the fourth element from the beginning of the array. d) Refer to array element 4. e) Assign the value 1.667 to array element nine. f) Assign the value 3.333 to the seventh element of the array. g) Print array elements 6 and 9 with two digits of precision to the right of the decimal

point, and show the output that is displayed on the screen. h) Print all the elements of the array, using a for repetition statement. Assume the integer

variable x has been defined as a control variable for the loop. Show the output.

6.4 Write statements to accomplish the following: a) Define table to be an integer array and to have 3 rows and 3 columns. Assume the sym-

bolic constant SIZE has been defined to be 3. b) How many elements does the array table contain? Print the total number of elements. c) Use a for repetition statement to initialize each element of table to the sum of its sub-

scripts. Assume the integer variables x and y are defined as control variables. d) Print the values of each element of array table. Assume the array was initialized with

the definition:

int table[ SIZE ][ SIZE ] = { { 1, 8 }, { 2, 4, 6 }, { 5 } };

6.5 Find the error in each of the following program segments and correct the error. a) #define SIZE 100; b) SIZE = 10; c) Assume int b[ 10 ] = { 0 }, i;

for ( i = 0; i <= 10; i++ ) {

b[ i ] = 1; }

d) #include <stdio.h>; e) Assume int a[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };

a[ 1, 1 ] = 5; f) #define VALUE = 120

Answers to Self-Review Exercises 6.1 a) Arrays. b) Name, type. c) Subscript. d) Symbolic constant. e) Sorting. f) Searching. g) Double-subscripted.

6.2 a) False. An array can store only values of the same type. b) False. An array subscript must be an integer or an integer expression. c) False. C automatically initializes the remaining elements to zero. d) True. e) False. Individual elements of an array are passed by value. If the entire array is passed to

a function, then any modifications will be reflected in the original.

Answers to Self-Review Exercises 241

6.3 a) #define SIZE 10 b) double fractions[ SIZE ] = { 0.0 }; c) fractions[ 3 ] d) fractions[ 4 ] e) fractions[ 9 ] = 1.667; f) fractions[ 6 ] = 3.333; g) printf( "%.2f %.2f\n", fractions[ 6 ], fractions[ 9 ] );

Output: 3.33 1.67. h) for ( x = 0; x < SIZE; x++ ) {

printf( "fractions[%d] = %f\n", x, fractions[ x ] ); }

Output: fractions[0] = 0.000000

fractions[1] = 0.000000

fractions[2] = 0.000000

fractions[3] = 0.000000

fractions[4] = 0.000000

fractions[5] = 0.000000

fractions[6] = 3.333000

fractions[7] = 0.000000

fractions[8] = 0.000000

fractions[9] = 1.667000

6.4 a) int table[ SIZE ][ SIZE ]; b) Nine elements. printf( "%d\n", SIZE * SIZE ); c) for ( x = 0; x < SIZE; x++ ) {

for ( y = 0; y < SIZE; y++ ) {

table[ x ][ y ] = x + y;

}

} d) for ( x = 0; x < SIZE; x++ ) {

for ( y = 0; y < SIZE; y++ ) {

printf( "table[%d][%d] = %d\n", x, y, table[ x ][ y ] );

}

} Output:

table[0][0] = 1 table[0][1] = 8 table[0][2] = 0 table[1][0] = 2 table[1][1] = 4 table[1][2] = 6 table[2][0] = 5 table[2][1] = 0 table[2][2] = 0

6.5 a) Error: Semicolon at end of #define preprocessor directive. Correction: Eliminate semicolon.

b) Error: Assigning a value to a symbolic constant using an assignment statement. Correction: Assign a value to the symbolic constant in a #define preprocessor directive without using the assignment operator as in #define SIZE 10.

c) Error: Referencing an array element outside the bounds of the array (b[ 10 ]). Correction: Change the final value of the control variable to 9.

242 Chapter 6 C Arrays

d) Error: Semicolon at end of #include preprocessor directive. Correction: Eliminate semicolon.

e) Error: Array subscripting done incorrectly. Correction: Change the statement to a[ 1 ][ 1 ] = 5;

f) Error: Assigning a value to a symbolic constant using an assignment statement. Correction: Assign a value to the symbolic constant in a #define preprocessor directive without using the assignment operator as in #define VALUE 120.

Exercises 6.6 Fill in the blanks in each of the following:

a) C stores lists of values in . b) The elements of an array are related by the fact that they . c) When referring to an array element, the position number contained within parentheses

is called a(n) . d) The names of the five elements of array p are , , ,

and . e) The contents of a particular element of an array is called the of that element. f) Naming an array, stating its type and specifying the number of elements in the array is

called the array. g) The process of placing the elements of an array into either ascending or descending or-

der is called . h) In a double-subscripted array, the first subscript (by convention) identifies the

of an element and the second subscript (by convention) identifies the of an element.

i) An m-by-n array contains rows, columns and elements. j) The name of the element in row 3 and column 5 of array d is .

6.7 State which of the following are true and which are false. If false, explain why. a) To refer to a particular location or element within an array, we specify the name of the

array and the value of the particular element. b) An array definition reserves space for the array. c) To indicate that 100 locations should be reserved for integer array p, write

p[ 100 ];

d) A C program that initializes the elements of a 15-element array to zero must contain one for statement.

e) A C program that totals the elements of a double-subscripted array must contain nested for statements.

f) The mean, median and mode of the following set of values are 5, 6 and 7, respectively: 1, 2, 5, 6, 7, 7, 7.

6.8 Write statements to accomplish each of the following: a) Display the value of the seventh element of character array f. b) Input a value into element 4 of single-subscripted floating-point array b. c) Initialize each of the five elements of single-subscripted integer array g to 8. d) Total the elements of floating-point array c of 100 elements. e) Copy array a into the first portion of array b. Assume double a[11], b[34]; f) Determine and print the smallest and largest values contained in 99-element floating-

point array w.

6.9 Consider a 2-by-5 integer array t. a) Write a definition for t.

Exercises 243

b) How many rows does t have? c) How many columns does t have? d) How many elements does t have? e) Write the names of all the elements in the second row of t. f) Write the names of all the elements in the third column of t. g) Write a single statement that sets the element of t in row 1 and column 2 to zero. h) Write a series of statements that initialize each element of t to zero. Do not use a repe-

tition structure. i) Write a nested for statement that initializes each element of t to zero. j) Write a statement that inputs the values for the elements of t from the terminal. k) Write a series of statements that determine and print the smallest value in array t. l) Write a statement that displays the elements of the first row of t. m) Write a statement that totals the elements of the fourth column of t. n) Write a series of statements that print the array t in tabular format. List the column sub-

scripts as headings across the top and list the row subscripts at the left of each row.

6.10 (Sales Commissions) Use a single-subscripted array to solve the following problem. A com- pany pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who grosses $3000 in sales in a week re- ceives $200 plus 9% of $3000, or a total of $470. Write a C program (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson’s salary is truncated to an integer amount):

a) $200–299 b) $300–399 c) $400–499 d) $500–599 e) $600–699 f) $700–799 g) $800–899 h) $900–999 i) $1000 and over

6.11 (Bubble Sort) The bubble sort presented in Fig. 6.15 is inefficient for large arrays. Make the following simple modifications to improve the performance of the bubble sort.

a) After the first pass, the largest number is guaranteed to be in the highest-numbered el- ement of the array; after the second pass, the two highest numbers are “in place,” and so on. Instead of making nine comparisons on every pass, modify the bubble sort to make eight comparisons on the second pass, seven on the third pass and so on.

b) The data in the array may already be in the proper order or near-proper order, so why make nine passes if fewer will suffice? Modify the sort to check at the end of each pass whether any swaps have been made. If none has been made, then the data must already be in the proper order, so the program should terminate. If swaps have been made, then at least one more pass is needed.

6.12 Write single statements that perform each of the following single-subscripted array opera- tions:

a) Initialize the 10 elements of integer array counts to zeros. b) Add 1 to each of the 15 elements of integer array bonus. c) Read the 12 values of floating-point array monthlyTemperatures from the keyboard. d) Print the five values of integer array bestScores in column format.

6.13 Find the error(s) in each of the following statements: a) Assume: char str[ 5 ];

scanf( "%s", str ); /* User types hello */

244 Chapter 6 C Arrays

b) Assume: int a[ 3 ]; printf( "$d %d %d\n", a[ 1 ], a[ 2 ], a[ 3 ] );

c) double f[ 3 ] = { 1.1, 10.01, 100.001, 1000.0001 }; d) Assume: double d[ 2 ][ 10 ];

d[ 1, 9 ] = 2.345;

6.14 (Mean, Median and Mode Program Modifications) Modify the program of Fig. 6.16 so function mode is capable of handling a tie for the mode value. Also modify function median so the two middle elements are averaged in an array with an even number of elements.

6.15 (Duplicate Elimination) Use a single-subscripted array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it’s not a duplicate of a number already read. Provide for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem.

6.16 Label the elements of 3-by-5 double-subscripted array sales to indicate the order in which they are set to zero by the following program segment:

for ( row = 0; row <= 2; row++ ) { for ( column = 0; column <= 4; column++ ) { sales[ row ][ column ] = 0; } }

6.17 What does the following program do?

1 /* ex06_17.c */ 2 /* What does this program do? */ 3 #include <stdio.h> 4 #define SIZE 10 5 6 int whatIsThis( const int b[], int p ); /* function prototype */ 7 8 /* function main begins program execution */ 9 int main( void )

10 { 11 int x; /* holds return value of function whatIsThis */ 12 13 /* initialize array a */ 14 int a[ SIZE ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 15 16 x = whatIsThis( a, SIZE ); 17 18 printf( "Result is %d\n", x ); 19 return 0; /* indicates successful termination */ 20 } /* end main */ 21 22 /* what does this function do? */ 23 int whatIsThis( const int b[], int p ) 24 { 25 /* base case */ 26 if ( p == 1 ) { 27 return b[ 0 ]; 28 } /* end if */ 29 else { /* recursion step */ 30 31 return b[ p - 1 ] + whatIsThis( b, p - 1 ); 32 } /* end else */ 33 } /* end function whatIsThis */

Exercises 245

6.18 What does the following program do?

6.19 (Dice Rolling) Write a program that simulates the rolling of two dice. The program should use rand to roll the first die, and should use rand again to roll the second die. The sum of the two values should then be calculated. [Note: Since each die can show an integer value from 1 to 6, then the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 the least frequent sums.] Figure 6.23 shows the 36 possible combinations of the two dice. Your pro- gram should roll the two dice 36,000 times. Use a single-subscripted array to tally the numbers of times each possible sum appears. Print the results in a tabular format. Also, determine if the totals are reasonable; i.e., there are six ways to roll a 7, so approximately one-sixth of all the rolls should be 7.

1 /* ex06_18.c */ 2 /* What does this program do? */ 3 #include <stdio.h> 4 #define SIZE 10 5 6 /* function prototype */ 7 void someFunction( const int b[], int startIndex, int size ); 8 9 /* function main begins program execution */

10 int main( void ) 11 { 12 int a[ SIZE ] = { 8, 3, 1, 2, 6, 0, 9, 7, 4, 5 }; /* initialize a */ 13 14 printf( "Answer is:\n" ); 15 someFunction( a, 0, SIZE ); 16 printf( "\n" ); 17 return 0; /* indicates successful termination */ 18 } /* end main */ 19 20 /* What does this function do? */ 21 void someFunction( const int b[], int startIndex, int size ) 22 { 23 if ( startIndex < size ) { 24 someFunction( b, startIndex + 1, size ); 25 printf( "%d ", b[ startIndex ] ); 26 } /* end if */ 27 } /* end function someFunction */

Fig. 6.23 | Dice rolling outcomes.

1 2 3 4 5 6

6 7 8 9 10 11

7 8 9 10 11 12

5

5 6 7 8 9 104

4 5 6 7 8 93

3 4 5 6 7 82

2 3 4 5 6 71

6

246 Chapter 6 C Arrays

6.20 (Game of Craps) Write a program that runs 1000 games of craps (without human interven- tion) and answers each of the following questions:

a) How many games are won on the first roll, second roll, …, twentieth roll and after the twentieth roll?

b) How many games are lost on the first roll, second roll, …, twentieth roll and after the twentieth roll?

c) What are the chances of winning at craps? [Note: You should discover that craps is one of the fairest casino games. What do you suppose this means?]

d) What is the average length of a game of craps? e) Do the chances of winning improve with the length of the game?

6.21 (Airline Reservations System) A small airline has just purchased a computer for its new au- tomated reservations system. The president has asked you to program the new system. You’ll write a program to assign seats on each flight of the airline’s only plane (capacity: 10 seats).

Your program should display the following menu of alternatives:

Please type 1 for "first class" Please type 2 for "economy"

If the person types 1, then your program should assign a seat in the first class section (seats 1– 5). If the person types 2, then your program should assign a seat in the economy section (seats 6– 10). Your program should then print a boarding pass indicating the person's seat number and whether it’s in the first class or economy section of the plane.

Use a single-subscripted array to represent the seating chart of the plane. Initialize all the ele- ments of the array to 0 to indicate that all seats are empty. As each seat is assigned, set the corre- sponding element of the array to 1 to indicate that the seat is no longer available.

Your program should, of course, never assign a seat that has already been assigned. When the first class section is full, your program should ask the person if it’s acceptable to be placed in the economy section (and vice versa). If yes, then make the appropriate seat assignment. If no, then print the message "Next flight leaves in 3 hours."

6.22 (Total Sales) Use a double-subscripted array to solve the following problem. A company has four salespeople (1 to 4) who sell five different products (1 to 5). Once a day, each salesperson passes in a slip for each different type of product sold. Each slip contains:

a) The salesperson number b) The product number c) The total dollar value of that product sold that day

Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information from all of the slips for last month is available. Write a program that will read all this information for last month’s sales and summarize the total sales by salesperson by product. All totals should be stored in the double-subscripted array sales. After processing all the information for last month, print the results in tabular format with each of the columns representing a particular salesperson and each of the rows representing a particular product. Cross total each row to get the total sales of each product for last month; cross total each column to get the total sales by salesperson for last month. Your tabular printout should include these cross totals to the right of the totaled rows and to the bottom of the totaled columns.

6.23 (Turtle Graphics) The Logo language, which is particularly popular among personal com- puter users, made the concept of turtle graphics famous. Imagine a mechanical turtle that walks around the room under the control of a C program. The turtle holds a pen in one of two positions, up or down. While the pen is down, the turtle traces out shapes as it moves; while the pen is up, the turtle moves about freely without writing anything. In this problem you’ll simulate the operation of the turtle and create a computerized sketchpad as well.

Exercises 247

Use a 50-by-50 array floor which is initialized to zeros. Read commands from an array that contains them. Keep track of the current position of the turtle at all times and whether the pen is currently up or down. Assume that the turtle always starts at position 0, 0 of the floor with its pen up. The set of turtle commands your program must process are shown in Fig. 6.24. Suppose that the turtle is somewhere near the center of the floor. The following “program” would draw and print a 12-by-12 square:

2 5,12 3 5,12 3 5,12 3 5,12 1 6 9

As the turtle moves with the pen down, set the appropriate elements of array floor to 1s. When the 6 command (print) is given, wherever there is a 1 in the array, display an asterisk, or some other character you choose. Wherever there is a zero, display a blank. Write a program to implement the turtle graphics capabilities discussed here. Write several turtle graphics programs to draw interest- ing shapes. Add other commands to increase the power of your turtle graphics language.

6.24 (Knight’s Tour) One of the more interesting puzzlers for chess buffs is the Knight’s Tour problem, originally proposed by the mathematician Euler. The question is this: Can the chess piece called the knight move around an empty chessboard and touch each of the 64 squares once and only once? We study this intriguing problem in depth here.

The knight makes L-shaped moves (over two in one direction and then over one in a per- pendicular direction). Thus, from a square in the middle of an empty chessboard, the knight can make eight different moves (numbered 0 through 7) as shown in Fig. 6.25.

a) Draw an 8-by-8 chessboard on a sheet of paper and attempt a Knight’s Tour by hand. Put a 1 in the first square you move to, a 2 in the second square, a 3 in the third, and so on. Before starting the tour, estimate how far you think you’ll get, remembering that a full tour consists of 64 moves. How far did you get? Were you close to the estimate?

b) Now let’s develop a program that will move the knight around a chessboard. The board itself is represented by an 8-by-8 double-subscripted array board. Each of the squares is initialized to zero. We describe each of the eight possible moves in terms of both their

Command Meaning

1 Pen up 2 Pen down 3 Turn right 4 Turn left

5, 10 Move forward 10 spaces (or a number other than 10) 6 Print the 50-by-50 array 9 End of data (sentinel)

Fig. 6.24 | Turtle commands.

248 Chapter 6 C Arrays

horizontal and vertical components. For example, a move of type 0 as shown in Fig. 6.25 consists of moving two squares horizontally to the right and one square verti- cally upward. Move 2 consists of moving one square horizontally to the left and two squares vertically upward. Horizontal moves to the left and vertical moves upward are indicated with negative numbers. The eight moves may be described by two single-sub- scripted arrays, horizontal and vertical, as follows:

horizontal[ 0 ] = 2 horizontal[ 1 ] = 1 horizontal[ 2 ] = -1 horizontal[ 3 ] = -2 horizontal[ 4 ] = -2 horizontal[ 5 ] = -1 horizontal[ 6 ] = 1 horizontal[ 7 ] = 2

vertical[ 0 ] = -1 vertical[ 1 ] = -2 vertical[ 2 ] = -2 vertical[ 3 ] = -1 vertical[ 4 ] = 1 vertical[ 5 ] = 2 vertical[ 6 ] = 2 vertical[ 7 ] = 1

Let the variables currentRow and currentColumn indicate the row and column of the knight’s current position on the board. To make a move of type moveNumber, where moveNumber is between 0 and 7, your program uses the statements

currentRow += vertical[ moveNumber ]; currentColumn += horizontal[ moveNumber ];

Keep a counter that varies from 1 to 64. Record the latest count in each square the knight moves to. Remember to test each potential move to see if the knight has already visited that square. And, of course, test every potential move to make sure that the knight does not land off the chessboard. Now write a program to move the knight around the chessboard. Run the program. How many moves did the knight make?

c) After attempting to write and run a Knight’s Tour program, you have probably devel- oped some valuable insights. We’ll use these to develop a heuristic (or strategy) for mov-

Fig. 6.25 | The eight possible moves of the knight.

0 1 2 3 4 5 6 7

4 4 7

3 K

2 3 0

1 2 1

0

5 5 6

6

7

Exercises 249

ing the knight. Heuristics do not guarantee success, but a carefully developed heuristic greatly improves the chance of success. You may have observed that the outer squares are in some sense more troublesome than the squares nearer the center of the board. In fact, the most troublesome, or inaccessible, squares are the four corners.

Intuition may suggest that you should attempt to move the knight to the most trou- blesome squares first and leave open those that are easiest to get to, so that when the board gets congested near the end of the tour, there will be a greater chance of success.

We may develop an “accessibility heuristic” by classifying each of the squares according to how accessible it is and always moving the knight to the square (within the knight’s L-shaped moves, of course) that is most inaccessible. We label a double- subscripted array accessibility with numbers indicating from how many squares each particular square is accessible. On a blank chessboard, the center squares are therefore rated as 8s, the corner squares are rated as 2s, and the other squares have accessibility numbers of 3, 4, or 6 as follows:

2 3 4 4 4 4 3 2 3 4 6 6 6 6 4 3 4 6 8 8 8 8 6 4 4 6 8 8 8 8 6 4 4 6 8 8 8 8 6 4 4 6 8 8 8 8 6 4 3 4 6 6 6 6 4 3 2 3 4 4 4 4 3 2

Now write a version of the Knight’s Tour program using the accessibility heuristic. At any time, the knight should move to the square with the lowest accessibility num- ber. In case of a tie, the knight may move to any of the tied squares. Therefore, the tour may begin in any of the four corners. [Note: As the knight moves around the chess- board, your program should reduce the accessibility numbers as more and more squares become occupied. In this way, at any given time during the tour, each available square’s accessibility number will remain equal to precisely the number of squares from which that square may be reached.] Run this version of your program. Did you get a full tour? Now modify the program to run 64 tours, one from each square of the chess- board. How many full tours did you get?

d) Write a version of the Knight’s Tour program which, when encountering a tie between two or more squares, decides what square to choose by looking ahead to those squares reachable from the “tied” squares. Your program should move to the square for which the next move would arrive at a square with the lowest accessibility number.

6.25 (Knight’s Tour: Brute-Force Approaches) In Exercise 6.24 we developed a solution to the Knight’s Tour problem. The approach used, called the “accessibility heuristic,” generates many so- lutions and executes efficiently.

As computers continue increasing in power, we’ll be able to solve many problems with sheer computer power and relatively unsophisticated algorithms. Let’s call this approach “brute-force” problem solving.

a) Use random number generation to enable the knight to walk around the chess board (in its legitimate L-shaped moves, of course) at random. Your program should run one tour and print the final chessboard. How far did the knight get?

b) Most likely, the preceding program produced a relatively short tour. Now modify your program to attempt 1000 tours. Use a single-subscripted array to keep track of the num- ber of tours of each length. When your program finishes attempting the 1000 tours, it should print this information in neat tabular format. What was the best result?

c) Most likely, the preceding program gave you some “respectable” tours but no full tours. Now “pull all the stops out” and simply let your program run until it produces a full

250 Chapter 6 C Arrays

tour. [Caution: This version of the program could run for hours on a powerful comput- er.] Once again, keep a table of the number of tours of each length and print this table when the first full tour is found. How many tours did your program attempt before pro- ducing a full tour? How much time did it take?

d) Compare the brute-force version of the Knight’s Tour with the accessibility heuristic version. Which required a more careful study of the problem? Which algorithm was more difficult to develop? Which required more computer power? Could we be certain (in advance) of obtaining a full tour with the accessibility heuristic approach? Could we be certain (in advance) of obtaining a full tour with the brute-force approach? Argue the pros and cons of brute-force problem solving in general.

6.26 (Eight Queens) Another puzzler for chess buffs is the Eight Queens problem. Simply stated: Is it possible to place eight queens on an empty chessboard so that no queen is “attacking” any oth- er—that is, so that no two queens are in the same row, the same column, or along the same diagonal? Use the kind of thinking developed in Exercise 6.24 to formulate a heuristic for solving the Eight Queens problem. Run your program. [Hint: It’s possible to assign a numeric value to each square of the chessboard indicating how many squares of an empty chessboard are “eliminated” once a queen is placed in that square. For example, each of the four corners would be assigned the value 22, as in Fig. 6.26.]

Once these “elimination numbers” are placed in all 64 squares, an appropriate heuristic might be: Place the next queen in the square with the smallest elimination number. Why is this strategy intuitively appealing?

6.27 (Eight Queens: Brute-Force Approaches) In this problem you’ll develop several brute-orce approaches to solving the Eight Queens problem introduced in Exercise 6.26.

a) Solve the Eight Queens problem, using the random brute-force technique developed in Exercise 6.25.

b) Use an exhaustive technique (i.e., try all possible combinations of eight queens on the chessboard).

c) Why do you suppose the exhaustive brute-force approach may not be appropriate for solving the Eight Queens problem?

d) Compare and contrast the random brute-force and exhaustive brute-force approaches in general.

6.28 (Duplicate Elimination) In Chapter 12, we explore the high-speed binary search tree data structure. One feature of a binary search tree is that duplicate values are discarded when insertions are made into the tree. This is referred to as duplicate elimination. Write a program that produces

Fig. 6.26 | The 22 squares eliminated by placing a queen in the upper-left corner.

* *****

* *

* *

* *

* *

* *

*

*

*

*

*

*

Recursion Exercises 251

20 random numbers between 1 and 20. The program should store all nonduplicate values in an ar- ray. Use the smallest possible array to accomplish this task.

6.29 (Knight’s Tour: Closed Tour Test) In the Knight’s Tour, a full tour is when the knight makes 64 moves touching each square of the chessboard once and only once. A closed tour occurs when the 64th move is one move away from the location in which the knight started the tour. Mod- ify the Knight’s Tour program you wrote in Exercise 6.24 to test for a closed tour if a full tour has occurred.

6.30 (The Sieve of Eratosthenes) A prime integer is any integer greater than 1 that can be divided evenly only by itself and 1. The Sieve of Eratosthenes is a method of finding prime numbers. It works as follows:

a) Create an array with all elements initialized to 1 (true). Array elements with prime sub- scripts will remain 1. All other array elements will eventually be set to zero.

b) Starting with array subscript 2 (subscript 1 is not prime), every time an array element is found whose value is 1, loop through the remainder of the array and set to zero every element whose subscript is a multiple of the subscript for the element with value 1. For array subscript 2, all elements beyond 2 in the array that are multiples of 2 will be set to zero (subscripts 4, 6, 8, 10, and so on.). For array subscript 3, all elements beyond 3 in the array that are multiples of 3 will be set to zero (subscripts 6, 9, 12, 15, and so on.).

When this process is complete, the array elements that are still set to 1 indicate that the subscript is a prime number. Write a program that uses an array of 1000 elements to determine and print the prime numbers between 1 and 999. Ignore element 0 of the array.

Recursion Exercises 6.31 (Palindromes) A palindrome is a string that is spelled the same way forward and backward. Some examples of palindromes are: “radar,” “able was i ere i saw elba,” and, if you ignore blanks, “a man a plan a canal panama.” Write a recursive function testPalindrome that returns 1 if the string stored in the array is a palindrome and 0 otherwise. The function should ignore spaces and punctu- ation in the string.

6.32 (Linear Search) Modify the program of Fig. 6.18 to use a recursive linearSearch function to perform the linear search of the array. The function should receive an integer array and the size of the array as arguments. If the search key is found, return the array subscript; otherwise, return –1.

6.33 (Binary Search) Modify the program of Fig. 6.19 to use a recursive binarySearch function to perform the binary search of the array. The function should receive an integer array and the start- ing subscript and ending subscript as arguments. If the search key is found, return the array sub- script; otherwise, return –1.

6.34 (Eight Queens) Modify the Eight Queens program you created in Exercise 6.26 to solve the problem recursively.

6.35 (Print an array) Write a recursive function printArray that takes an array and the size of the array as arguments, prints the array, and returns nothing. The function should stop processing and return when it receives an array of size zero.

6.36 (Print a string backward) Write a recursive function stringReverse that takes a character array as an argument, prints it back to front and returns nothing. The function should stop process- ing and return when the terminating null character of the string is encountered.

6.37 (Find the minimum value in an array) Write a recursive function recursiveMinimum that takes an integer array and the array size as arguments and returns the smallest element of the array. The function should stop processing and return when it receives an array of one element.

252 Chapter 6 C Arrays

Special Section: Sudoku The game of Sudoku exploded in popularity worldwide in 2005. Almost every major newspaper now publishes a Sudoku puzzle daily. Handheld game players let you play anytime, anywhere and create puzzles on demand at various levels of difficulty. Be sure to check out our Sudoku Resource Center at www.deitel.com/sudoku for downloads, tutorials, books, e-books and more that will help you master the game. And not for the faint of heart—try fiendishly difficult Sudokus with tricky twists, a circular Sudoku and a variant of the puzzle with five interlocking grids. Subscribe to our free newsletter, the Deitel® Buzz Online, for notifications of updates to our Sudoku Resource Center and to other Deitel Resource Centers at www.deitel.com that provide games, puzzles and other interesting programming projects.

A completed Sudoku puzzle is a 9× 9 grid (i.e., a two-dimensional array) in which the digits 1 through 9 appear once and only once in each row, each column and each of nine 3× 3 grids. In the partially completed 9× 9 grid of Fig. 6.27, row 1, column 1, and the 3× 3 grid in the upper-left cor- ner of the board each contain the digits 1 through 9 once and only once. We use C’s two-dimen- sional array row and column-numbering conventions, but we’re ignoring row 0 and column 0 in conformance with Sudoku community conventions.

The typical Sudoku puzzle provides many filled-in cells and many blanks, often arranged in a symmetrical pattern as is typical with crossword puzzles. The player’s task is to fill in the blanks to complete the puzzle. Some puzzles are easy to solve; some are quite difficult, requiring sophisti- cated solution strategies.

In Appendix D, Game Programming: Solving Sudoku, we’ll discuss various simple solution strategies, and suggest what to do when these fail. We’ll also present various approaches for pro- gramming Sudoku puzzle creators and solvers in C. Unfortunately, Standard C does not include graphics and GUI (graphical user interface) capabilities, so our representation of the board won’t be as elegant as we could make it in Java and other programming languages that support these capabil- ities. You may want to revisit your Sudoku programs after you read Appendix E, Game Program- ming with the Allegro C Library. Allegro, which is not part of Standard C, offers capabilities that will help you add graphics and even sounds to your Sudoku programs.

Fig. 6.27 | Partially completed 9× 9 Sudoku grid. Note the nine 3× 3 grids.

1 5 6 7 8 9432

1

5

6

7

8

9

4

3

2

5 9 7 6 2 8431

4 86

7 29

2

9

3

8

1

6

7C Pointers Addresses are given to us to conceal our whereabouts. —Saki (H. H. Munro)

By indirection find direction out. —William Shakespeare

Many things, having full reference To one consent, may work contrariously. —William Shakespeare

You will find it a very good practice always to verify your references, sir! —Dr. Routh

O b j e c t i v e s In this chapter, you’ll learn:

■ Pointers and pointer operators.

■ To use pointers to pass arguments to functions by reference.

■ The close relationships among pointers, arrays and strings.

■ To use pointers to functions.

■ To define and use arrays of strings.

254 Chapter 7 C Pointers

7.1 Introduction In this chapter, we discuss one of the most powerful features of the C programming lan- guage, the pointer.1 Pointers are among C’s most difficult capabilities to master. Pointers enable programs to simulate call-by-reference and to create and manipulate dynamic data structures, i.e., data structures that can grow and shrink at execution time, such as linked lists, queues, stacks and trees. This chapter explains basic pointer concepts. Chapter 10 ex- amines the use of pointers with structures. Chapter 12 introduces dynamic memory man- agement techniques and presents examples of creating and using dynamic data structures.

7.2 Pointer Variable Definitions and Initialization Pointers are variables whose values are memory addresses. Normally, a variable directly contains a specific value. A pointer, on the other hand, contains an address of a variable that contains a specific value. In this sense, a variable name directly references a value, and a pointer indirectly references a value (Fig. 7.1). Referencing a value through a pointer is called indirection.

7.1 Introduction 7.2 Pointer Variable Definitions and

Initialization 7.3 Pointer Operators 7.4 Passing Arguments to Functions by

Reference 7.5 Using the const Qualifier with

Pointers 7.6 Bubble Sort Using Call-by-Reference

7.7 sizeof Operator 7.8 Pointer Expressions and Pointer

Arithmetic 7.9 Relationship between Pointers and

Arrays 7.10 Arrays of Pointers 7.11 Case Study: Card Shuffling and

Dealing Simulation 7.12 Pointers to Functions

Summary | Terminology | Self-Review Exercises | Answers to Self-Review Exercises | Exercises Special Section: Building Your Own Computer | Array of Function Pointer Exercises |

Making a Difference

1. Pointers and pointer-based entities such as arrays and strings, when misused intentionally or acciden- tally, can lead to errors and security breaches. See our Secure C Programming Resource Center (www.deitel.com/SecureC/) for articles, books, white papers and forums on this important topic.

Fig. 7.1 | Directly and indirectly referencing a variable.

7 Pointer countPtr indirectly references a variable that contains the value 7

countcountPtr

7 count directly references a variable that contains the value 7

count

7.3 Pointer Operators 255

Pointers, like all variables, must be defined before they can be used. The definition

specifies that variable countPtr is of type int * (i.e., a pointer to an integer) and is read, “countPtr is a pointer to int” or “countPtr points to an object of type int.” Also, the vari- able count is defined to be an int, not a pointer to an int. The * only applies to countPtr in the definition. When * is used in this manner in a definition, it indicates that the variable being defined is a pointer. Pointers can be defined to point to objects of any type.

Pointers should be initialized either when they’re defined or in an assignment state- ment. A pointer may be initialized to NULL, 0 or an address. A pointer with the value NULL points to nothing. NULL is a symbolic constant defined in the <stddef.h> header (and sev- eral other headers, such as <stdio.h>). Initializing a pointer to 0 is equivalent to initializing a pointer to NULL, but NULL is preferred. When 0 is assigned, it’s first converted to a pointer of the appropriate type. The value 0 is the only integer value that can be assigned directly to a pointer variable. Assigning a variable’s address to a pointer is discussed in Section 7.3.

7.3 Pointer Operators The &, or address operator, is a unary operator that returns the address of its operand. For example, assuming the definitions

the statement

assigns the address of the variable y to pointer variable yPtr. Variable yPtr is then said to “point to” y. Figure 7.2 shows a schematic representation of memory after the preceding assignment is executed.

int *countPtr, count;

Common Programming Error 7.1 The asterisk (*) notation used to declare pointer variables does not distribute to all vari- able names in a declaration. Each pointer must be declared with the * prefixed to the name; e.g., if you wish to declare xPtr and yPtr as int pointers, use int *xPtr, *yPtr;.

Common Programming Error 7.2 Include the letters ptr in pointer variable names to make it clear that these variables are pointers and thus need to be handled appropriately.

Error-Prevention Tip 7.1 Initialize pointers to prevent unexpected results.

int y = 5; int *yPtr;

yPtr = &y;

Fig. 7.2 | Graphical representation of a pointer pointing to an integer variable in memory.

5

yyPtr

256 Chapter 7 C Pointers

Figure 7.3 shows the representation of the pointer in memory, assuming that integer variable y is stored at location 600000, and pointer variable yPtr is stored at location 500000. The operand of the address operator must be a variable; the address operator cannot be applied to constants, to expressions or to variables declared with the storage- class register.

The unary * operator, commonly referred to as the indirection operator or derefer- encing operator, returns the value of the object to which its operand (i.e., a pointer) points. For example, the statement

prints the value of variable y, namely 5. Using * in this manner is called dereferencing a pointer.

Figure 7.4 demonstrates the pointer operators & and *. The printf conversion speci- fier %p outputs the memory location as a hexadecimal integer on most platforms. (See Appendix C, Number Systems, for more information on hexadecimal integers.) Notice that the address of a and the value of aPtr are identical in the output, thus confirming that the address of a is indeed assigned to the pointer variable aPtr (line 11). The & and * oper- ators are complements of one another—when they’re both applied consecutively to aPtr in either order (line 21), the same result is printed. Figure 7.5 lists the precedence and asso- ciativity of the operators introduced to this point.

Fig. 7.3 | Representation of y and yPtr in memory.

printf( "%d", *yPtr );

Common Programming Error 7.3 Dereferencing a pointer that has not been properly initialized or that has not been assigned to point to a specific location in memory is an error. This could cause a fatal execution- time error, or it could accidentally modify important data and allow the program to run to completion with incorrect results.

1 /* Fig. 7.4: fig07_04.c 2 Using the & and * operators */ 3 #include <stdio.h> 4 5 int main( void ) 6 { 7 int a; /* a is an integer */ 8 9

10 a = 7; 11

Fig. 7.4 | Using the & and * pointer operators. (Part 1 of 2.)

5

y

600000 location 500000

yPtr

location 600000

int *aPtr; /* aPtr is a pointer to an integer */

aPtr = &a; /* aPtr set to address of a */

7.4 Passing Arguments to Functions by Reference 257

7.4 Passing Arguments to Functions by Reference There are two ways to pass arguments to a function—call-by-value and call-by-reference. All arguments in C are passed by value. As we saw in Chapter 5, return may be used to return one value from a called function to a caller (or to return control from a called func- tion without passing back a value). Many functions require the capability to modify one or more variables in the caller or to pass a pointer to a large data object to avoid the over-

12 13 printf( "The address of a is %p" 14 "\nThe value of aPtr is %p", , ); 15 16 printf( "\n\nThe value of a is %d" 17 "\nThe value of *aPtr is %d", a, ); 18 19 printf( "\n\nShowing that * and & are complements of " 20 "each other\n&*aPtr = %p" 21 "\n*&aPtr = %p\n", , ); 22 return 0; /* indicates successful termination */ 23 } /* end main */

The address of a is 0012FF7C The value of aPtr is 0012FF7C

The value of a is 7 The value of *aPtr is 7

Showing that * and & are complements of each other. &*aPtr = 0012FF7C *&aPtr = 0012FF7C

Operators Associativity Type

() [] left to right highest

+ - ++ -- ! * & (type) right to left unary

* / % left to right multiplicative

+ - left to right additive

< <= > s>= left to right relational

== != left to right equality

&& left to right logical AND

|| left to right logical OR

?: right to left conditional

= += -= *= /= %= right to left assignment

, left to right comma

Fig. 7.5 | Operator precedence and associativity.

Fig. 7.4 | Using the & and * pointer operators. (Part 2 of 2.)

&a aPtr

*aPtr

&*aPtr *&aPtr

258 Chapter 7 C Pointers

head of passing the object by value (which incurs the overhead of making a copy of the object). For these purposes, C provides the capabilities for simulating call-by-reference.

In C, you use pointers and the indirection operator to simulate call-by-reference. When calling a function with arguments that should be modified, the addresses of the arguments are passed. This is normally accomplished by applying the address operator (&) to the variable (in the caller) whose value will be modified. As we saw in Chapter 6, arrays are not passed using operator & because C automatically passes the starting location in memory of the array (the name of an array is equivalent to &arrayName[0]). When the address of a variable is passed to a function, the indirection operator (*) may be used in the function to modify the value at that location in the caller’s memory.

The programs in Fig. 7.6 and Fig. 7.7 present two versions of a function that cubes an integer—cubeByValue and cubeByReference. Figure 7.6 passes the variable number to function cubeByValue using call-by-value (line 14). The cubeByValue function cubes its argument and passes the new value back to main using a return statement. The new value is assigned to number in main (line 14).

Figure 7.7 passes the variable number using call-by-reference (line 15)—the address of number is passed—to function cubeByReference. Function cubeByReference takes as a parameter a pointer to an int called nPtr (line 22). The function dereferences the pointer and cubes the value to which nPtr points (line 24), then assigns the result to *nPtr (which

1 /* Fig. 7.6: fig07_06.c 2 Cube a variable using call-by-value */ 3 #include <stdio.h> 4 5 6 7 int main( void ) 8 { 9 int number = 5; /* initialize number */

10 11 printf( "The original value of number is %d", number ); 12 13 /* pass number by value to cubeByValue */ 14 15 16 printf( "\nThe new value of number is %d\n", number ); 17 return 0; /* indicates successful termination */ 18 } /* end main */ 19 20 21 22 23 24

The original value of number is 5 The new value of number is 125

Fig. 7.6 | Cube a variable using call-by-value.

int cubeByValue( int n ); /* prototype */

number = cubeByValue( number );

/* calculate and return cube of integer argument */ int cubeByValue( int n ) { return n * n * n; /* cube local variable n and return result */ } /* end function cubeByValue */

7.4 Passing Arguments to Functions by Reference 259

is really number in main), thus changing the value of number in main. Figure 7.8 and Fig. 7.9 analyze graphically the programs in Fig. 7.6 and Fig. 7.7, respectively.

A function receiving an address as an argument must define a pointer parameter to receive the address. For example, in Fig. 7.7 the header for function cubeByReference (line 22) is:

The header specifies that cubeByReference receives the address of an integer variable as an argument, stores the address locally in nPtr and does not return a value.

The function prototype for cubeByReference contains int * in parentheses. As with other variable types, it’s not necessary to include names of pointers in function prototypes. Names included for documentation purposes are ignored by the C compiler.

In the function header and in the prototype for a function that expects a single-sub- scripted array as an argument, the pointer notation in the parameter list of function cubeByReference may be used. The compiler does not differentiate between a function that receives a pointer and a function that receives a single-subscripted array. This, of course, means that the function must “know” when it’s receiving an array or simply a single variable for which it is to perform call by reference. When the compiler encounters

1 /* Fig. 7.7: fig07_07.c 2 Cube a variable using call-by-reference with a pointer argument */ 3 4 #include <stdio.h> 5 6 7 8 int main( void ) 9 {

10 int number = 5; /* initialize number */ 11 12 printf( "The original value of number is %d", number ); 13 14 /* pass address of number to cubeByReference */ 15 16 17 printf( "\nThe new value of number is %d\n", number ); 18 return 0; /* indicates successful termination */ 19 } /* end main */ 20 21 22 23 24 25

The original value of number is 5 The new value of number is 125

Fig. 7.7 | Cube a variable using call-by-reference with a pointer argument.

void cubeByReference( int *nPtr )

void cubeByReference( int *nPtr ); /* prototype */

cubeByReference( &number );

/* calculate cube of *nPtr; modifies variable number in main */ void cubeByReference( int *nPtr ) { *nPtr = *nPtr * *nPtr * *nPtr; /* cube *nPtr */ } /* end function cubeByReference */

260 Chapter 7 C Pointers

a function parameter for a single-subscripted array of the form int b[], the compiler con- verts the parameter to the pointer notation int *b. The two forms are interchangeable.

Fig. 7.8 | Analysis of a typical call-by-value.

Step 1: Before main calls cubeByValue:

int main( void ) { int number = 5;

number = cubeByValue( number ); }

125

125

125125

5

number

5

number

5

number

125

number

5

number

int cubeByValue( int n ) { return n * n * n; }

undefined

n

undefined

n

undefined

n

Step 2: After cubeByValue receives the call:

int main( void ) { int number = 5;

number = cubeByValue( number ); }

int cubeByValue( int n ) { return n * n * n; }

5

n

5

n

Step 3: After cubeByValue cubes parameter n and before cubeByValue returns to main:

int main( void ) { int number = 5;

number = cubeByValue( number ); }

int cubeByValue( int n ) {

return n * n * n; }

Step 4: After cubeByValue returns to main and before assigning the result to number:

int main( void ) { int number = 5;

number = cubeByValue( number ); }

int cubeByValue( int n ) { return n * n * n; }

Step 5: After main completes the assignment to number:

int main( void ) { int number = 5;

number = cubeByValue( number ); }

int cubeByValue( int n ) { return n * n * n; }

7.5 Using the const Qualifier with Pointers 261

7.5 Using the const Qualifier with Pointers The const qualifier enables you to inform the compiler that the value of a particular vari- able should not be modified. The const qualifier did not exist in early versions of C; it was added to the language by the ANSI C committee.

Fig. 7.9 | Analysis of a typical call-by-reference with a pointer argument.

Error-Prevention Tip 7.2 Use call-by-value to pass arguments to a function unless the caller explicitly requires the called function to modify the value of the argument variable in the caller’s environment. This prevents accidental modification of the caller’s arguments and is another example of the principle of least privilege.

Software Engineering Observation 7.1 The const qualifier can be used to enforce the principle of least privilege. Using the principle of least privilege to properly design software reduces debugging time and improper side effects, making a program easier to modify and maintain.

Portability Tip 7.1 Although const is well defined in Standard C, some compilers do not enforce it.

Step 1: Before main calls cubeByReference:

int main( void ) {

int number = 5;

cubeByReference( &number ); }

125

5

number

125

number

5

number

void cubeByReference( int *nPtr ) { *nPtr = *nPtr * *nPtr * *nPtr; }

undefined

nPtr

nPtr

nPtr

Step 2: After cubeByReference receives the call and before *nPtr is cubed:

int main( void ) {

int number = 5;

cubeByReference( &number ); }

void cubeByReference( int *nPtr ) { *nPtr = *nPtr * *nPtr * *nPtr; }

Step 3: After *nPtr is cubed and before program control returns to main:

int main( void ) {

int number = 5;

cubeByReference( &number ); }

void cubeByReference( int *nPtr ) {

*nPtr = *nPtr * *nPtr * *nPtr; }

called function modifies caller’s variable

call establishes this pointer

262 Chapter 7 C Pointers

Over the years, a large base of legacy code was written in early versions of C that did not use const because it was not available. For this reason, there are significant oppor- tunities for improvement in the reengineering of old C code.

Six possibilities exist for using (or not using) const with function parameters—two with call-by-value parameter passing and four with call-by-reference parameter passing. How do you choose one of the six possibilities? Let the principle of least privilege be your guide. Always award a function enough access to the data in its parameters to accomplish its specified task, but no more.

In Chapter 5, we explained that all calls in C are call-by-value—a copy of the argu- ment in the function call is made and passed to the function. If the copy is modified in the function, the original value in the caller does not change. In many cases, a value passed to a function is modified so the function can accomplish its task. However, in some instances, the value should not be altered in the called function, even though it manipulates only a copy of the original value.

Consider a function that takes a single-subscripted array and its size as arguments and prints the array. Such a function should loop through the array and output each array ele- ment individually. The size of the array is used in the function body to determine the high subscript of the array, so the loop can terminate when the printing is completed. Neither the size of the array nor its contents should change in the function body.

If an attempt is made to modify a value that is declared const, the compiler catches it and issues either a warning or an error, depending on the particular compiler.

There are four ways to pass a pointer to a function: a non-constant pointer to non- constant data, a constant pointer to nonconstant data, a non-constant pointer to con- stant data, and a constant pointer to constant data. Each of the four combinations pro- vides different access privileges. These are discussed in the next several examples.

Error-Prevention Tip 7.3 If a variable does not (or should not) change in the body of a function to which it’s passed, the variable should be declared const to ensure that it’s not accidentally modified.

Software Engineering Observation 7.2 Only one value in a calling function can be altered when using call-by-value. That value must be assigned from the return value of the function to a variable in the caller. To modify multiple variables from a calling function in a called function, use call-by-reference.

Error-Prevention Tip 7.4 Before using a function, check its function prototype to determine if the function is able to modify the values passed to it.

Common Programming Error 7.4 Being unaware that a function is expecting pointers as arguments for call-by-reference and passing arguments call-by-value. Some compilers take the values assuming they’re pointers and dereference the values as pointers. At runtime, memory-access violations or segmenta- tion faults are often generated. Other compilers catch the mismatch in types between ar- guments and parameters and generate error messages.

7.5 Using the const Qualifier with Pointers 263

Converting a String to Uppercase Using a Non-Constant Pointer to Non-Constant Data The highest level of data access is granted by a non-constant pointer to non-constant data. In this case, the data can be modified through the dereferenced pointer, and the pointer can be modified to point to other data items. A declaration for a non-constant pointer to non-constant data does not include const. Such a pointer might be used to receive a string as an argument to a function that uses pointer arithmetic to process (and possibly modify) each character in the string. Function convertToUppercase of Fig. 7.10 declares its pa- rameter, a non-constant pointer to non-constant data called sPtr (char *sPtr), in line 21. The function processes the array string (pointed to by sPtr) one character at a time using pointer arithmetic. C standard library function islower (called in line 25) tests the char- acter contents of the address pointed to by sPtr. If a character is in the range a to z, is- lower returns true and C standard library function toupper (line 26) is called to convert the character to its corresponding uppercase letter; otherwise, islower returns false and the next character in the string is processed. Line 29 moves the pointer to the next charac- ter in the string. Pointer arithmetic will be discussed in more detail in Section 7.8.

1 /* Fig. 7.10: fig07_10.c 2 Converting a string to uppercase using a 3 non-constant pointer to non-constant data */ 4 5 #include <stdio.h> 6 #include <ctype.h> 7 8 void convertToUppercase( ); /* prototype */ 9

10 int main( void ) 11 { 12 char string[] = "characters and $32.98"; /* initialize char array */ 13 14 printf( "The string before conversion is: %s", string ); 15 convertToUppercase( string ); 16 printf( "\nThe string after conversion is: %s\n", string ); 17 return 0; /* indicates successful termination */ 18 } /* end main */ 19 20 /* convert string to uppercase letters */ 21 void convertToUppercase( ) 22 { 23 while ( ) { /* current character is not '\0' */ 24 25 if ( ) { /* if character is lowercase, */ 26 /* convert to uppercase */ 27 } /* end if */ 28 29 /* move sPtr to the next character */ 30 } /* end while */ 31 } /* end function convertToUppercase */

Fig. 7.10 | Converting a string to uppercase using a non-constant pointer to non-constant data. (Part 1 of 2.)

char *sPtr

char *sPtr

*sPtr != '\0'

islower( *sPtr ) *sPtr = toupper( *sPtr );

++sPtr;

264 Chapter 7 C Pointers

Printing a String One Character at a Time Using a Non-Constant Pointer to Constant Data A non-constant pointer to constant data can be modified to point to any data item of the appropriate type, but the data to which it points cannot be modified. Such a pointer might be used to receive an array argument to a function that will process each element without modifying the data. For example, function printCharacters (Fig. 7.11) declares param- eter sPtr to be of type const char * (line 22). The declaration is read from right to left as “sPtr is a pointer to a character constant.” The function uses a for statement to output each character in the string until the null character is encountered. After each character is printed, pointer sPtr is incremented to point to the next character in the string.

The string before conversion is: characters and $32.98 The string after conversion is: CHARACTERS AND $32.98

1 /* Fig. 7.11: fig07_11.c 2 Printing a string one character at a time using 3 a non-constant pointer to constant data */ 4 5 #include <stdio.h> 6 7 void printCharacters( ); 8 9 int main( void )

10 { 11 /* initialize char array */ 12 char string[] = "print characters of a string"; 13 14 printf( "The string is:\n" ); 15 printCharacters( string ); 16 printf( "\n" ); 17 return 0; /* indicates successful termination */ 18 } /* end main */ 19 20 /* sPtr cannot modify the character to which it points, 21 i.e., sPtr is a "read-only" pointer */ 22 void printCharacters( const char *sPtr ) 23 { 24 25 26 27 28 } /* end function printCharacters */

The string is: print characters of a string

Fig. 7.11 | Printing a string one character at a time using a non-constant pointer to constant data.

Fig. 7.10 | Converting a string to uppercase using a non-constant pointer to non-constant data. (Part 2 of 2.)

const char *sPtr

/* loop through entire string */ for ( ; *sPtr != '\0'; sPtr++ ) { /* no initialization */ printf( "%c", *sPtr ); } /* end for */

7.5 Using the const Qualifier with Pointers 265

Figure 7.12 illustrates the attempt to compile a function that receives a non-constant pointer (xPtr) to constant data. This function attempts to modify the data pointed to by xPtr in line 20—which results in a compilation error. [Note: The actual error message you see will be compiler specific.]

As we know, arrays are aggregate data types that store related data items of the same type under one name. In Chapter 10, we’ll discuss another form of aggregate data type called a structure (sometimes called a record in other languages). A structure is capable of storing related data items of different data types under one name (e.g., storing information about each employee of a company). When a function is called with an array as an argu- ment, the array is automatically passed to the function by reference. However, structures are always passed by value—a copy of the entire structure is passed. This requires the exe- cution-time overhead of making a copy of each data item in the structure and storing it on the computer’s function call stack. When structure data must be passed to a function, we can use pointers to constant data to get the performance of call-by-reference and the pro- tection of call-by-value. When a pointer to a structure is passed, only a copy of the address at which the structure is stored must be made. On a machine with 4-byte addresses, a copy of 4 bytes of memory is made rather than a copy of possibly hundreds or thousands of bytes of the structure.

1 /* Fig. 7.12: fig07_12.c 2 Attempting to modify data through a 3 non-constant pointer to constant data. */ 4 #include <stdio.h> 5 void f( const int *xPtr ); /* prototype */ 6 7 8 int main( void ) 9 {

10 int y; /* define y */ 11 12 13 return 0; /* indicates successful termination */ 14 } /* end main */ 15 16 /* xPtr cannot be used to modify the 17 value of the variable to which it points */ 18 void f( ) 19 { 20 21 } /* end function f */

Compiling... FIG07_12.c c:\examples\ch07\fig07_12.c(22) : error C2166: l-value specifies const object Error executing cl.exe. FIG07_12.exe - 1 error(s), 0 warning(s)

Fig. 7.12 | Attempting to modify data through a non-constant pointer to constant data.

f( &y ); /* f attempts illegal modification */

const int *xPtr

*xPtr = 100; /* error: cannot modify a const object */

266 Chapter 7 C Pointers

Using pointers to constant data in this manner is an example of a time/space trade- off. If memory is low and execution efficiency is a concern, use pointers. If memory is in abundance and efficiency is not a major concern, pass data by value to enforce the prin- ciple of least privilege. Remember that some systems do not enforce const well, so call-by- value is still the best way to prevent data from being modified.

Attempting to Modify a Constant Pointer to Non-Constant Data A constant pointer to non-constant data always points to the same memory location, and the data at that location can be modified through the pointer. This is the default for an array name. An array name is a constant pointer to the beginning of the array. All data in the array can be accessed and changed by using the array name and array subscripting. A constant pointer to non-constant data can be used to receive an array as an argument to a function that accesses array elements using only array subscript notation. Pointers that are declared const must be initialized when they’re defined (if the pointer is a function pa- rameter, it’s initialized with a pointer that is passed to the function). Figure 7.13 attempts to modify a constant pointer. Pointer ptr is defined in line 12 to be of type int * const. The definition is read from right to left as “ptr is a constant pointer to an integer.” The pointer is initialized (line 12) with the address of integer variable x. The program attempts to assign the address of y to ptr (line 15), but the compiler generates an error message.

Performance Tip 7.1 Pass large objects such as structures using pointers to constant data to obtain the perfor- mance benefits of call-by-reference and the security of call-by-value.

1 /* Fig. 7.13: fig07_13.c 2 Attempting to modify a constant pointer to non-constant data */ 3 #include <stdio.h> 4 5 int main( void ) 6 { 7 int x; /* define x */ 8 int y; /* define y */ 9

10 11 12 13 14 *ptr = 7; /* allowed: *ptr is not const */ 15 16 return 0; /* indicates successful termination */ 17 } /* end main */

Compiling... FIG07_13.c c:\examples\ch07\FIG07_13.c(15) : error C2166: l-value specifies const object Error executing cl.exe.

FIG07_13.exe - 1 error(s), 0 warning(s)

Fig. 7.13 | Attempting to modify a constant pointer to non-constant data.

/* ptr is a constant pointer to an integer that can be modified through ptr, but ptr always points to the same memory location */ int * const ptr = &x;

ptr = &y; /* error: ptr is const; cannot assign new address */

7.6 Bubble Sort Using Call-by-Reference 267

Attempting to Modify a Constant Pointer to Constant Data The least access privilege is granted by a constant pointer to constant data. Such a pointer always points to the same memory location, and the data at that memory location cannot be modified. This is how an array should be passed to a function that only looks at the array using array subscript notation and does not modify the array. Figure 7.14 defines pointer variable ptr (line 13) to be of type const int *const, which is read from right to left as “ptr is a constant pointer to an integer constant.” The figure shows the error mes- sages generated when an attempt is made to modify the data to which ptr points (line 16) and when an attempt is made to modify the address stored in the pointer variable (line 17).

7.6 Bubble Sort Using Call-by-Reference Let’s improve the bubble sort program of Fig. 6.15 to use two functions—bubbleSort and swap. Function bubbleSort sorts the array. It calls function swap (line 51) to exchange the array elements array[j] and array[j + 1] (see Fig. ). Remember that C enforces infor- mation hiding between functions, so swap does not have access to individual array ele- ments in bubbleSort. Because bubbleSort wants swap to have access to the array elements to be swapped, bubbleSort passes each of these elements call-by-reference to swap—the address of each array element is passed explicitly. Although entire arrays are automatically passed by reference, individual array elements are scalars and are ordinarily passed by val-

1 /* Fig. 7.14: fig07_14.c 2 Attempting to modify a constant pointer to constant data. */ 3 #include <stdio.h> 4 5 int main( void ) 6 { 7 int x = 5; /* initialize x */ 8 int y; /* define y */ 9

10 /* ptr is a constant pointer to a constant integer. ptr always 11 points to the same location; the integer at that location 12 cannot be modified */ 13 14 15 printf( "%d\n", *ptr ); 16 17 18 return 0; /* indicates successful termination */ 19 } /* end main */

Compiling... FIG07_14.c c:\examples\ch07\FIG07_14.c(17) : error C2166: l-value specifies const object c:\examples\ch07\FIG07_14.c(18) : error C2166: l-value specifies const object Error executing cl.exe.

FIG07_12.exe - 2 error(s), 0 warning(s)

Fig. 7.14 | Attempting to modify a constant pointer to constant data.

const int *const ptr = &x;

*ptr = 7; /* error: *ptr is const; cannot assign new value */ ptr = &y; /* error: ptr is const; cannot assign new address */

268 Chapter 7 C Pointers

ue. Therefore, bubbleSort uses the address operator (&) on each of the array elements in the swap call (line 51) to effect call-by-reference as follows

Function swap receives &array[j] in pointer variable element1Ptr (line 59). Even though swap—because of information hiding—is not allowed to know the name array[j], swap may use *element1Ptr as a synonym for array[j]—when swap references *element1Ptr, it’s actually referencing array[j] in bubbleSort. Similarly, when swap references *element2Ptr, it’s actually referencing array[j + 1] in bubbleSort. Even though swap is not allowed to say

precisely the same effect is achieved by lines 61 through 63

swap( &array[ j ], &array[ j + 1 ] );

hold = array[ j ]; array[ j ] = array[ j + 1 ]; array[ j + 1 ] = hold;

int hold = *element1Ptr; *element1Ptr = *element2Ptr; *element2Ptr = hold;

1 /* Fig. 7.15: fig07_15.c 2 This program puts values into an array, sorts the values into 3 ascending order, and prints the resulting array. */ 4 #include <stdio.h> 5 #define SIZE 10 6 7 void bubbleSort( int * const array, const int size ); /* prototype */ 8 9 int main( void )

10 { 11 /* initialize array a */ 12 int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; 13 14 int i; /* counter */ 15 16 printf( "Data items in original order\n" ); 17 18 /* loop through array a */ 19 for ( i = 0; i < SIZE; i++ ) { 20 printf( "%4d", a[ i ] ); 21 } /* end for */ 22 23 bubbleSort( a, SIZE ); /* sort the array */ 24 25 printf( "\nData items in ascending order\n" ); 26 27 /* loop through array a */ 28 for ( i = 0; i < SIZE; i++ ) { 29 printf( "%4d", a[ i ] ); 30 } /* end for */ 31

Fig. 7.15 | Bubble sort with call-by-reference. (Part 1 of 2.)

7.6 Bubble Sort Using Call-by-Reference 269

Several features of function bubbleSort should be noted. The function header (line 37) declares array as int * const array rather than int array[] to indicate that bubble- Sort receives a single-subscripted array as an argument (again, these notations are inter- changeable). Parameter size is declared const to enforce the principle of least privilege. Although parameter size receives a copy of a value in main, and modifying the copy cannot change the value in main, bubbleSort does not need to alter size to accomplish its task. The size of the array remains fixed during the execution of function bubbleSort. Therefore, size is declared const to ensure that it’s not modified. If the size of the array is modified during the sorting process, the sorting algorithm might not run correctly.

The prototype for function swap (line 39) is included in the body of function bub- bleSort because bubbleSort is the only function that calls swap. Placing the prototype in

32 printf( "\n" ); 33 return 0; /* indicates successful termination */ 34 } /* end main */ 35 36 /* sort an array of integers using bubble sort algorithm */ 37 void bubbleSort( int * const array, const int size ) 38 { 39 40 int pass; /* pass counter */ 41 int j; /* comparison counter */ 42 43 /* loop to control passes */ 44 for ( pass = 0; pass < size - 1; pass++ ) { 45 46 /* loop to control comparisons during each pass */ 47 for ( j = 0; j < size - 1; j++ ) { 48 49 /* swap adjacent elements if they are out of order */ 50 if ( array[ j ] > array[ j + 1 ] ) { 51 52 } /* end if */ 53 } /* end inner for */ 54 } /* end outer for */ 55 } /* end function bubbleSort */ 56 57 58 59 60 61 62 63 64

Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items in ascending order 2 4 6 8 10 12 37 45 68 89

Fig. 7.15 | Bubble sort with call-by-reference. (Part 2 of 2.)

void swap( int *element1Ptr, int *element2Ptr ); /* prototype */

swap( &array[ j ], &array[ j + 1 ] );

/* swap values at memory locations to which element1Ptr and element2Ptr point */ void swap( int *element1Ptr, int *element2Ptr ) { int hold = *element1Ptr; *element1Ptr = *element2Ptr; *element2Ptr = hold; } /* end function swap */

270 Chapter 7 C Pointers

bubbleSort restricts proper calls of swap to those made from bubbleSort. Other functions that attempt to call swap do not have access to a proper function prototype, so the com- piler generates one automatically. This normally results in a prototype that does not match the function header (and generates a compilation warning or error) because the compiler assumes int for the return type and the parameter types.

Function bubbleSort receives the size of the array as a parameter (line 37). The func- tion must know the size of the array to sort the array. When an array is passed to a func- tion, the memory address of the first element of the array is received by the function. The address, of course, does not convey the number of elements in the array. Therefore, you must pass to the function the array size. [Note: Another common practice is to pass a pointer to the beginning of the array and a pointer to the location just beyond the end of the array. The difference of the two pointers is the length of the array and the resulting code is simpler.]

In the program, the size of the array is explicitly passed to function bubbleSort. There are two main benefits to this approach—software reusability and proper software engi- neering. By defining the function to receive the array size as an argument, we enable the function to be used by any program that sorts single-subscripted integer arrays of any size.

We could have stored the array’s size in a global variable that is accessible to the entire program. This would be more efficient, because a copy of the size is not made to pass to the function. However, other programs that require an integer array-sorting capability may not have the same global variable, so the function cannot be used in those programs.

The size of the array could have been programmed directly into the function. This restricts the use of the function to an array of a specific size and significantly reduces its reusability. Only programs processing single-subscripted integer arrays of the specific size coded into the function can use the function.

7.7 sizeof Operator C provides the special unary operator sizeof to determine the size in bytes of an array (or any other data type) during program compilation. When applied to the name of an array as in Fig. 7.16 (line 14), the sizeof operator returns the total number of bytes in the array

Software Engineering Observation 7.3 Placing function prototypes in the definitions of other functions enforces the principle of least privilege by restricting proper function calls to the functions in which the prototypes appear.

Software Engineering Observation 7.4 When passing an array to a function, also pass the size of the array. This helps make the function reusable in many programs.

Software Engineering Observation 7.5 Global variables usually violate the principle of least privilege and can lead to poor software engineering. Global variables should be used only to represent truly shared resources, such as the time of day.

7.7 sizeof Operator 271

as an integer. Variables of type float are normally stored in 4 bytes of memory, and array is defined to have 20 elements. Therefore, there are a total of 80 bytes in array.

The number of elements in an array also can be determined with sizeof. For example, consider the following array definition:

Variables of type double normally are stored in 8 bytes of memory. Thus, array real con- tains a total of 176 bytes. To determine the number of elements in the array, the following expression can be used:

The expression determines the number of bytes in array real and divides that value by the number of bytes used in memory to store the first element of array real (a double value).

Function getSize returns type size_t. Type size_t is a type defined by the C stan- dard as the integral type (unsigned or unsigned long) of the value returned by operator sizeof. Type size_t is defined in header <stddef.h> (which is included by several headers, such as <stdio.h>). [Note: If you attempt to compile Fig. 7.16 and receive errors,

Performance Tip 7.2 sizeof is a compile-time operator, so it does not incur any execution-time overhead.

1 /* Fig. 7.16: fig07_16.c 2 Applying sizeof to an array name returns 3 the number of bytes in the array. */ 4 #include <stdio.h> 5 6 size_t getSize( ); /* prototype */ 7 8 int main( void ) 9 {

10 float array[ 20 ]; /* create array */ 11 12 13 14 15 return 0; /* indicates successful termination */ 16 } /* end main */ 17 18 19 size_t getSize( ) 20 { 21 22 } /* end function getSize */

The number of bytes in the array is 80 The number of bytes returned by getSize is 4

Fig. 7.16 | Applying sizeof to an array name returns the number of bytes in the array.

double real[ 22 ];

sizeof( real ) / sizeof( real[ 0 ] )

float *ptr

printf( "The number of bytes in the array is %d" "\nThe number of bytes returned by getSize is %d\n", sizeof( array ), getSize( array ) );

/* return size of ptr */ float *ptr

return sizeof( ptr );

272 Chapter 7 C Pointers

simply include <stddef.h> in your program.] Figure 7.17 calculates the number of bytes used to store each of the standard data types. The results could be different between com- puters.

Portability Tip 7.2 The number of bytes used to store a particular data type may vary between systems. When writing programs that depend on data type sizes and that will run on several computer systems, use sizeof to determine the number of bytes used to store the data types.

1 /* Fig. 7.17: fig07_17.c 2 Demonstrating the sizeof operator */ 3 #include <stdio.h> 4 5 int main( void ) 6 { 7 char c; 8 short s; 9 int i;

10 long l; 11 float f; 12 double d; 13 long double ld; 14 int array[ 20 ]; /* create array of 20 int elements */ 15 int *ptr = array; /* create pointer to array */ 16 17 printf( " sizeof c = %d\tsizeof(char) = %d" 18 "\n sizeof s = %d\tsizeof(short) = %d" 19 "\n sizeof i = %d\tsizeof(int) = %d" 20 "\n sizeof l = %d\tsizeof(long) = %d" 21 "\n sizeof f = %d\tsizeof(float) = %d" 22 "\n sizeof d = %d\tsizeof(double) = %d" 23 "\n sizeof ld = %d\tsizeof(long double) = %d" 24 "\n sizeof array = %d" 25 "\n sizeof ptr = %d\n", 26 27 28 29 30 return 0; /* indicates successful termination */ 31 } /* end main */

sizeof c = 1 sizeof(char) = 1 sizeof s = 2 sizeof(short) = 2 sizeof i = 4 sizeof(int) = 4 sizeof l = 4 sizeof(long) = 4 sizeof f = 4 sizeof(float) = 4 sizeof d = 8 sizeof(double) = 8 sizeof ld = 8 sizeof(long double) = 8 sizeof array = 80 sizeof ptr = 4

Fig. 7.17 | Using operator sizeof to determine standard data type sizes.

sizeof c, sizeof( char ), sizeof s, sizeof( short ), sizeof i, sizeof( int ), sizeof l, sizeof( long ), sizeof f, sizeof( float ), sizeof d, sizeof( double ), sizeof ld, sizeof( long double ), sizeof array, sizeof ptr );

7.8 Pointer Expressions and Pointer Arithmetic 273

Operator sizeof can be applied to any variable name, type or value (including the value of an expression). When applied to a variable name (that is not an array name) or a constant, the number of bytes used to store the specific type of variable or constant is returned. The parentheses used with sizeof are required if a type name with two words is supplied as its operand (such as long double or unsigned short). Omitting the paren- theses in this case results in a syntax error. The parentheses are not required if a variable name or a one-word type name is supplied as its operand, but they can still be included without causing an error.

7.8 Pointer Expressions and Pointer Arithmetic Pointers are valid operands in arithmetic expressions, assignment expressions and com- parison expressions. However, not all the operators normally used in these expressions are valid in conjunction with pointer variables. This section describes the operators that can have pointers as operands, and how these operators are used.

A limited set of arithmetic operations may be performed on pointers. A pointer may be incremented (++) or decremented (--), an integer may be added to a pointer (+ or +=), an integer may be subtracted from a pointer (- or -=) and one pointer may be subtracted from another.

Assume that array int v[5] has been defined and its first element is at location 3000 in memory. Assume pointer vPtr has been initialized to point to v[0]—i.e., the value of vPtr is 3000. Figure 7.18 illustrates this situation for a machine with 4-byte integers. Vari- able vPtr can be initialized to point to array v with either of the statements

In conventional arithmetic, 3000 + 2 yields the value 3002. This is normally not the case with pointer arithmetic. When an integer is added to or subtracted from a pointer, the pointer is not incremented or decremented simply by that integer, but by that integer

vPtr = v; vPtr = &v[ 0 ];

Portability Tip 7.3 Most computers today have 2-byte or 4-byte integers. Some of the newer machines use 8- byte integers. Because the results of pointer arithmetic depend on the size of the objects a pointer points to, pointer arithmetic is machine dependent.

Fig. 7.18 | Array v and a pointer variable vPtr that points to v.

pointer variable vPtr

v[0] v[1] v[2] v[3] v[4]

3000 location

3004 3008 3012 3016

274 Chapter 7 C Pointers

times the size of the object to which the pointer refers. The number of bytes depends on the object’s data type. For example, the statement

would produce 3008 (3000 + 2 * 4), assuming an integer is stored in 4 bytes of memory. In the array v, vPtr would now point to v[2] (Fig. 7.19). If an integer is stored in 2 bytes of memory, then the preceding calculation would result in memory location 3004 (3000 + 2 * 2). If the array were of a different data type, the preceding statement would increment the pointer by twice the number of bytes that it takes to store an object of that data type. When performing pointer arithmetic on a character array, the results will be consistent with regular arithmetic, because each character is 1 byte long.

If vPtr had been incremented to 3016, which points to v[4], the statement

would set vPtr back to 3000—the beginning of the array. If a pointer is being incremented or decremented by one, the increment (++) and decrement (--) operators can be used. Ei- ther of the statements

increments the pointer to point to the next location in the array. Either of the statements

decrements the pointer to point to the previous element of the array. Pointer variables may be subtracted from one another. For example, if vPtr contains

the location 3000, and v2Ptr contains the address 3008, the statement

would assign to x the number of array elements from vPtr to v2Ptr, in this case 2 (not 8). Pointer arithmetic is meaningless unless performed on an array. We cannot assume that two variables of the same type are stored contiguously in memory unless they’re adjacent elements of an array.

vPtr += 2;

Fig. 7.19 | The pointer vPtr after pointer arithmetic.

vPtr -= 4;

++vPtr; vPtr++;

--vPtr; vPtr--;

x = v2Ptr - vPtr;

pointer variable vPtr

v[0] v[1] v[2] v[3] v[4]

3000 location

3004 3008 3012 3016

7.9 Relationship between Pointers and Arrays 275

A pointer can be assigned to another pointer if both have the same type. The exception to this rule is the pointer to void (i.e., void *), which is a generic pointer that can represent any pointer type. All pointer types can be assigned a pointer to void, and a pointer to void can be assigned a pointer of any type. In both cases, a cast operation is not required.

A pointer to void cannot be dereferenced. Consider this: The compiler knows that a pointer to int refers to 4 bytes of memory on a machine with 4-byte integers, but a pointer to void simply contains a memory location for an unknown data type—the precise number of bytes to which the pointer refers is not known by the compiler. The compiler must know the data type to determine the number of bytes to be dereferenced for a par- ticular pointer.

Pointers can be compared using equality and relational operators, but such compar- isons are meaningless unless the pointers point to elements of the same array. Pointer com- parisons compare the addresses stored in the pointers. A comparison of two pointers pointing to elements in the same array could show, for example, that one pointer points to a higher-numbered element of the array than the other pointer does. A common use of pointer comparison is determining whether a pointer is NULL.

7.9 Relationship between Pointers and Arrays Arrays and pointers are intimately related in C and often may be used interchangeably. An array name can be thought of as a constant pointer. Pointers can be used to do any oper- ation involving array subscripting.

Assume that integer array b[5] and integer pointer variable bPtr have been defined. Since the array name (without a subscript) is a pointer to the first element of the array, we can set bPtr equal to the address of the first element in array b with the statement

Common Programming Error 7.5 Using pointer arithmetic on a pointer that does not refer to an element in an array.

Common Programming Error 7.6 Subtracting or comparing two pointers that do not refer to elements in the same array.

Common Programming Error 7.7 Running off either end of an array when using pointer arithmetic.

Common Programming Error 7.8 Assigning a pointer of one type to a pointer of another type if neither is of type void * is a syntax error.

Common Programming Error 7.9 Dereferencing a void * pointer is a syntax error.

bPtr = b;

276 Chapter 7 C Pointers

This statement is equivalent to taking the address of the array’s first element as follows:

Array element b[3] can alternatively be referenced with the pointer expression

The 3 in the above expression is the offset to the pointer. When the pointer points to the beginning of an array, the offset indicates which element of the array should be referenced, and the offset value is identical to the array subscript. The preceding notation is referred to as pointer/offset notation. The parentheses are necessary because the precedence of * is higher than the precedence of +. Without the parentheses, the above expression would add 3 to the value of the expression *bPtr (i.e., 3 would be added to b[0], assuming bPtr points to the beginning of the array). Just as the array element can be referenced with a pointer expression, the address

can be written with the pointer expression

The array itself can be treated as a pointer and used in pointer arithmetic. For example, the expression

also refers to the array element b[3]. In general, all subscripted array expressions can be written with a pointer and an offset. In this case, pointer/offset notation was used with the name of the array as a pointer. The preceding statement does not modify the array name in any way; b still points to the first element in the array.

Pointers can be subscripted exactly as arrays can. For example, if bPtr has the value b, the expression

refers to the array element b[1]. This is referred to as pointer/subscript notation. Remember that an array name is essentially a constant pointer; it always points to the

beginning of the array. Thus, the expression

is invalid because it attempts to modify the value of the array name with pointer arith- metic.

Figure 7.20 uses the four methods we have discussed for referring to array elements— array subscripting, pointer/offset with the array name as a pointer, pointer subscripting, and pointer/offset with a pointer—to print the four elements of the integer array b.

bPtr = &b[ 0 ];

*( bPtr + 3 )

&b[ 3 ]

bPtr + 3

*( b + 3 )

bPtr[ 1 ]

b += 3

Common Programming Error 7.10 Attempting to modify an array name with pointer arithmetic is a syntax error.

7.9 Relationship between Pointers and Arrays 277

1 /* Fig. 7.20: fig07_20.cpp 2 Using subscripting and pointer notations with arrays */ 3 4 #include <stdio.h> 5 6 int main( void ) 7 { 8 int b[] = { 10, 20, 30, 40 }; /* initialize array b */ 9 int *bPtr = b; /* set bPtr to point to array b */

10 int i; /* counter */ 11 int offset; /* counter */ 12 13 /* output array b using array subscript notation */ 14 printf( "Array b printed with:\nArray subscript notation\n" ); 15 16 /* loop through array b */ 17 for ( i = 0; i < 4; i++ ) { 18 printf( "b[ %d ] = %d\n", i, ); 19 } /* end for */ 20 21 /* output array b using array name and pointer/offset notation */ 22 printf( "\nPointer/offset notation where\n" 23 "the pointer is the array name\n" ); 24 25 /* loop through array b */ 26 for ( offset = 0; offset < 4; offset++ ) { 27 printf( "*( b + %d ) = %d\n", offset, *( ) ); 28 } /* end for */ 29 30 /* output array b using bPtr and array subscript notation */ 31 printf( "\nPointer subscript notation\n" ); 32 33 /* loop through array b */ 34 for ( i = 0; i < 4; i++ ) { 35 printf( "bPtr[ %d ] = %d\n", i, ); 36 } /* end for */ 37 38 /* output array b using bPtr and pointer/offset notation */ 39 printf( "\nPointer/offset notation\n" ); 40 41 /* loop through array b */ 42 for ( offset = 0; offset < 4; offset++ ) { 43 printf( "*( bPtr + %d ) = %d\n", offset, *( ) ); 44 } /* end for */ 45 46 return 0; /* indicates successful termination */ 47 } /* end main */

Array b printed with: Array subscript notation b[ 0 ] = 10 b[ 1 ] = 20

Fig. 7.20 | Using four methods of referencing array elements. (Part 1 of 2.)

b[ i ]

b + offset

bPtr[ i ]

bPtr + offset

278 Chapter 7 C Pointers

To further illustrate the interchangeability of arrays and pointers, let’s look at the two string-copying functions—copy1 and copy2—in the program of Fig. 7.21. Both functions copy a string (possibly a character array) into a character array. After a comparison of the function prototypes for copy1 and copy2, the functions appear identical. They accomplish the same task; however, they’re implemented differently.

b[ 2 ] = 30 b[ 3 ] = 40 Pointer/offset notation where the pointer is the array name *( b + 0 ) = 10 *( b + 1 ) = 20 *( b + 2 ) = 30 *( b + 3 ) = 40

Pointer subscript notation bPtr[ 0 ] = 10 bPtr[ 1 ] = 20 bPtr[ 2 ] = 30 bPtr[ 3 ] = 40

Pointer/offset notation *( bPtr + 0 ) = 10 *( bPtr + 1 ) = 20 *( bPtr + 2 ) = 30 *( bPtr + 3 ) = 40

1 /* Fig. 7.21: fig07_21.c 2 Copying a string using array notation and pointer notation. */ 3 #include <stdio.h> 4 5 void copy1( char * const s1, const char * const s2 ); /* prototype */ 6 void copy2( char *s1, const char *s2 ); /* prototype */ 7 8 int main( void ) 9 {

10 char string1[ 10 ]; /* create array string1 */ 11 char *string2 = "Hello"; /* create a pointer to a string */ 12 char string3[ 10 ]; /* create array string3 */ 13 char string4[] = "Good Bye"; /* create a pointer to a string */ 14 15 copy1( string1, string2 ); 16 printf( "string1 = %s\n", string1 ); 17 18 copy2( string3, string4 ); 19 printf( "string3 = %s\n", string3 ); 20 return 0; /* indicates successful termination */ 21 } /* end main */

Fig. 7.21 | Copying a string using array notation and pointer notation. (Part 1 of 2.)

Fig. 7.20 | Using four methods of referencing array elements. (Part 2 of 2.)

7.9 Relationship between Pointers and Arrays 279

Function copy1 uses array subscript notation to copy the string in s2 to the character array s1. The function defines counter variable i as the array subscript. The for statement header (line 29) performs the entire copy operation—its body is the empty statement. The header specifies that i is initialized to zero and incremented by one on each iteration of the loop. The expression s1[i] = s2[i] copies one character from s2 to s1. When the null character is encountered in s2, it’s assigned to s1, and the value of the assignment becomes the value assigned to the left operand (s1). The loop terminates because the integer value of the null character is zero (false).

Function copy2 uses pointers and pointer arithmetic to copy the string in s2 to the character array s1. Again, the for statement header (line 38) performs the entire copy operation. The header does not include any variable initialization. As in function copy1, the expression (*s1 = *s2) performs the copy operation. Pointer s2 is dereferenced, and the resulting character is assigned to the dereferenced pointer *s1. After the assignment in the condition, the pointers are incremented to point to the next element of array s1 and the next character of string s2, respectively. When the null character is encountered in s2, it’s assigned to the dereferenced pointer s1 and the loop terminates.

The first argument to both copy1 and copy2 must be an array large enough to hold the string in the second argument. Otherwise, an error may occur when an attempt is made to write into a memory location that is not part of the array. Also, the second param- eter of each function is declared as const char * (a constant string). In both functions, the second argument is copied into the first argument—characters are read from it one at a time, but the characters are never modified. Therefore, the second parameter is declared to point to a constant value so that the principle of least privilege is enforced—neither

22 23 24 void copy1( char * const s1, const char * const s2 ) 25 { 26 int i; /* counter */ 27 28 /* loop through strings */ 29 30 31 32 } /* end function copy1 */ 33 34 35 void copy2( char *s1, const char *s2 ) 36 { 37 /* loop through strings */ 38 39 40 41 } /* end function copy2 */

string1 = Hello string3 = Good Bye

Fig. 7.21 | Copying a string using array notation and pointer notation. (Part 2 of 2.)

/* copy s2 to s1 using array notation */

for ( i = 0; ( s1[ i ] = s2[ i ] ) != '\0'; i++ ) { ; /* do nothing in body */ } /* end for */

/* copy s2 to s1 using pointer notation */

for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) { ; /* do nothing in body */ } /* end for */

280 Chapter 7 C Pointers

function requires the capability of modifying the second argument, so neither function is provided with that capability.

7.10 Arrays of Pointers Arrays may contain pointers. A common use of an array of pointers is to form an array of strings, referred to simply as a string array. Each entry in the array is a string, but in C a string is essentially a pointer to its first character. So each entry in an array of strings is ac- tually a pointer to the first character of a string. Consider the definition of string array suit, which might be useful in representing a deck of cards.

The suit[4] portion of the definition indicates an array of 4 elements. The char * por- tion of the declaration indicates that each element of array suit is of type “pointer to char.” Qualifier const indicates that the strings pointed to by each element pointer will not be modified. The four values to be placed in the array are "Hearts", "Diamonds", "Clubs" and "Spades". Each is stored in memory as a null-terminated character string that is one character longer than the number of characters between quotes. The four strings are 7, 9, 6 and 7 characters long, respectively. Although it appears as though these strings are being placed in the suit array, only pointers are actually stored in the array (Fig. 7.22). Each pointer points to the first character of its corresponding string. Thus, even though the suit array is fixed in size, it provides access to character strings of any length. This flex- ibility is one example of C’s powerful data-structuring capabilities.

The suits could have been placed in a two-dimensional array, in which each row would represent a suit and each column would represent a letter from a suit name. Such a data structure would have to have a fixed number of columns per row, and that number would have to be as large as the largest string. Therefore, considerable memory could be wasted when a large number of strings were being stored with most strings shorter than the longest string. We use string arrays to represent a deck of cards in the next section.

7.11 Case Study: Card Shuffling and Dealing Simulation In this section, we use random number generation to develop a card shuffling and dealing simulation program. This program can then be used to implement programs that play specific card games. To reveal some subtle performance problems, we have intentionally used suboptimal shuffling and dealing algorithms. In this chapter’s exercises and in Chapter 10, we develop more efficient algorithms.

const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };

Fig. 7.22 | Graphical representation of the suit array.

'S'suit[3]

suit[2]

suit[1]

suit[0]

'p' 'a' 'd' 'e' 's' '\0'

'C' 'l' 'u' 'b' 's' '\0'

'D' 'i' 'a' 'm' 'o' 'n' 'd' 's' '\0'

'H' 'e' 'a' 'r' 't' 's' '\0'

7.11 Case Study: Card Shuffling and Dealing Simulation 281

Using the top-down, stepwise refinement approach, we develop a program that will shuffle a deck of 52 playing cards and then deal each of the 52 cards. The top-down approach is particularly useful in attacking larger, more complex problems than we have seen in the early chapters.

We use 4-by-13 double-subscripted array deck to represent the deck of playing cards (Fig. 7.23). The rows correspond to the suits—row 0 corresponds to hearts, row 1 to dia- monds, row 2 to clubs and row 3 to spades. The columns correspond to the face values of the cards—columns 0 through 9 correspond to ace through ten respectively, and columns 10 through 12 correspond to jack, queen and king. We shall load string array suit with character strings representing the four suits, and string array face with character strings representing the thirteen face values.

This simulated deck of cards may be shuffled as follows. First the array deck is cleared to zeros. Then, a row (0–3) and a column (0–12) are each chosen at random. The number 1 is inserted in array element deck[row][column] to indicate that this card is going to be the first one dealt from the shuffled deck. This process continues with the numbers 2, 3, …, 52 being randomly inserted in the deck array to indicate which cards are to be placed second, third, …, and fifty-second in the shuffled deck. As the deck array begins to fill with card numbers, it’s possible that a card will be selected twice—i.e., deck[row] [column] will be nonzero when it’s selected. This selection is simply ignored and other rows and columns are repeatedly chosen at random until an unselected card is found. Eventually, the numbers 1 through 52 will occupy the 52 slots of the deck array. At this point, the deck of cards is fully shuffled.

This shuffling algorithm could execute indefinitely if cards that have already been shuffled are repeatedly selected at random. This phenomenon is known as indefinite post- ponement. In the exercises, we discuss a better shuffling algorithm that eliminates the pos- sibility of indefinite postponement.

Fig. 7.23 | Double-subscripted array representation of a deck of cards.

Performance Tip 7.3 Sometimes an algorithm that emerges in a “natural” way can contain subtle performance problems, such as indefinite postponement. Seek algorithms that avoid indefinite post- ponement.

0 543

deck[2][12] represents the King of Clubs

Clubs King

21

1

2

0

3

Diamonds

Clubs

Hearts

Spades

6 7 98 10 11 12

A ce

Si x

Fi ve

Fo ur

Th re

e

Tw o

Se ve

n

Ei gh

t

Te n

N in

e

Ja ck

Q ue

en

Ki ng

282 Chapter 7 C Pointers

To deal the first card, we search the array for deck[row][column] equal to 1. This is accomplished with a nested for statement that varies row from 0 to 3 and column from 0 to 12. What card does that element of the array correspond to? The suit array has been preloaded with the four suits, so to get the suit, we print the character string suit[row]. Similarly, to get the face value of the card, we print the character string face[column]. We also print the character string " of ". Printing this information in the proper order enables us to print each card in the form "King of Clubs", "Ace of Diamonds" and so on.

Let’s proceed with the top-down, stepwise refinement process. The top is simply

Our first refinement yields:

“Shuffle the deck” may be expanded as follows:

“Deal 52 cards” may be expanded as follows:

Incorporating these expansions yields our complete second refinement:

“Place card number in randomly selected unoccupied slot of deck” may be expanded as:

“Find card number in deck array and print face and suit of card” may be expanded as:

Shuffle and deal 52 cards

Initialize the suit array Initialize the face array Initialize the deck array Shuffle the deck Deal 52 cards

For each of the 52 cards Place card number in randomly selected unoccupied slot of deck

For each of the 52 cards Find card number in deck array and print face and suit of card

Initialize the suit array Initialize the face array Initialize the deck array

For each of the 52 cards Place card number in randomly selected unoccupied slot of deck

For each of the 52 cards Find card number in deck array and print face and suit of card

Choose slot of deck randomly

While chosen slot of deck has been previously chosen Choose slot of deck randomly

Place card number in chosen slot of deck

For each slot of the deck array If slot contains card number

Print the face and suit of the card

7.11 Case Study: Card Shuffling and Dealing Simulation 283

Incorporating these expansions yields our third refinement:

This completes the refinement process. This program is more efficient if the shuffle and deal portions of the algorithm are combined so that each card is dealt as it’s placed in the deck. We have chosen to program these operations separately because normally cards are dealt after they’re shuffled (not while they’re shuffled).

The card shuffling and dealing program is shown in Fig. 7.24, and a sample execution is shown in Fig. 7.25. Conversion specifier %s is used to print strings of characters in the calls to printf. The corresponding argument in the printf call must be a pointer to char (or a char array). The format specification "%5s of %-8s" (line 73) prints a character string right justified in a field of five characters followed by " of " and a character string left jus- tified in a field of eight characters. The minus sign in %-8s signifies left justification.

Initialize the suit array Initialize the face array Initialize the deck array

For each of the 52 cards Choose slot of deck randomly

While slot of deck has been previously chosen Choose slot of deck randomly

Place card number in chosen slot of deck

For each of the 52 cards For each slot of deck array

If slot contains desired card number Print the face and suit of the card

1 /* Fig. 7.24: fig07_24.c 2 Card shuffling dealing program */ 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <time.h> 6 7 /* prototypes */ 8 void shuffle( int wDeck[][ 13 ] ); 9 void deal( const int wDeck[][ 13 ], const char *wFace[],

10 const char *wSuit[] ); 11 12 int main( void ) 13 { 14 15 16 17 18 19 20 21

Fig. 7.24 | Card dealing program. (Part 1 of 3.)

/* initialize suit array */ const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };

/* initialize face array */ const char *face[ 13 ] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

284 Chapter 7 C Pointers

22 23 /* initialize deck array */ 24 int deck[ 4 ][ 13 ] = { 0 }; 25 26 srand( time( 0 ) ); /* seed random-number generator */ 27 28 shuffle( deck ); /* shuffle the deck */ 29 deal( deck, face, suit ); /* deal the deck */ 30 return 0; /* indicates successful termination */ 31 } /* end main */ 32 33 /* shuffle cards in deck */ 34 void shuffle( int wDeck[][ 13 ] ) 35 { 36 int row; /* row number */ 37 int column; /* column number */ 38 int card; /* counter */ 39 40 /* for each of the 52 cards, choose slot of deck randomly */ 41 for ( card = 1; card <= 52; card++ ) { 42 43 44 45 46 47 48 49 /* place card number in chosen slot of deck */ 50 wDeck[ row ][ column ] = card; 51 } /* end for */ 52 } /* end function shuffle */ 53 54 /* deal cards in deck */ 55 void deal( const int wDeck[][ 13 ], const char *wFace[], 56 const char *wSuit[] ) 57 { 58 int card; /* card counter */ 59 int row; /* row counter */ 60 int column; /* column counter */ 61 62 /* deal each of the 52 cards */ 63 for ( card = 1; card <= 52; card++ ) { 64 /* loop through rows of wDeck */ 65 66 for ( row = 0; row <= 3; row++ ) { 67 68 /* loop through columns of wDeck for current row */ 69 for ( column = 0; column <= 12; column++ ) { 70 71 /* if slot contains current card, display card */ 72 if ( wDeck[ row ][ column ] == card ) { 73 74

Fig. 7.24 | Card dealing program. (Part 2 of 3.)

/* choose new random location until unoccupied slot found */ do { row = rand() % 4; column = rand() % 13; } while( wDeck[ row ][ column ] != 0 ); /* end do...while */

printf( "%5s of %-8s%c", wFace[ column ], wSuit[ row ], card % 2 == 0 ? '\n' : '\t' );

7.12 Pointers to Functions 285

There’s a weakness in the dealing algorithm. Once a match is found, the two inner for statements continue searching the remaining elements of deck for a match. We correct this deficiency in this chapter’s exercises and in a Chapter 10 case study.

7.12 Pointers to Functions A pointer to a function contains the address of the function in memory. In Chapter 6, we saw that an array name is really the address in memory of the first element of the array. Similarly, a function name is really the starting address in memory of the code that per- forms the function’s task. Pointers to functions can be passed to functions, returned from functions, stored in arrays and assigned to other function pointers.

To illustrate the use of pointers to functions, Fig. 7.26 presents a modified version of the bubble sort program in Fig. 7.15. The new version consists of main and functions bubble, swap, ascending and descending. Function bubbleSort receives a pointer to a function—either function ascending or function descending—as an argument, in addi-

75 } /* end if */ 76 } /* end for */ 77 } /* end for */ 78 } /* end for */ 79 } /* end function deal */

Nine of Hearts Five of Clubs Queen of Spades Three of Spades Queen of Hearts Ace of Clubs King of Hearts Six of Spades Jack of Diamonds Five of Spades Seven of Hearts King of Clubs Three of Clubs Eight of Hearts Three of Diamonds Four of Diamonds Queen of Diamonds Five of Diamonds Six of Diamonds Five of Hearts Ace of Spades Six of Hearts Nine of Diamonds Queen of Clubs Eight of Spades Nine of Clubs Deuce of Clubs Six of Clubs Deuce of Spades Jack of Clubs Four of Clubs Eight of Clubs Four of Spades Seven of Spades Seven of Diamonds Seven of Clubs King of Spades Ten of Diamonds Jack of Hearts Ace of Hearts Jack of Spades Ten of Clubs Eight of Diamonds Deuce of Diamonds Ace of Diamonds Nine of Spades Four of Hearts Deuce of Hearts King of Diamonds Ten of Spades Three of Hearts Ten of Hearts

Fig. 7.25 | Sample run of card dealing program.

Fig. 7.24 | Card dealing program. (Part 3 of 3.)

286 Chapter 7 C Pointers

tion to an integer array and the size of the array. The program prompts the user to choose whether the array should be sorted in ascending or in descending order. If the user enters 1, a pointer to function ascending is passed to function bubble, causing the array to be sorted into increasing order. If the user enters 2, a pointer to function descending is passed to function bubble, causing the array to be sorted into decreasing order. The output of the program is shown in Fig. 7.27.

1 /* Fig. 7.26: fig07_26.c 2 Multipurpose sorting program using function pointers */ 3 #include <stdio.h> 4 #define SIZE 10 5 6 /* prototypes */ 7 void bubble( int work[], const int size, ); 8 int ascending( int a, int b ); 9 int descending( int a, int b );

10 11 int main( void ) 12 { 13 int order; /* 1 for ascending order or 2 for descending order */ 14 int counter; /* counter */ 15 16 /* initialize array a */ 17 int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; 18 19 printf( "Enter 1 to sort in ascending order,\n" 20 "Enter 2 to sort in descending order: " ); 21 scanf( "%d", &order ); 22 23 printf( "\nData items in original order\n" ); 24 25 /* output original array */ 26 for ( counter = 0; counter < SIZE; counter++ ) { 27 printf( "%5d", a[ counter ] ); 28 } /* end for */ 29 30 /* sort array in ascending order; pass function ascending as an 31 argument to specify ascending sorting order */ 32 if ( order == 1 ) { 33 34 printf( "\nData items in ascending order\n" ); 35 } /* end if */ 36 else { /* pass function descending */ 37 38 printf( "\nData items in descending order\n" ); 39 } /* end else */ 40 41 /* output sorted array */ 42 for ( counter = 0; counter < SIZE; counter++ ) { 43 printf( "%5d", a[ counter ] ); 44 } /* end for */

Fig. 7.26 | Multipurpose sorting program using function pointers. (Part 1 of 2.)

int (*compare)( int a, int b )

bubble( a, SIZE, ascending );

bubble( a, SIZE, descending );

7.12 Pointers to Functions 287

45 46 printf( "\n" ); 47 return 0; /* indicates successful termination */ 48 } /* end main */ 49 50 /* multipurpose bubble sort; parameter compare is a pointer to 51 the comparison function that determines sorting order */ 52 void bubble( int work[], const int size, ) 53 { 54 int pass; /* pass counter */ 55 int count; /* comparison counter */ 56 57 void swap( int *element1Ptr, int *element2ptr ); /* prototype */ 58 59 /* loop to control passes */ 60 for ( pass = 1; pass < size; pass++ ) { 61 62 /* loop to control number of comparisons per pass */ 63 for ( count = 0; count < size - 1; count++ ) { 64 65 /* if adjacent elements are out of order, swap them */ 66 if ( ) { 67 swap( &work[ count ], &work[ count + 1 ] ); 68 } /* end if */ 69 } /* end for */ 70 } /* end for */ 71 } /* end function bubble */ 72 73 /* swap values at memory locations to which element1Ptr and 74 element2Ptr point */ 75 void swap( int *element1Ptr, int *element2Ptr ) 76 { 77 int hold; /* temporary holding variable */ 78 79 hold = *element1Ptr; 80 *element1Ptr = *element2Ptr; 81 *element2Ptr = hold; 82 } /* end function swap */ 83 84 85 86 87 88 89 90 91 92 93 94 95 96

Fig. 7.26 | Multipurpose sorting program using function pointers. (Part 2 of 2.)

int (*compare)( int a, int b )

(*compare)( work[ count ], work[ count + 1 ] )

/* determine whether elements are out of order for an ascending order sort */ int ascending( int a, int b ) { return b < a; /* swap if b is less than a */ } /* end function ascending */

/* determine whether elements are out of order for a descending order sort */ int descending( int a, int b ) { return b > a; /* swap if b is greater than a */ } /* end function descending */

288 Chapter 7 C Pointers

The following parameter appears in the function header for bubble (line 52)

This tells bubble to expect a parameter (compare) that is a pointer to a function that re- ceives two integer parameters and returns an integer result. Parentheses are needed around *compare to group * with compare to indicate that compare is a pointer. If we had not in- cluded the parentheses, the declaration would have been

which declares a function that receives two integers as parameters and returns a pointer to an integer.

The function prototype for bubble is shown in line 7. The prototype could have been written as

without the function-pointer name and parameter names. The function passed to bubble is called in an if statement (line 66) as follows:

Just as a pointer to a variable is dereferenced to access the value of the variable, a pointer to a function is dereferenced to use the function.

The call to the function could have been made without dereferencing the pointer as in

which uses the pointer directly as the function name. We prefer the first method of calling a function through a pointer because it explicitly illustrates that compare is a pointer to a function that is dereferenced to call the function. The second method of calling a function through a pointer makes it appear as though compare is an actual function. This may be confusing to a user of the program who would like to see the definition of function com- pare and finds that it’s never defined in the file.

Enter 1 to sort in ascending order, Enter 2 to sort in descending order: 1

Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items in ascending order 2 4 6 8 10 12 37 45 68 89

Enter 1 to sort in ascending order, Enter 2 to sort in descending order: 2

Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items in descending order 89 68 45 37 12 10 8 6 4 2

Fig. 7.27 | The outputs of the bubble sort program in Fig. 7.26.

int (*compare)( int a, int b )

int *compare( int a, int b )

int (*)( int, int );

if ( (*compare)( work[ count ], work[ count + 1 ] ) )

if ( compare( work[ count ], work[ count + 1 ] ) )

7.12 Pointers to Functions 289

Using Function Pointers to Create a Menu-Driven System A common use of function pointers is in text-based menu-driven systems. A user is prompted to select an option from a menu (possibly from 1 to 5) by typing the menu item’s number. Each option is serviced by a different function. Pointers to each function are stored in an array of pointers to functions. The user’s choice is used as a subscript in the array, and the pointer in the array is used to call the function.

Figure 7.28 provides a generic example of the mechanics of defining and using an array of pointers to functions. We define three functions—function1, function2 and function3—that each take an integer argument and return nothing. We store pointers to these three functions in array f, which is defined in line 14.

1 /* Fig. 7.28: fig07_28.c 2 Demonstrating an array of pointers to functions */ 3 #include <stdio.h> 4 5 /* prototypes */ 6 7 8 9

10 int main( void ) 11 { 12 13 14 15 16 int choice; /* variable to hold user's choice */ 17 18 printf( "Enter a number between 0 and 2, 3 to end: " ); 19 scanf( "%d", &choice ); 20 21 /* process user's choice */ 22 while ( choice >= 0 && choice < 3 ) { 23 24 25 26 27 28 printf( "Enter a number between 0 and 2, 3 to end: "); 29 scanf( "%d", &choice ); 30 } /* end while */ 31 32 printf( "Program execution completed.\n" ); 33 return 0; /* indicates successful termination */ 34 } /* end main */ 35 36 37 { 38 printf( "You entered %d so function1 was called\n\n", a ); 39 } /* end function1 */ 40

Fig. 7.28 | Demonstrating an array of pointers to functions. (Part 1 of 2.)

void function1( int a ); void function2( int b ); void function3( int c );

/* initialize array of 3 pointers to functions that each take an int argument and return void */ void (*f[ 3 ])( int ) = { function1, function2, function3 };

/* invoke function at location choice in array f and pass choice as an argument */ (*f[ choice ])( choice );

void function1( int a )

290 Chapter 7 C Pointers

The definition is read beginning in the leftmost set of parentheses, “f is an array of 3 pointers to functions that each take an int as an argument and return void.” The array is initialized with the names of the three functions. When the user enters a value between 0 and 2, the value is used as the subscript into the array of pointers to functions. In the func- tion call (line 26), f[choice] selects the pointer at location choice in the array. The pointer is dereferenced to call the function, and choice is passed as the argument to the function. Each function prints its argument’s value and its function name to demonstrate that the function is called correctly. In this chapter’s exercises, you’ll develop several text- based, menu-driven systems.

41 42 { 43 printf( "You entered %d so function2 was called\n\n", b ); 44 } /* end function2 */ 45 46 47 { 48 printf( "You entered %d so function3 was called\n\n", c ); 49 } /* end function3 */

Enter a number between 0 and 2, 3 to end: 0 You entered 0 so function1 was called

Enter a number between 0 and 2, 3 to end: 1 You entered 1 so function2 was called

Enter a number between 0 and 2, 3 to end: 2 You entered 2 so function3 was called

Enter a number between 0 and 2, 3 to end: 3 Program execution completed.

Fig. 7.28 | Demonstrating an array of pointers to functions. (Part 2 of 2.)

void function2( int b )

void function3( int c )

Summary Section 7.2 Pointer Variable Definitions and Initialization • A pointer contains an address of another variable that contains a value. In this sense, a variable

name directly references a value, and a pointer indirectly references a value.

• Referencing a value through a pointer is called indirection.

• Pointers can be defined to point to objects of any type.

• Pointers should be initialized either when they’re defined or in an assignment statement. A point- er may be initialized to NULL, 0 or an address. A pointer with the value NULL points to nothing. Initializing a pointer to 0 is equivalent to initializing a pointer to NULL, but NULL is preferred. The value 0 is the only integer value that can be assigned directly to a pointer variable.

• NULL is a symbolic constant defined in the <stddef.h> header (and several other headers).

Section 7.3 Pointer Operators • The &, or address operator, is a unary operator that returns the address of its operand.

Summary 291

• The operand of the address operator must be a variable.

• The indirection operator * returns the value of the object to which its operand points.

• The printf conversion specifier %p outputs a memory location as a hexadecimal integer on most platforms.

Section 7.4 Passing Arguments to Functions by Reference • All arguments in C are passed by value.

• C provides the capabilities for simulating call-by-reference using pointers and the indirection op- erator. To pass a variable by reference, apply the address operator (&) to the variable’s name.

• When the address of a variable is passed to a function, the indirection operator (*) may be used in the function to modify the value at that location in the caller’s memory.

• A function receiving an address as an argument must define a pointer parameter to receive the address.

• The compiler does not differentiate between a function that receives a pointer and a function that receives a single-subscripted array. A function must “know” when it’s receiving an array vs. a sin- gle variable passed by reference.

• When the compiler encounters a function parameter for a single-subscripted array of the form int b[], the compiler converts the parameter to the pointer notation int *b.

Section 7.5 Using the const Qualifier with Pointers • The const qualifier indicates that the value of a particular variable should not be modified.

• If an attempt is made to modify a value that is declared const, the compiler catches it and issues either a warning or an error, depending on the particular compiler.

• There are four ways to pass a pointer to a function: a non-constant pointer to non-constant data, a constant pointer to non-constant data, a non-constant pointer to constant data, and a constant pointer to constant data.

• With a non-constant pointer to non-constant data, the data can be modified through the deref- erenced pointer, and the pointer can be modified to point to other data items.

• A non-constant pointer to constant data can be modified to point to any data item of the appro- priate type, but the data to which it points cannot be modified.

• A constant pointer to non-constant data always points to the same memory location, and the data at that location can be modified through the pointer. This is the default for an array name.

• A constant pointer to constant data always points to the same memory location, and the data at that memory location cannot be modified.

Section 7.7 sizeof Operator • Unary operator sizeof determine the size in bytes of a variable or type at compilation time.

• When applied to the name of an array, sizeof returns the total number of bytes in the array.

• Type size_t is an integral type (unsigned or unsigned long) returned by operator sizeof. Type size_t is defined in header <stddef.h>.

• Operator sizeof can be applied to any variable name, type or value.

• The parentheses used with sizeof are required if a type name is supplied as its operand.

Section 7.8 Pointer Expressions and Pointer Arithmetic • A limited set of arithmetic operations may be performed on pointers. A pointer may be incre-

mented (++) or decremented (--), an integer may be added to a pointer (+ or +=), an integer may be subtracted from a pointer (- or -=) and one pointer may be subtracted from another.

292 Chapter 7 C Pointers

• When an integer is added to or subtracted from a pointer, the pointer is incremented or decre- mented by that integer times the size of the object to which the pointer refers.

• Two pointers to elements of the same array may be subtracted from one another to determine the number of elements between them.

• A pointer can be assigned to another pointer if both have the same type. An exception to this is the pointer of type void * which can represent any pointer type. All pointer types can be assigned a void * pointer, and a void * pointer can be assigned a pointer of any type.

• A void * pointer cannot be dereferenced.

• Pointers can be compared using equality and relational operators, but such comparisons are meaningless unless the pointers point to elements of the same array. Pointer comparisons com- pare the addresses stored in the pointers.

• A common use of pointer comparison is determining whether a pointer is NULL.

Section 7.9 Relationship between Pointers and Arrays • Arrays and pointers are intimately related in C and often may be used interchangeably.

• An array name can be thought of as a constant pointer.

• Pointers can be used to do any operation involving array subscripting.

• When a pointer points to the beginning of an array, adding an offset to the pointer indicates which element of the array should be referenced, and the offset value is identical to the array sub- script. This is referred to as pointer/offset notation.

• An array name can be treated as a pointer and used in pointer arithmetic expressions that do not attempt to modify the address of the pointer.

• Pointers can be subscripted exactly as arrays can. This is referred to as pointer/subscript notation.

• A parameter of type const char * typically represents a constant string.

Section 7.10 Arrays of Pointers • Arrays may contain pointers. A common use of an array of pointers is to form an array of strings.

Each entry in the array is a string, but in C a string is essentially a pointer to its first character. So each entry in an array of strings is actually a pointer to the first character of a string.

Section 7.12 Pointers to Functions • A pointer to a function contains the address of the function in memory. A function name is really

the starting address in memory of the code that performs the function’s task.

• Pointers to functions can be passed to functions, returned from functions, stored in arrays and assigned to other function pointers.

• A pointer to a function is dereferenced to call the function. A function pointer can be used di- rectly as the function name when calling the function.

• A common use of function pointers is in text-based, menu-driven systems.

Terminology address operator (&) 255 array of pointers 280 array of strings 280 call-by-reference 257 call-by-value 257 const qualifier 261 constant pointer to constant data 262

constant pointer to non-constant data 262 dereferencing a pointer 256 dereferencing operator (*) 256 function pointer 289 indefinite postponement 281 indirection 254 indirection operator (*) 256

Self-Review Exercises 293

non-constant pointer to constant data 262 non-constant pointer to non-constant data 262 offset to a pointer 276 pointer 254 pointer arithmetic 263 pointer/offset notation 276 pointer/subscript notation 276 pointer subscripting 276 pointer to a function 285 pointer to void (void *) 275

principle of least privilege 262 record 265 simulating call-by-reference 258 sizeof operator 270 size_t type 271 string array 280 structure 265 time/space tradeoff 266 void * (pointer to void) 275

Self-Review Exercises 7.1 Answer each of the following:

a) A pointer variable contains as its value the of another variable. b) The three values that can be used to initialize a pointer are , and

. c) The only integer that can be assigned to a pointer is .

7.2 State whether the following are true or false. If the answer is false, explain why. a) The address operator (&) can be applied only to constants, to expressions and to vari-

ables declared with the storage-class register. b) A pointer that is declared to be void can be dereferenced. c) Pointers of different types may not be assigned to one another without a cast operation.

7.3 Answer each of the following. Assume that single-precision floating-point numbers are stored in 4 bytes, and that the starting address of the array is at location 1002500 in memory. Each part of the exercise should use the results of previous parts where appropriate.

a) Define an array of type float called numbers with 10 elements, and initialize the ele- ments to the values 0.0, 1.1, 2.2, …, 9.9. Assume the symbolic constant SIZE has been defined as 10.

b) Define a pointer, nPtr, that points to an object of type float. c) Print the elements of array numbers using array subscript notation. Use a for statement

and assume the integer control variable i has been defined. Print each number with 1 position of precision to the right of the decimal point.

d) Give two separate statements that assign the starting address of array numbers to the pointer variable nPtr.

e) Print the elements of array numbers using pointer/offset notation with the pointer nPtr. f) Print the elements of array numbers using pointer/offset notation with the array name

as the pointer. g) Print the elements of array numbers by subscripting pointer nPtr. h) Refer to element 4 of array numbers using array subscript notation, pointer/offset no-

tation with the array name as the pointer, pointer subscript notation with nPtr and pointer/offset notation with nPtr.

i) Assuming that nPtr points to the beginning of array numbers, what address is referenced by nPtr + 8? What value is stored at that location?

j) Assuming that nPtr points to numbers[5], what address is referenced by nPtr –= 4. What is the value stored at that location?

7.4 For each of the following, write a statement that performs the indicated task. Assume that floating-point variables number1 and number2 are defined and that number1 is initialized to 7.3.

a) Define the variable fPtr to be a pointer to an object of type float. b) Assign the address of variable number1 to pointer variable fPtr.

294 Chapter 7 C Pointers

c) Print the value of the object pointed to by fPtr. d) Assign the value of the object pointed to by fPtr to variable number2. e) Print the value of number2. f) Print the address of number1. Use the %p conversion specifier. g) Print the address stored in fPtr. Use the %p conversion specifier. Is the value printed the

same as the address of number1?

7.5 Do each of the following: a) Write the function header for a function called exchange that takes two pointers to

floating-point numbers x and y as parameters and does not return a value. b) Write the function prototype for the function in part (a). c) Write the function header for a function called evaluate that returns an integer and

that takes as parameters integer x and a pointer to function poly. Function poly takes an integer parameter and returns an integer.

d) Write the function prototype for the function in part (c).

7.6 Find the error in each of the following program segments. Assume

int *zPtr; /* zPtr will reference array z */ int *aPtr = NULL; void *sPtr = NULL; int number, i; int z[ 5 ] = { 1, 2, 3, 4, 5 }; sPtr = z;

a) ++zptr; b) /* use pointer to get first value of array; assume zPtr is initialized */

number = zPtr; c) /* assign array element 2 (the value 3) to number;

assume zPtr is initialized */

number = *zPtr[ 2 ]; d) /* print entire array z; assume zPtr is initialized */

for ( i = 0; i <= 5; i++ ) {

printf( "%d ", zPtr[ i ] );

} e) /* assign the value pointed to by sPtr to number */

number = *sPtr; f) ++z;

Answers to Self-Review Exercises 7.1 a) address. b) 0, NULL, an address. c) 0.

7.2 a) False. The address operator can be applied only to variables. The address operator can- not be applied to variables declared with storage class register.

b) False. A pointer to void cannot be dereferenced, because there is no way to know exactly how many bytes of memory to dereference.

c) False. Pointers of type void can be assigned pointers of other types, and pointers of type void can be assigned to pointers of other types.

7.3 a) float numbers[ SIZE ] = { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9 };

b) float *nPtr; c) for ( i = 0; i < SIZE; i++ ) {

printf( "%.1f ", numbers[ i ] ); }

Exercises 295

d) nPtr = numbers; nPtr = &numbers[ 0 ];

e) for ( i = 0; i < SIZE; i++ ) { printf( "%.1f ", *( nPtr + i ) );

} f) for ( i = 0; i < SIZE; i++ ) {

printf( "%.1f ", *( numbers + i ) );

} g) for ( i = 0; i < SIZE; i++ ) {

printf( "%.1f ", nPtr[ i ] ); }

h) numbers[ 4 ] *( numbers + 4 )

nPtr[ 4 ]

*( nPtr + 4 ) i) The address is 1002500 + 8 * 4 = 1002532. The value is 8.8. j) The address of numbers[ 5 ] is 1002500 + 5 * 4 = 1002520.

The address of nPtr -= 4 is 1002520 - 4 * 4 = 1002504. The value at that location is 1.1.

7.4 a) float *fPtr; b) fPtr = &number1; c) printf( "The value of *fPtr is %f\n", *fPtr ); d) number2 = *fPtr; e) printf( "The value of number2 is %f\n", number2 ); f) printf( "The address of number1 is %p\n", &number1 ); g) printf( "The address stored in fptr is %p\n", fPtr );

Yes, the value is the same.

7.5 a) void exchange( float *x, float *y ) b) void exchange( float *x, float *y ); c) int evaluate( int x, int (*poly)( int ) ) d) int evaluate( int x, int (*poly)( int ) );

7.6 a) Error: zPtr has not been initialized. Correction: Initialize zPtr with zPtr = z; before performing the pointer arithmetic.

b) Error: The pointer is not dereferenced. Correction: Change the statement to number = *zPtr;

c) Error: zPtr[ 2 ] is not a pointer and should not be dereferenced. Correction: Change *zPtr[ 2 ] to zPtr[ 2 ].

d) Error: Referring to an array element outside the array bounds with pointer subscripting. Correction: Change the operator <= in the for condition to <.

e) Error: Dereferencing a void pointer. Correction: To dereference the pointer, it must first be cast to an integer pointer. Change the statement to number = *( ( int * ) sPtr );

f) Error: Trying to modify an array name with pointer arithmetic. Correction: Use a pointer variable instead of the array name to accomplish pointer arithmetic, or subscript the array name to refer to a specific element.

Exercises 7.7 Answer each of the following:

a) The operator returns the location in memory where its operand is stored.

296 Chapter 7 C Pointers

b) The operator returns the value of the object to which its operand points. c) To simulate call-by-reference when passing a nonarray variable to a function, it’s nec-

essary to pass the of the variable to the function.

7.8 State whether the following are true or false. If false, explain why. a) Two pointers that point to different arrays cannot be compared meaningfully. b) Because the name of an array is a pointer to the first element of the array, array names

may be manipulated in precisely the same manner as pointers.

7.9 Answer each of the following. Assume that unsigned integers are stored in 2 bytes and that the starting address of the array is at location 1002500 in memory.

a) Define an array of type unsigned int called values with five elements, and initialize the elements to the even integers from 2 to 10. Assume the symbolic constant SIZE has been defined as 5.

b) Define a pointer vPtr that points to an object of type unsigned int. c) Print the elements of array values using array subscript notation. Use a for statement

and assume integer control variable i has been defined. d) Give two separate statements that assign the starting address of array values to pointer

variable vPtr. e) Print the elements of array values using pointer/offset notation. f) Print the elements of array values using pointer/offset notation with the array name as

the pointer. g) Print the elements of array values by subscripting the pointer to the array. h) Refer to element 5 of array values using array subscript notation, pointer/offset nota-

tion with the array name as the pointer, pointer subscript notation, and pointer/offset notation.

i) What address is referenced by vPtr + 3? What value is stored at that location? j) Assuming vPtr points to values[4], what address is referenced by vPtr -= 4. What val-

ue is stored at that location?

7.10 For each of the following, write a single statement that performs the indicated task. Assume that long integer variables value1 and value2 have been defined and that value1 has been initialized to 200000.

a) Define the variable lPtr to be a pointer to an object of type long. b) Assign the address of variable value1 to pointer variable lPtr. c) Print the value of the object pointed to by lPtr. d) Assign the value of the object pointed to by lPtr to variable value2. e) Print the value of value2. f) Print the address of value1. g) Print the address stored in lPtr. Is the value printed the same as the address of value1?

7.11 Do each of the following: a) Write the function header for function zero, which takes a long integer array parameter

bigIntegers and does not return a value. b) Write the function prototype for the function in part a. c) Write the function header for function add1AndSum, which takes an integer array pa-

rameter oneTooSmall and returns an integer. d) Write the function prototype for the function described in part c.

Note: Exercise 7.12 through Exercise 7.15 are reasonably challenging. Once you have done these problems, you ought to be able to implement most popular card games easily. 7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card poker hand. Then write the following additional functions:

Exercises 297

a) Determine if the hand contains a pair. b) Determine if the hand contains two pairs. c) Determine if the hand contains three of a kind (e.g., three jacks). d) Determine if the hand contains four of a kind (e.g., four aces). e) Determine if the hand contains a flush (i.e., all five cards of the same suit). f) Determine if the hand contains a straight (i.e., five cards of consecutive face values).

7.13 (Project: Card Shuffling and Dealing) Use the functions developed in Exercise 7.12 to write a program that deals two five-card poker hands, evaluates each hand, and determines which is the better hand.

7.14 (Project: Card Shuffling and Dealing) Modify the program developed in Exercise 7.13 so that it can simulate the dealer. The dealer's five-card hand is dealt “face down” so the player cannot see it. The program should then evaluate the dealer’s hand, and based on the quality of the hand, the dealer should draw one, two or three more cards to replace the corresponding number of un- needed cards in the original hand. The program should then re-evaluate the dealer's hand. [Caution: This is a difficult problem!]

7.15 (Project: Card Shuffling and Dealing) Modify the program developed in Exercise 7.14 so that it can handle the dealer’s hand automatically, but the player is allowed to decide which cards of the player's hand to replace. The program should then evaluate both hands and determine who wins. Now use this new program to play 20 games against the computer. Who wins more games, you or the computer? Have one of your friends play 20 games against the computer. Who wins more games? Based on the results of these games, make appropriate modifications to refine your poker playing program (this, too, is a difficult problem). Play 20 more games. Does your modified pro- gram play a better game?

7.16 (Card Shuffling and Dealing Modification) In the card shuffling and dealing program of Fig. 7.24, we intentionally used an inefficient shuffling algorithm that introduced the possibility of indefinite postponement. In this problem, you’ll create a high-performance shuffling algorithm that avoids indefinite postponement.

Modify the program of Fig. 7.24 as follows. Begin by initializing the deck array as shown in Fig. 7.29. Modify the shuffle function to loop row-by-row and column-by-column through the array, touching every element once. Each element should be swapped with a randomly selected ele- ment of the array. Print the resulting array to determine if the deck is satisfactorily shuffled (as in Fig. 7.30, for example). You may want your program to call the shuffle function several times to ensure a satisfactory shuffle.

Although the approach in this problem improves the shuffling algorithm, the dealing algo- rithm still requires searching the deck array for card 1, then card 2, then card 3, and so on. Worse yet, even after the dealing algorithm locates and deals the card, the algorithm continues searching through the remainder of the deck. Modify the program of Fig. 7.24 so that once a card is dealt, no

Unshuffled deck array

0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12 13

1 14 15 16 17 18 19 20 21 22 23 24 25 26

2 27 28 29 30 31 32 33 34 35 36 37 38 39

3 40 41 42 43 44 45 46 47 48 49 50 51 52

Fig. 7.29 | Unshuffled deck array.

298 Chapter 7 C Pointers

further attempts are made to match that card number, and the program immediately proceeds with dealing the next card. In Chapter 10, we develop a dealing algorithm that requires only one opera- tion per card.

7.17 (Simulation: The Tortoise and the Hare) In this problem, you’ll recreate one of the truly great moments in history, namely the classic race of the tortoise and the hare. You’ll use random number generation to develop a simulation of this memorable event.

Our contenders begin the race at “square 1” of 70 squares. Each square represents a possible position along the race course. The finish line is at square 70. The first contender to reach or pass square 70 is rewarded with a pail of fresh carrots and lettuce. The course weaves its way up the side of a slippery mountain, so occasionally the contenders lose ground.

There is a clock that ticks once per second. With each tick of the clock, your program should adjust the position of the animals according to the rules of Fig. 7.31.

Use variables to keep track of the positions of the animals (i.e., position numbers are 1–70). Start each animal at position 1 (i.e., the “starting gate”). If an animal slips left before square 1, move the animal back to square 1.

Generate the percentages in the preceding table by producing a random integer, i, in the range 1 ≤ i ≤10. For the tortoise, perform a “fast plod” when 1 ≤ i ≤5, a “slip” when 6 ≤ i ≤7, or a “slow plod” when 8 ≤ i ≤10. Use a similar technique to move the hare.

Begin the race by printing

BANG !!!!! AND THEY'RE OFF !!!!!

Then, for each tick of the clock (i.e., each repetition of a loop), print a 70-position line showing the letter T in the position of the tortoise and the letter H in the position of the hare. Occasionally,

Sample shuffled deck array

0 1 2 3 4 5 6 7 8 9 10 11 12 0 19 40 27 25 36 46 10 34 35 41 18 2 44 1 13 28 14 16 21 30 8 11 31 17 24 7 1 2 12 33 15 42 43 23 45 3 29 32 4 47 26 3 50 38 52 39 48 51 9 5 37 49 22 6 20

Fig. 7.30 | Sample shuffled deck array.

Animal Move type Percentage of the time Actual move

Tortoise Fast plod Slip Slow plod

50% 20% 30%

3 squares to the right 6 squares to the left 1 square to the right

Hare Sleep Big hop Big slip Small hop Small slip

20% 20% 10% 30% 20%

No move at all 9 squares to the right 12 squares to the left 1 square to the right 2 squares to the left

Fig. 7.31 | Tortoise and hare rules for adjusting positions.

Exercises 299

the contenders will land on the same square. In this case, the tortoise bites the hare and your pro- gram should print OUCH!!! beginning at that position. All print positions other than the T, the H, or the OUCH!!! (in case of a tie) should be blank.

After each line is printed, test if either animal has reached or passed square 70. If so, then print the winner and terminate the simulation. If the tortoise wins, print TORTOISE WINS!!! YAY!!! If the hare wins, print Hare wins. Yuch. If both animals win on the same tick of the clock, you may want to favor the turtle (the “underdog”), or you may want to print It's a tie. If neither animal wins, per- form the loop again to simulate the next tick of the clock. When you’re ready to run your program, assemble a group of fans to watch the race. You’ll be amazed at how involved your audience gets!

7.18 (Card Shuffling and Dealing Modification) Modify the card shuffling and dealing program of Fig. 7.24 so the shuffling and dealing operations are performed by the same function (shuffle- AndDeal). The function should contain one nested looping structure that is similar to function shuffle in Fig. 7.24.

7.19 What does this program do?

7.20 What does this program do?

1 /* ex07_19.c */ 2 /* What does this program do? */ 3 #include <stdio.h> 4 5 void mystery1( char *s1, const char *s2 ); /* prototype */ 6 7 int main( void ) 8 { 9 char string1[ 80 ]; /* create char array */

10 char string2[ 80 ]; /* create char array */ 11 12 printf( "Enter two strings: " ); 13 scanf( "%s%s" , string1, string2 ); 14 mystery1( string1, string2 ); 15 printf("%s", string1 ); 16 return 0; /* indicates successful termination */ 17 } /* end main */ 18 19 /* What does this function do? */ 20 void mystery1( char *s1, const char *s2 ) 21 { 22 while ( *s1 != '\0' ) { 23 s1++; 24 } /* end while */ 25 26 for ( ; *s1 = *s2; s1++, s2++ ) { 27 ; /* empty statement */ 28 } /* end for */ 29 } /* end function mystery1 */

1 /* ex07_20.c */ 2 /* what does this program do? */ 3 #include <stdio.h> 4 5 int mystery2( const char *s ); /* prototype */ 6 7 int main( void ) 8 { 9 char string[ 80 ]; /* create char array */

300 Chapter 7 C Pointers

7.21 Find the error in each of the following program segments. If the error can be corrected, ex- plain how.

a) int *number; printf( "%d\n", *number );

b) float *realPtr; long *integerPtr;

integerPtr = realPtr; c) int * x, y;

x = y; d) char s[] = "this is a character array";

int count;

for ( ; *s != '\0'; s++)

printf( "%c ", *s ); e) short *numPtr, result;

void *genericPtr = numPtr;

result = *genericPtr + 7; f) float x = 19.34;

float xPtr = &x;

printf( "%f\n", xPtr ); g) char *s;

printf( "%s\n", s );

7.22 (Maze Traversal) The following grid is a double-subscripted array representation of a maze.

10 11 printf( "Enter a string: "); 12 scanf( "%s", string ); 13 printf( "%d\n", mystery2( string ) ); 14 return 0; /* indicates successful termination */ 15 } /* end main */ 16 17 /* What does this function do? */ 18 int mystery2( const char *s ) 19 { 20 int x; /* counter */ 21 22 /* loop through string */ 23 for ( x = 0; *s != '\0'; s++ ) { 24 x++; 25 } /* end for */ 26 27 return x; 28 } /* end function mystery2 */

# # # # # # # # # # # # # . . . # . . . . . . # . . # . # . # # # # . # # # # . # . . . . # . # # . . . . # # # . # . . # # # # . # . # . # . # # . . # . # . # . # . # # # . # . # . # . # . # # . . . . . . . . # . # # # # # # # . # # # . # # . . . . . . # . . . # # # # # # # # # # # # #

Exercises 301

The # symbols represent the walls of the maze, and the periods (.) represent squares in the possible paths through the maze.

There is a simple algorithm for walking through a maze that guarantees finding the exit (assuming there is an exit). If there is not an exit, you’ll arrive at the starting location again. Place your right hand on the wall to your right and begin walking forward. Never remove your hand from the wall. If the maze turns to the right, you follow the wall to the right. As long as you do not remove your hand from the wall, eventually you’ll arrive at the exit of the maze. There may be a shorter path than the one you have taken, but you’re guaranteed to get out of the maze.

Write recursive function mazeTraverse to walk through the maze. The function should receive as arguments a 12-by-12 character array representing the maze and the starting location of the maze. As mazeTraverse attempts to locate the exit from the maze, it should place the character X in each square in the path. The function should display the maze after each move so the user can watch as the maze is solved.

7.23 (Generating Mazes Randomly) Write a function mazeGenerator that takes as an argument a double-subscripted 12-by-12 character array and randomly produces a maze. The function should also provide the starting and ending locations of the maze. Try your function mazeTraverse from Exercise 7.22 using several randomly generated mazes.

7.24 (Mazes of Any Size) Generalize functions mazeTraverse and mazeGenerator of Exercise 7.22 and Exercise 7.23 to process mazes of any width and height.

7.25 (Arrays of Pointers to Functions) Rewrite the program of Fig. 6.22 to use a menu-driven interface. The program should offer the user four options as follows:

One restriction on using arrays of pointers to functions is that all the pointers must have the same type. The pointers must be to functions of the same return type that receive arguments of the same type. For this reason, the functions in Fig. 6.22 must be modified so that they each return the same type and take the same parameters. Modify functions minimum and maximum to print the minimum or maximum value and return nothing. For option 3, modify function average of Fig. 6.22 to out- put the average for each student (not a specific student). Function average should return nothing and take the same parameters as printArray, minimum and maximum. Store the pointers to the four functions in array processGrades and use the choice made by the user as the subscript into the array for calling each function.

7.26 What does this program do?

Enter a choice: 0 Print the array of grades 1 Find the minimum grade 2 Find the maximum grade 3 Print the average on all tests for each student 4 End program

1 /* ex07_26.c */ 2 /* What does this program do? */ 3 #include <stdio.h> 4 5 int mystery3( const char *s1, const char *s2 ); /* prototype */ 6 7 int main( void ) 8 { 9 char string1[ 80 ]; /* create char array */

10 char string2[ 80 ]; /* create char array */ 11 12 printf( "Enter two strings: " );

302 Chapter 7 C Pointers

Special Section: Building Your Own Computer In the next several problems, we take a temporary diversion away from the world of high-level lan- guage programming. We “peel open” a computer and look at its internal structure. We introduce machine-language programming and write several machine-language programs. To make this an especially valuable experience, we then build a computer (through the technique of software-based simulation) on which you can execute your machine-language programs!

7.27 (Machine-Language Programming) Let’s create a computer we’ll call the Simpletron. As its name implies, it’s a simple machine, but as we’ll soon see, a powerful one as well. The Simpletron runs programs written in the only language it directly understands—that is, Simpletron Machine Language, or SML for short.

The Simpletron contains an accumulator—a “special register” in which information is put before the Simpletron uses that information in calculations or examines it in various ways. All information in the Simpletron is handled in terms of words. A word is a signed four-digit decimal number such as +3364, -1293, +0007, -0001 and so on. The Simpletron is equipped with a 100- word memory, and these words are referenced by their location numbers 00, 01, …, 99.

Before running an SML program, we must load or place the program into memory. The first instruction (or statement) of every SML program is always placed in location 00.

Each instruction written in SML occupies one word of the Simpletron's memory (and hence instructions are signed four-digit decimal numbers). We assume that the sign of an SML instruction is always plus, but the sign of a data word may be either plus or minus. Each location in the Simpletron’s memory may contain either an instruction, a data value used by a program or an unused (and hence undefined) area of memory. The first two digits of each SML instruction are the operation code, which specifies the operation to be performed. SML operation codes are summarized in Fig. 7.32.

13 scanf( "%s%s", string1 , string2 ); 14 printf( "The result is %d\n", mystery3( string1, string2 ) ); 15 return 0; /* indicates successful termination */ 16 } /* end main */ 17 18 int mystery3( const char *s1, const char *s2 ) 19 { 20 for ( ; *s1 != '\0' && *s2 != '\0'; s1++, s2++ ) { 21 22 if ( *s1 != *s2 ) { 23 return 0; 24 } /* end if */ 25 } /* end for */ 26 27 return 1; 28 } /* end function mystery3 */

Operation code Meaning

Input/output operations:

#define READ 10 Read a word from the terminal into a specific location in memory.

#define WRITE 11 Write a word from a specific location in memory to the terminal.

Fig. 7.32 | Simpletron Machine Language (SML) operation codes. (Part 1 of 2.)

Special Section: Building Your Own Computer 303

The last two digits of an SML instruction are the operand, which is the address of the memory location containing the word to which the operation applies. Now let’s consider several simple SML programs. The following SML program reads two numbers from the keyboard, and com- putes and prints their sum.

Load/store operations:

#define LOAD 20 Load a word from a specific location in memory into the accumulator.

#define STORE 21 Store a word from the accumulator into a specific location in memory.

Arithmetic operations:

#define ADD 30 Add a word from a specific location in memory to the word in the accumulator (leave result in accumulator).

#define SUBTRACT 31 Subtract a word from a specific location in memory from the word in the accumulator (leave result in accumulator).

#define DIVIDE 32 Divide a word from a specific location in memory into the word in the accumulator (leave result in accumulator).

#define MULTIPLY 33 Multiply a word from a specific location in memory by the word in the accumulator (leave result in accumulator).

Transfer of control operations:

#define BRANCH 40 Branch to a specific location in memory. #define BRANCHNEG 41 Branch to a specific location in memory if the accumulator is

negative.

#define BRANCHZERO 42 Branch to a specific location in memory if the accumulator is zero.

#define HALT 43 Halt—i.e., the program has completed its task.

Example 1 Location Number Instruction

00 +1007 (Read A) 01 +1008 (Read B) 02 +2007 (Load A) 03 +3008 (Add B) 04 +2109 (Store C) 05 +1109 (Write C) 06 +4300 (Halt) 07 +0000 (Variable A) 08 +0000 (Variable B) 09 +0000 (Result C)

Operation code Meaning

Fig. 7.32 | Simpletron Machine Language (SML) operation codes. (Part 2 of 2.)

304 Chapter 7 C Pointers

The instruction +1007 reads the first number from the keyboard and places it into location 07 (which has been initialized to zero). Then +1008 reads the next number into location 08. The load instruction, +2007, puts the first number into the accumulator, and the add instruction, +3008, adds the second number to the number in the accumulator. All SML arithmetic instructions leave their results in the accumulator. The store instruction, +2109, places the result back into memory location 09, from which the write instruction, +1109, takes the number and prints it (as a signed four-digit decimal number). The halt instruction, +4300, terminates execution.

The following SML program reads two numbers from the keyboard, and determines and prints the larger value. Note the use of the instruction +4107 as a conditional transfer of control, much the same as C’s if statement.

Now write SML programs to accomplish each of the following tasks. a) Use a sentinel-controlled loop to read 10 positive integers and compute and print their

sum. b) Use a counter-controlled loop to read seven numbers, some positive and some negative,

and compute and print their average. c) Read a series of numbers and determine and print the largest number. The first number

read indicates how many numbers should be processed.

7.28 (A Computer Simulator) It may at first seem outrageous, but in this problem you’re going to build your own computer. No, you won’t be soldering components together. Rather, you’ll use the powerful technique of software-based simulation to create a software model of the Simpletron. You’ll not be disappointed. Your Simpletron simulator will turn the computer you’re using into a Simpletron, and you’ll actually be able to run, test and debug the SML programs you wrote in Exercise 7.27.

When you run your Simpletron simulator, it should begin by printing:

*** Welcome to Simpletron! *** *** Please enter your program one instruction *** *** (or data word) at a time. I will type the *** *** location number and a question mark (?). *** *** You then type the word for that location. *** *** Type the sentinel -99999 to stop entering *** *** your program. ***

Example 2 Location Number Instruction

00 +1009 (Read A)

01 +1010 (Read B)

02 +2009 (Load A)

03 +3110 (Subtract B)

04 +4107 (Branch negative to 07)

05 +1109 (Write A)

06 +4300 (Halt)

07 +1110 (Write B)

08 +4300 (Halt)

09 +0000 (Variable A)

10 +0000 (Variable B)

Special Section: Building Your Own Computer 305

Simulate the memory of the Simpletron with a single-subscripted array memory that has 100 elements. Now assume that the simulator is running, and let’s examine the dialog as we enter the program of Example 2 of Exercise 7.27:

00 ? +1009 01 ? +1010 02 ? +2009 03 ? +3110 04 ? +4107 05 ? +1109 06 ? +4300 07 ? +1110 08 ? +4300 09 ? +0000 10 ? +0000 11 ? -99999 *** Program loading completed *** *** Program execution begins ***

The SML program has now been placed (or loaded) into the array memory. Now the Sim- pletron executes the SML program. It begins with the instruction in location 00 and, continues sequentially, unless directed to some other part of the program by a transfer of control.

Use the variable accumulator to represent the accumulator register. Use the variable instruc- tionCounter to keep track of the location in memory that contains the instruction being per- formed. Use the variable operationCode to indicate the operation currently being performed—i.e., the left two digits of the instruction word. Use the variable operand to indicate the memory loca- tion on which the current instruction operates. Thus, operand is the rightmost two digits of the instruction currently being performed. Do not execute instructions directly from memory. Rather, transfer the next instruction to be performed from memory to a variable called instruction- Register. Then “pick off ” the left two digits and place them in the variable operationCode, and “pick off” the right two digits and place them in operand.

When Simpletron begins execution, the special registers are initialized as follows:

accumulator +0000 instructionCounter 00 instructionRegister +0000 operationCode 00 operand 00

Now let’s “walk through” the execution of the first SML instruction, +1009 in memory loca- tion 00. This is called an instruction execution cycle.

The instructionCounter tells us the location of the next instruction to be performed. We fetch the contents of that location from memory by using the C statement

instructionRegister = memory[ instructionCounter ];

The operation code and the operand are extracted from the instruction register by the state- ments

operationCode = instructionRegister / 100; operand = instructionRegister % 100;

Now the Simpletron must determine that the operation code is actually a read (versus a write, a load, and so on). A switch differentiates among the twelve operations of SML.

In the switch statement, the behavior of various SML instructions is simulated as follows (we leave the others to the reader):

read: scanf( "%d", &memory[ operand ] ); load: accumulator = memory[ operand ]; add: accumulator += memory[ operand ];

306 Chapter 7 C Pointers

Various branch instructions: We'll discuss these shortly. halt: This instruction prints the message

*** Simpletron execution terminated ***

then prints the name and contents of each register as well as the complete contents of memory. Such a printout is often called a computer dump. To help you program your dump function, a sam- ple dump format is shown in Fig. 7.33. A dump after executing a Simpletron program would show the actual values of instructions and data values at the moment execution terminated.

Let’s proceed with the execution of our program’s first instruction, namely the +1009 in loca- tion 00. As we have indicated, the switch statement simulates this by performing the C statement

scanf( "%d", &memory[ operand ] );

A question mark (?) should be displayed on the screen before the scanf is executed to prompt the user for input. The Simpletron waits for the user to type a value and then press the Return key. The value is then read into location 09.

At this point, simulation of the first instruction is completed. All that remains is to prepare the Simpletron to execute the next instruction. Since the instruction just performed was not a transfer of control, we need merely increment the instruction counter register as follows:

++instructionCounter;

This completes the simulated execution of the first instruction. The entire process (i.e., the instruction execution cycle) begins anew with the fetch of the next instruction to be executed.

Now let’s consider how the branching instructions—the transfers of control—are simulated. All we need to do is adjust the value in the instruction counter appropriately. Therefore, the unconditional branch instruction (40) is simulated within the switch as

instructionCounter = operand;

The conditional “branch if accumulator is zero” instruction is simulated as

if ( accumulator == 0 ) { instructionCounter = operand;

}

At this point, you should implement your Simpletron simulator and run the SML programs you wrote in Exercise 7.27. You may embellish SML with additional features and provide for these in your simulator.

REGISTERS: accumulator +0000 instructionCounter 00 instructionRegister +0000 operationCode 00 operand 00

MEMORY: 0 1 2 3 4 5 6 7 8 9 0 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 10 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 20 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 30 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 40 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 50 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 60 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 70 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 80 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 90 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000

Fig. 7.33 | Sample Simpletron dump format.

Array of Function Pointer Exercises 307

Your simulator should check for various types of errors. During the program loading phase, for example, each number the user types into the Simpletron’s memory must be in the range -9999 to +9999. Your simulator should use a while loop to test that each number entered is in this range, and, if not, keep prompting the user to reenter the number until the user enters a correct number.

During the execution phase, your simulator should check for various serious errors, such as attempts to divide by zero, attempts to execute invalid operation codes and accumulator overflows (i.e., arithmetic operations resulting in values larger than +9999 or smaller than -9999). Such seri- ous errors are called fatal errors. When a fatal error is detected, your simulator should print an error message such as:

*** Attempt to divide by zero *** *** Simpletron execution abnormally terminated ***

and should print a full computer dump in the format we have discussed previously. This will help the user locate the error in the program.

7.29 (Modifications to the Simpletron Simulator) In Exercise 7.28, you wrote a software simu- lation of a computer that executes programs written in Simpletron Machine Language (SML). In this exercise, we propose several modifications and enhancements to the Simpletron Simulator. In Exercises 12.26 and 12.27, we propose building a compiler that converts programs written in a high-level programming language (a variation of BASIC) to Simpletron Machine Language. Some of the following modifications and enhancements may be required to execute the programs pro- duced by the compiler.

a) Extend the Simpletron Simulator’s memory to contain 1000 memory locations to en- able the Simpletron to handle larger programs.

b) Allow the simulator to perform remainder calculations. This requires an additional Simpletron Machine Language instruction.

c) Allow the simulator to perform exponentiation calculations. This requires an additional Simpletron Machine Language instruction.

d) Modify the simulator to use hexadecimal values rather than integer values to represent Simpletron Machine Language instructions.

e) Modify the simulator to allow output of a newline. This requires an additional Sim- pletron Machine Language instruction.

f) Modify the simulator to process floating-point values in addition to integer values. g) Modify the simulator to handle string input. [Hint: Each Simpletron word can be di-

vided into two groups, each holding a two-digit integer. Each two-digit integer repre- sents the ASCII decimal equivalent of a character. Add a machine-language instruction that will input a string and store the string beginning at a specific Simpletron memory location. The first half of the word at that location will be a count of the number of characters in the string (i.e., the length of the string). Each succeeding half word con- tains one ASCII character expressed as two decimal digits. The machine-language in- struction converts each character into its ASCII equivalent and assigns it to a half word.]

h) Modify the simulator to handle output of strings stored in the format of part (g). [Hint: Add a machine-language instruction that prints a string beginning at a specified Sim- pletron memory location. The first half of the word at that location is the length of the string in characters. Each succeeding half word contains one ASCII character expressed as two decimal digits. The machine-language instruction checks the length and prints the string by translating each two-digit number into its equivalent character.]

Array of Function Pointer Exercises 7.30 (Calculating Circle Circumference, Circle Area or Sphere Volume Using Function Pointers) Using the techniques you learned in Fig. 7.28, create a text-based, menu-driven program that allows

308 Chapter 7 C Pointers

the user to choose whether to calculate the circumference of a circle, the area of a circle or the vol- ume of a sphere. The program should then input a radius from the user, perform the appropriate calculation and display the result. Use an array of function pointers in which each pointer represents a function that returns void and receives a double parameter. The corresponding functions should each display messages indicating which calculation was performed, the value of the radius and the result of the calculation.

7.31 (Calculator Using Function Pointers) Using the techniques you learned in Fig. 7.28, create a text-based, menu-driven program that allows the user to choose whether to add, subtract, multiply or divide two numbers. The program should then input two double values from the user, perform the appropriate calculation and display the result. Use an array of function pointers in which each pointer represents a function that returns void and receives two double parameters. The corre- sponding functions should each display messages indicating which calculation was performed, the values of the parameters and the result of the calculation.

Making a Difference 7.32 (Polling) The Internet and the web are enabling more people to network, join a cause, voice opinions, and so on. The U.S. presidential candidates in 2008 used the Internet intensively to get out their messages and raise money for their campaigns. In this exercise, you’ll write a simple polling program that allows users to rate five social-consciousness issues from 1 (least important) to 10 (most important). Pick five causes that are important to you (e.g., political issues, global environ- mental issues). Use a one-dimensional array topics (of type char *) to store the five causes. To sum- marize the survey responses, use a 5-row, 10-column two-dimensional array responses (of type int), each row corresponding to an element in the topics array. When the program runs, it should ask the user to rate each issue. Have your friends and family respond to the survey. Then have the program display a summary of the results, including:

a) A tabular report with the five topics down the left side and the 10 ratings across the top, listing in each column the number of ratings received for each topic.

b) To the right of each row, show the average of the ratings for that issue. c) Which issue received the highest point total? Display both the issue and the point total. d) Which issue received the lowest point total? Display both the issue and the point total.

7.33 (CarbonFootprint Calculator: Arrays of Function Pointers) Using arrays of function point- ers, as you learned in this chapter, you can specify a set of functions that are called with the same types of arguments and return the same type of data. Governments and companies worldwide are becoming increasingly concerned with carbon footprints (annual releases of carbon dioxide into the atmosphere) from buildings burning various types of fuels for heat, vehicles burning fuels for power, and the like. Many scientists blame these greenhouse gases for the phenomenon called global warm- ing. Create three functions that help calculate the carbon footprint of a building, a car and a bicycle, respectively. Each function should input appropriate data from the user then calculate and display the carbon footprint. (Check out a few websites that explain how to calculate carbon footprints.) Each function should receive no parameters and return void. Write a program that prompts the user to enter the type of carbon footprint to calculate, then calls the corresponding function in the array of function pointers. For each type of carbon footprint, display some identifying information and the object’s carbon footprint.

8C Characters and Strings Vigorous writing is concise. A sentence should contain no unnecessary words, a paragraph no unnecessary sentences. —William Strunk, Jr.

I have made this letter longer than usual, because I lack the time to make it short. —Blaise Pascal

The difference between the almost-right word and the right word is really a large matter— it’s the difference between the lightning bug and the lightning. —Mark Twains

O b j e c t i v e s In this chapter, you’ll learn:

■ To use the functions of the character-handling library (<ctype.h>).

■ To use the string-conversion functions of the general utilities library (<stdlib.h>).

■ To use the string and character input/output functions of the standard input/output library (<stdio.h>).

■ To use the string-processing functions of the string handling library (<string.h>).

■ The power of function libraries for achieving software reusability.

310 Chapter 8 C Characters and Strings

8.1 Introduction In this chapter, we introduce the C Standard Library functions that facilitate string and character processing.1 The functions enable programs to process characters, strings, lines of text and blocks of memory.

The chapter discusses the techniques used to develop editors, word processors, page layout software, computerized typesetting systems and other kinds of text-processing soft- ware. The text manipulations performed by formatted input/output functions like printf and scanf can be implemented using the functions discussed in this chapter.

8.2 Fundamentals of Strings and Characters Characters are the fundamental building blocks of source programs. Every program is composed of a sequence of characters that—when grouped together meaningfully—is in- terpreted by the computer as a series of instructions used to accomplish a task. A program may contain character constants. A character constant is an int value represented as a character in single quotes. The value of a character constant is the integer value of the char- acter in the machine’s character set. For example, 'z' represents the integer value of z, and '\n' the integer value of newline (122 and 10 in ASCII, respectively).

A string is a series of characters treated as a single unit. A string may include letters, digits and various special characters such as +, -, *, / and $. String literals, or string con- stants, in C are written in double quotation marks as follows:

A string in C is an array of characters ending in the null character ('\0'). A string is accessed via a pointer to the first character in the string. The value of a string is the address

8.1 Introduction 8.2 Fundamentals of Strings and

Characters 8.3 Character-Handling Library 8.4 String-Conversion Functions 8.5 Standard Input/Output Library

Functions 8.6 String-Manipulation Functions of the

String-Handling Library

8.7 Comparison Functions of the String- Handling Library

8.8 Search Functions of the String- Handling Library

8.9 Memory Functions of the String- Handling Library

8.10 Other Functions of the String- Handling Library

Summary | Terminology | Self-Review Exercises | Answers to Self-Review Exercises | Exercises | Special Section: Advanced String-Manipulation Exercises |

A Challenging String-Manipulation Project | Making a Difference

1. Pointers and pointer-based entities such as arrays and strings, when misused intentionally or acciden- tally, can lead to errors and security breaches. See our Secure C Programming Resource Center (www.deitel.com/SecureC/) for articles, books, white papers and forums on this important topic.

“John Q. Doe” (a name) “99999 Main Street” (a street address) “Waltham, Massachusetts” (a city and state) “(201) 555-1212” (a telephone number)

8.2 Fundamentals of Strings and Characters 311

of its first character. Thus, in C, it is appropriate to say that a string is a pointer—in fact, a pointer to the string’s first character. In this sense, strings are like arrays, because an array is also a pointer to its first element.

A character array or a variable of type char * can be initialized with a string in a def- inition. The definitions

each initialize a variable to the string "blue". The first definition creates a 5-element array color containing the characters 'b', 'l', 'u', 'e' and '\0'. The second definition creates pointer variable colorPtr that points to the string "blue" somewhere in memory.

The preceding array definition could also have been written

When defining a character array to contain a string, the array must be large enough to store the string and its terminating null character. The preceding definition automatically de- termines the size of the array based on the number of initializers in the initializer list.

A string can be stored in an array using scanf. For example, the following statement stores a string in character array word[20]:

The string entered by the user is stored in word. Variable word is an array, which is, of course, a pointer, so the & is not needed with argument word. Recall from Section 6.4 that function scanf will read characters until a space, tab, newline or end-of-file indicator is encountered. So, it is possible that the user input could exceed 19 characters and that your program might crash! For this reason, use the conversion specifier %19s so that scanf reads

char color[] = "blue"; const char *colorPtr = "blue";

Portability Tip 8.1 When a variable of type char * is initialized with a string literal, some compilers may place the string in a location in memory where the string cannot be modified. If you might need to modify a string literal, it should be stored in a character array to ensure modifia- bility on all systems.

char color[] = { 'b', 'l', 'u', 'e', '\0' };

Common Programming Error 8.1 Not allocating sufficient space in a character array to store the null character that termi- nates a string is an error.

Common Programming Error 8.2 Printing a “string” that does not contain a terminating null character is an error.

Error-Prevention Tip 8.1 When storing a string of characters in a character array, be sure that the array is large enough to hold the largest string that will be stored. C allows strings of any length to be stored. If a string is longer than the character array in which it is to be stored, characters beyond the end of the array will overwrite data in memory following the array.

scanf( "%s", word );

312 Chapter 8 C Characters and Strings

up to 19 characters and saves the last character for the terminating null character. This pre- vents scanf from writing characters into memory beyond the end of s. (For reading input lines of arbitrary length, there is a nonstandard—yet widely supported—function read- line, usually included in stdio.h.) For a character array to be printed as a string, the array must contain a terminating null character.

8.3 Character-Handling Library The character-handling library (<ctype.h>) includes several functions that perform use- ful tests and manipulations of character data. Each function receives a character—repre- sented as an int—or EOF as an argument. As we discussed in Chapter 4, characters are often manipulated as integers, because a character in C is usually a 1-byte integer. EOF nor- mally has the value –1, and some hardware architectures do not allow negative values to be stored in char variables, so the character-handling functions manipulate characters as integers. Figure 8.1 summarizes the functions of the character-handling library.

Common Programming Error 8.3 Processing a single character as a string. A string is a pointer—probably a respectably large integer. However, a character is a small integer (ASCII values range 0–255). On many systems this causes an error, because low memory addresses are reserved for special purposes such as operating-system interrupt handlers—so “access violations” occur.

Common Programming Error 8.4 Passing a character as an argument to a function when a string is expected (and vice versa) is a compilation error.

Prototype Function description

int isdigit( int c ); Returns a true value if c is a digit and 0 (false) otherwise. int isalpha( int c ); Returns a true value if c is a letter and 0 otherwise.

int isalnum( int c ); Returns a true value if c is a digit or a letter and 0 otherwise.

int isxdigit( int c ); Returns a true value if c is a hexadecimal digit character and 0 otherwise. (See Appendix C, Number Systems, for a detailed explanation of binary numbers, octal numbers, decimal num- bers and hexadecimal numbers.)

int islower( int c ); Returns a true value if c is a lowercase letter and 0 otherwise.

int isupper( int c ); Returns a true value if c is an uppercase letter and 0 otherwise.

int tolower( int c ); If c is an uppercase letter, tolower returns c as a lowercase let- ter. Otherwise, tolower returns the argument unchanged.

int toupper( int c ); If c is a lowercase letter, toupper returns c as an uppercase let- ter. Otherwise, toupper returns the argument unchanged.

int isspace( int c ); Returns a true value if c is a white-space character—newline ('\n'), space (' '), form feed ('\f'), carriage return ('\r'), horizontal tab ('\t') or vertical tab ('\v')—and 0 otherwise.

Fig. 8.1 | Character-handling library (<ctype.h>) functions. (Part 1 of 2.)

8.3 Character-Handling Library 313

Functions isdigit, isalpha, isalnum and isxdigit Figure 8.2 demonstrates functions isdigit, isalpha, isalnum and isxdigit. Function isdigit determines whether its argument is a digit (0–9). Function isalpha determines whether its argument is an uppercase (A–Z) or lowercase letter (a–z). Function isalnum determines whether its argument is an uppercase letter, a lowercase letter or a digit. Func- tion isxdigit determines whether its argument is a hexadecimal digit (A–F, a–f, 0–9).

int iscntrl( int c ); Returns a true value if c is a control character and 0 otherwise.

int ispunct( int c ); Returns a true value if c is a printing character other than a space, a digit, or a letter and returns 0 otherwise.

int isprint( int c ); Returns a true value if c is a printing character including a space (' ') and returns 0 otherwise.

int isgraph( int c ); Returns a true value if c is a printing character other than a space (' ') and returns 0 otherwise.

1 /* Fig. 8.2: fig08_02.c 2 Using functions isdigit, isalpha, isalnum, and isxdigit */ 3 #include <stdio.h> 4 #include <ctype.h> 5 6 int main( void ) 7 { 8 printf( "%s\n%s%s\n%s%s\n\n", "According to isdigit: ", 9 ? "8 is a " : "8 is not a ", "digit",

10 ? "# is a " : "# is not a ", "digit" ); 11 12 printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n", 13 "According to isalpha:", 14 ? "A is a " : "A is not a ", "letter", 15 ? "b is a " : "b is not a ", "letter", 16 ? "& is a " : "& is not a ", "letter", 17 ? "4 is a " : "4 is not a ", "letter" ); 18 19 printf( "%s\n%s%s\n%s%s\n%s%s\n\n", 20 "According to isalnum:", 21 ? "A is a " : "A is not a ", 22 "digit or a letter", 23 ? "8 is a " : "8 is not a ", 24 "digit or a letter", 25 ? "# is a " : "# is not a ", 26 "digit or a letter" ); 27 28 printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n", 29 "According to isxdigit:",

Fig. 8.2 | Using isdigit, isalpha, isalnum and isxdigit. (Part 1 of 2.)

Prototype Function description

Fig. 8.1 | Character-handling library (<ctype.h>) functions. (Part 2 of 2.)

isdigit( '8' ) isdigit( '#' )

isalpha( 'A' ) isalpha( 'b' ) isalpha( '&' ) isalpha( '4' )

isalnum( 'A' )

isalnum( '8' )

isalnum( '#' )

314 Chapter 8 C Characters and Strings

Figure 8.2 uses the conditional operator (?:) to determine whether the string " is a " or the string " is not a " should be printed in the output for each character tested. For example, the expression

indicates that if '8' is a digit, the string "8 is a " is printed, and if '8' is not a digit (i.e., isdigit returns 0), the string "8 is not a " is printed.

Functions islower, isupper, tolower and toupper Figure 8.3 demonstrates functions islower, isupper, tolower and toupper. Function islower determines whether its argument is a lowercase letter (a–z). Function isupper determines whether its argument is an uppercase letter (A–Z). Function tolower converts an uppercase letter to a lowercase letter and returns the lowercase letter. If the argument is not an uppercase letter, tolower returns the argument unchanged. Function toupper con- verts a lowercase letter to an uppercase letter and returns the uppercase letter. If the argu- ment is not a lowercase letter, toupper returns the argument unchanged.

30 ? "F is a " : "F is not a ", 31 "hexadecimal digit", 32 ? "J is a " : "J is not a ", 33 "hexadecimal digit", 34 ? "7 is a " : "7 is not a ", 35 "hexadecimal digit", 36 ? "$ is a " : "$ is not a ", 37 "hexadecimal digit", 38 ? "f is a " : "f is not a ", 39 "hexadecimal digit" ); 40 return 0; /* indicates successful termination */ 41 } /* end main */

According to isdigit: 8 is a digit # is not a digit

According to isalpha: A is a letter b is a letter & is not a letter 4 is not a letter

According to isalnum: A is a digit or a letter 8 is a digit or a letter # is not a digit or a letter

According to isxdigit: F is a hexadecimal digit J is not a hexadecimal digit 7 is a hexadecimal digit $ is not a hexadecimal digit f is a hexadecimal digit

isdigit( '8' ) ? "8 is a " : "8 is not a "

Fig. 8.2 | Using isdigit, isalpha, isalnum and isxdigit. (Part 2 of 2.)

isxdigit( 'F' )

isxdigit( 'J' )

isxdigit( '7' )

isxdigit( '$' )

isxdigit( 'f' )

8.3 Character-Handling Library 315

1 /* Fig. 8.3: fig08_03.c 2 Using functions islower, isupper, tolower, toupper */ 3 #include <stdio.h> 4 #include <ctype.h> 5 6 int main( void ) 7 { 8 printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n", 9 "According to islower:",

10 ? "p is a " : "p is not a ", 11 "lowercase letter", 12 ? "P is a " : "P is not a ", 13 "lowercase letter", 14 ? "5 is a " : "5 is not a ", 15 "lowercase letter", 16 ? "! is a " : "! is not a ", 17 "lowercase letter" ); 18 19 printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n", 20 "According to isupper:", 21 ? "D is an " : "D is not an ", 22 "uppercase letter", 23 ? "d is an " : "d is not an ", 24 "uppercase letter", 25 ? "8 is an " : "8 is not an ", 26 "uppercase letter", 27 ? "$ is an " : "$ is not an ", 28 "uppercase letter" ); 29 30 printf( "%s%c\n%s%c\n%s%c\n%s%c\n", 31 "u converted to uppercase is ", , 32 "7 converted to uppercase is ", , 33 "$ converted to uppercase is ", , 34 "L converted to lowercase is ", ); 35 return 0; /* indicates successful termination */ 36 } /* end main */

According to islower: p is a lowercase letter P is not a lowercase letter 5 is not a lowercase letter ! is not a lowercase letter According to isupper: D is an uppercase letter d is not an uppercase letter 8 is not an uppercase letter $ is not an uppercase letter u converted to uppercase is U 7 converted to uppercase is 7 $ converted to uppercase is $ L converted to lowercase is l

Fig. 8.3 | Using functions islower, isupper, tolower and toupper.

islower( 'p' )

islower( 'P' )

islower( '5' )

islower( '!' )

isupper( 'D' )

isupper( 'd' )

isupper( '8' )

isupper( '$' )

toupper( 'u' ) toupper( '7' ) toupper( '$' ) tolower( 'L' )

316 Chapter 8 C Characters and Strings

Functions isspace, iscntrl, ispunct, isprint and isgraph Figure 8.4 demonstrates functions isspace, iscntrl, ispunct, isprint and isgraph. Function isspace determines if a character is one of the following white-space characters: space (' '), form feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal tab ('\t') or vertical tab ('\v'). Function iscntrl determines if a character is one of the following control characters: horizontal tab ('\t'), vertical tab ('\v'), form feed ('\f'), alert ('\a'), backspace ('\b'), carriage return ('\r') or newline ('\n'). Function ispunct de- termines if a character is a printing character other than a space, a digit or a letter, such as $, #, (, ), [, ], {, }, ;, : or %. Function isprint determines if a character can be displayed on the screen (including the space character). Function isgraph is the same as isprint, except that the space character is not included.

1 /* Fig. 8.4: fig08_04.c 2 Using functions isspace, iscntrl, ispunct, isprint, isgraph */ 3 #include <stdio.h> 4 #include <ctype.h> 5 6 int main( void ) 7 { 8 printf( "%s\n%s%s%s\n%s%s%s\n%s%s\n\n", 9 "According to isspace:",

10 "Newline", ? " is a " : " is not a ", 11 "whitespace character", "Horizontal tab", 12 ? " is a " : " is not a ", 13 "whitespace character", 14 ? "% is a " : "% is not a ", 15 "whitespace character" ); 16 17 printf( "%s\n%s%s%s\n%s%s\n\n", "According to iscntrl:", 18 "Newline", ? " is a " : " is not a ", 19 "control character", ? "$ is a " : 20 "$ is not a ", "control character" ); 21 22 printf( "%s\n%s%s\n%s%s\n%s%s\n\n", 23 "According to ispunct:", 24 ? "; is a " : "; is not a ", 25 "punctuation character", 26 ? "Y is a " : "Y is not a ", 27 "punctuation character", 28 ? "# is a " : "# is not a ", 29 "punctuation character" ); 30 31 printf( "%s\n%s%s\n%s%s%s\n\n", "According to isprint:", 32 ? "$ is a " : "$ is not a ", 33 "printing character", 34 "Alert", ? " is a " : " is not a ", 35 "printing character" ); 36 37 printf( "%s\n%s%s\n%s%s%s\n", "According to isgraph:", 38 ? "Q is a " : "Q is not a ", 39 "printing character other than a space",

Fig. 8.4 | Using isspace, iscntrl, ispunct, isprint and isgraph. (Part 1 of 2.)

isspace( '\n' )

isspace( '\t' )

isspace( '%' )

iscntrl( '\n' ) iscntrl( '$' )

ispunct( ';' )

ispunct( 'Y' )

ispunct( '#' )

isprint( '$' )

isprint( '\a' )

isgraph( 'Q' )

8.4 String-Conversion Functions 317

8.4 String-Conversion Functions This section presents the string-conversion functions from the general utilities library (<stdlib.h>). These functions convert strings of digits to integer and floating-point val- ues. Figure 8.5 summarizes the string-conversion functions. Note the use of const to de- clare variable nPtr in the function headers (read from right to left as “nPtr is a pointer to a character constant”); const specifies that the argument value will not be modified.

40 "Space", ? " is a " : " is not a ", 41 "printing character other than a space" ); 42 return 0; /* indicates successful termination */ 43 } /* end main */

According to isspace: Newline is a whitespace character Horizontal tab is a whitespace character % is not a whitespace character

According to iscntrl: Newline is a control character $ is not a control character

According to ispunct: ; is a punctuation character Y is not a punctuation character # is a punctuation character

According to isprint: $ is a printing character Alert is not a printing character

According to isgraph: Q is a printing character other than a space Space is not a printing character other than a space

Function prototype Function description

double atof( const char *nPtr ); Converts the string nPtr to double. int atoi( const char *nPtr ); Converts the string nPtr to int. long atol( const char *nPtr ); Converts the string nPtr to long int. double strtod( const char *nPtr, char **endPtr );

Converts the string nPtr to double.

long strtol( const char *nPtr, char **endPtr, int base ); Converts the string nPtr to long.

unsigned long strtoul( const char *nPtr, char **endPtr, int base ); Converts the string nPtr to unsigned long.

Fig. 8.5 | String-conversion functions of the general utilities library.

Fig. 8.4 | Using isspace, iscntrl, ispunct, isprint and isgraph. (Part 2 of 2.)

isgraph( ' ' )

318 Chapter 8 C Characters and Strings

Function atof Function atof (Fig. 8.6) converts its argument—a string that represents a floating-point number—to a double value. The function returns the double value. If the converted value cannot be represented—for example, if the first character of the string is a letter—the be- havior of function atof is undefined.

Function atoi Function atoi (Fig. 8.7) converts its argument—a string of digits that represents an inte- ger—to an int value. The function returns the int value. If the converted value cannot be represented, the behavior of function atoi is undefined.

1 /* Fig. 8.6: fig08_06.c 2 Using atof */ 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 int main( void ) 7 { 8 double d; /* variable to hold converted string */ 9

10 11 12 printf( "%s%.3f\n%s%.3f\n", 13 "The string \"99.0\" converted to double is ", d, 14 "The converted value divided by 2 is ", d / 2.0 ); 15 return 0; /* indicates successful termination */ 16 } /* end main */

The string "99.0" converted to double is 99.000 The converted value divided by 2 is 49.500

Fig. 8.6 | Using atof.

1 /* Fig. 8.7: fig08_07.c 2 Using atoi */ 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 int main( void ) 7 { 8 int i; /* variable to hold converted string */ 9

10 11 12 printf( "%s%d\n%s%d\n", 13 "The string \"2593\" converted to int is ", i, 14 "The converted value minus 593 is ", i - 593 ); 15 return 0; /* indicates successful termination */ 16 } /* end main */

Fig. 8.7 | Using atoi. (Part 1 of 2.)

d = atof( "99.0" );

i = atoi( "2593" );

8.4 String-Conversion Functions 319

Function atol Function atol (Fig. 8.8) converts its argument—a string of digits representing a long in- teger—to a long value. The function returns the long value. If the converted value cannot be represented, the behavior of function atol is undefined. If int and long are both stored in 4 bytes, function atoi and function atol work identically.

Function strtod Function strtod (Fig. 8.9) converts a sequence of characters representing a floating-point value to double. The function receives two arguments—a string (char *) and a pointer to a string (char **). The string contains the character sequence to be converted to double. The pointer is assigned the location of the first character after the converted portion of the string. Line 14

indicates that d is assigned the double value converted from string, and stringPtr is as- signed the location of the first character after the converted value (51.2) in string.

The string "2593" converted to int is 2593 The converted value minus 593 is 2000

1 /* Fig. 8.8: fig08_08.c 2 Using atol */ 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 int main( void ) 7 { 8 long l; /* variable to hold converted string */ 9

10 11 12 printf( "%s%ld\n%s%ld\n", 13 "The string \"1000000\" converted to long int is ", l, 14 "The converted value divided by 2 is ", l / 2 ); 15 return 0; /* indicates successful termination */ 16 } /* end main */

The string "1000000" converted to long int is 1000000 The converted value divided by 2 is 500000

Fig. 8.8 | Using atol.

d = strtod( string, &stringPtr );

1 /* Fig. 8.9: fig08_09.c 2 Using strtod */ 3 #include <stdio.h>

Fig. 8.9 | Using strtod. (Part 1 of 2.)

Fig. 8.7 | Using atoi. (Part 2 of 2.)

l = atol( "1000000" );

320 Chapter 8 C Characters and Strings

Function strtol Function strtol (Fig. 8.10) converts to long a sequence of characters representing an in- teger. The function receives three arguments—a string (char *), a pointer to a string and an integer. The string contains the character sequence to be converted. The pointer is as- signed the location of the first character after the converted portion of the string. The in- teger specifies the base of the value being converted. Line 13

indicates that x is assigned the long value converted from string. The second argument, remainderPtr, is assigned the remainder of string after the conversion. Using NULL for the second argument causes the remainder of the string to be ignored. The third argument, 0, indicates that the value to be converted can be in octal (base 8), decimal (base 10) or hexadecimal (base 16) format. The base can be specified as 0 or any value between 2 and 36. See Appendix C, Number Systems, for a detailed explanation of the octal, decimal and hexadecimal number systems. Numeric representations of integers from base 11 to base 36 use the characters A–Z to represent the values 10 to 35. For example, hexadecimal val- ues can consist of the digits 0–9 and the characters A–F. A base-11 integer can consist of the digits 0–9 and the character A. A base-24 integer can consist of the digits 0–9 and the characters A–N. A base-36 integer can consist of the digits 0–9 and the characters A–Z.

4 #include <stdlib.h> 5 6 int main( void ) 7 { 8 /* initialize string pointer */ 9

10 11 double d; /* variable to hold converted sequence */ 12 char *stringPtr; /* create char pointer */ 13 14 15 16 printf( "The string \"%s\" is converted to the\n", string ); 17 printf( "double value %.2f and the string \"%s\"\n", d, stringPtr ); 18 return 0; /* indicates successful termination */ 19 } /* end main */

The string "51.2% are admitted" is converted to the double value 51.20 and the string "% are admitted"

x = strtol( string, &remainderPtr, 0 );

1 /* Fig. 8.10: fig08_10.c 2 Using strtol */ 3 #include <stdio.h> 4 #include <stdlib.h> 5

Fig. 8.10 | Using strtol. (Part 1 of 2.)

Fig. 8.9 | Using strtod. (Part 2 of 2.)

const char *string = "51.2% are admitted"; /* initialize string */

d = strtod( string, &stringPtr );

8.4 String-Conversion Functions 321

Function strtoul Function strtoul (Fig. 8.11) converts to unsigned long a sequence of characters repre- senting an unsigned long integer. The function works identically to function strtol. The statement

in Fig. 8.11 indicates that x is assigned the unsigned long value converted from string. The second argument, &remainderPtr, is assigned the remainder of string after the con- version. The third argument, 0, indicates that the value to be converted can be in octal, decimal or hexadecimal format.

6 int main( void ) 7 { 8 9

10 char *remainderPtr; /* create char pointer */ 11 long x; /* variable to hold converted sequence */ 12 13 14 15 printf( "%s\"%s\"\n%s%ld\n%s\"%s\"\n%s%ld\n", 16 "The original string is ", string, 17 "The converted value is ", x, 18 "The remainder of the original string is ", 19 remainderPtr, 20 "The converted value plus 567 is ", x + 567 ); 21 return 0; /* indicates successful termination */ 22 } /* end main */

The original string is "-1234567abc" The converted value is -1234567 The remainder of the original string is "abc" The converted value plus 567 is -1234000

x = strtoul( string, &remainderPtr, 0 );

1 /* Fig. 8.11: fig08_11.c 2 Using strtoul */ 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 int main( void ) 7 { 8 9 unsigned long x; /* variable to hold converted sequence */

10 char *remainderPtr; /* create char pointer */ 11 12 13

Fig. 8.11 | Using strtoul. (Part 1 of 2.)

Fig. 8.10 | Using strtol. (Part 2 of 2.)

const char *string = "-1234567abc"; /* initialize string pointer */

x = strtol( string, &remainderPtr, 0 );

const char *string = "1234567abc"; /* initialize string pointer */

x = strtoul( string, &remainderPtr, 0 );

322 Chapter 8 C Characters and Strings

8.5 Standard Input/Output Library Functions This section presents several functions from the standard input/output library (<stdio.h>) specifically for manipulating character and string data. Figure 8.12 summarizes the char- acter and string input/output functions of the standard input/output library.

14 printf( "%s\"%s\"\n%s%lu\n%s\"%s\"\n%s%lu\n", 15 "The original string is ", string, 16 "The converted value is ", x, 17 "The remainder of the original string is ", 18 remainderPtr, 19 "The converted value minus 567 is ", x - 567 ); 20 return 0; /* indicates successful termination */ 21 } /* end main */

The original string is "1234567abc" The converted value is 1234567 The remainder of the original string is "abc" The converted value minus 567 is 1234000

Function prototype Function description

int getchar( void ); Inputs the next character from the standard input and returns it as an integer.

char *fgets( char *s, int n, FILE *stream);

Inputs characters from the specified stream into the array s until a newline or end-of-file character is encountered, or until n - 1 bytes are read. In this chapter, we specify the stream as stdin—the standard input stream, which is typi- cally used to read characters from the keyboard. A termi- nating null character is appended to the array. Returns the string that was read into s.

int putchar( int c ); Prints the character stored in c and returns it as an integer.

int puts( const char *s ); Prints the string s followed by a newline character. Returns a non-zero integer if successful, or EOF if an error occurs.

int sprintf( char *s, const char *format, ... );

Equivalent to printf, except the output is stored in the array s instead of printed on the screen. Returns the num- ber of characters written to s, or EOF if an error occurs.

int sscanf( char *s, const char *format, ... );

Equivalent to scanf, except the input is read from the array s rather than from the keyboard. Returns the number of items successfully read by the function, or EOF if an error occurs.

Fig. 8.12 | Standard input/output library character and string functions.

Fig. 8.11 | Using strtoul. (Part 2 of 2.)

8.5 Standard Input/Output Library Functions 323

Functions fgets and putchar Figure 8.13 uses functions fgets and putchar to read a line of text from the standard input (keyboard) and recursively output the characters of the line in reverse order. Function fgets reads characters from the standard input into its first argument—an array of chars— until a newline or the end-of-file indicator is encountered, or until the maximum number of characters is read. The maximum number of characters is one fewer than the value spec- ified in fgets’s second argument. The third argument specifies the stream from which to read characters—in this case, we use the standard input stream (stdin). A null character ('\0') is appended to the array when reading terminates. Function putchar prints its char- acter argument. The program calls recursive function reverse to print the line of text back- ward. If the first character of the array received by reverse is the null character '\0', reverse returns. Otherwise, reverse is called again with the address of the subarray be- ginning at element s[1], and character s[0] is output with putchar when the recursive call is completed. The order of the two statements in the else portion of the if statement causes reverse to walk to the terminating null character of the string before a character is printed. As the recursive calls are completed, the characters are output in reverse order.

1 /* Fig. 8.13: fig08_13.c 2 Using gets and putchar */ 3 #include <stdio.h> 4 5 6 7 int main( void ) 8 { 9 char sentence[ 80 ]; /* create char array */

10 11 printf( "Enter a line of text:\n" ); 12 13 14 15 16 printf( "\nThe line printed backward is:\n" ); 17 18 return 0; /* indicates successful termination */ 19 } /* end main */ 20 21 /* recursively outputs characters in string in reverse order */ 22 void reverse( const char * const sPtr ) 23 { 24 /* if end of the string */ 25 if ( sPtr[ 0 ] == '\0' ) { /* base case */ 26 return; 27 } /* end if */ 28 else { /* if not end of the string */ 29 30 31 } /* end else */ 32 } /* end function reverse */

Fig. 8.13 | Using fgets and putchar. (Part 1 of 2.)

void reverse( const char * const sPtr ); /* prototype */

/* use fgets to read line of text */ fgets( sentence, 80, stdin );

reverse( sentence );

reverse( &sPtr[ 1 ] ); /* recursion step */ putchar( sPtr[ 0 ] ); /* use putchar to display character */

324 Chapter 8 C Characters and Strings

Functions getchar and puts Figure 8.14 uses functions getchar and puts to read characters from the standard input into character array sentence and print the array of characters as a string. Function getchar reads a character from the standard input and returns the character as an integer. Function puts takes a string (char *) as an argument and prints the string followed by a newline character. The program stops inputting characters when getchar reads the new- line character entered by the user to end the line of text. A null character is appended to array sentence (line 19) so that the array may be treated as a string. Then, function puts prints the string contained in sentence.

Enter a line of text: Characters and Strings The line printed backward is: sgnirtS dna sretcarahC

Enter a line of text: able was I ere I saw elba The line printed backward is: able was I ere I saw elba

1 /* Fig. 8.14: fig08_14.c 2 Using getchar and puts */ 3 #include <stdio.h> 4 5 int main( void ) 6 { 7 char c; /* variable to hold character input by user */ 8 char sentence[ 80 ]; /* create char array */ 9 int i = 0; /* initialize counter i */

10 11 /* prompt user to enter line of text */ 12 13 14 15 16 17 18 19 20 21 /* use puts to display sentence */ 22 23 24 return 0; /* indicates successful termination */ 25 } /* end main */

Fig. 8.14 | Using getchar and puts. (Part 1 of 2.)

Fig. 8.13 | Using fgets and putchar. (Part 2 of 2.)

puts( "Enter a line of text:" );

/* use getchar to read each character */ while ( ( c = getchar() ) != '\n') { sentence[ i++ ] = c; } /* end while */

sentence[ i ] = '\0'; /* terminate string */

puts( "\nThe line entered was:" ); puts( sentence );

8.5 Standard Input/Output Library Functions 325

Function sprintf Figure 8.15 uses function sprintf to print formatted data into array s—an array of char- acters. The function uses the same conversion specifiers as printf (see Chapter 9 for a de- tailed discussion of formatting). The program inputs an int value and a double value to be formatted and printed to array s. Array s is the first argument of sprintf.

Function sprintf Figure 8.16 uses function sscanf to read formatted data from character array s. The func- tion uses the same conversion specifiers as scanf. The program reads an int and a double from array s and stores the values in x and y, respectively. The values of x and y are printed. Array s is the first argument of sscanf.

Enter a line of text: This is a test.

The line entered was: This is a test.

1 /* Fig. 8.15: fig08_15.c 2 Using sprintf */ 3 #include <stdio.h> 4 5 int main( void ) 6 { 7 char s[ 80 ]; /* create char array */ 8 int x; /* x value to be input */ 9 double y; /* y value to be input */

10 11 printf( "Enter an integer and a double:\n" ); 12 scanf( "%d%lf", &x, &y ); 13 14 15 16 17 18 return 0; /* indicates successful termination */ 19 } /* end main */

Enter an integer and a double: 298 87.375 The formatted output stored in array s is: integer: 298 double: 87.38

Fig. 8.15 | Using sprintf.

Fig. 8.14 | Using getchar and puts. (Part 2 of 2.)

sprintf( s, "integer:%6d\ndouble:%8.2f", x, y );

printf( "%s\n%s\n", "The formatted output stored in array s is:", s );

326 Chapter 8 C Characters and Strings

8.6 String-Manipulation Functions of the String- Handling Library The string-handling library (<string.h>) provides many useful functions for manipulat- ing string data (copying strings and concatenating strings), comparing strings, searching strings for characters and other strings, tokenizing strings (separating strings into logical pieces) and determining the length of strings. This section presents the string-manipula- tion functions of the string-handling library. The functions are summarized in Fig. 8.17. Every function—except for strncpy—appends the null character to its result.

1 /* Fig. 8.16: fig08_16.c 2 Using sscanf */ 3 #include <stdio.h> 4 5 int main( void ) 6 { 7 char s[] = "31298 87.375"; /* initialize array s */ 8 int x; /* x value to be input */ 9 double y; /* y value to be input */

10 11 12 13 14 15 return 0; /* indicates successful termination */ 16 } /* end main */

The values stored in character array s are: integer: 31298 double: 87.375

Fig. 8.16 | Using sscanf.

Function prototype Function description

char *strcpy( char *s1, const char *s2 ) Copies string s2 into array s1. The value of s1 is returned.

char *strncpy( char *s1, const char *s2, size_t n ) Copies at most n characters of string s2 into array s1. The value of s1 is returned.

char *strcat( char *s1, const char *s2 ) Appends string s2 to array s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned.

char *strncat( char *s1, const char *s2, size_t n ) Appends at most n characters of string s2 to array s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned.

Fig. 8.17 | String-manipulation functions of the string-handling library.

sscanf( s, "%d%lf", &x, &y ); printf( "%s\n%s%6d\n%s%8.3f\n", "The values stored in character array s are:", "integer:", x, "double:", y );

8.6 String-Manipulation Functions of the String-Handling Library 327

Functions strncpy and strncat specify a parameter of type size_t, which is a type defined by the C standard as the integral type of the value returned by operator sizeof.

Function strcpy copies its second argument (a string) into its first argument (a char- acter array that must be large enough to store the string and its terminating null character, which is also copied). Function strncpy is equivalent to strcpy, except that strncpy spec- ifies the number of characters to be copied from the string into the array. Function strncpy does not necessarily copy the terminating null character of its second argument. A terminating null character is written only if the number of characters to be copied is at least one more than the length of the string. For example, if "test" is the second argu- ment, a terminating null character is written only if the third argument to strncpy is at least 5 (four characters in "test" plus a terminating null character). If the third argument is larger than 5, null characters are appended to the array until the total number of char- acters specified by the third argument are written.

Functions strcpy and strncpy Figure 8.18 uses strcpy to copy the entire string in array x into array y and uses strncpy to copy the first 14 characters of array x into array z. A null character ('\0') is appended to array z, because the call to strncpy in the program does not write a terminating null character (the third argument is less than the string length of the second argument).

Portability Tip 8.2 Type size_t is a system-dependent synonym for either type unsigned long or type un- signed int.

Error-Prevention Tip 8.2 When using functions from the string-handling library, include the <string.h> header.

Common Programming Error 8.5 Not appending a terminating null character to the first argument of a strncpy when the third argument is less than or equal to the length of the string in the second argument.

1 /* Fig. 8.18: fig08_18.c 2 Using strcpy and strncpy */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main( void ) 7 { 8 char x[] = "Happy Birthday to You"; /* initialize char array x */ 9 char y[ 25 ]; /* create char array y */

10 char z[ 15 ]; /* create char array z */ 11 12 /* copy contents of x into y */ 13 printf( "%s%s\n%s%s\n", 14 "The string in array x is: ", x, 15 "The string in array y is: ",

Fig. 8.18 | Using strcpy and strncpy. (Part 1 of 2.)

strcpy( y, x ) );

328 Chapter 8 C Characters and Strings

Functions strcat and strncat Function strcat appends its second argument (a string) to its first argument (a character array containing a string). The first character of the second argument replaces the null ('\0') that terminates the string in the first argument. You must ensure that the array used to store the first string is large enough to store the first string, the second string and the terminating null character copied from the second string. Function strncat appends a specified number of characters from the second string to the first string. A terminating null character is automatically appended to the result. Figure 8.19 demonstrates function strcat and function strncat.

16 17 18 19 20 21 22 printf( "The string in array z is: %s\n", z ); 23 return 0; /* indicates successful termination */ 24 } /* end main */

The string in array x is: Happy Birthday to You The string in array y is: Happy Birthday to You The string in array z is: Happy Birthday

1 /* Fig. 8.19: fig08_19.c 2 Using strcat and strncat */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main( void ) 7 { 8 char s1[ 20 ] = "Happy "; /* initialize char array s1 */ 9 char s2[] = "New Year "; /* initialize char array s2 */

10 char s3[ 40 ] = ""; /* initialize char array s3 to empty */ 11 12 printf( "s1 = %s\ns2 = %s\n", s1, s2 ); 13 14 /* concatenate s2 to s1 */ 15 printf( "strcat( s1, s2 ) = %s\n", ); 16 17 18 19 20 21 /* concatenate s1 to s3 */ 22 printf( "strcat( s3, s1 ) = %s\n", ); 23 return 0; /* indicates successful termination */ 24 } /* end main */

Fig. 8.19 | Using strcat and strncat. (Part 1 of 2.)

Fig. 8.18 | Using strcpy and strncpy. (Part 2 of 2.)

/* copy first 14 characters of x into z. Does not copy null character */ strncpy( z, x, 14 ); z[ 14 ] = '\0'; /* terminate string in z */

strcat( s1, s2 )

/* concatenate first 6 characters of s1 to s3. Place '\0' after last character */ printf( "strncat( s3, s1, 6 ) = %s\n", strncat( s3, s1, 6 ) );

strcat( s3, s1 )

8.7 Comparison Functions of the String-Handling Library 329

8.7 Comparison Functions of the String-Handling Library This section presents the string-handling library’s string-comparison functions, strcmp and strncmp. Fig. 8.20 contains their prototypes and a brief description of each function.

Figure 8.21 compares three strings using strcmp and strncmp. Function strcmp com- pares its first string argument with its second string argument, character by character. The function returns 0 if the strings are equal, a negative value if the first string is less than the second string and a positive value if the first string is greater than the second string. Func- tion strncmp is equivalent to strcmp, except that strncmp compares up to a specified number of characters. Function strncmp does not compare characters following a null character in a string. The program prints the integer value returned by each function call.

s1 = Happy s2 = New Year strcat( s1, s2 ) = Happy New Year strncat( s3, s1, 6 ) = Happy strcat( s3, s1 ) = Happy Happy New Year

Function prototype Function description

int strcmp( const char *s1, const char *s2 ); Compares the string s1 with the string s2. The function returns 0, less than 0 or greater than 0 if s1 is equal to, less than or greater than s2, respectively.

int strncmp( const char *s1, const char *s2, size_t n ); Compares up to n characters of the string s1 with the string s2. The function returns 0, less than 0 or greater than 0 if s1 is equal to, less than or greater than s2, respectively.

Fig. 8.20 | String-comparison functions of the string-handling library.

1 /* Fig. 8.21: fig08_21.c 2 Using strcmp and strncmp */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main( void ) 7 { 8 const char *s1 = "Happy New Year"; /* initialize char pointer */ 9 const char *s2 = "Happy New Year"; /* initialize char pointer */

10 const char *s3 = "Happy Holidays"; /* initialize char pointer */ 11

Fig. 8.21 | Using strcmp and strncmp. (Part 1 of 2.)

Fig. 8.19 | Using strcat and strncat. (Part 2 of 2.)

330 Chapter 8 C Characters and Strings

To understand just what it means for one string to be “greater than” or “less than” another string, consider the process of alphabetizing a series of last names. The reader would, no doubt, place “Jones” before “Smith,” because the first letter of “Jones” comes before the first letter of “Smith” in the alphabet. But the alphabet is more than just a list of 26 letters—it is an ordered list of characters. Each letter occurs in a specific position within the list. “Z” is more than merely a letter of the alphabet; “Z” is specifically the 26th

letter of the alphabet. How does the computer know that one particular letter comes before another? All

characters are represented inside the computer as numeric codes; when the computer com- pares two strings, it actually compares the numeric codes of the characters in the strings.

In an effort to standardize character representations, most computer manufacturers have designed their machines to utilize one of two popular coding schemes—ASCII or EBCDIC. ASCII stands for “American Standard Code for Information Interchange,” and

12 printf("%s%s\n%s%s\n%s%s\n\n%s%2d\n%s%2d\n%s%2d\n\n", 13 "s1 = ", s1, "s2 = ", s2, "s3 = ", s3, 14 "strcmp(s1, s2) = ", , 15 "strcmp(s1, s3) = ", , 16 "strcmp(s3, s1) = ", ); 17 18 printf("%s%2d\n%s%2d\n%s%2d\n", 19 "strncmp(s1, s3, 6) = ", , 20 "strncmp(s1, s3, 7) = ", , 21 "strncmp(s3, s1, 7) = ", ); 22 return 0; /* indicates successful termination */ 23 } /* end main */

s1 = Happy New Year s2 = Happy New Year s3 = Happy Holidays

strcmp(s1, s2) = 0 strcmp(s1, s3) = 1 strcmp(s3, s1) = -1

strncmp(s1, s3, 6) = 0 strncmp(s1, s3, 7) = 6 strncmp(s3, s1, 7) = -6

Common Programming Error 8.6 Assuming that strcmp and strncmp return 1 when their arguments are equal is a logic error. Both functions return 0 (strangely, the equivalent of C's false value) for equality. Therefore, when testing two strings for equality, the result of function strcmp or strncmp should be compared with 0 to determine if the strings are equal.

Portability Tip 8.3 The internal numeric codes used to represent characters may be different on different com- puters.

Fig. 8.21 | Using strcmp and strncmp. (Part 2 of 2.)

strcmp( s1, s2 ) strcmp( s1, s3 ) strcmp( s3, s1 )

strncmp( s1, s3, 6 ) strncmp( s1, s3, 7 ) strncmp( s3, s1, 7 )

8.8 Search Functions of the String-Handling Library 331

EBCDIC stands for “Extended Binary Coded Decimal Interchange Code.” There are other coding schemes, but these two are the most popular. The Unicode® Standard out- lines a specification to produce consistent encoding of the vast majority of the world’s characters and symbols. To learn more about Unicode, visit www.unicode.org.

ASCII, EBCDIC and Unicode are called character sets. String and character manip- ulations actually involve the manipulation of the appropriate numeric codes and not the characters themselves. This explains the interchangeability of characters and small integers in C. Since it is meaningful to say that one numeric code is greater than, less than or equal to another numeric code, it becomes possible to relate various characters or strings to one another by referring to the character codes. Appendix B lists the ASCII character codes.

8.8 Search Functions of the String-Handling Library This section presents the functions of the string-handling library used to search strings for characters and other strings. The functions are summarized in Fig. 8.22. The functions strcspn and strspn return size_t.

Function prototype and description

char *strchr( const char *s, int c ); Locates the first occurrence of character c in string s. If c is found, a pointer to c in s is returned. Otherwise, a NULL pointer is returned.

size_t strcspn( const char *s1, const char *s2 ); Determines and returns the length of the initial segment of string s1 consisting of charac- ters not contained in string s2.

size_t strspn( const char *s1, const char *s2 ); Determines and returns the length of the initial segment of string s1 consisting only of characters contained in string s2.

char *strpbrk( const char *s1, const char *s2 ); Locates the first occurrence in string s1 of any character in string s2. If a character from string s2 is found, a pointer to the character in string s1 is returned. Otherwise, a NULL pointer is returned.

char *strrchr( const char *s, int c ); Locates the last occurrence of c in string s. If c is found, a pointer to c in string s is returned. Otherwise, a NULL pointer is returned.

char *strstr( const char *s1, const char *s2 ); Locates the first occurrence in string s1 of string s2. If the string is found, a pointer to the string in s1 is returned. Otherwise, a NULL pointer is returned.

char *strtok( char *s1, const char *s2 ); A sequence of calls to strtok breaks string s1 into “tokens”—logical pieces such as words in a line of text—separated by characters contained in string s2. The first call contains s1 as the first argument, and subsequent calls to continue tokenizing the same string contain NULL as the first argument. A pointer to the current token is returned by each call. If there are no more tokens when the function is called, NULL is returned.

Fig. 8.22 | String-manipulation functions of the string-handling library.

332 Chapter 8 C Characters and Strings

Function strchr Function strchr searches for the first occurrence of a character in a string. If the character is found, strchr returns a pointer to the character in the string; otherwise, strchr returns NULL. Figure 8.23 uses strchr to search for the first occurrences of 'a' and 'z' in the string "This is a test".

Function strcspn Function strcspn (Fig. 8.24) determines the length of the initial part of the string in its first argument that does not contain any characters from the string in its second argument. The function returns the length of the segment.

1 /* Fig. 8.23: fig08_23.c 2 Using strchr */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main( void ) 7 { 8 const char *string = "This is a test"; /* initialize char pointer */ 9 char character1 = 'a'; /* initialize character1 */

10 char character2 = 'z'; /* initialize character2 */ 11 12 /* if character1 was found in string */ 13 14 printf( "\'%c\' was found in \"%s\".\n", 15 character1, string ); 16 } /* end if */ 17 else { /* if character1 was not found */ 18 printf( "\'%c\' was not found in \"%s\".\n", 19 character1, string ); 20 } /* end else */ 21 22 /* if character2 was found in string */ 23 24 printf( "\'%c\' was found in \"%s\".\n", 25 character2, string ); 26 } /* end if */ 27 else { /* if character2 was not found */ 28 printf( "\'%c\' was not found in \"%s\".\n", 29 character2, string ); 30 } /* end else */ 31 32 return 0; /* indicates successful termination */ 33 } /* end main */

'a' was found in "This is a test". 'z' was not found in "This is a test".

Fig. 8.23 | Using strchr.

if ( strchr( string, character1 ) != NULL ) {

if ( strchr( string, character2 ) != NULL ) {

8.8 Search Functions of the String-Handling Library 333

Function strpbrk Function strpbrk searches its first string argument for the first occurrence of any character in its second string argument. If a character from the second argument is found, strpbrk returns a pointer to the character in the first argument; otherwise, strpbrk returns NULL. Figure 8.25 shows a program that locates the first occurrence in string1 of any character from string2.

1 /* Fig. 8.24: fig08_24.c 2 Using strcspn */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main( void ) 7 { 8 /* initialize two char pointers */ 9 const char *string1 = "The value is 3.14159";

10 const char *string2 = "1234567890"; 11 12 printf( "%s%s\n%s%s\n\n%s\n%s%u\n", 13 "string1 = ", string1, "string2 = ", string2, 14 "The length of the initial segment of string1", 15 "containing no characters from string2 = ", 16 ); 17 return 0; /* indicates successful termination */ 18 } /* end main */

string1 = The value is 3.14159 string2 = 1234567890 The length of the initial segment of string1 containing no characters from string2 = 13

Fig. 8.24 | Using strcspn.

1 /* Fig. 8.25: fig08_25.c 2 Using strpbrk */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main( void ) 7 { 8 const char *string1 = "This is a test"; /* initialize char pointer */ 9 const char *string2 = "beware"; /* initialize char pointer */

10 11 printf( "%s\"%s\"\n'%c'%s\n\"%s\"\n", 12 "Of the characters in ", string2, 13 , 14 " appears earliest in ", string1 ); 15 return 0; /* indicates successful termination */ 16 } /* end main */

Fig. 8.25 | Using strpbrk. (Part 1 of 2.)

strcspn( string1, string2 )

*strpbrk( string1, string2 )

334 Chapter 8 C Characters and Strings

Function strrchr Function strrchr searches for the last occurrence of the specified character in a string. If the character is found, strrchr returns a pointer to the character in the string; otherwise, strrchr returns NULL. Figure 8.26 shows a program that searches for the last occurrence of the character 'z' in the string "A zoo has many animals including zebras."

Function strspn Function strspn (Fig. 8.27) determines the length of the initial part of the string in its first argument that contains only characters from the string in its second argument. The function returns the length of the segment.

Of the characters in "beware" 'a' appears earliest in "This is a test"

1 /* Fig. 8.26: fig08_26.c 2 Using strrchr */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main( void ) 7 { 8 /* initialize char pointer */ 9 const char *string1 = "A zoo has many animals including zebras";

10 11 int c = 'z'; /* character to search for */ 12 13 printf( "%s\n%s'%c'%s\"%s\"\n", 14 "The remainder of string1 beginning with the", 15 "last occurrence of character ", c, 16 " is: ", ); 17 return 0; /* indicates successful termination */ 18 } /* end main */

The remainder of string1 beginning with the last occurrence of character 'z' is: "zebras"

Fig. 8.26 | Using strrchr.

1 /* Fig. 8.27: fig08_27.c 2 Using strspn */ 3 #include <stdio.h> 4 #include <string.h> 5

Fig. 8.27 | Using strspn. (Part 1 of 2.)

Fig. 8.25 | Using strpbrk. (Part 2 of 2.)

strrchr( string1, c )

8.8 Search Functions of the String-Handling Library 335

Function strstr Function strstr searches for the first occurrence of its second string argument in its first string argument. If the second string is found in the first string, a pointer to the location of the string in the first argument is returned. Figure 8.28 uses strstr to find the string "def" in the string "abcdefabcdef".

6 int main( void ) 7 { 8 /* initialize two char pointers */ 9 const char *string1 = "The value is 3.14159";

10 const char *string2 = "aehi lsTuv"; 11 12 printf( "%s%s\n%s%s\n\n%s\n%s%u\n", 13 "string1 = ", string1, "string2 = ", string2, 14 "The length of the initial segment of string1", 15 "containing only characters from string2 = ", 16 ); 17 return 0; /* indicates successful termination */ 18 } /* end main */

string1 = The value is 3.14159 string2 = aehi lsTuv

The length of the initial segment of string1 containing only characters from string2 = 13

1 /* Fig. 8.28: fig08_28.c 2 Using strstr */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main( void ) 7 { 8 const char *string1 = "abcdefabcdef"; /* string to search */ 9 const char *string2 = "def"; /* string to search for */

10 11 printf( "%s%s\n%s%s\n\n%s\n%s%s\n", 12 "string1 = ", string1, "string2 = ", string2, 13 "The remainder of string1 beginning with the", 14 "first occurrence of string2 is: ", 15 ); 16 return 0; /* indicates successful termination */ 17 } /* end main */

string1 = abcdefabcdef string2 = def

The remainder of string1 beginning with the first occurrence of string2 is: defabcdef

Fig. 8.28 | Using strstr.

Fig. 8.27 | Using strspn. (Part 2 of 2.)

strspn( string1, string2 )

strstr( string1, string2 )

336 Chapter 8 C Characters and Strings

Function strtok Function strtok (Fig. 8.29) is used to break a string into a series of tokens. A token is a sequence of characters separated by delimiters (usually spaces or punctuation marks, but a delimiter can be any character). For example, in a line of text, each word can be consid- ered a token, and the spaces and punctuation separating the words can be considered de- limiters.

Multiple calls to strtok are required to tokenize a string—i.e., break it into tokens (assuming that the string contains more than one token). The first call to strtok contains two arguments: a string to be tokenized, and a string containing characters that separate the tokens. In Fig. 8.29, the statement

1 /* Fig. 8.29: fig08_29.c 2 Using strtok */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main( void ) 7 { 8 /* initialize array string */ 9 char string[] = "This is a sentence with 7 tokens";

10 char *tokenPtr; /* create char pointer */ 11 12 printf( "%s\n%s\n\n%s\n", 13 "The string to be tokenized is:", string, 14 "The tokens are:" ); 15 16 17 18 /* continue tokenizing sentence until tokenPtr becomes NULL */ 19 while ( tokenPtr != NULL ) { 20 printf( "%s\n", tokenPtr ); 21 22 } /* end while */ 23 24 return 0; /* indicates successful termination */ 25 } /* end main */

The string to be tokenized is: This is a sentence with 7 tokens The tokens are: This is a sentence with 7 tokens

Fig. 8.29 | Using strtok.

tokenPtr = strtok( string, " " ); /* begin tokenizing sentence */

tokenPtr = strtok( string, " " ); /* begin tokenizing sentence */

tokenPtr = strtok( NULL, " " ); /* get next token */

8.9 Memory Functions of the String-Handling Library 337

assigns tokenPtr a pointer to the first token in string. The second argument, " ", indi- cates that tokens are separated by spaces. Function strtok searches for the first character in string that is not a delimiting character (space). This begins the first token. The func- tion then finds the next delimiting character in the string and replaces it with a null ('\0') character to terminate the current token. Function strtok saves a pointer to the next char- acter following the token in string and returns a pointer to the current token.

Subsequent strtok calls in line 21 continue tokenizing string. These calls contain NULL as their first argument. The NULL argument indicates that the call to strtok should continue tokenizing from the location in string saved by the last call to strtok. If no tokens remain when strtok is called, strtok returns NULL. You can change the delimiter string in each new call to strtok. Figure 8.29 uses strtok to tokenize the string "This is a sentence with 7 tokens". Each token is printed separately. Function strtok modifies the input string by placing \0 at the end of each token; therefore, a copy of the string should be made if the string will be used again in the program after the calls to strtok.

8.9 Memory Functions of the String-Handling Library The string-handling library functions presented in this section manipulate, compare and search blocks of memory. The functions treat blocks of memory as character arrays and can manipulate any block of data. Figure 8.30 summarizes the memory functions of the string-handling library. In the function discussions, “object” refers to a block of data.

Function prototype Function description

void *memcpy( void *s1, const void *s2, size_t n ); Copies n characters from the object pointed to by s2 into the object pointed to by s1. A pointer to the resulting object is returned.

void *memmove( void *s1, const void *s2, size_t n ); Copies n characters from the object pointed to by s2 into the object pointed to by s1. The copy is performed as if the characters were first copied from the object pointed to by s2 into a temporary array and then from the temporary array into the object pointed to by s1. A pointer to the resulting object is returned.

int memcmp( const void *s1, const void *s2, size_t n ); Compares the first n characters of the objects pointed to by s1 and s2. The function returns 0, less than 0 or greater than 0 if s1 is equal to, less than or greater than s2.

void *memchr( const void *s, int c, size_t n ); Locates the first occurrence of c (converted to unsigned char) in the first n characters of the object pointed to by s. If c is found, a pointer to c in the object is returned. Otherwise, NULL is returned.

void *memset( void *s, int c, size_t n );

Copies c (converted to unsigned char) into the first n characters of the object pointed to by s. A pointer to the result is returned.

Fig. 8.30 | Memory functions of the string-handling library.

338 Chapter 8 C Characters and Strings

The pointer parameters to these functions are declared void * so they can be used to manipulate memory for any data type. In Chapter 7, we saw that a pointer to any data type can be assigned directly to a pointer of type void *, and a pointer of type void * can be assigned directly to a pointer to any data type. For this reason, these functions can receive pointers to any data type. Because a void * pointer cannot be dereferenced, each function receives a size argument that specifies the number of characters (bytes) the function will process. For simplicity, the examples in this section manipulate character arrays (blocks of characters).

Function memcpy Function memcpy copies a specified number of characters from the object pointed to by its second argument into the object pointed to by its first argument. The function can receive a pointer to any type of object. The result of this function is undefined if the two objects overlap in memory (i.e., if they are parts of the same object)—in such cases, use memmove. Figure 8.31 uses memcpy to copy the string in array s2 to array s1.

Function memmove Function memmove, like memcpy, copies a specified number of bytes from the object pointed to by its second argument into the object pointed to by its first argument. Copying is per- formed as if the bytes were copied from the second argument into a temporary character array, then copied from the temporary array into the first argument. This allows characters from one part of a string to be copied into another part of the same string. Figure 8.32 uses memmove to copy the last 10 bytes of array x into the first 10 bytes of array x.

1 /* Fig. 8.31: fig08_31.c 2 Using memcpy */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main( void ) 7 { 8 char s1[ 17 ]; /* create char array s1 */ 9 char s2[] = "Copy this string"; /* initialize char array s2 */

10 11 12 printf( "%s\n%s\"%s\"\n", 13 "After s2 is copied into s1 with memcpy,", 14 "s1 contains ", s1 ); 15 return 0; /* indicates successful termination */ 16 } /* end main */

After s2 is copied into s1 with memcpy, s1 contains "Copy this string"

Fig. 8.31 | Using memcpy.

Common Programming Error 8.7 String-manipulation functions other than memmove that copy characters have undefined results when copying takes place between parts of the same string.

memcpy( s1, s2, 17 );

8.9 Memory Functions of the String-Handling Library 339

Function memcmp Function memcmp (Fig. 8.33) compares the specified number of characters of its first argu- ment with the corresponding characters of its second argument. The function returns a value greater than 0 if the first argument is greater than the second, returns 0 if the argu- ments are equal and returns a value less than 0 if the first argument is less than the second.

1 /* Fig. 8.32: fig08_32.c 2 Using memmove */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main( void ) 7 { 8 char x[] = "Home Sweet Home"; /* initialize char array x */ 9

10 printf( "%s%s\n", "The string in array x before memmove is: ", x ); 11 printf( "%s%s\n", "The string in array x after memmove is: ", 12 ); 13 return 0; /* indicates successful termination */ 14 } /* end main */

The string in array x before memmove is: Home Sweet Home The string in array x after memmove is: Sweet Home Home

Fig. 8.32 | Using memmove.

1 /* Fig. 8.33: fig08_33.c 2 Using memcmp */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main( void ) 7 { 8 char s1[] = "ABCDEFG"; /* initialize char array s1 */ 9 char s2[] = "ABCDXYZ"; /* initialize char array s2 */

10 11 printf( "%s%s\n%s%s\n\n%s%2d\n%s%2d\n%s%2d\n", 12 "s1 = ", s1, "s2 = ", s2, 13 "memcmp( s1, s2, 4 ) = ", , 14 "memcmp( s1, s2, 7 ) = ", , 15 "memcmp( s2, s1, 7 ) = ", ); 16 return 0; /* indicate successful termination */ 17 } /* end main */

s1 = ABCDEFG s2 = ABCDXYZ memcmp( s1, s2, 4 ) = 0 memcmp( s1, s2, 7 ) = -1 memcmp( s2, s1, 7 ) = 1

Fig. 8.33 | Using memcmp.

memmove( x, &x[ 5 ], 10 )

memcmp( s1, s2, 4 ) memcmp( s1, s2, 7 ) memcmp( s2, s1, 7 )

340 Chapter 8 C Characters and Strings

Function memchr Function memchr searches for the first occurrence of a byte, represented as unsigned char, in the specified number of bytes of an object. If the byte is found, a pointer to the byte in the object is returned; otherwise, a NULL pointer is returned. Figure 8.34 searches for the character (byte) 'r' in the string "This is a string".

Function memset Function memset copies the value of the byte in its second argument into the first n bytes of the object pointed to by its first argument, where n is specified by the third argument. Figure 8.35 uses memset to copy 'b' into the first 7 bytes of string1.

1 /* Fig. 8.34: fig08_34.c 2 Using memchr */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main( void ) 7 { 8 const char *s = "This is a string"; /* initialize char pointer */ 9

10 printf( "%s\'%c\'%s\"%s\"\n", 11 "The remainder of s after character ", 'r', 12 " is found is ", ); 13 return 0; /* indicates successful termination */ 14 } /* end main */

The remainder of s after character 'r' is found is "ring"

Fig. 8.34 | Using memchr.

1 /* Fig. 8.35: fig08_35.c 2 Using memset */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main( void ) 7 { 8 char string1[ 15 ] = "BBBBBBBBBBBBBB"; /* initialize string1 */ 9

10 printf( "string1 = %s\n", string1 ); 11 printf( "string1 after memset = %s\n", ); 12 return 0; /* indicates successful termination */ 13 } /* end main */

string1 = BBBBBBBBBBBBBB string1 after memset = bbbbbbbBBBBBBB

Fig. 8.35 | Using memset.

memchr( s, 'r', 16 )

memset( string1, 'b', 7 )

8.10 Other Functions of the String-Handling Library 341

8.10 Other Functions of the String-Handling Library The two remaining functions of the string-handling library are strerror and strlen. Figure 8.36 summarizes the strerror and strlen functions.

Function strerror Function strerror takes an error number and creates an error message string. A pointer to the string is returned. Figure 8.37 demonstrates strerror.

Function strlen Function strlen takes a string as an argument and returns the number of characters in the string—the terminating null character is not included in the length. Figure 8.38 demon- strates function strlen.

Function prototype Function description

char *strerror( int errornum );

Maps errornum into a full text string in a compiler- and locale-spe- cific manner (e.g. the message may appear in different languages based on its location). A pointer to the string is returned.

size_t strlen( const char *s ); Determines the length of string s. The number of characters pre- ceding the terminating null character is returned.

Fig. 8.36 | Other functions of the string-handling library.

1 /* Fig. 8.37: fig08_37.c 2 Using strerror */ 3 #include <stdio.h> 4 #include <string.h> 5 6 int main( void ) 7 { 8 printf( "%s\n", ); 9 return 0; /* indicates successful termination */

10 } /* end main */

No such file or directory

Fig. 8.37 | Using strerror.

1 /* Fig. 8.38: fig08_38.c 2 Using strlen */ 3 #include <stdio.h> 4 #include <string.h>

Fig. 8.38 | Using strlen. (Part 1 of 2.)

strerror( 2 )

342 Chapter 8 C Characters and Strings

5 6 int main( void ) 7 { 8 /* initialize 3 char pointers */ 9 const char *string1 = "abcdefghijklmnopqrstuvwxyz";

10 const char *string2 = "four"; 11 const char *string3 = "Boston"; 12 13 printf("%s\"%s\"%s%lu\n%s\"%s\"%s%lu\n%s\"%s\"%s%lu\n", 14 "The length of ", string1, " is ", 15 ( unsigned long ) , 16 "The length of ", string2, " is ", 17 ( unsigned long ) , 18 "The length of ", string3, " is ", 19 ( unsigned long ) ); 20 return 0; /* indicates successful termination */ 21 } /* end main */

The length of "abcdefghijklmnopqrstuvwxyz" is 26 The length of "four" is 4 The length of "Boston" is 6

Fig. 8.38 | Using strlen. (Part 2 of 2.)

strlen( string1 )

strlen( string2 )

strlen( string3 )

Summary Section 8.2 Fundamentals of Strings and Characters • Characters are the fundamental building blocks of source programs. Every program is composed

of a sequence of characters that—when grouped together meaningfully—is interpreted by the computer as a series of instructions used to accomplish a task.

• A character constant is an int value represented as a character in single quotes. The value of a character constant is the character’s integer value in the machine’s character set.

• A string is a series of characters treated as a single unit. A string may include letters, digits and various special characters such as +, -, *, / and $. String literals, or string constants, in C are writ- ten in double quotation marks.

• A string in C is an array of characters ending in the null character ('\0').

• A string is accessed via a pointer to its first character. The value of a string is the address of its first character.

• A character array or a variable of type char * can be initialized with a string in a definition.

• When defining a character array to contain a string, the array must be large enough to store the string and its terminating null character.

• A string can be stored in an array using scanf. Function scanf will read characters until a space, tab, newline or end-of-file indicator is encountered.

• For a character array to be printed as a string, the array must contain a terminating null character.

Section 8.3 Character-Handling Library • Function islower determines whether its argument is a lowercase letter (a–z).

• Function isupper determines whether its argument is an uppercase letter (A–Z).

Summary 343

• Function isdigit determines whether its argument is a digit (0–9).

• Function isalpha determines whether its argument is an uppercase letter (A–Z) or a lowercase let- ter (a–z).

• Function isalnum determines whether its argument is an uppercase letter (A–Z), a lowercase letter (a–z) or a digit (0–9).

• Function isxdigit determines whether its argument is a hexadecimal digit (A–F, a–f, 0–9).

• Function toupper converts a lowercase letter to uppercase and returns the uppercase letter.

• Function tolower converts an uppercase letter to lowercase and returns the lowercase letter.

• Function isspace determines whether its argument is one of the following white-space charac- ters: ' ' (space), '\f', '\n', '\r', '\t' or '\v'.

• Function iscntrl determines whether its argument is one of the following control characters: '\t', '\v', '\f', '\a', '\b', '\r' or '\n'.

• Function ispunct determines whether its argument is a printing character other than a space, a digit or a letter.

• Function isprint determines whether its argument is any printing character including the space character.

• Function isgraph determines whether its argument is a printing character other than the space character.

Section 8.4 String-Conversion Functions • Function atof converts its argument—a string beginning with a series of digits that represents a

floating-point number—to a double value.

• Function atoi converts its argument—a string beginning with a series of digits that represents an integer—to an int value.

• Function atol converts its argument—a string beginning with a series of digits that represents a long integer—to a long value.

• Function strtod converts a sequence of characters representing a floating-point value to double. The function receives two arguments—a string (char *) and a pointer to char *. The string con- tains the character sequence to be converted, and the location specified by the pointer to char * is assigned the address of the remainder of the string after the conversion.

• Function strtol converts a sequence of characters representing an integer to long. The function receives three arguments—a string (char *), a pointer to char * and an integer. The string con- tains the character sequence to be converted, the location specified by the pointer to char * is assigned the address of the remainder of the string after the conversion and the integer specifies the base of the value being converted.

• Function strtoul converts a sequence of characters representing an integer to unsigned long. The function receives three arguments—a string (char *), a pointer to char * and an integer. The string contains the character sequence to be converted, the location specified by the pointer to char * is assigned the address of the remainder of the string after the conversion and the integer specifies the base of the value being converted.

Section 8.5 Standard Input/Output Library Functions • Function fgets reads characters until a newline character or the end-of-file indicator is encoun-

tered. The arguments to fgets are an array of type char, the maximum number of characters that can be read and the stream from which to read. A null character ('\0') is appended to the array after reading terminates.

• Function putchar prints its character argument.

344 Chapter 8 C Characters and Strings

• Function getchar reads a single character from the standard input and returns the character as an integer. If the end-of-file indicator is encountered, getchar returns EOF.

• Function puts takes a string (char *) as an argument and prints the string followed by a newline character.

• Function sprintf uses the same conversion specifications as function printf to print formatted data into an array of type char.

• Function sscanf uses the same conversion specifications as function scanf to read formatted data from a string.

Section 8.6 String-Manipulation Functions of the String-Handling Library • Function strcpy copies its second argument (a string) into its first argument (a character array).

You must ensure that the array is large enough to store the string and its terminating null character.

• Function strncpy is equivalent to strcpy, except that a call to strncpy specifies the number of characters to be copied from the string into the array. The terminating null character will be cop- ied only if the number of characters to be copied is one more than the length of the string.

• Function strcat appends its second string argument—including the terminating null charac- ter—to its first string argument. The first character of the second string replaces the null ('\0') character of the first string. You must ensure that the array used to store the first string is large enough to store both the first string and the second string.

• Function strncat appends a specified number of characters from the second string to the first string. A terminating null character is appended to the result.

Section 8.7 Comparison Functions of the String-Handling Library • Function strcmp compares its first string argument to its second string argument, character by

character. It returns 0 if the strings are equal, returns a negative value if the first string is less than the second and returns a positive value if the first string is greater than the second.

• Function strncmp is equivalent to strcmp, except that strncmp compares a specified number of characters. If one of the strings is shorter than the number of characters specified, strncmp com- pares characters until the null character in the shorter string is encountered.

Section 8.8 Search Functions of the String-Handling Library • Function strchr searches for the first occurrence of a character in a string. If the character is

found, strchr returns a pointer to the character in the string; otherwise, strchr returns NULL.

• Function strcspn determines the length of the initial part of the string in its first argument that does not contain any characters from the string in its second argument. The function returns the length of the segment.

• Function strpbrk searches for the first occurrence in its first argument of any character in its sec- ond argument. If a character from the second argument is found, strpbrk returns a pointer to the character; otherwise, strpbrk returns NULL.

• Function strrchr searches for the last occurrence of a character in a string. If the character is found, strrchr returns a pointer to the character in the string; otherwise, strrchr returns NULL.

• Function strspn determines the length of the initial part of the string in its first argument that contains only characters from the string in its second argument. The function returns the length of the segment.

• Function strstr searches for the first occurrence of its second string argument in its first string argument. If the second string is found in the first string, a pointer to the location of the string in the first argument is returned.

Terminology 345

• A sequence of calls to strtok breaks the first string s1 into tokens that are separated by characters contained in the second string s2. The first call contains s1 as the first argument, and subsequent calls to continue tokenizing the same string contain NULL as the first argument. A pointer to the current token is returned by each call. If there are no more tokens when the function is called, a NULL pointer is returned.

Section 8.9 Memory Functions of the String-Handling Library • Function memcpy copies a specified number of characters from the object to which its second ar-

gument points into the object to which its first argument points. The function can receive a pointer to any type of object. Function memcpy manipulates the bytes of the object as characters.

• Function memmove copies a specified number of bytes from the object pointed to by its second argument to the object pointed to by its first argument. Copying is accomplished as if the bytes were copied from the second argument to a temporary character array and then copied from the temporary array to the first argument.

• Function memcmp compares the specified number of characters of its first and second arguments.

• Function memchr searches for the first occurrence of a byte, represented as unsigned char, in the specified number of bytes of an object. If the byte is found, a pointer to the byte is returned; oth- erwise, a NULL pointer is returned.

• Function memset copies its second argument, treated as an unsigned char, to a specified number of bytes of the object pointed to by the first argument.

Section 8.10 Other Functions of the String-Handling Library • Function strerror maps an integer error number into a full text string in a locale specific man-

ner. A pointer to the string is returned.

• Function strlen takes a string as an argument and returns the number of characters in the string—the terminating null character is not included in the length of the string.

Terminology ASCII (American Standard Code for

Information Interchange) 330 atof function 318 atoi function 318 atol function 319 character constant 310 character-handling library 312 character set 310 comparing string 326 concatenating strings 326 control characters 316 copying string 326 delimiter 336 determining the length of string 326 EBCDIC (Extended Binary Coded Decimal

Interchange Code) 330 fgets function 323 general utilities library (<stdlib.h>) 317 getchar function 324 hexadecimal 313 isalnum function 313

isalpha function 313 iscntrl function 316 isdigit function 313 isgraph function 316 islower function 314 isprint function 316 ispunct function 316 isspace function 316 isupper function 314 isxdigit function 313 memchr function 340 memcmp function 339 memcpy function 338 memmove function 338 memset function 340 null character (’\0’) 310 numeric code 330 printing character 316 putchar function 323 puts function 324 special character 310

346 Chapter 8 C Characters and Strings

sprintf function 325 sscanf function 325 <stdio.h> header 322 <stdlib.h> header 317 strcat function 328 strchr function 332 strcmp function 329 strcspn function 332 strerror function 341 string 310 string comparison function 329 string constant 310 string-conversion function 317 string is a pointer 311 string literal 310

strlen function 341 strncat function 327 strncmp function 329 strncpy function 327 strpbrk function 333 strrchr function 334 strstr function 335 strtod function 319 strtok function 336 strtol function 320 strtoul function 321 token 336 tokenizing strings 326 tolower function 314 toupper function 314

Self-Review Exercises 8.1 Write a single statement to accomplish each of the following. Assume that variables c (which stores a character), x, y and z are of type int, variables d, e and f are of type double, variable ptr is of type char * and arrays s1[100] and s2[100] are of type char.

a) Convert the character stored in variable c to an uppercase letter. Assign the result to variable c.

b) Determine if the value of variable c is a digit. Use the conditional operator as shown in Figs. 8.2–8.4 to print " is a " or " is not a " when the result is displayed.

c) Convert the string "1234567" to long and print the value. d) Determine if the value of variable c is a control character. Use the conditional operator

to print " is a " or " is not a " when the result is displayed. e) Read a line of text into array s1 from the keyboard. Do not use scanf. f) Print the line of text stored in array s1. Do not use printf. g) Assign ptr the location of the last occurrence of c in s1. h) Print the value of variable c. Do not use printf. i) Convert the string "8.63582" to double and print the value. j) Determine if the value of c is a letter. Use the conditional operator to print " is a " or

" is not a " when the result is displayed. k) Read a character from the keyboard and store the character in variable c. l) Assign ptr the location of the first occurrence of s2 in s1. m) Determine if the value of variable c is a printing character. Use the conditional operator

to print " is a " or " is not a " when the result is displayed. n) Read three double values into variables d, e and f from the string "1.27 10.3 9.432". o) Copy the string stored in array s2 into array s1. p) Assign ptr the location of the first occurrence in s1 of any character from s2. q) Compare the string in s1 with the string in s2. Print the result. r) Assign ptr the location of the first occurrence of c in s1. s) Use sprintf to print the values of integer variables x, y and z into array s1. Each value

should be printed with a field width of 7. t) Append 10 characters from the string in s2 to the string in s1. u) Determine the length of the string in s1. Print the result. v) Convert the string "-21" to int and print the value. w) Assign ptr to the location of the first token in s2. Tokens in the string s2 are separated

by commas (,).

Answers to Self-Review Exercises 347

8.2 Show two different methods of initializing character array vowel with the string of vowels "AEIOU".

8.3 What, if anything, prints when each of the following C statements is performed? If the statement contains an error, describe the error and indicate how to correct it. Assume the following variable definitions:

char s1[ 50 ] = "jack", s2[ 50 ] = " jill", s3[ 50 ], *sptr;

a) printf( "%c%s", toupper( s1[ 0 ] ), &s1[ 1 ] ); b) printf( "%s", strcpy( s3, s2 ) ); c) printf( "%s", strcat( strcat( strcpy( s3, s1 ), " and " ), s2 ) ); d) printf( "%u", strlen( s1 ) + strlen( s2 ) ); e) printf( "%u", strlen( s3 ) );

8.4 Find the error in each of the following program segments and explain how to correct it: a) char s[ 10 ];

strncpy( s, "hello", 5 );

printf( "%s\n", s ); b) printf( "%s", 'a' ); c) char s[ 12 ];

strcpy( s, "Welcome Home" ); d) if ( strcmp( string1, string2 ) ) {

printf( "The strings are equal\n" );

}

Answers to Self-Review Exercises 8.1 a) c = toupper( c );

b) printf( "'%c'%sdigit\n", c, isdigit( c ) ? " is a " : " is not a " ); c) printf( "%ld\n", atol( "1234567" ) ); d) printf( "'%c'%scontrol character\n",

c, iscntrl( c ) ? " is a " : " is not a " ); e) fgets( s1, 100, stdin ); f) puts( s1 ); g) ptr = strrchr( s1, c ); h) putchar( c ); i) printf( "%f\n", atof( "8.63582" ) ); j) printf( "'%c'%sletter\n", c, isalpha( c ) ? " is a " : " is not a " ); k) c = getchar(); l) ptr = strstr( s1, s2 ); m) printf( "'%c'%sprinting character\n",

c, isprint( c ) ? " is a " : " is not a " ); n) sscanf( "1.27 10.3 9.432", "%f%f%f", &d, &e, &f ); o) strcpy( s1, s2 ); p) ptr = strpbrk( s1, s2 ); q) printf( "strcmp( s1, s2 ) = %d\n", strcmp( s1, s2 ) ); r) ptr = strchr( s1, c ); s) sprintf( s1, "%7d%7d%7d", x, y, z ); t) strncat( s1, s2, 10 ); u) printf( "strlen(s1) = %u\n", strlen( s1 ) ); v) printf( "%d\n", atoi( "-21" ) ); * w) ptr = strtok( s2, "," );

348 Chapter 8 C Characters and Strings

8.2 char vowel[] = "AEIOU"; char vowel[] = { 'A', 'E', 'I', 'O', 'U', '\0' };

8.3 a) Jack b) jill c) jack and jill d) 8 e) 13

8.4 a) Error: Function strncpy does not write a terminating null character to array s, because its third argument is equal to the length of the string "hello". Correction: Make the third argument of strncpy 6, or assign '\0' to s[ 5 ].

b) Error: Attempting to print a character constant as a string. Correction: Use %c to output the character, or replace 'a' with "a".

c) Error: Character array s is not large enough to store the terminating null character. Correction: Declare the array with more elements.

d) Error: Function strcmp returns 0 if the strings are equal; therefore, the condition in the if statement is false, and the printf will not be executed. Correction: Compare the result of strcmp with 0 in the condition.

Exercises 8.5 (Character Testing) Write a program that inputs a character from the keyboard and tests the character with each of the functions in the character-handling library. The program should print the value returned by each function.

8.6 (Displaying Strings in Uppercase and Lowercase) Write a program that inputs a line of text into char array s[100]. Output the line in uppercase letters and in lowercase letters.

8.7 (Converting Strings to Integers for Calculations) Write a program that inputs four strings that represent integers, converts the strings to integers, sums the values and prints the total of the four values.

8.8 (Converting Strings to Floating Point for Calculations) Write a program that inputs four strings that represent floating-point values, converts the strings to double values, sums the values and prints the total of the four values.

8.9 (Comparing Strings) Write a program that uses function strcmp to compare two strings in- put by the user. The program should state whether the first string is less than, equal to or greater than the second string.

8.10 (Comparing Portions of Strings) Write a program that uses function strncmp to compare two strings input by the user. The program should input the number of characters to be compared, then display whether the first string is less than, equal to or greater than the second string.

8.11 (Random Sentences) Write a program that uses random number generation to create sen- tences. The program should use four arrays of pointers to char called article, noun, verb and prep- osition. The program should create a sentence by selecting a word at random from each array in the following order: article, noun, verb, preposition, article and noun. As each word is picked, it should be concatenated to the previous words in an array large enough to hold the entire sentence. The words should be separated by spaces. When the final sentence is output, it should start with a capital letter and end with a period. The program should generate 20 such sentences. The arrays should be filled as follows: The article array should contain the articles "the", "a", "one", "some" and "any"; the noun array should contain the nouns "boy", "girl", "dog", "town" and "car"; the verb array should contain th