The Waite Group's. New. Primer Plus. Second Edition. Mitchell Waite and Stephen Prata SAMS

Similar documents
Primer Plus. Fourth Edition. Stephen Prata. 800 East 96th St., Indianapolis, Indiana, USA

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

C-LANGUAGE CURRICULAM

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

CSCI 171 Chapter Outlines

Contents. Preface. Introduction. Introduction to C Programming


Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Review of the C Programming Language for Principles of Operating Systems

Practical C++ Programming

Practical C Programming. Steve Oualline

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

Review of the C Programming Language

Model Viva Questions for Programming in C lab

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

SOME ASSEMBLY REQUIRED

UNIT I : OVERVIEW OF COMPUTERS AND C-PROGRAMMING

KLiC C Programming. (KLiC Certificate in C Programming)

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Course Title: C Programming Full Marks: Course no: CSC110 Pass Marks: Nature of course: Theory + Lab Credit hours: 3

UNIT- 3 Introduction to C++

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING UNIT-1

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Problem Solving with C++

MPATE-GE 2618: C Programming for Music Technology. Syllabus

Absolute C++ Walter Savitch

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Advanced C Programming Topics

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

C Programming for Electronic Engineers

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

Model Viva Questions for Programming in C lab

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANGULATHUR

I BCA[ ] SEMESTER I CORE: C PROGRAMMING - 106A Multiple Choice Questions.

Structure of this course. C and C++ Past Exam Questions. Text books

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

C & Data Structures syllabus

Tokens, Expressions and Control Structures

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

edunepal_info

The Design of C: A Rational Reconstruction (cont.)

Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

C Programming Multiple. Choice

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

SAURASHTRA UNIVERSITY

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Programming in C and C++

A S H O R T I S H O V E R V I E W O F T H E A N S I C P R O G R A M M I N G L A N G U A G E

V.S.B ENGINEERING COLLEGE DEPARTMENT OF INFORMATION TECHNOLOGY I IT-II Semester. Sl.No Subject Name Page No. 1 Programming & Data Structures-I 2

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

File (1A) Young Won Lim 11/25/16

CS PROGRAMMING & ATA STRUCTURES I. UNIT I Part - A

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

CERTIFICATE IN WEB PROGRAMMING

Fundamentals of Programming


Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

Advanced C Programming and Introduction to Data Structures

The Design of C: A Rational Reconstruction (cont.)" Jennifer Rexford!

C Programming Language (Chapter 2 of K&R) Variables and Constants

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Computers Programming Course 5. Iulian Năstac

Introduction to Programming Using Java (98-388)

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Part I Part 1 Expressions

CS201 Some Important Definitions

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Fundamental of Programming (C)

Appendix A Developing a C Program on the UNIX system

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Computer Programming C++ (wg) CCOs

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Computers Programming Course 6. Iulian Năstac

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Problem Solving and 'C' Programming

FUNCTIONS POINTERS. Pointers. Functions

C for Electronic Engineering

MISRA-C:2012 Standards Model Summary for C / C++

A Fast Review of C Essentials Part I

Borland 105, 278, 361, 1135 Bounded array Branch instruction 7 break statement 170 BTree 873 Building a project 117 Built in data types 126

Contents. 1 Introduction to Computers, the Internet and the World Wide Web 1. 2 Introduction to C Programming 26

Short Notes of CS201

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

Chapter 14 - Advanced C Topics

Engineering Computing I

KLiC C. About. Tools: Turbo C++ Syllabus:

Transcription:

The Waite Group's New Primer Plus Second Edition Mitchell Waite and Stephen Prata SAMS PUBLISHING A Division of Prentice Hall Computer Publishing 11711 North College, Carmel, Indiana 46032 USA

Contents 1 Getting Ready 1 Whence С? 2 Why С? 2 Design Features 2 Efficiency 2 Portability '. 2 You Can Take С Home 3 Power and Flexibility 3 Programmer Oriented 4 Shortcomings 4 Whither C? 4 Using C: Seven Steps 6 Step 1: Define the Program Objectives 6 Step 2: Design the Program 6 Step 3: Write the Code 6 Step 4: Compile 7 Step 5: Run the Program 7 Step 6: Test and Debug the Program 8 Step 7: Maintain and Modify the Program 8 Commentary 8 Programming Mechanics 9 Source Code Files 10 Object Code Files, Executable Files, and Libraries 10 Preparing а С Program on a UNIX System 11 Preparing а С Program on an IBM PC 13 Integrated Development Environments (IDEs) 17 Think С 5.0 on the Macintosh 17 Why Compile? 18 Language Standards 18 Some Conventions 19 Typeface 19 Screen Output 19 Input and Output Devices 19 Chapter Summary 20 Review Questions 20 Programming Exercise 20

The Waite Group's New Primer Plus 2 Introducing С 23 A Simple Sample of С 24 The Explanation 24 Pass 1: Quick Synopsis 25 Pass 2: Details 27 The Structure of a Simple Program 33 Tips on Making Your Programs Readable 34 Taking Another Step 35 Documentation 35 Multiple Declarations 35 Multiplication 36 Printing Multiple Values 36 While We're at It...Multiple Functions 36 Debugging 38 Syntax Errors 38 Semantic Errors 39 Program State 41 Keywords 41 Chapter Summary 42 Review Questions 42 Programming Exercises 43 3 Data and С 45 A Sample Program 46 What's New in This Program? 47 Data: Variables and Constants 48 Data: Data-Type Keywords 49 Integer Versus Floating-Point Types 50 The Integer 50 The Floating-Point Number 51 С Data Types 52 The int Type 52 Other Integer Types 55 Using Characters: Type char 59 Types float and double 65 Other Types 67 Type Sizes 71 Using Data Types 72 Arguments and Pitfalls 73 One More Example 74 What Happens 74 A Possible Problem 75 Chapter Summary 76 Review Questions 77 VIII Programming Exercises 78

Character Strings and Formatted Input/Output 79 Introductory Program 80 Character Strings An Introduction 81 Type char Arrays and the Null Character 81 Using Strings 82 String Length strlen() 83 Constants and the С Preprocessor 85 Using #definean& #includetogether 89 С A Master of Disguise: Creating Aliases 89 Manifest Constants on the Job 91 Exploring and Exploiting printfo and scanf() 91 The printfi) Function 92 Using printfo 93 Conversion Specification Modifiers tot printfo 95 The Meaning of Conversion 101 Using scanf() 107 The * Modifier with printfo and scanfi) 112 Usage Tips 114 Chapter Summary 115 Review Questions 116 Programming Exercises 117 Operators, Expressions, and Statements 119 Introducing Loops 120 Fundamental Operators 122 Assignment Operator: = 122 Addition Operator: + 125 Subtraction Operator: 125 Sign Operators: - and + 125 Multiplication Operator: * 126 Division Operator: / 128 Operator Precedence 129 Precedence and the Order of Evaluation 131 Some Additional Operators 133 The sizeof Operator 133 Modulus Operator: % 134 Increment and Decrement Operators: ++ and 135 Decrementing: 138 Precedence 139 Don't Be Too Clever 140 Expressions and Statements 141 Expressions 141 Statements 142 Compound Statements (Blocks) 145

The Waite Group's New Primer Plus Type Conversions 147 The Cast Operator 149 Function with Arguments 151 K&R Function Declarations and Headings 152 An Example Program 153 Chapter Summary 155 Review Questions 156 Programming Exercises 158 6 С Control Statements: Looping 159 An Initial Example 160 Program Comments 161 C-Style Reading Loop 162 The while Statement 163 Terminating a ivhilehoop 163 When a Loop Terminates 164 while An Entry-Condition Loop 165 Syntax Points 165 Which Is Bigger: Using Relational Operators and Expressions 167 What Is Truth? 168 What Else Is True? 169 Troubles with Truth 170 Precedence of Relational Operators 172 Indefinite Loops and Counting Loops 174 The for Loop 175 Using for for Flexibility! 177 More Assignment Operators: +=, -=, *=, /=, %= 182 The Comma Operator 183 Zeno Meets the for Loop 185 An Exit-Condition Loop: do while 187 Which Loop? 189 Nested Loops 190 Discussion 191 A Nested Variation 191 Arrays 192 Using ayorloop with an Array 194 A Loop Example Using a Function Return Value 195 Program Discussion 198 Using Functions with Return Values 198 Chapter Summary 199 Review Questions 200 Programming Exercises 202 X

Contents 7 С Control Statements: Branching and Jumps 205 The if Statement 206 if Basics 207 Adding else to the if Statement 208 Another Example: Introducing getcbar() and putchar() 209 Multiple Choice: else if 212 Pairing elsewit\\ if. 215 More Nested ifs 216 Let's Get Logical 221 Precedence 223 Order of Evaluation 223 A Word-Count Program 224 The Conditional Operator:?: 227 Loop Aids: continue and break 229 The continue Statement 229 The break Statement 231 Multiple Choice: switch and break 233 Using the switch Statement 235 Reading Only the First Character of a Line 237 Multiple Labels 237 switch and if else 239 The goto Statement 240 Avoiding goto 240 Chapter Summary 244 Review Questions 245 Programming Exercises 247 8 Character Input/Output and Redirection 249 Single-Character I/O: getchar() and putchar() 250 Buffers 251 Terminating Keyboard Input 253 Files, Streams, and Keyboard Input 253 The End of File 254 Redirection and Files 257 UNIX and DOS Redirection 257 Comment 260 A Graphic Example 261 Creating a Friendlier User Interface 263 Working with Buffered Input 263 Mixing Numeric and Character Input 265 Character Sketches 269 Analyzing the Program 271 Chapter Summary 273 Review Questions 274 Programming Exercises 275 XI

The Waite Group's New Primer Plus Functions 277 Review 278 Creating and Using a Simple Function 279 Function Arguments 282 Defining a Function with an Argument: Formal Arguments 284 Prototyping a Function with Arguments 285 Calling a Function with an Argument: Actual Arguments 285 The Black Box Viewpoint 286 Returning a Value from a Function with return 286 Function Types 289 ANSI С Function Prototyping 290 The Problem 290 The ANSI Solution 291 No Arguments and Unspecified Arguments 293 Recursion 293 Recursion Revealed 293 Recursion Fundamentals 295 Tail Recursion 296 Recursion and Reversal 298 All С Functions Are Created Equal 299 Compiling Programs with Two or More Functions 300 UNIX 300 Microsoft С Versions 4.0 7.0 (Command-Line) 300 Microsoft С 6.0-7.0 (PWB) 300 QuickC 301 Turbo C/Borland С 301 Think С 301 Using Header Files 301 Finding Addresses: The & Operator 305 Altering Variables in the Calling Function 306 Pointers: A First Look 308 The Indirection Operator: * 309 Declaring Pointers 310 Using Pointers to Communicate Between Functions 311 Chapter Summary 316 Review Questions 317 Programming Exercises 317 Щ Arrays and Pointers 319 Arrays 320 Initialization and Storage Classes 320 More Array Initialization 322 Assigning Array Values 325 xii

Contents Pointers to Arrays 326 Functions, Arrays, and Pointers 329 Array Names as Arguments 330 Using Pointer Arguments 331 Comment: Pointers and Arrays 333 Pointer Operations 333 Another Example 336 Multidimensional Arrays 337 Initializing a Two-Dimensional Array 339 More Dimensions 340 Pointers and Multidimensional Arrays 340 Functions and Multidimensional Arrays 344 Planning a Program 349 General Plan 349 The read_array() Function 350 The sbow_array() Function 351 The mean() Function 352 The Result 352 Chapter Summary 354 Review Questions 355 Programming Exercises 356 11 Character Strings and String Functions 359 Defining Strings Within a Program 361 Character String Constants 361 Character String Arrays and Initialization 362 Array Versus Pointer 363 Specifying Storage Explicitly 365 Arrays of Character Strings 366 Pointers and Strings 367 String Input 368 Creating Space 369 The gets() Function 369 The scanfo Function 371 String Output 373 The puts() Function 373 The printfo Function 374 The Do-It-Yourself Option 375 String Functions 377 The strlen() Function 377 The strcato Function 378 The strcmpo Function 379 The strcpyo Function 383 The sprintfi) Function 386

The Waite Group's New Primer Plus Other String Functions 387 A String Example: Sorting Strings 388 Sorting 390 The ctype.h Character Functions 390 Command-Line Arguments 393 Command-Line Arguments in Integrated Environments 395 Command-Line Arguments with Think С 395 String to Number Conversions 396 Chapter Summary 397 Review Questions 398 Programming Exercises 400 12 File Input/Output 403 Communicating with Files 404 What Is a File? 404 Levels of I/O 405 Standard Files 406 Standard I/O 406 Checking for Command-Line Arguments 407 The fopen() Function 408 The getc() and putc() Functions 409 The fclose() Function 410 Standard Files 410 A Simple-Minded File-Condensing Program 410 File I/O: Jprintfl), fscanfo, fgets(), m&fyuts() 412 The fprintfo and fscanfi') Functions 412 The fgets() and jputs() Functions 414 Adventures in Random Access: fseek() and ftell() 416 UowfieekO and fiell() Work 417 Binary Versus Text Mode 418 Portability 418 Using Random-Access in a Text Mode 419 Portability 421 Behind the Scenes with Standard I/O 422 Other Standard I/O Functions 422 int ungetc(int c, FILE *fp) 423 intfflush(file*fi>) 423 int setvbuf(file *fp, char *buf, int mode, size_t size) 423 Binary I/O: fread() and fwrite() 424 size_t fwrite(void *ptr, size_t size, size_t nmemb, FILE *fp) 426 size-tfread(void *ptr, size_t size, size_t nmemb, FILE *Jp) 426 intfeofifile *jp) and intferror(file *fi>) 427 An Example 427 xiv

Chapter Summary 430 Review Questions 430 Programming Exercises 431 13 Storage Classes and Program Development 433 Storage Classes and Scope 434 Scope, Linkage, and Storage Duration 436 Automatic Variables 437 External Variables 438 Definitions and Declarations 441 Static Variables 442 External Static Variables 443 Multiple Files 444 Scope and Functions 444 Register Variables 444 Which Storage Class? 445 A Random Number Function 446 Roll'Em 450 Sorting Numbers 452 Global Decisions 453 Reading Numeric Data 454 The getarrayo Function 456 Sorting the Data 458 Printing the Data 460 Results 461 Comments 462 ANSI С Type Qualifiers 462 The consttype Qualifier 462 The volatiletypc Qualifier 463 Chapter Summary 464 Review Questions 465 Programming Exercises 466 14 Structures and Other Data Forms 469 Example Problem: Creating an Inventory of Books 470 Setting Up the Structure Template 471 Defining a Structure Variable 472 Initializing a Structure 473 Gaining Access to Structure Members 474 Arrays of Structures 475 Declaring an Array of Structures 477 Identifying Members of an Array of Structures 477 Program Details 479

The Waite Group's New Primer Plus Nested Structures 479 Pointers to Structures 481 Declaring and Initializing a Structure Pointer 482 Member Access by Pointer 483 Telling Functions About Structures 483 Passing Structure Members 484 Using the Structure Address 485 Passing a Structure as an Argument 486 More on the New, Improved Structure Status 487 Structures or Pointer to Structures? 490 Functions Using an Array of Structures 491 Saving the Structure Contents in a File 493 Program Points 496 Structures: What Next? 497 Unions A Quick Look 498 typedef- A Quick Look 501 Fancy Declarations 504 Functions and Pointers 505 Chapter Summary 507 Review Questions 508 Programming Exercises 510 15 Bit Fiddling 513 Binary Numbers, Bits, and Bytes 514 Binary Integers 514 Signed Integers 515 Binary Floating Point 516 Other Bases 516 Octal 516 Hexadecimal 517 C's Bitwise Operators 518 Bitwise Logical Operators 518 Usage: Masks 520 Usage: Turning Bits On 521 Usage: Turning Bits Off 522 Usage: Toggling Bits 522 Usage: Checking the Value of a Bit 522 Bitwise Shift Operators 523 Programming Example 524 Another Example 525 Bit Fields 527 Bit-Field Example 528 Chapter Summary 532 Review Questions 533 Programming Exercises 534

16 The С Preprocessor and the С Library 535 Manifest Constants: #define 536 Tokens 540 Redefining Constants 540 Using Arguments with #define 541 Including the Macro Argument in a String 543 Macro or Function? 544 File Inclusion: Mnclude 545 Header Files: An Example 546 Uses for Header Files 547 Other Directives 548 The #undef Directive 548 Conditional Compilation 549 Enumerated Types 552 enum Constants 553 Default Values 553 Assigned Values 553 Usage 554 The С Library 555 Gaining Access to the С Library 555 Using the Library Descriptions 556 The Math Library 557 The General Utilities Library 559 The exit() and atexit() Functions 560 Memory Allocation: malloc() andfieef) 562 The calloc() Function 565 Storage Classes and Dynamic Memory Allocation 566 Chapter Summary 567 Review Questions 567 Programming Exercises 568 17 Advanced Data Representation 571 Exploring Data Representation 573 Beyond the Array to the Linked List 575 Using a Linked List 580 Afterthoughts 583 Abstract Data Types (ADTs) 583 Getting Abstract 585 Building an Interface 586 Using the Interface 590 Implementing the Interface 591 Getting Queued with an ADT 598 Implementing the Interface Data Representation 599 Testing the Queue 608

The Waite Group's New Primer Plus Simulating with a Queue 610 The Linked List Versus the Array 616 Binary Search Trees 620 A Binary Tree ADT 621 The Binary Search Tree Interface 622 The Binary Tree Implementation 624 Trying the Tree 631 Tree Thoughts 635 Other Directions 637 Chapter Summary 637 Review Questions 638 Programming Exercises 638 A Additional Reading 641 С Language 641 Programming 642 Reference 643 В С Operators 645 Arithmetic Operators 646 Relational Operators 646 Relational Expressions 647 Assignment Operators 647 Example 647 Logical Operators 648 Logical Expressions 648 Order of Evaluation for Logical Expressions 648 Examples 648 The Conditional Operator 648 Examples 648 Pointer-Related Operators 648 Example 649 Sign Operators 649 Structure and Union Operators 649 The Membership Operator 649 Example 649 The Indirect Membership Operator 649 Example 650 Bitwise Operators 650 Examples 650 Miscellaneous Operators 651 Example 651 xviii

Contents I ^^^B С Basic Types and Storage Classes 653 Summary: The Basic Data Types 653 Keywords 653 Signed Integers 653 Unsigned Integers 654 Characters 654 Floating Point 654 Summary: How to Declare a Simple Variable 654 Summary: Qualifiers 656 Keywords 656 General Comments 656 Properties 656 D Expressions, Statements, and Program Flow 657 Summary: Expressions and Statements 657 Expressions 657 Statements 658 Summary: The while Statement 658 Keyword 658 General Comments 658 Form 658 Examples 658 Summary: The for Statement 659 Keyword 659 General Comments 659 Form 659 Example 659 Summary: The do while Statement 659 Keywords 659 General Comments 659 Form 660 Example 660 Summary: Using if Statements for Making Choices 660 Keywords 660 General Comments 660 Form 1 660 Form 2 660 Form 3 661 Example 661 Summary: Multiple Choice with switch 661 Keyword 661 General Comments 661 Form 661 xix

The Waite Group's New Primer Plus Examples 662 Summary: Program Jumps 662 Keywords 662 General Comments 662 The break Command 662 The continue Command 663 The goto Command 663 E ASCII Table 665 F Standard I/O Functions (ANSI C) 671 G Answers to the Review Questions 675 Chapter 1 675 Chapter 2 676 Chapter 3 677 Chapter 4 678 Chapter 5 679 Chapter 6 681 Chapter 7 683 Chapter 8 684 Chapter 9 685 Chapter 10 686 Chapter 11 687 Chapter 12 689 Chapter 13 691 Chapter 14 691 Chapter 15 693 Chapter 16 694 Chapter 17 695 Index 699 XX