1. Trang chủ
  2. » Công Nghệ Thông Tin

Programming in c stephen g cochan

564 1,2K 0
Tài liệu đã được kiểm tra trùng lặp

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

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Programming in C
Tác giả Stephen G. Kochan
Trường học Sams Publishing
Chuyên ngành Programming
Thể loại sách hướng dẫn
Năm xuất bản 2005
Thành phố Indianapolis
Định dạng
Số trang 564
Dung lượng 4,95 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Đây là bộ sách tiếng anh cho dân công nghệ thông tin chuyên về bảo mật,lập trình.Thích hợp cho những ai đam mê về công nghệ thông tin,tìm hiểu về bảo mật và lập trình.

Trang 2

Programming in C

Third Edition

Trang 3

Developer’s Library

Programming in Objective-C

Stephen G Kochan 0-672-32586-1

Unix Shell Programming,Third Edition

Stephen G Kochan 0-672-32490-3

Microsoft Visual C# NET 2003Developer’s Cookbook

Mark Schmidt, Simon Robinson 0-672-32580-2

ASP.NET Developer’s Cookbook

Steven Smith, Rob Howard 0-672-32524-1

PHP and MySQL Web Development,2nd Edition

Luke T.Welling, Laura Thomson 0-672-32525-X

Advanced PHP Programming

George Schlossnagle 0-672-32561-6

Perl Developer’s Dictionary

Clinton Pierce 0-672-32067-3

MySQL, Second Edition

Paul Dubois 0-7357-1212-3

Apache Administrator’s Handbook

Rich Bowen, Daniel Ridrueio, Allan Liska

0-672-32274-9

HTTP Developer’s Handbook

Chris Shiflett 0-672-32454-7

mod_perl Developer’s Cookbook

Geoffrey Young, Paul Lindner, Randy Kobes

0-672-32240-4

PostgreSQL Developer’s Handbook

Ewald Geschwinde, Hans-Juergen Schoenig

0-672-32260-9

Trang 4

Programming in C

Sams Publishing, 800 East 96th Street, Indianapolis, Indiana 46240

DEVELOPER’S LIBRARY

Stephen G Kochan Third Edition

Trang 5

Programming in C, Third Edition

Copyright © 2005 by Sams Publishing All rights reserved No part of this book shall be reproduced, stored

in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher No patent liability is assumed with respect to the use of the information contained herein Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omis- sions Nor is any liability assumed for damages resulting from the use

of the information contained herein.

International Standard Book Number: 0-672-32666-3 Library of Congress Catalog Card Number: 2004093272 Printed in the United States of America

First Printing: July 2004

Trademarks

All terms mentioned in this book that are known to be trademarks

or service marks have been appropriately capitalized Sams Publishing cannot attest to the accuracy of this information Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark.

Warning and Disclaimer

Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied.The infor- mation provided is on an “as is” basis.The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book.

Bulk Sales

Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales For more information, please contact

U.S Corporate and Government Sales 1-800-382-3419

corpsales@pearsontechgroup.com

For sales outside of the United States, please contact

International Sales 1-317-428-3341 international@pearsontechgroup.com

Trang 6

To my mother and father

Trang 7

Contents At a Glance

Preface xvii

1 Introduction 1

2 Some Fundamentals 5

3 Compiling and Running Your First Program 11

4 Variables, Data Types, and Arithmetic Expressions 21

5 Program Looping 43

6 Making Decisions 65

7 Working with Arrays 95

8 Working with Functions 119

9 Working with Structures 165

10 Character Strings 195

11 Pointers 235

12 Operations on Bits 279

13 The Preprocessor 299

14 More on Data Types 321

15 Working with Larger Programs 333

16 Input and Output Operations in C 347

17 Miscellaneous and Advanced Features 373

18 Debugging Programs 389

19 Object-Oriented Programming 411

A C Language Summary 425

B The Standard C Library 467

C Compiling Programs with gcc 493

D Common Programming Mistakes 497

E Resources 501

Index 505

Trang 8

Table of Contents

1 Introduction 1

Programming 5Higher-Level Languages 6Operating Systems 6Compiling Programs 7Integrated Development Environments 10Language Interpreters 10

3 Compiling and Running Your First

Compiling Your Program 11Running Your Program 12Understanding Your First Program 13Displaying the Values of Variables 15Comments 17

unsigned, and signed 28Working with Arithmetic Expressions 30Integer Arithmetic and the Unary MinusOperator 33

The Modulus Operator 35Integer and Floating-Point Conversions 36

Trang 9

Combining Operations with Assignment:TheAssignment Operators 38

Types _Complexand _Imaginary 39Exercises 40

The forStatement 44Relational Operators 46Aligning Output 50Program Input 51Nested forLoops 53

forLoop Variants 54The whileStatement 56The doStatement 60The breakStatement 62The continueStatement 62Exercises 63

6 Making Decisions 65

The ifStatement 65The if-elseConstruct 69Compound Relational Tests 72Nested ifStatements 75The else ifConstruct 76The switchStatement 84Boolean Variables 87The Conditional Operator 91Exercises 93

7 Working with Arrays 95

Defining an Array 96Using Array Elements as Counters 100Generating Fibonacci Numbers 103Using an Array to Generate Prime Numbers104

Initializing Arrays 106

Trang 10

ix Contents

Character Arrays 108Base Conversion Using Arrays 109The constQualifier 111

Multidimensional Arrays 113Variable-Length Arrays 115Exercises 117

8 Working with Functions 119

Defining a Function 119Arguments and Local Variables 122Function Prototype Declaration 124Automatic Local Variables 124Returning Function Results 126Functions Calling Functions Calling… 131Declaring Return Types and Argument Types134

Checking Function Arguments 135Top-Down Programming 137

Functions and Arrays 137Assignment Operators 142Sorting Arrays 143Multidimensional Arrays 146Global Variables 152

Automatic and Static Variables 156Recursive Functions 159

Exercises 162

9 Working with Structures 165

A Structure for Storing the Date 166Using Structures in Expressions 168Functions and Structures 171

A Structure for Storing the Time 177Initializing Structures 180

Compound Literals 181Arrays of Structures 182Structures Containing Structures 185

Trang 11

Structures Containing Arrays 187Structure Variants 190

Exercises 191

10 Character Strings 195

Arrays of Characters 196Variable-Length Character Strings 198Initializing and Displaying Character Strings 201

Testing Two Character Strings for Equality 204Inputting Character Strings 206

Single-Character Input 208The Null String 213Escape Characters 216More on Constant Strings 218Character Strings, Structures, and Arrays 219

A Better Search Method 222Character Operations 227

Exercises 230

11 Pointers 235

Defining a Pointer Variable 235Using Pointers in Expressions 239Working with Pointers and Structures 240Structures Containing Pointers 243Linked Lists 244

The Keyword constand Pointers 253Pointers and Functions 254

Pointers and Arrays 259

A Slight Digression About Program Optimization 263

Is It an Array or Is It a Pointer? 264Pointers to Character Strings 266Constant Character Strings and Pointers 267The Increment and Decrement OperatorsRevisited 268

Operations on Pointers 272

Trang 12

xi Contents

Pointers to Functions 273Pointers and Memory Addresses 274Exercises 276

12 Operations on Bits 279

Bit Operators 280The Bitwise AND Operator 281The Bitwise Inclusive-OR Operator 283The Bitwise Exclusive-OR Operator 284The Ones Complement Operator 285The Left Shift Operator 287

The Right Shift Operator 287

A Shift Function 288Rotating Bits 290Bit Fields 292

Exercises 297

13 The Preprocessor 299

The #defineStatement 299Program Extendability 303Program Portability 305More Advanced Types of Definitions 306The #Operator 312

The ##Operator 313The #includeStatement 313System Include Files 316Conditional Compilation 316The #ifdef,#endif,#else, and #ifndef

Statements 316The #ifand #elifPreprocessor Statements 318

The #undefStatement 319Exercises 320

14 More on Data Types 321

Enumerated Data Types 321The typedefStatement 325

Trang 13

Data Type Conversions 327Sign Extension 329Argument Conversion 329Exercises 330

15 Working with Larger Programs 333

Dividing Your Program into Multiple Files 333Compiling Multiple Source Files from theCommand Line 334

Communication Between Modules 336External Variables 336

StaticVersus ExternVariables and Functions 339

Using Header Files Effectively 341Other Utilities for Working with Larger Programs 342

The makeUtility 343The cvsUtility 344Unix Utilities:ar,grep,sed, and so on 345

16 Input and Output Operations in C 347

Character I/O:getcharand putchar 348Formatted I/O:printfand scanf 348The printfFunction 348The scanfFunction 355Input and Output Operations with Files 359Redirecting I/O to a File 359

End of File 361Special Functions for Working with Files 363The fopenFunction 363

The getcand putcFunctions 365The fcloseFunction 365

The feofFunction 367The fprintfand fscanfFunctions 368The fgetsand fputsFunctions 368

stdin,stdout, and stderr 369The exitFunction 370

Renaming and Removing Files 371Exercises 371

Trang 14

xiii Contents

17 Miscellaneous and Advanced Features 373

Miscellaneous Language Statements 373The gotoStatement 373

The nullStatement 374Working with Unions 375The Comma Operator 378Type Qualifiers 378The registerQualifier 378The volatileQualifier 379The restrictQualifier 379Command-Line Arguments 380Dynamic Memory Allocation 383The callocand mallocFunctions 384The sizeofOperator 385

The freeFunction 387

Debugging with the Preprocessor 389Debugging Programs with gdb 395Working with Variables 398Source File Display 399Controlling Program Execution 400Getting a Stack Trace 405

Calling Functions and Setting Arrays andStructures 405

Getting Help with gdbCommands 406Odds and Ends 408

19 Object-Oriented Programming 411

What Is an Object Anyway? 411Instances and Methods 412Writing a C Program to Work with Fractions 413Defining an Objective-C Class to Work with Fractions 414

Defining a C++ Class to Work with Fractions 419Defining a C# Class to Work with Fractions 422

Trang 15

A C Language Summary 425

1.0 Digraphs and Identifiers 4251.1 Digraph Characters 4251.2 Identifiers 425

2.0 Comments 4263.0 Constants 4273.1 Integer Constants 4273.2 Floating-Point Constants 4273.3 Character Constants 4283.4 Character String Constants 4293.5 Enumeration Constants 4304.0 Data Types and Declarations 4304.1 Declarations 430

4.2 Basic Data Types 4304.3 Derived Data Types 4324.4 Enumerated Data Types 4384.5 The typedefStatement 4384.6 Type Modifiers const,volatile, and

restrict 4395.0 Expressions 4395.1 Summary of C Operators 4405.2 Constant Expressions 4425.3 Arithmetic Operators 4435.4 Logical Operators 4445.5 Relational Operators 4445.6 Bitwise Operators 4455.7 Increment and Decrement Operators 4455.8 Assignment Operators 446

5.9 Conditional Operators 4465.10 Type Cast Operator 4465.11 sizeofOperator 4475.12 Comma Operator 4475.13 Basic Operations with Arrays 4475.14 Basic Operations with Structures 4485.15 Basic Operations with Pointers 4485.16 Compound Literals 450

5.17 Conversion of Basic Data Types 451

Trang 16

xv Contents

6.0 Storage Classes and Scope 4526.1 Functions 452

6.2 Variables 4527.0 Functions 4547.1 Function Definition 4547.2 Function Call 4557.3 Function Pointers 4568.0 Statements 456

8.1 Compound Statements 4568.2 The break Statement 4568.3 The continueStatement 4578.4 The doStatement 4578.5 The forStatement 4578.6 The gotoStatement 4588.7 The ifStatement 4588.8 The nullStatement 4588.9 The returnStatement 4598.10 The switchStatement 4598.11 The whileStatement 4609.0 The Preprocessor 460

9.1 Trigraph Sequences 4609.2 Preprocessor Directives 4619.3 Predefined Identifiers 466

B The Standard C Library 467

Standard Header Files 467

Trang 17

Dynamic Memory Allocation Functions 481Math Functions 482

Complex Arithmetic 488General Utility Functions 490

C Compiling Programs with gcc 493

General Command Format 493Command-Line Options 494

E Resources 501

Answers to Exercises, Errata, etc 501The C Programming Language 501Books 501

Web Sites 502Newsgroups 502

C Compilers and Integrated DevelopmentEnvironments 502

gcc 502

CygWin 502Visual Studio 503CodeWarrior 503Kylix 503Miscellaneous 503Object-Oriented Programming 503The C++ Language 503

The C# Language 503The Objective-C Language 503Development Tools 504

Index 505

Trang 18

It’s hard to believe that 20 years have passed since I first wrote Programming in C At that time the Kernighan & Ritchie book The C Programming Language was the only other

book on the market How times have changed!

When talk about an ANSI C standard emerged in the early 1980s, this book was split

into two titles:The original was still called Programming in C, and the title that covered ANSI C was called Programming in ANSI C.This was done because it took several years

for the compiler vendors to release their ANSI C compilers and for them to becomeubiquitous I felt it was too confusing to try to cover both ANSI and non-ANSI C inthe same tutorial text, thus the reason for the split

The ANSI C standard has changed several times since the first standard was published

in 1989.The latest version, called C99, is the major reason for this edition.This editionaddresses the changes made to the language as a result of that standard

In addition to covering C99 features, this book also includes two new chapters Thefirst discusses debugging C programs.The second offers a brief overview of the pervasivefield of object-oriented programming, or OOP.This chapter was added because severalpopular OOP languages are based on C: C++, C#, Java, and Objective-C

For those who have stayed with this text through the years, I am sincerely grateful.The feedback I have received has been enormously gratifying It remains my main moti-vation for continuing to write today

For newcomers, I welcome your input and hope that this book satisfies your tions

expecta-Stephen KochanJune 2004steve@kochan-wood.com

Trang 19

About the AuthorStephen Kochanhas been developing software with the C programming language forover 20 years He is the author and coauthor of several bestselling titles on the C

language, including Programming in C, Programming in ANSI C, and Topics in C Programming, and several Unix titles, including Exploring the Unix System, Unix Shell Programming, and Unix System Security Mr Kochan’s most recent title, Programming in Objective-C, is a tutorial on an object-oriented programming language that is based on C.

Trang 20

I wish to thank the following people for their help in the preparation of various versions

of this text: Douglas McCormick, Jim Scharf, Henry Tabickman, Dick Fritz, Steve Levy,Tony Ianinno, and Ken Brown I also want to thank Henry Mullish of New YorkUniversity for teaching me so much about writing and for getting me started in thepublishing business

From Sams Publishing, I’d like to thank my development editor Mark Renfrow and

my project editor Dan Knott.Thanks also to my copy editor, Karen Annett, and mytechnical editor, Bradley Jones Finally, I’d like to thank all the other people from Samswho were involved on this project, even if I did not work with them directly

Trang 21

We Want to Hear from You

As the reader of this book, you are our most important critic and commentator.We value

your opinion and want to know what we’re doing right, what we could do better, whatareas you’d like to see us publish in, and any other words of wisdom you’re willing topass our way

As an associate publisher for Sams Publishing, I welcome your comments.You canemail or write me directly to let me know what you did or didn’t like about thisbook—as well as what we can do to make our books better

Please note that I cannot help you with technical problems related to the topic of this book.We

do have a User Services group, however, where I will forward specific technical questions related to the book.

When you write, please be sure to include this book’s title and author as well as yourname, email address, and phone number I will carefully review your comments and sharethem with the author and editors who worked on the book

Email: feedback@samspublishing.comMail: Michael Stephens

Associate PublisherSams Publishing

800 East 96th StreetIndianapolis, IN 46240 USAFor more information about this book or another Sams Publishing title, visit our Website at www.samspublishing.com.Type the ISBN (excluding hyphens) or the title of abook in the Search field to find the page you’re looking for

Trang 22

Introduction

THEC PROGRAMMING LANGUAGE WASpioneered by Dennis Ritchie at AT&T BellLaboratories in the early 1970s It was not until the late 1970s, however, that this pro-gramming language began to gain widespread popularity and support.This was becauseuntil that time C compilers were not readily available for commercial use outside of BellLaboratories Initially, C’s growth in popularity was also spurred on in part by the equal,

if not faster, growth in popularity of the Unix operating system.This operating system,which was also developed at Bell Laboratories, had C as its “standard” programming lan-guage In fact, well over 90% of the operating system itself was written in the C lan-guage!

The enormous success of the IBM PC and its look-alikes soon made MS-DOS themost popular environment for the C language As C grew in popularity across differentoperating systems, more and more vendors hopped on the bandwagon and started mar-keting their own C compilers For the most part, their version of the C language was

based on an appendix found in the first C programming text—The C Programming

Language—by Brian Kernighan and Dennis Ritchie Unfortunately, this appendix did not

provide a complete and unambiguous definition of C, meaning that vendors were left tointerpret some aspects of the language on their own

In the early 1980s, a need was seen to standardize the definition of the C language.The American National Standards Institute (ANSI) is the organization that handles suchthings, so in 1983 an ANSI C committee (called X3J11) was formed to standardize C In

1989, the committee’s work was ratified, and in 1990, the first official ANSI standard inition of C was published

def-Because C is used around the world, the International Standard Organization (ISO)soon got involved.They adopted the standard, where it was called ISO/IEC 9899:1990.Since that time, additional changes have been made to the C language.The most recentstandard was adopted in 1999 It is known as ANSI C99, or ISO/IEC 9899:1999 It isthis version of the language upon which this book is based

C is a “higher-level language,” yet it provides capabilities that enable the user to “get

in close” with the hardware and deal with the computer on a much lower level.This is

Trang 23

because, although C is a general-purpose structured programming language, it was nally designed with systems programming applications in mind and, as such, provides theuser with an enormous amount of power and flexibility.

origi-This book proposes to teach you how to program in C It assumes no previous sure to the language and was designed to appeal to novice and experienced programmersalike If you have previous programming experience, you will find that C has a uniqueway of doing things that probably differs from other languages you have used

expo-Every feature of the C language is treated in this text As each new feature is

present-ed, a small complete program example is usually provided to illustrate the feature.This

reflects the overriding philosophy that has been used in writing this book: to teach byexample Just as a picture is worth a thousand words, so is a properly chosen programexample If you have access to a computer facility that supports the C programming lan-guage, you are strongly encouraged to download and run each program presented in thisbook and to compare the results obtained on your system to those shown in the text Bydoing so, not only will you learn the language and its syntax, but you will also becomefamiliar with the process of typing in, compiling, and running C programs

You will find that program readability has been stressed throughout the book.This isbecause I strongly believe that programs should be written so that they can be easilyread—either by the author or by somebody else.Through experience and commonsense, you will find that such programs are almost always easier to write, debug, andmodify Furthermore, developing programs that are readable is a natural result of a trueadherence to a structured programming discipline

Because this book was written as a tutorial, the material covered in each chapter isbased on previously presented material.Therefore, maximum benefit will be derivedfrom this book by reading each chapter in succession, and you are highly discouragedfrom “skipping around.”You should also work through the exercises that are presented atthe end of each chapter before proceeding on to the next chapter

Chapter 2, “Some Fundamentals,” which covers some fundamental terminology abouthigher-level programming languages and the process of compiling programs, has beenincluded to ensure that you understand the language used throughout the remainder ofthe text From Chapter 3, “Compiling and Running Your First Program,” on, you will beslowly introduced to the C language By the time Chapter 16, “Input and OutputOperations in C,” rolls around, all the essential features of the language will have beencovered Chapter 16 goes into more depth about I/O operations in C Chapter 17,

“Miscellaneous and Advanced Features,” includes those features of the language that are

of a more advanced or esoteric nature

Chapter 18, “Debugging Programs,” shows how you can use the C preprocessor tohelp debug your programs It also introduces you to interactive debugging.The populardebugger gdbwas chosen to illustrate this debugging technique

Over the last decade, the programming world has been abuzz with the notion ofobject-oriented programming, or OOP for short C is not an OOP language; however,several other programming languages that are based on C are OOP languages Chapter

19, “Object-Oriented Programming,” gives a brief introduction to OOP and some of its

Trang 24

3 Introduction

terminology It also gives a brief overview of three OOP languages that are based on C,namely C++, C#, and Objective-C

Appendix A, “C Language Summary,” provides a complete summary of the languageand is provided for reference purposes

Appendix B, “The Standard C Library,” provides a summary of many of the standardlibrary routines that you will find on all systems that support C

Appendix C, “Compiling Programs with gcc,” summarizes many of the commonlyused options when compiling programs with GNU’s C compiler gcc

In Appendix D, “Common Programming Mistakes,” you’ll find a list of common gramming mistakes

pro-Finally, Appendix E, “Resources,” provides a list of resources you can turn to for moreinformation about the C language and to further your studies

Answers to the quizzes at the end of chapters can be found at www.kochan-wood.com.This book makes no assumptions about a particular computer system or operatingsystem on which the C language is implemented.The text makes brief mention of how

to compile and execute programs using the popular GNU C compiler gcc

I want to thank the following people for their help in the preparation of various sions of this text: Douglas McCormick, Jim Scharf, Henry Tabickman, Dick Fritz, SteveLevy,Tony Ianinno, and Ken Brown I also want to thank Henry Mullish of New YorkUniversity for teaching me so much about writing and for getting me started in thepublishing business

ver-An earlier edition of this book was also dedicated to the memory of MaureenConnelly, a former production editor at Hayden Book Company, the publishers of thefirst edition of this book

Trang 26

what is known as the computer’s instruction set.

To solve a problem using a computer, you must express the solution to the problem

in terms of the instructions of the particular computer A computer program is just a

col-lection of the instructions necessary to solve a specific problem.The approach or method

that is used to solve the problem is known as an algorithm For example, if you want to

develop a program that tests if a number is odd or even, the set of statements that solvesthe problem becomes the program.The method that is used to test if the number is even

or odd is the algorithm Normally, to develop a program to solve a particular problem,you first express the solution to the problem in terms of an algorithm and then develop

a program that implements that algorithm So, the algorithm for solving the even/oddproblem might be expressed as follows: First, divide the number by two If the remainder

of the division is zero, the number is even; otherwise, the number is odd.With the rithm in hand, you can then proceed to write the instructions necessary to implementthe algorithm on a particular computer system.These instructions would be expressed inthe statements of a particular computer language, such as Visual Basic, Java, C++, or C

Trang 27

algo-Higher-Level Languages

When computers were first developed, the only way they could be programmed was interms of binary numbers that corresponded directly to specific machine instructions andlocations in the computer’s memory.The next technological software advance occurred

in the development of assembly languages, which enabled the programmer to work with

the machine on a slightly higher level Instead of having to specify sequences of binarynumbers to carry out particular tasks, the assembly language permits the programmer touse symbolic names to perform various operations and to refer to specific memory loca-

tions A special program, known as an assembler, translates the assembly language program

from its symbolic format into the specific machine instructions of the computer system.Because a one-to-one correspondence still exists between each assembly languagestatement and a specific machine instruction, assembly languages are regarded as low-level languages.The programmer must still learn the instruction set of the particularcomputer system to write a program in assembly language, and the resulting program is

not portable; that is, the program will not run on a different processor type without being

rewritten.This is because different processor types have different instruction sets, andbecause assembly language programs are written in terms of these instruction sets, theyare machine dependent

Then, along came the so-called higher-level languages, of which the FORTRAN(FORmula TRANslation) language was one of the first Programmers developing pro-grams in FORTRAN no longer had to concern themselves with the architecture of theparticular computer, and operations performed in FORTRAN were of a much moresophisticated or higher level, far removed from the instruction set of the particular

machine One FORTRAN instruction or statement resulted in many different machine

instructions being executed, unlike the one-to-one correspondence found betweenassembly language statements and machine instructions

Standardization of the syntax of a higher-level language meant that a program could

be written in the language to be machine independent.That is, a program could run onany machine that supported the language with few or no changes

To support a higher-level language, a special computer program must be developedthat translates the statements of the program developed in the higher-level language into

a form that the computer can understand—in other words, into the particular

instruc-tions of the computer Such a program is known as a compiler.

Operating Systems

Before continuing with compilers, it is worthwhile to understand the role that is played

by a computer program known as an operating system.

An operating system is a program that controls the entire operation of a computersystem All input and output (that is, I/O) operations that are performed on a computersystem are channeled through the operating system.The operating system must alsomanage the computer system’s resources and must handle the execution of programs.One of the most popular operating systems today is the Unix operating system,which was developed at Bell Laboratories Unix is a rather unique operating system in

Trang 28

7 Compiling Programs

that it can be found on many different types of computer systems, and in different

“flavors,” such as Linux or Mac OS X Historically, operating systems were typically ciated with only one type of computer system But because Unix was written primarily

asso-in the C language and made very few assumptions about the architecture of the

comput-er, it has been successfully ported to many different computer systems with a relativelysmall amount of effort

Microsoft Windows XP is another example of a popular operating system.That tem is found running primarily on Pentium (or Pentium-compatible) processors

sys-Compiling Programs

A compiler is a software program that is, in principle, no different than the ones you willsee in this book, although it is certainly much more complex A compiler analyzes a pro-gram developed in a particular computer language and then translates it into a form that

is suitable for execution on your particular computer system

Figure 2.1 shows the steps that are involved in entering, compiling, and executing acomputer program developed in the C programming language and the typical Unixcommands that would be entered from the command line

The program that is to be compiled is first typed into a file on the computer system.

Computer installations have various conventions that are used for naming files, but ingeneral, the choice of the name is up to you C programs can typically be given anyname provided the last two characters are “.c” (this is not so much a requirement as it is

a convention) So, the name prog1.cmight be a valid filename for a C program on yoursystem

A text editor is usually used to enter the C program into a file For example,viis apopular text editor used on Unix systems The program that is entered into the file is

known as the source program because it represents the original form of the program

expressed in the C language After the source program has been entered into a file, youcan then proceed to have it compiled

The compilation process is initiated by typing a special command on the system

When this command is entered, the name of the file that contains the source programmust also be specified For example, under Unix, the command to initiate program com-pilation is called cc If you are using the popular GNU C compiler, the command youuse is gcc.Typing the line

1.Technically speaking, the C compiler normally makes a prepass of the program looking for cial statements.This preprocessing phase is described in detail in Chapter 13, “The Preprocessor.”

Trang 29

spe-during this phase, they are reported to the user and the compilation process ends rightthere.The errors then have to be corrected in the source program (with the use of aneditor), and the compilation process must be restarted.Typical errors reported during thisphase of compilation might be due to an expression that has unbalanced parentheses(syntactic error), or due to the use of a variable that is not “defined” (semantic error).

Start

Edit

Compile (and assemble)

Object program (file.o)

Executable object (a.out)

vi file.c

cc file.c

a.out

Libraries and other object programs

Figure 2.1 Typical steps for entering, compiling, and executing C programs

from the command line.

Trang 30

9 Compiling Programs

When all the syntactic and semantic errors have been removed from the program, thecompiler then proceeds to take each statement of the program and translate it into a

“lower” form On most systems, this means that each statement is translated by the piler into the equivalent statement or statements in assembly language needed to per-form the identical task

com-After the program has been translated into an equivalent assembly language program,the next step in the compilation process is to translate the assembly language statementsinto actual machine instructions.This step might or might not involve the execution of a

separate program known as an assembler On most systems, the assembler is executed

automatically as part of the compilation process

The assembler takes each assembly language statement and converts it into a binary

format known as object code, which is then written into another file on the system.This

file typically has the same name as the source file under Unix, with the last letter an “o”

(for object) instead of a “c” Under Windows, the suffix letters "obj"typically replace the

“c” in the filename

After the program has been translated into object code, it is ready to be linked This

process is once again performed automatically whenever the ccor gcccommand isissued under Unix.The purpose of the linking phase is to get the program into a finalform for execution on the computer If the program uses other programs that were pre-viously processed by the compiler, then during this phase the programs are linked

together Programs that are used from the system’s program library are also searched and

linked together with the object program during this phase

The process of compiling and linking a program is often called building.

The final linked file, which is in an executable object code format, is stored in another file on the system, ready to be run or executed Under Unix, this file is called a.outbydefault Under Windows, the executable file usually has the same name as the source file,with the cextension replaced by an exeextension

To subsequently execute the program, all you do is type in the name of the cutable object file So, the command

exe-a.out

has the effect of loading the program called a.outinto the computer’s memory and ating its execution

initi-When the program is executed, each of the statements of the program is sequentially

executed in turn If the program requests any data from the user, known as input, the

program temporarily suspends its execution so that the input can be entered Or, the

program might simply wait for an event, such as a mouse being clicked, to occur Results that are displayed by the program, known as output, appear in a window, sometimes called the console Or, the output might be directly written to a file on the system.

If all goes well (and it probably won’t the first time the program is executed), the gram performs its intended functions If the program does not produce the desiredresults, it is necessary to go back and reanalyze the program’s logic.This is known as the

pro-debugging phase, during which an attempt is made to remove all the known problems or bugs from the program.To do this, it will most likely be necessary to make changes to

Trang 31

the original source program In that case, the entire process of compiling, linking, andexecuting the program must be repeated until the desired results are obtained.

Integrated Development Environments

The individual steps involved in developing C programs were outlined earlier, showingtypical commands that would be entered for each step.This process of editing, compil-ing, running, and debugging programs is often managed by a single integrated applica-tion known as an Integrated Development Environment, or IDE for short An IDE is awindows-based program that allows you to easily manage large software programs, editfiles in windows, and compile, link, run, and debug your programs

On Mac OS X, CodeWarrior and Xcode are two IDEs that are used by many grammers Under Windows, Microsoft Visual Studio is a good example of a popular IDE.Kylix is a popular IDE for developing applications under Linux All the IDE applicationsgreatly simplify the entire process involved in program development so it is worth yourwhile to learn how to use one Most IDEs also support program development in severaldifferent programming languages in addition to C, such as C# and C++

pro-For more information about IDEs, consult Appendix E, “Resources.”

Language Interpreters

Before leaving this discussion of the compilation process, note that there is anothermethod used for analyzing and executing programs developed in a higher-level language

With this method, programs are not compiled but are interpreted An interpreter analyzes

and executes the statements of a program at the same time.This method usually allowsprograms to be more easily debugged On the other hand, interpreted languages are typi-cally slower than their compiled counterparts because the program statements are notconverted into their lowest-level form in advance of their execution

BASIC and Java are two programming languages in which programs are often

inter-preted and not compiled Other examples include the Unix system’s shell and Python.

Some vendors also offer interpreters for the C programming language

Trang 32

To begin with, you’ll choose a rather simple example—a program that displays thephrase “Programming is fun.” in your window Program 3.1 shows a C program toaccomplish this task.

Program 3.1 Writing Your First C Program

In the C programming language, lowercase and uppercase letters are distinct In addition,

in C, it does not matter where on the line you begin typing—you can begin typing yourstatement at any position on the line.This fact can be used to your advantage in devel-oping programs that are easier to read.Tab characters are often used by programmers as aconvenient way to indent lines

Compiling Your Program

Returning to your first C program, you first need to type it into a file Any text editorcan be used for this purpose Unix users often use an editor such as vi or emacs

Trang 33

Most C compilers recognize filenames that end in the two characters “.” and “c” as Cprograms So, assume you type Program 3.1 into a file called prog1.c Next, you need tocompile the program.

Using the GNU C compiler, this can be as simple as issuing the gcccommand at theterminal followed by the filename, like this:

$ gcc prog1.c

$

If you’re using the standard Unix C compiler, the command is ccinstead of gcc Here,the text you typed is entered in bold.The dollar sign is your command prompt if you’recompiling your C program from the command line.Your actual command prompt might

be some characters other than the dollar sign

If you make any mistakes keying in your program, the compiler lists them after youenter the gcccommand, typically identifying the line numbers from your program thatcontain the errors If, instead, another command prompt appears, as is shown in the pre-ceding example, no errors were found in your program

When the compiler compiles and links your program, it creates an executable version

of your program Using the GNU or standard C compiler, this program is called a.out

by default Under Windows, it is often called a.exeinstead

Running Your Program

You can now run the executable by simply typing its name on the command line1:

$ gcc prog1.c –o prog1

compiles the program prog1.c, placing the executable in the file prog1, which can sequently be executed just by specifying its name:

Trang 34

13 Understanding Your First Program

Understanding Your First Program

Take a closer look at your first program.The first line of the program

#include <stdio.h>

should be included at the beginning of just about every program you write It tells thecompiler information about the printfoutput routine that is used later in the program

Chapter 13, “The Preprocessor,” discusses in detail what this line does

The line of the program that reads

int main (void)

informs the system that the name of the program is main, and that it returns an integer

value, which is abbreviated “int.”mainis a special name that indicates precisely where the

program is to begin execution.The open and close parentheses immediately following

mainspecify that mainis the name of a function.The keyword voidthat is enclosed inthe parentheses specifies that the function maintakes no arguments (that is, it is void of

arguments).These concepts are explained in great detail in Chapter 8, “Working withFunctions.”

Now that you have identified mainto the system, you are ready to specify preciselywhat this routine is to perform.This is done by enclosing all program statements of theroutine within a pair of curly braces All program statements included between the bracesare taken as part of the mainroutine by the system In Program 3.1, you have only twosuch statements.The first statement specifies that a routine named printfis to be

invoked or called.The parameter or argument to be passed to the printfroutine is thestring of characters

"Programming is fun.\n"

The printfroutine is a function in the C library that simply prints or displays its ment (or arguments, as you will see shortly) on your screen.The last two characters inthe string, namely the backslash (\) and the letter n, are known collectively as the newline

argu-character A newline character tells the system to do precisely what its name implies—

that is, go to a new line Any characters to be printed after the newline character thenappear on the next line of the display In fact, the newline character is similar in concept

to the carriage return key on a typewriter (Remember those?)

All program statements in C must be terminated by a semicolon (;).This is the reasonfor the semicolon that appears immediately following the closing parenthesis of the

This exit status can be tested by other programs (such as the Unix shell) to see whetherthe program ran successfully

Trang 35

Now that you’ve finished analyzing your first program, you can modify it to also play the phrase “And programming in C is even more fun.”This can be done by thesimple addition of another call to the printfroutine, as shown in Program 3.2.

dis-Remember that every C program statement must be terminated by a semicolon.Program 3.2

#include <stdio.h>

int main (void) {

printf ("Programming is fun.\n");

printf ("And programming in C is even more fun.\n");

And programming in C is even more fun.

As you will see from the next program example, it is not necessary to make a separatecall to the printfroutine for each line of output Study the program listed in Program3.3 and try to predict the results before examining the output (No cheating now!)Program 3.3 Displaying Multiple Lines of Output

Trang 36

15 Displaying the Values of Variables

Displaying the Values of Variables

The printfroutine is the most commonly used routine in this book.This is because itprovides an easy and convenient means to display program results Not only can simple

phrases be displayed, but the values of variables and the results of computations can also

be displayed In fact, Program 3.4 uses the printfroutine to display the results of addingtwo numbers, namely 50 and 25

Program 3.4 Displaying Variables

The sum of 50 and 25 is 75

In Program 3.4, the first C program statement declares the variablesumto be of type

inte-ger C requires that all program variables be declared before they are used in a program.

The declaration of a variable specifies to the C compiler how a particular variable will

be used by the program.This information is needed by the compiler to generate the rect instructions to store and retrieve values into and out of the variable A variabledeclared as type intcan only be used to hold integral values; that is, values without dec-imal places Examples of integral values are 3, 5, –20, and 0 Numbers with decimal

cor-places, such as 3.14, 2.455, and 27.0, for example, are known as floating-point or real

num-bers

The integer variable sumis used to store the result of the addition of the two integers

50 and 25 A blank line was intentionally left following the declaration of this variable tovisually separate the variable declarations of the routine from the program statements;

this is strictly a matter of style Sometimes, the addition of a single blank line in a gram can help to make the program more readable

pro-The program statement

Trang 37

The printfroutine call in Program 3.4 now has two items or arguments enclosed

within the parentheses.These arguments are separated by a comma.The first argument tothe printfroutine is always the character string to be displayed However, along withthe display of the character string, you might also frequently want to have the value ofcertain program variables displayed In this case, you want to have the value of the vari-able sumdisplayed at the terminal after the characters

The sum of 50 and 25 is

are displayed.The percent character inside the first argument is a special character nized by the printffunction.The character that immediately follows the percent sign

recog-specifies what type of value is to be displayed at that point In the preceding program, the

letter iis recognized by the printfroutine as signifying that an integer value is to bedisplayed.2

Whenever the printfroutine finds the %icharacters inside a character string, itautomatically displays the value of the next argument to the printfroutine Because

sumis the next argument to printf, its value is automatically displayed after the ters “The sum of 50 and 25 is” are displayed

charac-Now try to predict the output from Program 3.5

Program 3.5 Displaying Multiple Values

sum = value1 + value2;

printf ("The sum of %i and %i is %i\n", value1, value2, sum);

return 0;

}

Program 3.5 Output

The sum of 50 and 25 is 75

2 Note that printf also allows you to specify %d format characters to display an integer.This book consistently uses throughout the remaining chapters.

Trang 38

17 Comments

The first program statement declares three variables called value1,value2, and sumall to

be of type int.This statement could have equivalently been expressed using three rate declaratory statements as follows:

The call to the printfroutine now contains four arguments Once again, the first

argument, commonly called the format string, describes to the system how the remaining

arguments are to be displayed.The value of value1is to be displayed immediately lowing the display of the characters “The sum of.” Similarly, the values of value2and

fol-sumare to be printed at the appropriate points, as indicated by the next two occurrences

of the %icharacters in the format string

Comments

The final program in this chapter (Program 3.6) introduces the concept of the comment.

A comment statement is used in a program to document a program and to enhance itsreadability As you will see from the following example, comments serve to tell the reader

of the program—the programmer or someone else whose responsibility it is to maintainthe program—just what the programmer had in mind when he or she wrote a particularprogram or a particular sequence of statements

Program 3.6 Using Comments in a Program

/* This program adds two integer values and displays the results */

#include <stdio.h>

int main (void) {

// Declare variables int value1, value2, sum;

// Assign values and calculate their sum value1 = 50;

value2 = 25;

sum = value1 + value2;

Trang 39

// Display the result printf ("The sum of %i and %i is %i\n", value1, value2, sum);

return 0;

}

Program 3.6 Output

The sum of 50 and 25 is 75

There are two ways to insert comments into a C program A comment can be

initiat-ed by the two characters /and *.This marks the beginning of the comment.These types

of comments have to be terminated.To end the comment, the characters *and /are usedwithout any embedded spaces All characters included between the opening /*and theclosing */are treated as part of the comment statement and are ignored by the C com-piler.This form of comment is often used when comments span several lines in the pro-gram.The second way to add a comment to your program is by using two consecutiveslash characters // Any characters that follow these slashes up to the end of the line areignored by the compiler

In Program 3.6, four separate comment statements were used.This program is wise identical to Program 3.5 Admittedly, this is a contrived example because only thefirst comment at the head of the program is useful (Yes, it is possible to insert so manycomments into a program that the readability of the program is actually degraded instead

other-of improved!)The intelligent use of comment statements inside a program cannot be overempha-sized Many times, a programmer returns to a program that he coded perhaps only sixmonths ago, only to discover to his dismay that he could not for the life of him remem-ber the purpose of a particular routine or of a particular group of statements A simplecomment statement judiciously inserted at that particular point in the program mighthave saved a significant amount of time otherwise wasted on rethinking the logic of theroutine or set of statements

It is a good idea to get into the habit of inserting comment statements into the gram as the program is being written or typed in.There are good reasons for this First, it

pro-is far easier to document the program while the particular program logic pro-is still fresh inyour mind than it is to go back and rethink the logic after the program has been com-pleted Second, by inserting comments into the program at such an early stage of thegame, you get to reap the benefits of the comments during the debug phase, when pro-gram logic errors are being isolated and debugged A comment can not only help youread through the program, but it can also help point the way to the source of the logicmistake Finally, I have yet to discover a programmer who actually enjoyed documenting

a program In fact, after you have finished debugging your program, you will probablyProgram 3.6 Continued

Trang 40

19 Exercises

not relish the idea of going back to the program to insert comments Insertingcomments while developing the program makes this sometimes tedious task a bit easier

to swallow

This concludes this introductory chapter on developing programs in C By now, youshould have a good feel as to what is involved in writing a program in C, and youshould be able to develop a small program on your own In the next chapter, you begin

to learn some of the finer intricacies of this wonderfully powerful and flexible ming language But first, try your hand at the following exercises to make certain youunderstand the concepts presented in this chapter

program-Exercises

1 Type in and run the six programs presented in this chapter Compare the outputproduced by each program with the output presented after each program in thetext

2 Write a program that prints the following text at the terminal

1 In C, lowercase letters are significant

2 main is where program execution begins

3 Opening and closing braces enclose program statements in a routine

4 All program statements must be terminated by a semicolon

3 What output would you expect from the following program?

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

TỪ KHÓA LIÊN QUAN

w