C Programming: A Modern Approach [2 ed.] 0393979504, 9780393979503

The first edition of C Programming: A Modern Approach was popular with students and faculty alike because of its clarity

262 25 49MB

English Pages 832 [830] Year 2008

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Preface
Brief Contents
Full Contents
1 Introducing C
1.1 History of C
Origins
Standardization
C-Based Languages
1.2 Strengths and Weaknesses of C
Strengths
Weaknesses
Effective Use of C
2 C Fundamentals
2.1 Writing a Simple Program
Program: Printing a Pun
Compiling and Linking
Integrated Development Environments
2.2 The General Form of a Simple Program
Directives
Functions
Statements
Printing Strings
2.3 Comments
2.4 Variables and Assignment
Types
Declarations
Assignment
Printing the Value of a Variable
Program: Computing the Dimensional Weight of a Box
Initialization
Printing Expressions
2.5 Reading Input
Program: Computing the Dimensional Weight of a Box (Revisited)
2.6 Defining Names for Constants
Program: Converting from Fahrenheit to Celsius
2.7 Identifiers
Keywords
2.8 Layout of a C Program
3 Formatted Input/Output
3.1 The printf Function
Conversion Specifications
Program: Using printf to Format Numbers
Escape Sequences
3.2 The scanf Function
How scanf Works
Ordinary Characters in Format Strings
Confusing printf with scanf
Program: Adding Fractions
4 Expressions
4.1 Arithmetic Operators
Operator Precedence and Associativity
Program: Computing a UPC Check Digit
4.2 Assignment Operators
Simple Assignment
Lvalues
Compound Assignment
4.3 Increment and Decrement Operators
4.4 Expression Evaluation
Order of Subexpression Evaluation
4.5 Expression Statements
5 Selection Statements
5.1 Logical Expressions
Relational Operators
Equality Operators
Logical Operators
5.2 The if Statement
Compound Statements
The else Clause
Cascaded if Statements
Program: Calculating a Broker's Commission
The "Dangling else" Problem
Conditional Expressions
Boolean Values in C89
Boolean Values in C99
5.3 The switch Statement
The Role of the break Statement
Program: Printing a Date in Legal Form
6 Loops
6.1 The while Statement
Infinite Loops
Program: Printing a Table of Squares
Program: Summing a Series of Numbers
6.2 The do Statement
Program: Calculating the Number of Digits in an Integer
6.3 The for Statement
for Statement Idioms
Omitting Expressions in a for Statement
for Statements in C99
The Comma Operator
Program: Printing a Table of Squares (Revisited)
6.4 Exiting from a Loop
The break Statement
The continue Statement
The goto Statement
Program: Balancing a Checkbook
6.5 The Null Statement
7 Basic Types
7.1 Integer Types
Integer Types in C99
Integer Constants
Integer Constants in C99
Integer Overflow
Reading and Writing Integers
Program: Summing a Series of Numbers (Revisited)
7.2 Floating Types
Floating Constants
Reading and Writing Floating-Point Numbers
7.3 Character Types
Operations on Characters
Signed and Unsigned Characters
Arithmetic Types
Escape Sequences
Character-Handling Functions
Reading and Writing Characters using scanf and printf
Reading and Writing Characters using getchar and putchar
Program: Determining the Length of a Message
7.4 Type Conversion
The Usual Arithmetic Conversions
Conversion During Assignment
Implicit Conversions in C99
Casting
7.5 Type Definitions
Advantages of Type Definitions
Type Definitions and Portability
7.6 The sizeof Operator
8 Arrays
8.1 One-Dimensional Arrays
Array Subscripting
Program: Reversing a Series of Numbers
Array Initialization
Designated Initializers
Program: Checking a Number for Repeated Digits
Using the sizeof Operator with Arrays
Program: Computing Interest
8.2 Multidimensional Arrays
Initializing a Multidimensional Array
Constant Arrays
Program: Dealing a Hand of Cards
8.3 Variable-Length Arrays (C99)
9 Functions
9.1 Defining and Calling Functions
Program: Computing Averages
Program: Printing a Countdown
Program: Printing a Pun (Revisited)
Function Definitions
Function Calls
Program: Testing Whether a Number Is Prime
9.2 Function Declarations
9.3 Arguments
Argument Conversions
Array Arguments
Variable-Length Array Parameters
Using static in Array Parameter Declarations
Compound Literals
9.4 The return Statement
9.5 Program Termination
The exit Function
9.6 Recursion
The Quicksort Algorithm
Program: Quicksort
10 Program Organization
10.1 Local Variables
Static Local Variables
Parameters
10.2 External Variables
Example: Using External Variables to Implement a Stack
Pros and Cons of External Variables
Program: Guessing a Number
10.3 Blocks
10.4 Scope
10.5 Organizing a C Program
Program: Classifying a Poker Hand
11 Pointers
11.1 Pointer Variables
Declaring Pointer Variables
11.2 The Address and Indirection Operators
The Address Operator
The Indirection Operator
11.3 Pointer Assignment
11.4 Pointers as Arguments
Program: Finding the Largest and Smallest Elements in an Array
Using const to Protect Arguments
11.5 Pointers as Return Values
12 Pointers and Arrays
12.1 Pointer Arithmetic
Adding an Integer to a Pointer
Subtracting an Integer from a Pointer
Subtracting One Pointer from Another
Comparing Pointers
Pointers to Compound Literals
12.2 Using Pointers for Array Processing
Combining the * and ++ Operators
12.3 Using an Array Name as a Pointer
Program: Reversing a Series of Numbers (Revisited)
Array Arguments (Revisited)
Using a Pointer as an Array Name
12.4 Pointers and Multidimensional Arrays
Processing the Elements of a Multidimensional Array
Processing the Rows of a Multidimensional Array
Processing the Columns of a Multidimensional Array
Using the Name of a Multidimensional Array as a Pointer
12.5 Pointers and Variable-Length Arrays (C99)
13 Strings
13.1 String Literals
Escape Sequences in String Literals
Continuing a String Literal
How String Literals Are Stored
Operations on String Literals
String Literals versus Character Constants
13.2 String Variables
Initializing a String Variable
Character Arrays versus Character Pointers
13.3 Reading and Writing Strings
Writing Strings Using printf and puts
Reading Strings Using scanf and gets
Reading Strings Character by Character
13.4 Accessing the Characters in a String
13.5 Using the C String Library
The strcpy (String Copy) Function
The strlen (String Length) Function
The strcat (String Concatenation) Function
The strcmp (String Comparison) Function
Program: Printing a One-Month Reminder List
13.6 String Idioms
Searching for the End of a String
Copying a String
13.7 Arrays of Strings
Command-Line Arguments
Program: Checking Planet Names
14 The Preprocessor
14.1 How the Preprocessor Works
14.2 Preprocessing Directives
14.3 Macro Definitions
Simple Macros
Parameterized Macros
The # Operator
The ## Operator
General Properties of Macros
Parentheses in Macro Definitions
Creating Longer Macros
Predefined Macros
Additional Predefined Macros in C99
Empty Macro Arguments
Macros with a Variable Number of Arguments
The __func__ Identifier
14.4 Conditional Compilation
The #if and #endif Directives
The defined Operator
The #ifdef and #ifndef Directives
The #elif and #else Directives
Uses of Conditional Compilation
14.5 Miscellaneous Directives
The #error Directive
The #line Directive
The #pragma Directive
The _Pragma Operator
15 Writing Large Programs
15.1 Source Files
15.2 Header Files
The #include Directive
Sharing Macro Definitions and Type Definitions
Sharing Function Prototypes
Sharing Variable Declarations
Nested Includes
Protecting Header Files
#error Directives in Header Files
15.3 Dividing a Program into Files
Program: Text Formatting
15.4 Building a Multiple-File Program
Makefiles
Errors During Linking
Rebuilding a Program
Defining Macros Outside a Program
16 Structures, Unions, and Enumerations
16.1 Structure Variables
Declaring Structure Variables
Initializing Structure Variables
Designated Initializers
Operations on Structures
16.2 Structure Types
Declaring a Structure Tag
Defining a Structure Type
Structures as Arguments and Return Values
Compound Literals
16.3 Nested Arrays and Structures
Nested Structures
Arrays of Structures
Initializing an Array of Structures
Program: Maintaining a Parts Database
16.4 Unions
Using Unions to Save Space
Using Unions to Build Mixed Data Structures
Adding a "Tag Field" to a Union
16.5 Enumerations
Enumeration Tags and Type Names
Enumerations as Integers
Using Enumerations to Declare "Tag Fields"
17 Advanced Uses of Pointers
17.1 Dynamic Storage Allocation
Memory Allocation Functions
Null Pointers
17.2 Dynamically Allocated Strings
Using malloc to Allocate Memory for a String
Using Dynamic Storage Allocation in String Functions
Arrays of Dynamically Allocated Strings
Program: Printing a One-Month Reminder List (Revisited)
17.3 Dynamically Allocated Arrays
Using malloc to Allocate Storage for an Array
The calloc Function
The realloc Function
17.4 Deallocating Storage
The free Function
The "Dangling Pointer" Problem
17.5 Linked Lists
Declaring a Node Type
Creating a Node
The -> Operator
Inserting a Node at the Beginning of a Linked List
Searching a Linked List
Deleting a Node from a Linked List
Ordered Lists
Program: Maintaining a Parts Database (Revisited)
17.6 Pointers to Pointers
17.7 Pointers to Functions
Function Pointers as Arguments
The qsort Function
Other Uses of Function Pointers
Program: Tabulating the Trigonometric Functions
17.8 Restricted Pointers (C99)
17.9 Flexible Array Members (C99)
18 Declarations
18.1 Declaration Syntax
18.2 Storage Classes
Properties of Variables
The auto Storage Class
The static Storage Class
The extern Storage Class
The register Storage Class
The Storage Class of a Function
Summary
18.3 Type Qualifiers
18.4 Declarators
Deciphering Complex Declarations
Using Type Definitions to Simplify Declarations
18.5 Initializers
Uninitialized Variables
18.6 Inline Functions (C99)
Inline Definitions
Restrictions on Inline Functions
Using Inline Functions with GCC
19 Program Design
19.1 Modules
Cohesion and Coupling
Types of Modules
19.2 Information Hiding
A Stack Module
19.3 Abstract Data Types
Encapsulation
Incomplete Types
19.4 A Stack Abstract Data Type
Defining the Interface for the Stack ADT
Implementing the Stack ADT Using a Fixed-Length Array
Changing the Item Type in the Stack ADT
Implementing the Stack ADT Using a Dynamic Array
Implementing the Stack ADT Using a Linked List
19.5 Design Issues for Abstract Data Types
Naming Conventions
Error Handling
Generic ADTs
ADTs in Newer Languages
20 Low-Level Programming
20.1 Bitwise Operators
Bitwise Shift Operators
Bitwise Complement, And, Exclusive Or, and Inclusive Or
Using the Bitwise Operators to Access Bits
Using the Bitwise Operators to Access Bit-Fields
Program: XOR Encryption
20.2 Bit-Fields in Structures
How Bit-Fields Are Stored
20.3 Other Low-Level Techniques
Defining Machine-Dependent Types
Using Unions to Provide Multiple Views of Data
Using Pointers as Addresses
Program: Viewing Memory Locations
The volatile Type Qualifier
21 The Standard Library
21.1 Using the Library
Restrictions on Names Used in the Library
Functions Hidden by Macros
21.2 C89 Library Overview
21.3 C99 Library Changes
21.4 The Header: Common Definitions
21.5 The Header (C99): Boolean Type and Values
22 Input/Output
22.1 Streams
File Pointers
Standard Streams and Redirection
Text Files versus Binary Files
22.2 File Operations
Opening a File
Modes
Closing a File
Attaching a File to an Open Stream
Obtaining File Names from the Command Line
Program: Checking Whether a File Can Be Opened
Temporary Files
File Buffering
Miscellaneous File Operations
22.3 Formatted I/O
The …printf Functions
…printf Conversion Specifications
C99 Changes to …printf Conversion Specifications
Examples of …printf Conversion Specifications
The …scanf Functions
…scanf Format Strings
…scanf Conversion Specifications
C99 Changes to …scanf Conversion Specifications
scanf Examples
Detecting End-of-File and Error Conditions
22.4 Character I/O
Output Functions
Input Functions
Program: Copying a File
22.5 Line I/O
Output Functions
Input Functions
22.6 Block I/O
22.7 File Positioning
Program: Modifying a File of Part Records
22.8 String I/O
Output Functions
Input Functions
23 Library Support for Numbers and Character Data
23.1 The Header: Characteristics of Floating Types
23.2 The Header: Sizes of Integer Types
23.3 The Header (C89): Mathematics
Errors
Trigonometric Functions
Hyperbolic Functions
Exponential and Logarithmic Functions
Power Functions
Nearest Integer, Absolute Value, and Remainder Functions
23.4 The Header (C99): Mathematics
IEEE Floating-Point Standard
Types
Macros
Errors
Functions
Classification Macros
Trigonometric Functions
Hyperbolic Functions
Exponential and Logarithmic Functions
Power and Absolute Value Functions
Error and Gamma Functions
Nearest Integer Functions
Remainder Functions
Manipulation Functions
Maximum, Minimum, and Positive Difference Functions
Floating Multiply-Add
Comparison Macros
23.5 The Header: Character Handling
Character-Classification Functions
Program: Testing the Character-Classification Functions
Character Case-Mapping Functions
Program: Testing the Case-Mapping Functions
23.6 The Header: String Handling
Copying Functions
Concatenation Functions
Comparison Functions
Search Functions
Miscellaneous Functions
24 Error Handling
24.1 The Header: Diagnostics
24.2 The Header: Errors
The perror and strerror Functions
24.3 The Header: Signal Handling
Signal Macros
The signal Function
Predefined Signal Handlers
The raise Function
Program: Testing Signals
24.4 The Header: Nonlocal Jumps
Program: Testing setjmp/longjmp
25 International Features
25.1 The Header: Localization
Categories
The setlocale Function
The localeconv Function
25.2 Multibyte Characters and Wide Characters
Multibyte Characters
Wide Characters
Unicode and the Universal Character Set
Encodings of Unicode
Multibyte/Wide-Character Conversion Functions
Multibyte/Wide-String Conversion Functions
25.3 Digraphs and Trigraphs
Trigraphs
Digraphs
The Header: Alternative Spellings
25.4 Universal Character Names (C99)
25.5 The Header (C99): Extended Multibyte and Wide-Character Utilities
Stream Orientation
Formatted Wide-Character Input/Output Functions
Wide-Character Input/Output Functions
General Wide-String Utilities
Wide-Character Time-Conversion Functions
Extended Multibyte/Wide-Character Conversion Utilities
25.6 The Header (C99): Wide-Character Classification and Mapping Utilities
Wide-Character Classification Functions
Extensible Wide-Character Classification Functions
Wide-Character Case-Mapping Functions
Extensible Wide-Character Case-Mapping Functions
26 Miscellaneous Library Functions
26.1 The Header: Variable Arguments
Calling a Function with a Variable Argument List
The v…printf Functions
The v…scanf Functions
26.2 The Header: General Utilities
Numeric Conversion Functions
Program: Testing the Numeric Conversion Functions
Pseudo-Random Sequence Generation Functions
Program: Testing the Pseudo-Random Sequence Generation Functions
Communication with the Environment
Searching and Sorting Utilities
Program: Determining Air Mileage
Integer Arithmetic Functions
26.3 The Header: Date and Time
Time Manipulation Functions
Time Conversion Functions
Program: Displaying the Date and Time
27 Additional C99 Support for Mathematics
27.1 The Header (C99): Integer Types
Types
Limits of Specified-Width Integer Types
Limits of Other Integer Types
Macros for Integer Constants
27.2 The Header (C99): Format Conversion of Integer Types
Macros for Format Specifiers
Functions for Greatest-Width Integer Types
27.3 Complex Numbers (C99)
Definition of Complex Numbers
Complex Arithmetic
Complex Types in C99
Operations on Complex Numbers
Conversion Rules for Complex Types
27.4 The Header (C99): Complex Arithmetic
Macros
The CX_LIMITED_RANGE Pragma
Functions
Trigonometric Functions
Hyperbolic Functions
Exponential and Logarithmic Functions
Power and Absolute-Value Functions
Manipulation Functions
Program: Finding the Roots of a Quadratic Equation
27.5 The Header (C99): Type-Generic Math
Type-Generic Macros
Invoking a Type-Generic Macro
27.6 The Header (C99): Floating-Point Environment
Floating-Point Status Flags and Control Modes
Macros
The FENV_ACCESS Pragma
Floating-Point Exception Functions
Rounding Functions
Environment Functions
Appendix A – C Operators
Appendix B – C99 versus C89
Appendix C – C89 versus K&R C
Appendix D – Standard Library Functions
Appendix E – ASCII Character Set
Bibliography
Recommend Papers

C Programming: A Modern Approach [2 ed.]
 0393979504, 9780393979503

  • Commentary
  • Added text layer (OCR) and bookmarks to existing version (http://libgen.rs/book/index.php?md5=73D3BAA5D44BE8BC61693E6512FD402D)
  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

C89 ^ . andC99

Covers both

K . N .K IN G

onmlkjihgfedcbaZYXWVU hgfe

P R O G R A M M IN G ^ AModernApproach

s E c o N D E o m o N

aZY

K .N .K IN G

P R O G R A M M IN G

ZYXWVUTSRQPONMLKJIHGFEDCBA

^ T "hgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA

A jp p V O C b C h

S E C O N D

E D IT IO N

a n d I lo o k f o r w a r d t o u s in g i t in

c o m p kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA

f u t u r e c o u r s e s ." — K a re n R e id , S e n io r L e c tu re r,

!H /n n n ff'

U n iv e r s ity o f T o ro n to

^■ ' ir < ifb o th * t " T h e s e c o n d e d it io n o f K in g 's C P r o g r a m m in g im p r o v e s o n a n a lr e a d y im p r e s s iv e b a s e , a n d is t h e b o o k T re c o m m e n d t o a n y o n e w h o

— P e te r S e e b a ch , m o d e r a to r, c o m p .la n g .c m o d e r a te d

y e a r e n g in e e r in g s t u d e n t s . It is

FEA TU RES OF THE SECO N D ED ITIO N

c o n c is e , c le a r, a c c e s s ib le t o t h e b e g in n e r , a n d y e t a ls o c o v e r s a ll

— P ro fe s s o r M a rk u s B u s s m a n n , D e p a r t m e n t o f M e c h a n ic a l a n d

o f T o ro n to onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA

New coverage ofabslratT daia types 1Jpdalcd to reflect todays CPUs and operating systems Nearly 5()0 exorcises aiul programming projecls—60% more than in the first edition Source code and solutions lo selected exercises and programming projecls for students, available al the author’s websit e cbaZYXWVUTSRQPONMLKJIHGFEDCBA (ki/khty.cviri) Password-protected instructor sil.e (also al kH khuj.com ) cuiiiaining solutions to lhc remaining exe.ruises and prujeris, plus PowerPoint presentations formost chapters

K . N . K IN G (P h .D ., U n i v e r s i t y o f C a lif o r n ia , B e r k e le y ) is a n

s c ie n c e a t G e o r g ia S ta te U n iv e r s it y . H e is a ls o t h e a u t h o r o f M o d u la - 2 : A C o m p le t e G u id e a n d J a v a P r o g r a m m in g : F r o m t h e B e g in n in g .

PREFACE

ZYXWVUTSRQPONMLKJIHGFEDCBA

In c o m p u tin g , tu r n in g th e o b v io u s in to th e u s e fu l

is a liv in g d e fin itio n o f th e w o r d “fr u s tr a tio n .” onmlkjihgfedcbaZYXW

In the years since the first edition of cbaZYXWVUTSRQPONMLKJIHGFEDCBA C Program m ing: A M odern A pproach was published, a host of new C-based languages have sprung up— Java and C# forem ost among them— and related languages such as C++ and Perl have achieved greater prominence. Still, C remains as popular as ever, plugging away in the background, quietly powering much o fth e world's software. It remains the lin g u a fra n c a o f the computer universe, as it was in 1996. But even C must change with the times. The need for a new edition of C P ro­ gram m ing: A M odern A pproach became apparent when the C99 standard was pub­ lished. Moreover, the first edition, with its references to DOS and 16-bit processors, was becom ing dated. The second edition is fully up-to-date and has been improved in many other ways as well.dcbaZYXWVUTSRQPONMLKJIHGFEDCBA

W h a t ’s N e w in th e S e c o n d E d itio n Here’s a list of new features and improvements in the second edition: ■ C om plete coverage o fb o th th e C89 sta n d a rd a n d th e C99 standard. The big­ g est difference between the first and second editions is coveUrrage of the C99 stanO dard. My goal was to cover every significant difference between C89 and C99, including all the language features and library functions added in C99. Each C99 change is clearly marked, either with “C99” in the heading o fa se ctio n or— in the case o fsh o rter discussions— with a special icon in the left margin. I did this partly to draw attention to the changes and partly so that readers who aren’t interested in C99 or don’t have access to a C99 com piler will know what to skip. Many of the C99 additions are of interest only to a specialized audience, but some of the new features will be of use to nearly all C programmers.

xxi

P r e fa c e cbaZYXWVUTSRQPONMLKJIHGFEDCBA xxii ZYXWVUTSRQPONMLKJIHGFEDCBA

Appendix ■ In c lu d e s a q u ic k referen ce to a ll C89 a n d C99 library fu n c tio n s . onmlkjihgfedcbaZYXWVUTSRQPON D in the first edition described all C89 standard library functions. In this edi­ tion, the appendix covers all C89 and C99 library functions. ■ E x p a n d e d coverage o f GCC. In the years since the first edition, use of GCC (originally the GNU C Compiler, now the GNU Compiler Collection) has spread. GCC has some significant advantages, including high quality, low (i.e., no) cost, and portability across a variety of hardware and software platform s. In recognition o fits growing importance, I ’ve included more information about GCC in this edition, including& discussions o fh o w to use it as well as common GCC error messages and warnings,GFEDCBA c?

o

■ N ew coverage o f abstract data types. In the first edition, a significant portion of Chapter 19 was devoted to C++. This material seems less relevant today, since students may already have learned C++, Java, or C# before reading this book. In this edition, coverage of C++ has been replaced by a discussion of how to set up abstract data types in C. ■ E x p a n d e d coverage o fin te r n a tio n a l fe a tu re s. Chapter 25, which is devoted to C ’s international features, is now much loncg5er and more detailed. Inform ation about the Unicode/UCS character set and its encodings is a highlight of the expanded coverage. S^

4^

t«»

■ U pdated to reflect to d a y ’s CPU s a n d op era tin g system s. When I wrote the first edition, 16-bit architectures and the DOS operating system were still rele­ vant to many readers, but such is not the case today, f v e updated the discus­ sion to focus more on 32-bit and 64-bit architectures. The rise of Linux and other versions of UND< has dictated a stronger focus on that family ofo p erating systems, although aspects ofW indow s and the Mac OS operating system that affect C program m ers are mentioned as well. ■ M o re exercises a n d p ro g ra m m in g projects. The first edition o fth is book con­ tained 311 exercises. This edition has nearly 500 (498, to be exact), divided into two groups: exercises and programming projects. ■ S o lu tio n s to selected exercises a n d p ro g ra m m in g pro jects. The most frequent request 1 received from readers of the first edition was to provide answers to the exercises. In response to this request, Tve put the answers to roughly onethird of the exercises and programming projects on the web at kn kin g .co m / books/c2. This feature is particularly useful for readers who arenT enrolled in a college course and need a way to check their work. Exercises and projects for which answers are provided are marked with a © icon (the “W ” stands for “answer available on the Web”). ■ P assw ord-protected in stru c to r website. For this edition, Tve built a new in­ structor resource site (accessible through knking.com fo ooks/c2) containing solutions to the remaining exercises and projects, plus PowerPoint presenta­ tions for most chapters. Faculty may contact me at cbook@ knking.com for a password. Please use your campus email address and include a Jink to your departm ent’s website so that I can verify your identity.

P re fa c e hgfedcbaZYXWVUTSRQPONMLKJIHGF xxiii onmlkjihgfedcbaZYXWV

I ’ve also taken the opportunity to improve wording and explanations through­ out the book. The changes are extensive and painstaking: every sentence has been checked and— if necessary— rewritten. A lthou ^s>h much haG s changed in this edition, I’ve tried to retain othe original chapter and section num bering as much as possible. Only one chapter (the last one) is entirely new, but many chapters have additional sections. In a few cases, existing sections have been renum bered. One appendix (C syntax) has been dropped, but a new appendix that com pares C99 with C89 has been added.dcbaZYXWVUTSRQPONMLKJIHGFEDCBA

G o a ls The goals of this edition remain the same as those o f the first edition:cbaZYXWVUTSRQPONMLKJIHGFEDCBA ■ B e clear, readable, a n d p o ssib ly even e n te rta in in g . M any C books are too concise for the average reader. Others arc badly written or just plain dull. Tve tried to give clear, thorough explanations, leavened with enough hum or to hold the reader's interest. ■ B e a ccessible to a b ro a d ra n g e o f readers. 1 assum e that the reader has at least a little previous program m ing experience, but 1 d on’t assum e know ledge o f a particular language. I've tried to keep jargon to a minim um and to define the terms that I use. Tve also attem pted lo separate advanced material from more elem entary topics, so that the beginner w on’t gel discouraged. ■ B e a u th o rita tiv e without, b e in g p ed a n tic. To avoid arbitrarily deciding what to include and whal not to include. I ’ve tried to cover all the features of the C lan­ guage and library. At the sam e time, Tve tried to avoid burdening the reader with unnecessary detail. ■ B e o r g a n iz e d fo r easy le a rn in g . My experience in teaching C underscores the im portance of presenting the features of C gradually. I use a spiral approach, in which difficult topics are introduced briefly, then revisited one or m ore limes later in the book with details added each time. Pacing is deliberate, with each chapter building gradually on what has com e before. For most students, this is probably the best approach: il avoids the extrem es o fb o red o m on the one hand, or “ inform ation overload” on the other. ■ M o tiva te la n g u a g e fe a tu r e s . Instead o fju s t describing each feature o f the lan­ guage and giving a few sim ple exam ples o fh o w the feature is used, I ’ve tried to motivate each feature and discuss how it's used in practical situations. ■ E m p h a size style. Il’s im portant for every C program m er to develop a consis­ tent style. Rather than dictating what this style should be, though, I usually describe a few possibilities and let the reader choose the one that's most appealing. Knowing alternative styles is a big help when reading other people’s programs (which program m ers often spend a great deal o f time doing). ■ A v o id d e p en d e n ce on a p a r tic u la r m a c h in e , com piler, o r o p era tin g system . Since C is available on such a wide variety o f platform s, I’ve tried to avoid

x x iv

P re fa c e

onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA ZYXWVUTSRQPONMLKJIHGFEDCBA

dependence on any particular machine, compiler, or operating system. All pro­ gram s are designed to be portable to a wide variety of platforms.cbaZYXWVUTSRQPONMLKJIHGFEDCBA ■ Use illu stra tio n s to clarify key concepts, I ’ve tried to put in as many figures as I could, since I think these are crucial for understanding many aspects of C. hi particular, I’ve tried to “anim ate” algorithms whenever possible by showing snapshots of data at different points in the computation.dcbaZYXWVUTSRQPONMLKJIHGFEDCBA

W h a t ’s S o M o d e r n a b o u t A RQPONMLKJIHGFEDCBA M odern A p p ro a ch ? One of my most important goals has been to take a “modern approach” to C. Here are some of the ways I ’ve tried to achieve this goal: ■ P u t C in perspective. Instead oftreating C as the only programming language worth knowing, I treat it as one of many useful languages. I discuss what kind of applications C is best suited for; I also show how to capitalize on C ’s strengoths while mionimizing its weaknesses, ■ E m p h a size sta n d a rd versions o f C. I pay minimal attention to versions of the language prior to the C89 standard. There arejust a few scattered references to K&R C (the 1978 version o fth e language described in the first edition ofB rian Kernighan and Dennis Ritchie’s book, The C Program m ing Language}. Appen­ dix C lists the major differences between C89 and K&R C. ■ D e b u n k m yth s. Today’s compilers are often at odds with commonly held assumptions about C. I don’t hesitate to debunk some of the myths about C or challenge beliefs that have long been part of the C folklore (for example, the beliefthat pointer arithmetic is always faster than array subscripting). I’ve re­ examined the old conventions o fC , keeping the ones that are still helpful. ■ E m p h a size so ftw a re engin eerin g. I treat C as a mature software engineering tool, emphasizing how to use it to cope with issues that arise during programming-in-the-large. I stress making programs readable, maintainable, reliable, and portable, and I put special emphasis on information hiding. ■ P o stpone C ’s lo w -level fe a tu re s. These features, although handy for the kind ofsystem s programming originally done in C, are not as relevant now that C is used for a great variety of applications. Instead of introducing them in the early chapters, as many C books do, 1 postpone them until Chapter 20. ■ D e-em p h a size “m a n u a l o p tim iza tio n .” Many books teach the reader to write tricky code in order to gain small savings in program efficiency. With today’s abundance of optimizing C compilers, these techniques are often no longer necessary; in fact, they can result in programs that are less efficient.

Q & A S e c tio n s Each chapter ends with a “Q&A section”— a series ofquestions and answers related to material covered in the chapter. Topics addressed in these sections include:

P re fa c e

xxv cbaZYXWVUTSRQPONM

Tve tried to answ er questions that coine up fre­ ■ F re q u e n tly a s k e d q uestions. onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA quently in my own courses, in other books, and on newsgroups related to C. ■ A d d itio n a l d iscu ssio n a n d cla rifica tio n o f tricky issues. Although readers with experience in a variety of languages may be satisfied with a brief expla­ nation and a couple of examples, readers with less experience need more. ■ S id e issu es th a t d o n ’t b e lo n g in th e m a in flo w . Som e questions raise techni­ cal issues that w on’t be of interest to all readers. ■ M a te ria l too a d v a n ce d o r too esoteric to in te re st th e average reader. Q ues­ tions of this nature are marked with an asterisk (*). Curious readers with a fair bit o f program m ing experience may wish lo delve into these questions im m e­ diately; others should definitely skip them on a first reading. W arning: These questions often refer to topics covered in later chapters. ■ C o m m o n d iffe re n c e s a m o n g C com pilers. I discuss som e frequently used (but nonstandard) features provided by particular compilers.dcbaZYXWVUTSRQPONMLKJIHGFEDCBA

Q&A

Some questions in Q&A sections relate directly to specific places in the chap­ ter; these places are marked by a special icon lo signal the reader that additional inform ation is available.

O t h e r F e a tu r e s aZYXWVUTSRQPONMLKJIHGFEDCBA In addition to Q&A sections, I’ve included a num ber o f useful features, many of which are marked with sim ple but distinctive icons (shown at left). W a r n in g s alert readers to common pitfalls. C is famous for its traps; docu­ ■

menting them all is a hopeless— if not im possible— task. I’ve tried to pick out the pitfalls that are most com mon and/or m ost im portant. C r o s s -r e fe r e n c e s provide a hypertext-like ability to locate inform ation. A1■

>P retace c r o s s - r e f e r e n c e s BA

though many o f these are pointers lo topics covered later in the book, some point to previous topics that lhe reader may wish to review. id io m

I d io m s — code patterns frequently seen in C program s— are marked for quick ■

reference. ■ P o r ta b il it y tip s give hints for writing programs that are independent of a par­

p o r ta b ility tip

ticular machine, compiler, or operating system . ■

S id e b a r s cover topics that aren’t strictly part of C but that every know ledge­

able C program m er should be aware of. (See “Source Code” on the next page for an exam ple of a sidebar.) ■

A p p e n d ic e s provide valuable reference information.

P rogram s Choosing illustrative programs isn't an easyjob. Tfprogram s are too brief and arti­ ficial, readers w on’t gel any sense ofh o w the features are used in the real world. On the other hand, if a program is too realistic, its point can easily be lost in a forest of

xxvi

P r e f a c e onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA ZYXWVUTSRQPONMLKJIHGFEDCBA

details. I’ve chosen a m iddle course, using sm all, sim ple exam ples to m ake con­ cepts clear when they’re first introduced, then gradually building up to com plete program s. 1 haven’t included program s o f great length; it’s been my experience that instructors d o n 't have the tim e to cover them and students d o n ’t have the patience to read them. 1 d o n ’t ignore the issues that arise in the creation o f large program s, though— C hapter 15 (W riting Large Program s) and C hapter 19 (Program D esign) cover them in detail. I’ve resisted the urge to rewrite program s to take advantage of the features of C99, since not every reader m ay have access to a C99 com piler or wish to use C99. I have, however, used C 9 9 ’s < s t d b o o l .h > header in a few program s, because it conveniently defines m acros nam ed b o o l , t r u e , and f a l s e . If your com piler doesn’t support the < s t d b o o l . h > header, y o u ’ll need to provide your own defi­ nitions for these nam es. The program s in this edition have undergone one very m inor change. T he m a i n function now has the form i n t m a i n ( v o i d ) { ... } in most cases. This change reflects recom m ended practice and is com patible with C99, w hich requires an explicit return type for each function.

S o u rc e C o d e

S o u rc e c o d e fo r all p ro g ra m s is a v a ila b le a t k n k in g .c o m /b o o k s /c 2 . U p d a te s , c o rr e c ­ tio n s , a n d n e w s a b o u t th e b o o k c a n a ls o b e fo u n d a t th is site. dcbaZYXWVUTSRQPONMLKJIHGFEDCBA

A u d ie n c e This book is designed as a prim ary text for a C course at the undergraduate level. Previous program m ing experience in a high-level language or assem bler is helpful but not necessary for a com puter-literate reader (an “adept beginner,” as one o f my form er editors put it). Since the book is self-contained and usable for reference as well as learning, it m akes an excellent com panion text for a course in data structures, com piler design, operating system s, com puter graphics, em bedded system s, or other courses that use C for project work. T hanks to its Q&A sections and em phasis on practical prob­ lems, the book will also appeal to readers who are enrolled in a training class or who are learning C by self-study.

O r g a n iz a tio n T he book is divided into four parts:RQPONMLKJIHGFEDCBA C hapters 1-10 cover enough o f C to allow the reader to w rite single-file program s using arrays and functions.



B a s ic F e a tu r e s o f C .

■ A

C hapters 11-20 build on the m aterial in the earlier chapters. T he topics becom e a little harder in these chapters, w hich provide ind v a n c e d F e a tu r e s o f C .

x x v ii onmlkjihgfedcbaZYXWV P re fa c e hgfedcbaZYXWVUTSRQPONMLKJIHGF

depth coverage of pointers, strings, the preprocessor, structures, unions, enu­ merations, and low-level features o fC . In addition, two chapters (15 and 19) offer guidance on program design.RQPONMLKJIHGFEDCBA ■



Chapters 21-27 focus on the C library, a large col­ lection of functions that come with every compiler. These chapters are most likely to be used as reference material, although portions are suitable for lec­ tures. T h e S ta n d a r d C L ib r a ry .

Appendix A gives a complete list of C operators. Appendix B de­ scribes the major differences between C99 and C89, and Appendix C covers the differences between C89 and K&R C. Appendix D is an alphabetical listing of all functions in the C89 and C99 standard libraries, with a thorough descrip­ tion of each. Appendix E lists the ASCII character set. An annotated bibliogra­ phy points the reader toward other sources of information. R e fe re n c e .

A full-blown course on C should cover Chapters 1-20 in sequence, with topics from Chapters 21-27 added as needed. (Chapter 22, which includes coverage of file inpuL/output, is the most important chapter ofthis group.) A shortercourse can omit the following topics without losing continuity: Section 8.3 (variable-length arrays), Section 9.6 (recursion), Section 12.4 (pointers and multidimensional arrays). Sec­ tion 12.5 (pointers and variable-length arrays), Section 14.5 (miscellaneous direc­ tives), Section 17.7 (pointers to functions), Section 17.8 (restricted pointers), Section 17.9 (flexible array members). Section 18.6 (inline functions), Chapter 19 (program design), Section 20.2 (bit-fields in structures), and Section 20.3 (other low-level techniques).dcbaZYXWVUTSRQPONMLKJIHGFEDCBA

E x e r c is e s a n d P r o g r a m m in g P r o j e c t s Having a variety of good problems is obviously essential for a textbook. This edi­ tion of the book contains both exercises (shorterprobIems that don’t require writing a full program) and programming projects (problems that require writing or modi­ fying an entire program). A few exercises have nonobvious answers (some individuals uncharitably call these “trick questions”— the nerve!). Since C programs often contain abundant examples ofsuch code, I feel it’s necessary to provide some practice. However, Tll play fair by marking these exercises with an asterisk (*). Be careful with a starred exercise: either pay close attention and think hard or skip it entirely.

E r r o r s , L a c k o f (? ) Tve taken great pains to ensure the accuracy of this book. Inevitably, however, any book of this size contains a few errors. If you spot one, please contact me at c b o o k @ k n k i n g .c o m . Td also appreciate hearing about which features you found especially helpful, which ones you could do without, and what yotfd like to see added.

xxviii

P re fa c e

ZYXWVUTSRQPONMLKJIHGFEDCBA dcbaZYXWVUTSRQPONMLKJIHGFEDCBA

A c k n o w le d g m e n ts onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA First, I’d like to thank my editors at Norton, Fred McFarland and Aaron Javsicas. Fred got the second edition underway and Aaron stepped in with brisk efficiency to bring it to completion. I’d also like to thank associate managing editor Kim Yi, copy editor Mary Kelly, production manager Roy Tedoff, and editorial assistant Carly Fraser. I owe a huge debt to the following colleagues, who reviewed some or all of the manuscript for the second edition: Markus Bussinann, University ofToron(o Jim Clarke, University ofToronto Karen Reid, University ofToronto Peter Seebach, moderator of conip.lang.c.m cbaZYXWVUTSRQPONMLKJIHGFEDCBA oderated

Jim and Peter deserve special mention for their detailed reviews, which saved m e from a number of embarrassing slips. The reviewers for the first edition, in alpha­ betical order, were: Susan Anderson-Freed, Manuel E. Bermudez, Lisa J. Brown, Steven C. Cater, Patrick Harrison, Brian Harvey, Henry H. Leitner, Darrell Long, Arthur B. Maccabe, Carolyn Rosner, and Patrick Terry. I received many useful comments from readers of the first edition; I thank everyone who took the tim e to write. Students and colleagues at Georgia State Uni­ versity also provided valuable feedback. Ed Bullwinkel and his wife Nancy were kind enough to read much o fth e manuscript. ITn particularly grateful to my depart­ ment chair, Yi Pan, who was very supportive of the project. My wife, Susan Cole, was a pillar of strength as always. Our cats, Dennis, Pounce, and Tex, were also instrumental in the completion of the book. Pounce and Tex were happy to contribute the occasional catfight to help keep me awake while I was working late at night. Finally, I’d like to acknowledge the late Alan J. Perlis, whose epigrams appear at the beginning of each chapter. I had the privilege of studying briefly under Alan at Yale in the mid-70s. I think he’d be amused at finding his epigrams in a C book.

B R IE F C O N T E N T S

aZYXWVUTSRQPONMLKJIHGFEDCBA

T h e S t a n d a r d C L ib r a r y

B a s ic F e a t u r e s o f C 1

In tr o d u c in g C

1

21

T h e S ta n d a rd L ib ra ry

529

2

C F u n d a m e n ta ls

9

22

ln p u t/O u tp u t

539

3

F o rm a tte d ln p u t/O u tp u t

37

23

L ib ra ry S u p p o r t fo r N u m b e rs

4

E x p re s s io n s

53

5

S e le c tio n S ta te m e n ts

73

6

Loops

7

a n d C h a r a c te r D a ta

589

24

E rro r H a n d lin g

627

99

25

In te rn a tio n a l F e a tu re s

641

B a s ic T y p e s

125

26

M is c e lla n e o u s L ib ra ry

8

A rra y s

161

9

F u n c tio n s

183

P ro g ra m O rg a n iz a tio n

219

10

F u n c tio n s 27

A d d itio n a l C 9 9 S u p p o rt fo r M a th e m a tic s

A d v a n c e d F e a tu re s o f C

677

705

R e fe r e n c e

11

P o in te rs

241

A

C O p e ra to rs

735

12

P o in t e r s a n d A r r a y s

257

B

C 9 9 v e rs u s C 8 9

737

13

S trin g s

277

C

C 8 9 v e rs u s K & R C

743

14

T h e P re p ro c e s s o r

315

D

S ta n d a rd L ib ra ry F u n c tio n s

747

15

W ritin g L a rg e P ro g ra m s

349

E

A S C II C h a r a c te r S e t

801

16

S tru c tu re s , U n io n s , a n d

B ib lio g ra p h y

803

In d e x

807

E n u m e ra tio n s

377

17

A d v a n c e d U s e s o f P o in t e r s

413

18

D e c la ra tio n s

457

19

P ro g ra m D e s ig n

483

20

L o w -L e v e l P ro g ra m m in g

509

v

CO NTEN TS

aZYXWVUTSRQPONMLKJIHGFEDCBA

P re fa c e

1

IN T R O D U C I N G C

1

1.1

H is to r y o fC

1

O rig in s

1

S ta n d a rd iz a tio n

2

C -B a s e d L a n g u a g e s

3

S tre n g th s a n d W e a k n e s s e s o f C

4

S tre n g th s

4

W eaknesses

5

E ffe c tiv e U s e o f C

6

1 .2

2

xxi

C FUNDAM ENTALS

9

2.1

W r it in g a S im p le P r o g r a m

9

P ro g ra m : P rin tin g a P u n

9

C o m p ilin g a n d L in k in g

10

In te g ra te d D e v e lo p m e n t E n v iro n m e n ts

11

T h e G e n e r a lF o r m o fa S im p le P r o g r a m

12

D ire c tiv e s

12

F u n c tio n s S ta te m e n ts

13 14

P r in tin g S tr in g s

14

2 .3

C o m m e n ts

15

2 .4

V a r ia b le s a n d A s s ig n m e n t

17

Types

17

D e c la ra tio n s

17

A s s ig n m e n t

18

2 .2

v ii

V III ZYXWVUTSRQPONMLKJIHGFEDCBA C o n te n ts

2 .5

P rin tin g th e V a lu e o f a V a ria b le

19

P ro g ra m : C o m p u tin g th e D im e n s io n a l W e ig h t o f a B o x

20

In itia liz a tio n

21

P rin tin g E x p re s s io n s

2 2 aZYXWVUTS

R e a d in g In p u t

22

P ro g ra m : C o m p u tin g th e D im e n s io n a l W e ig h t o f a B o x 2 .6

2 .7

2 .8

3

3 .2

D e f in in g N a m e s f o r C o n s t a n t s

23

P ro g ra m : C o n v e rtin g fro m F a h re n h e it to C e ls iu s

24

Id e n t if ie r s

25

K e y w o rd s

26

L a y o u t o f a C P ro g r a m

27

37

T h e p r i n t f F u n c t io n

37

C o n v e rs io n S p e c ific a tio n s

38

P ro g ra m : U s in g p r i n t f to F o rm a t N u m b e rs

40

E scape S eq ue nc es

41

T h e s c a n f F u n c t io n

42

H o w s c a n f W o rk s

43

O rd in a ry C h a ra c te rs in F o rm a t S trin g s

45

C o n fu s in g p r i n t f w ith s c a n f

45

P ro g ra m : A d d in g F ra c tio n s

46

E X P R E S S IO N S

53

A r it h m e t ic O p e r a t o r s

54

O p e ra to r P re c e d e n c e a n d A s s o c ia tiv ity

55

P ro g ra m : C o m p u tin g a U P C C h e c k D ig it

56

A s s ig n m e n t O p e r a t o r s

58

S im p le A s s ig n m e n t

58

L v a lu e s

59

C o m p o u n d A s s ig n m e n t

60

4 .3

In c r e m e n t a n d D e c r e m e n t O p e r a t o r s

61

4 .4

E x p r e s s io n E v a lu a t io n

62

O rd e r o f S u b e x p re s s io n E v a lu a tio n

64

E x p r e s s io n S t a t e m e n t s

65

S E L E C T IO N S T A T E M E N T S

73

4.1

4 .2

4 .5

5

22

F O R M A T T E D IN P U T /O U T P U T 3.1

4

(R e v is ite d )

5.1

5 .2

L o g ic a l E x p r e s s io n s

74

R e la tio n a l O p e ra to rs

74

E q u a lity O p e ra to rs

75

L o g ic a l O p e ra to rs

75

T h e i f S ta te m e n t

76

C o m p o u n d S ta te m e n ts

77

ix C o n te n ts hgfedcbaZYXWVUTSRQPONMLK

5 .3

6

78

C a s c a d e d i f S ta te m e n ts P ro g ra m : C a lc u la tin g a B ro k e r’s C o m m is s io n

80 81

T h e “ D a n g lin g e l s e ” P ro b le m

82

C o n d itio n a l E x p re s s io n s

83

B o o le a n V a lu e s in C 8 9 B o o le a n V a lu e s in C 9 9

84 8 5 aZYXWVUTSRQP

T h e s w i t c h S ta te m e n t

86

T h e R o le o f th e b r e a k S ta te m e n t P ro g ra m : P rin tin g a D a te in L e g a l F o rm

88 89

LOOPS 6.1

6 .2

6 .3

6 .4

6 .5

7

T h e e l s e C la u s e

T h e w h ile S ta te m e n t

7 .2

7 .3

99

In finite L o o p s

101

P ro g ra m : P rin tin g a T a b le o f S q u a re s

102

P ro g ra m : S u m m in g a S e rie s o f N u m b e rs

102

T h e d o S ta te m e n t

103

P ro g ra m : C a lc u la tin g th e N u m b e r o f D ig its in an In te g e r

104

T h e fo rS ta te m e n t

105

f o r S ta te m e n t Id io m s

106

O m ittin g E x p re s s io n s in a f o r S ta te m e n t

107

f o r S t a t e m e n t s in C 9 9

108

T h e C o m m a O p e r a to r

109

P ro g ra m : P rin tin g a T a b le o f S q u a re s (R e v is ite d )

110

E x itin g fr o m a L o o p

111

T h e b r e a k S ta te m e n t T h e c o n t i n u e S ta te m e n t T h e g o t o S ta te m e n t

111 112 113

P ro g ra m : B a la n c in g a C h e c k b o o k

114

T h e N u llS ta te m e n t

116

B A S IC T Y P E S 7.1

99

125

ln t e g e r T y p e s

125

In te g e r T yp es in C 9 9 ln te g e rC o n s ta n ts In te g e r C o n s ta n ts in C 9 9

128 128 129

ln te g e rO v e rflo w

130

R e a d in g a n d W ritin g In te g e rs

130

P ro g ra m : S u m m in g a S e rie s o f N u m b e rs (R e vis ite d )

131

F lo a t i n g T y p e s

132

F lo a tin g C o n s ta n ts R e a d in g a n d W ritin g F lo a tin g -P o in t N u m b e rs

133 134

C h a ra c te rT y p e s

134

O p e ra tio n s on C h a ra c te rs

135

S ig n e d a n d U n s ig n e d C h a r a c te r s

136

X ZYXWVUTSRQPONMLKJIHGFEDCBA C o n te n ts

7 .4

7 .5

7 .6

8

136 137

C h a ra c te r-H a n d lin g F u n c tio n s R e a d in g a n d W ritin g C h a ra c te rs u sing s c a n f a n d p r i n t f R e a d in g a n d W ritin g C h a ra c te rs u sing g e t c h a r a n d

138 139

p u ,tc h a r

140

P ro g ra m : D e te rm in in g th e L e n g th o f a M e s s a g e

141 aZYXWVUTSRQ

T y p e C o n v e r s io n

142

T h e U su a l A rith m e tic C o n v e rs io n s

143

C o n v e r s io n D u rin g A s s ig n m e n t

145

lm p lic itC o n v e r s io n s in C 9 9

146

C a s tin g

147

T y p e D e f i n it io n s

149

A d v a n ta g e s o f Type D e fin itio n s

149

T ype D e fin itio n s a n d P o rta b ility

150

T h e s i z e o f O p e ra to r

151

ARRAYS

161

8.1

O n e - D im e n s io n a l A r r a y s

161

A rr a y S u b s c rip tin g

162

P ro g ra m : R e v e rs in g a S e rie s o f N u m b e rs

164

A rra y in itia liz a tio n

164

D e s ig n a te d In itia lize rs

165

P ro g ra m : C h e c k in g a N u m b e r fo r R e p e a te d D ig its

166

U s in g th e s i z e o f O p e ra to r w ith A rra y s P ro g ra m : C o m p u tin g In te re s t

167 168

M u lt i d im e n s io n a lA r r a y s

169

In itia liz in g a M u ltid im e n s io n a l A rra y

171

C o n s ta n t A rra y s

172

P ro g ra m : D e a lin g a H a n d o f C a rd s

172

V a r ia b le - L e n g t h A r r a y s ( C 9 9 )

174

8 .2

8 .3

9

A rith m e tic T y p e s Escape S e q ue n ce s

F U N C T IO N S

183

9.1

D e fin in g a n d C a llin g F u n c tio n s

183

P ro g ra m : C o m p u tin g A v e ra g e s

184

P ro g ra m : P rin tin g a C o u n td o w n

185

P ro g ra m : P rin tin g a P un (R e vis ite d )

186

F u n c tio n D e fin itio n s

187

F u n c tio n C a lls P ro g ra m : T e s tin g W h e th e r a N u m b e r Is P rim e

189 190

9 .2

F u n c t io n D e c la r a tio n s

191

9 .3

A rg u m e n ts

193

A rg u m e n tC o n v e rs io n s

194

A rra y A rg u m e n ts

195

V a ria b le -L e n g th A rra y P a ra m e te rs

198

C o n te n ts hgfedcbaZYXWVUTSRQPONMLKJIHGF xi

U s in g s t a t i c in A rra y P a ra m e te r D e c la ra tio n s

200

C o m p o u n d L ite ra ls

2 0 0 aZYXWVUTSRQPONM

9 .4

T h e r e t u r n S ta te m e n t

201

9 .5

P r o g r a m T e r m in a t io n

202

T h e e x i t F u n c tio n

203

R e c u r s io n

204

T h e Q u ic k s o rt A lg o rith m

205

P ro g ra m : Q u ic k s o rt

207

9 .6

10

P R O G R A M O R G A N IZ A T IO N 1 0 .1

L o c a lV a r ia b le s

219

S ta tic L o ca l V a ria b le s

220

P a ra m e te rs

221

E x t e r n a l V a r ia b le s

221

E x a m p le : U s in g E x te rn a l V a ria b le s to Im p le m e n t a S ta c k

221

P ro s a n d C o n s o f E x te rn a lV a ria b le s

222

P ro g ra m : G u e s s in g a N u m b e r

224

1 0 .3

B lo c k s

227

1 0 .4

Scope

228

1 0 .5

O r g a n iz in g a C P r o g r a m

229

P ro g ra m : C la s s ify in g a P o k e r H a n d

230

1 0 .2

11

219

P O IN T E R S 11.1

241

P o i n t e r V a r ia b le s

241

D e c la rin g P o in te r V a ria b le s

242

T h e A d d r e s s a n d In d ir e c t io n O p e r a t o r s

243

T h e A d d re s s O p e ra to r

243

T h e In d ire ctio n O p e ra to r

244

1 1 .3

P o in t e r A s s ig n m e n t

245

1 1 .4

P o in te r s a s A r g u m e n ts

247

1 1 .2

P ro g ra m : F in d in g th e L a rg e s t a n d S m a lle s t E le m e n ts in a n

1 1 .5

12

A rra y

249

U s in g c o n s t to P ro te c t A rg u m e n ts

250

P o in te r s a s R e tu r n V a lu e s

251

P O IN T E R S A N D A R R A Y S

257

12.1

P o in t e r A r i t h m e t i c

257

A d d in g an In te g e r to a P o in te r

258

S u b tra c tin g an In te g e r fro m a P o in te r

259

S u b tra c tin g O n e P o in te r fro m A n o th e r

259

C o m p a rin g P o in te rs

260

P o in te rs to C o m p o u n d L ite ra ls

260

U s in g P o in te r s fo r A r r a y P r o c e s s in g

260

C o m b in in g th e * a n d ++ O p e ra to rs

262

1 2 .2

C o n te n ts aZYXWVUTSRQPONMLKJIHGFEDCBA x ii ZYXWVUTSRQPONMLKJIHGFEDCBA 12. 3

12. 4

12. 5

13

263

P ro g ra m : R e v e rs in g a S e rie s o f N u m b e rs (R e v is ite d )

264

A rra y A rg u m e n ts (R e v is ite d )

265

U s in g a P o in te r a s a n A rra y N a m e

266

P o in te r s a n d M u ltid im e n s io n a lA r r a y s

267

P ro c e s s in g th e E le m e n ts o f a M u ltid im e n s io n a l A rra y

267

P ro c e s s in g th e R o w s o f a M u ltid im e n s io n a l A rra y

268

P ro c e s s in g th e C o lu m n s o f a M u ltid im e n s io n a l A rra y

269

U s in g th e N a m e o f a M u ltid im e n s io n a l A rra y as a P o in te r

269

P o in te r s a n d V a r ia b le -L e n g th A r r a y s (C 9 9 )

270

S T R IN G S

277

S t r in g L i t e r a ls

277

E s c a p e S e q u e n c e s in S trin g L ite ra ls

278

C o n tin u in g a S trin g L ite ra l

278

H o w S trin g L ite ra ls A re S to re d

279

O p e ra tio n s o n S trin g L ite ra ls

279

S trin g L ite ra ls v e rs u s C h a ra c te r C o n s ta n ts

280

S tr in g V a r ia b le s

281

In itia liz in g a S trin g V a ria b le

281

C h a ra c te r A rra y s v e rs u s C h a ra c te r P o in te rs

283

R e a d in g a n d W r itin g S tr in g s

284

W ritin g S trin g s U s in g p r i n t f a n d p u t s

284

R e a d in g S trin g s U s in g s c a n f a n d g e t s

285

R e a d in g S trin g s C h a ra c te r b y C h a ra c te r

286

1 3 .4

A c c e s s in g th e C h a r a c te r s in a S tr in g

287

1 3 .5

U s in g th e C S tr in g L ib r a r y

289

T h e s t r c p y (S trin g C o p y ) F u n c tio n

290

T h e s t r l e n (S trin g L e n g th ) F u n c tio n

291

T h e s t r c a t (S trin g C o n c a te n a tio n ) F u n c tio n

291

T h e s t r c m p (S trin g C o m p a ris o n ) F u n c tio n

292

P ro g ra m : P rin tin g a O n e -M o n th R e m in d e r L ist

293

S tr in g id io m s

296

S e a rc h in g fo r th e E n d o f a S trin g

296

C o p y in g a S trin g

298

A r r a y s o fS tr in g s

300

C o m m a n d - L in e A rg u m e n ts

302

P ro g ra m : C h e c k in g P la n e t N a m e s

303

1 3 .1

1 3 .2

1 3 .3

1 3 .6

1 3 .7

14

U s in g a n A r r a y N a m e a s a P o in te r

THEPREPROCESSO R

315

1 4 .1

H o w th e P re p ro c e s s o rW o rk s

315

1 4 .2

P r e p r o c e s s in g D ir e c tiv e s

318

1 4 .3

M a c r o D e f i n i t io n s

319

S im p le M a c r o s

319

P a ra m e te riz e d M a c ro s

321

x iii C o n te n ts hgfedcbaZYXWVUTSRQPONMLKJIHG T h e # O p e ra to r

324

T h e # # O p e ra to r

324

G e n e ra l P ro p e r tie s o f M a c ro s

325

P a re n th e s e s in M a c ro D e fin itio n s

326

C re a tin g L o n g e r M a c ro s

328

P re d e fin e d M a c ro s A d d itio n a l P re d e fin e d M a c ro s in C 9 9

329 330

E m p ty M a c ro A rg u m e n ts

331

M a c ro s w ith a V a ria b le N u m b e r o f A rg u m e n ts

332

The

3 3 3 aZYXWVUTSRQPONM

fu n c

Id e n tifie r

1 4 . 4 C o n d it io n a l C o m p ila t io n

T h e # i f a n d # e n d i f D ire c tiv e s

334

T h e d e fin e d O p e r a to r

335

T h e # i f d e f a n d # i f n d e f D ire c tiv e s

335

T h e # e l i f a n d # e l s e D ire c tiv e s

336

U s e s o f C o n d itio n a l C o m p ila tio n

337

1 4 . 5 M is c e ll a n e o u s D ir e c t iv e s

15

338

T h e # e r r o r D ire c tiv e

338

T h e # l i n e D ire c tiv e

339

T h e # p r a g m a D ire c tiv e

340

T h e _ P r a g m a O p e ra to r

341

W R IT IN G L A R G E P R O G R A M S

349

1 5 .1

S o u r c e F ile s

349

1 5 .2

H e a d e r F ile s

350

T h e # i n c l u d e D ire c tiv e S h a rin g M a c ro D e fin itio n s a n d T yp e D e fin itio n s

351 353

S h a rin g F u n c tio n P ro to ty p e s

354

S h a rin g V a ria b le D e c la ra tio n s

355

N e s te d In c lu d e s

357

P ro te c tin g H e a d e r F ile s

357

# e r r o r D ire c tiv e s in H e a d e r F ile s

358

D iv id in g a P r o g r a m in to F ile s

359

P ro g ra m : T e x t F o rm a ttin g

359

B u ild in g a M u ltip le -F ile P r o g r a m

366

M a k e file s

366

E rro rs D u rin g L in k in g

368

R e b u ild in g a P ro g ra m

369

D e fin in g M a c ro s O u ts id e a P ro g ra m

371

1 5 .3

1 5 .4

16

333

S T R U C T U R E S , U N IO N S , A N D E N U M E R A T I O N S 1 6 .1

377

S t r u c t u r e V a r ia b le s

377

D e c la rin g S tr u c tu re V a ria b le s

378

In itia liz in g S tr u c tu re V a ria b le s

379

D e s ig n a te d In itia liz e rs

380

O p e ra tio n s on S tru c tu re s

381

x iv ZYXWVUTSRQPONMLKJIHGFEDCBA C o n te n ts aZYXWVUTSRQPONMLKJIHGFEDCBA 1 6 .2

1 6 .3

1 6 .4

1 6 .5

17

S tru c tu re T y p e s

382

D e c la rin g a S tru c tu re T a g

383

D e fin in g a S tru c tu re T y p e

384

S tr u c tu re s a s A rg u m e n ts a n d R e tu rn V a lu e s

384

C o m p o u n d L ite ra ls

386

N e s te d A rra y s a n d S tru c tu re s

386

N e s te d S tru c tu re s

387

A rra y s o f S tru c tu r e s

387

In itia liz in g a n A rra y o f S tr u c tu re s

388

P ro g ra m : M a in ta in in g a P a rts D a ta b a s e

389

U n io n s

396

U s in g U n io n s to S a v e S p a c e

398

U s in g U n io n s to B u ild M ix e d D a ta S tru c tu re s

399

A d d in g a “T ag F ie ld " to a U n io n

400

E n u m e ra tio n s

401

E n u m e ra tio n T a g s a n d T y p e N a m e s

402

E n u m e r a tio n s a s In te g e rs

403

U s in g E n u m e ra tio n s to D e c la re ‘T a g F ie ld s "

404

A D V A N C E D U S E S O F P O IN T E R S 1 7 .1

1 7 .2

1 7 .3

1 7 .4

1 7 .5

1 7 .6

413

D y n a m ic S to r a g e A llo c a tio n

414

M e m o ry A llo c a tio n F u n c tio n s

414

N u ll P o in te rs

414

D y n a m ic a lly A (lo c a te d S tr in g s

416

U s in g m a l l o c to A llo c a te M e m o r y fo r a S trin g

416

U s in g D y n a m ic S to ra g e A llo c a tio n in S trin g F u n c tio n s

417

A rra y s o f D y n a m ic a lly A llo c a te d S trin g s

418

P ro g ra m : P rin tin g a O n e - M o n th R e m in d e r L ist (R e v is ite d )

418

D y n a m ic a lly A llo c a te d A r r a y s

420

U s in g m a l l o c to A llo c a te S to ra g e fo r a n A rra y

420

T h e c a l l o c F u n c tio n

421

T h e r e a l l o c F u n c tio n

421

D e a ll o c a t in g S t o r a g e

422

T h e f r e e F u n c tio n

423

T h e “ D a n g lin g P o in te r” P ro b le m

424

L in k e d L is ts

424

D e c la rin g a N o d e T yp e

425

C re a tin g a N o d e

425

T h e - > O p e ra to r

426

In s e rtin g a N o d e a t th e B e g in n in g o f a L in ke d L is t

427

S e a rc h in g a L in k e d L is t

429

D e le tin g a N o d e fro m a L in k e d L is t

431

O rd e re d L is ts

433

P ro g ra m : M a in ta in in g a P a rts D a ta b a s e (R e v is ite d )

433

P o in te r s to P o in te r s

438

xv C o n te n ts aZYXWVUTSRQPONMLKJIHGFEDCBA 17. 7

18

4 3 9 hgfedcbaZYXWVUTSR

F u n c tio n P o in te rs a s A rg u m e n ts

439

T h e q s o r t F u n c tio n

440

O th e r U s e s o f F u n c tio n P o in te rs

442

P ro g ra m : T a b u la tin g th e T r ig o n o m e tr ic F u n c tio n s

443

17. 8

R e s t r ic t e d P o i n t e r s ( C 9 9 )

445

17. 9

F le x ib le A r r a y M e m b e r s ( C 9 9 )

447

D E C L A R A T IO N S

457

1 8 .1

D e c l a r a t io n S y n t a x

457

1 8 .2

S to r a g e C la s s e s

459

P ro p e rtie s o f V a ria b le s

459

T h e a u t o S to ra g e C la s s

460

T h e s t a t i c S to ra g e C la s s

461

T h e e x t e r n S to ra g e C la s s

462

T h e r e g i s t e r S to ra g e C la s s

463

T h e S to ra g e C la s s o f a F u n c tio n

464

S u m m a ry

465

1 8 .3

T y p e Q u a lifie r s

466

1 8 .4

D e c la r a t o r s

467

D e c ip h e rin g C o m p le x D e c la ra tio n s

468

U s in g T yp e D e fin itio n s to S im p lify D e c la ra tio n s

470

In it ia liz e r s

470

U n in itia liz e d V a ria b le s

472

ln lin e F u n c tio n s (C 9 9 )

472

In lin e D e fin itio n s

473

R e s tric tio n s o n In lin e F u n c tio n s

474

U s in g In lin e F u n c tio n s w ith G C C

475

1 8 .5

1 8 .6

19

P o in te r s to F u n c tio n s

P R O G R A M D E S IG N

483

M o d u le s

484

C o h e s io n a n d C o u p lin g

486

T y p e s o f M o d u le s

486

In f o r m a t i o n H id in g

487

A S ta c k M o d u le

487

A b s tra c tD a ta T y p e s

491

E n c a p s u la tio n

492

In c o m p le te T y p e s

492

A S ta c k A b s tra c tD a ta T y p e

493

D e fin in g th e In te rfa c e fo r th e S ta c k A D T

493

Im p le m e n tin g th e S ta c k A D T U s in g a F ix e d -L e n g th A rra y

495

C h a n g in g th e Ite m T y p e in th e S ta c k A D T

496

Im p le m e n tin g th e S ta c k A D T U s in g a D y n a m ic A rra y

497

Im p le m e n tin g th e S ta c k A D T U s in g a L in k e d L is t

499

1 9 .1

1 9 .2

1 9 .3

1 9 .4

x v i ZYXWVUTSRQPONMLKJIHGFEDCBA C o n te n ts aZYXWVUTSRQPONMLKJIHGFEDCBA 1 9 .5

20

502

N a m in g C o n v e n tio n s

502

E rro r H a n d lin g

502

G e n e ric A D T s A D T s in N e w e r L a n g u a g e s

503 503

L O W - L E V E L P R O G R A M M IN G

509

2 0 .1

B itw is e O p e r a to r s

509

B itw is e S h iftO p e r a to r s

510

B itw ise C o m p le m e n t, A n d , E x c lu s ive Or, a n d In c lu s ive O r

511

U sing th e B itw is e O p e ra to rs to A c c e s s B its

512

U s in g th e B itw is e O p e ra to rs to A c c e s s B it-F ie ld s P ro g ra m : X O R E n c ry p tio n

513 514

B it-F ie ld s in S tr u c tu r e s

516

H o w B it- F ie ld s A r e S to r e d

517

O t h e r L o w - L e v e lT e c h n iq u e s

518

D e fin in g M a c h in e -D e p e n d e n t T yp e s U s in g U n io n s to P ro v id e M u ltip le V ie w s o f D a ta

518 519

U s in g P o in te rs as A d d re s s e s P ro g ra m : V ie w in g M e m o ry L o c a tio n s

520 521

T h e v o l a t i l e T ype Q u a lifie r

523

2 0 .2 2 0 .3

21

D e s ig n ls s u e s fo r A b s tr a c tD a ta T y p e s

T H E S T A N D A R D L IB R A R Y

529

21.1

U s in g th e L ib r a r y

529

R e s tric tio n s o n N a m e s U se d in th e L ib ra ry F u n c tio n s H id d e n by M a c ro s

530 531

2 1 .2

C 8 9 L ib r a r y O v e r v ie w

531

2 1 .3

C 9 9 L ib r a r y C h a n g e s

534

2 1 .4

T h e < s td d e f.h > H e a d e r :C o m m o n D e fin itio n s

535

2 1 .5

T h e < s t d b o o l . h > H e a d e r (C 9 9 ): B o o le a n T y p e a n d V a lu e s

22

536

IN P U T /O U T P U T

539

2 2 .1

S tre a m s

540

F ile P o inte rs

540

S ta n d a rd S tre a m s a n d R e d ire c tio n

540

Text F ile s v e rs u s B in a ry F ile s

541

F ile O p e r a t io n s

543

O p e n in g a F ile M odes

543 544

C lo s in g a File A tta c h in g a F ile to an O p e n S tre a m

545 546

O b ta in in g F ile N a m e s fro m th e C o m m a n d L in e P ro g ra m : C h e c k in g W h e th e r a F ile C a n B e O p e n e d

546 547

2 2 .2

x v ii C o n te n ts hgfedcbaZYXWVUTSRQPONMLKJIHG 548

File B u ffe rin g M is c e lla n e o u s F ile O p e ra tio n s

549 551 aZYXWVUTSRQPONM

F o r m a t te d I/O

551

T h e ...p r in tfF u n c tio n s . . . p r i n t f C o n v e rs io n S p e c ific a tio n s C 9 9 C h a n g e s to . . . p r i n t f C o n ve rs io n S p e c ific a tio n s

552 552 555

E x a m p le s o f . . . p r i n t f C o n v e rsio n S p e c ific a tio n s T h e ...s c a n f F u n c tio n s

556 558

...s c a n f F o rm a t S trin g s

559

...s c a n f C o n v e rs io n S p e c ific a tio n s

560

C 9 9 C h a n g e s to .. . s c a n f C o n v e rsio n S p e c ific a tio n s s c a n f E x a m p le s

562 563

D e te c tin g E n d -o f-F ile a n d E rro r C o n d itio n s

564

C h a r a c t e r I/O

566

O u tp u t F u n c tio n s

566

In p u t F u n c tio n s P ro g ra m : C o p y in g a File

567 568

L in e I/O

569

O u tp u t F u n c tio n s In p u t F u n c tio n s

56 9 570

2 2 .6

B lo c k I/O

571

2 2 .7

F ile P o s itio n in g

572

P ro g ra m : M o d ify in g a File o f P a rt R e c o rd s

574

S t r in g I/O

575

O u tp u t F u n c tio n s In p u t F u n c tio n s

576 576

2 2 .3

2 2 .4

2 2 .5

2 2 .8

23

T e m p o ra ry F ile s

L IB R A R Y S U P P O R T F O R N U M B E R S A N D C H A R A C T E R 589

DATA 2 3 .1

T h e < f l o a t . h > H e a d e r : C h a r a c t e r is t ic s o f F lo a t in g Types

589

2 3 .2

T h e < l i m i t s . h > H e a d e r : S iz e s o f In t e g e r T y p e s

591

2 3 .3

T h e < m a t h . h > H e a d e r (C 8 9 ): M a t h e m a tic s

593

E rro rs

593

T rig o n o m e tric F u n c tio n s

594

H y p e rb o lic F u n c tio n s

595

E x p o n e n tia l a n d L o g a rith m ic F u n c tio n s P o w e r F u n c tio n s N e a re s t Integer, A b s o lu te V alue, a n d R e m a in d e r F u n c tio n s

595 596 596

T h e < m a t h . h > H e a d e r (C 9 9 ): M a t h e m a tic s

597

IE E E F lo a tin g -P o in t S ta n d a rd

598

T yp e s M a c ro s

599 600

2 3 .4

x v iii

C o n te n ts ZYXWVUTSRQPONMLKJIHGFEDCBA

E rro rs

600

F u n c tio n s

601

C la s s ific a tio n M a c ro s

602

T r ig o n o m e tric F u n c tio n s

603

H y p e rb o lic F u n c tio n s

603

E x p o n e n tia l a n d L o g a rith m ic F u n c tio n s

604

P o w e r a n d A b s o lu te V a lu e F u n c tio n s

605

E rro r a n d G a m m a F u n c tio n s

606

N e a re s t In te g e r F u n c tio n s

606

R e m a in d e r F u n c tio n s

608

M a n ip u la tio n F u n c tio n s

608

M a x im u m , M in im u m , a n d P o s itiv e D iffe re n c e F u n c tio n s

609

F lo a tin g M u ltip ly - A d d C o m p a ris o n M a c ro s

610 611

2 3 . 5 T h e < c t y p e . h > H e a d e r: C h a ra c te r H a n d lin g C h a ra c te r-C la s s ific a tio n F u n c tio n s

612

P ro g ra m : T e s tin g th e C h a ra c te r-C la s s ific a tio n F u n c tio n s

613

C h a ra c te r C a s e - M a p p in g F u n c tio n s

614

P ro g ra m : T e s tin g th e C a s e -M a p p in g F u n c tio n s

614

23. 6 T h e < s tr in g .h > H e a d e r :S tr in g H a n d lin g

24

615

C o p y in g F u n c tio n s

616

C o n c a te n a tio n F u n c tio n s

617

C o m p a ris o n F u n c tio n s

617

S e a rc h F u n c tio n s

619

M is c e lla n e o u s F u n c tio n s

6 2 2 aZYXWVUTSRQP

E R R O R H A N D L IN G

627

24.1

T h e < a s s e r t.h > H e a d e r :D ia g n o s tic s

628

2 4 .2

T h e < e rm o .h > H e a d e r:E rro rs

629

T h e p e r r o r a n d s t r e r r o r F u n c tio n s

630

T h e < s i g n a i . h > H e a d e r: S ig n a l H a n d lin g

631

S ig n a l M a c ro s T h e s i g n a l F u n c tio n

631 632

P re d e fin e d S ig n a l H a n d le rs

633

T h e r a i s e F u n c tio n

634

P ro g ra m : T e s tin g S ig n a ls

634

T h e < s e t jm p . h > H e a d e r : N o n lo c a lJ u r n p s

635

P r o g r a m :T e s tin g s e t j m p / l o n g j m p

636

24.3

2 4 .4

25

612

IN T E R N A T IO N A L F E A T U R E S

25.1

2 5 .2

641

T h e < io c a ie .h > H e a d e r :L o c a liz a tio n

642

C a te g o rie s

642

T h e s e t l o c a l e F u n c tio n

643

T h e l o c a l e c o n v F u n c tio n

644

M u ltib y te C h a ra c te rs a n d W id e C h a ra c te rs

647

x ix C o n te n ts hgfedcbaZYXWVUTSRQPONMLKJIHG M u ltib y te C h a ra c te rs

648

W id e C h a ra c te rs U n ico d e a n d th e U n iv e rsa l C h a ra c te r S e t

649 649

E n c o d in g s o f U n ic o d e M u ltib y te A A /id e -C h a ra c ter C o n v e rsio n F u n c tio n s

650 651

M u ltib y te A M d e -S trin g C o n v e rsio n F u n c tio n s

6 5 3 aZYXWVUTSRQPONM

D ig r a p h s a n d T r ig r a p h s

654

T rig ra p h s

654

D ig ra p h s T h e < is o 6 4 6 . h > H e a d e r: A lte rn a tiv e S p e llin g s

655 656

2 5 .4

U n iv e r s a l C h a r a c t e r N a m e s ( C 9 9 )

656

2 5 .5

T h e < w c h a r . h > H e a d e r (C 9 9 ): E x t e n d e d M u ltib y t e a n d

2 5 .3

2 5 .6

26

W id e - C h a r a c t e r U tilit ie s

657

S tre a m O rie n ta tio n F o rm a tte d W id e -C h a ra c te r ln p u t/O u tp u t F u n c tio n s

658 659

W id e -C h a ra c te r ln p u t/O u tp u t F u n c tio n s

661

G e n e ra l W id e -S trin g U tilitie s W id e -C h a ra c te r T im e -C o n v e rs io n F u n c tio n s

662 667

E x te n d e d M u ltib y te A M d e -C h a ra c te r C o n v e rs io n U tilitie s

667

T h e < w c t y p e . h > H e a d e r (C 9 9 ): W id e - C h a r a c t e r C la s s if ic a tio n a n d M a p p in g U t ilitie s

671

W id e -C h a ra c te r C la s s ific a tio n F u n c tio n s

671

E x te n s ib le W id e -C h a ra c te r C la s s ific a tio n F u n c tio n s W id e -C h a ra c te r C a s e -M a p p in g F u n c tio n s

672 673

E x te n s ib le W id e -C h a ra c te r C a s e -M a p p in g F u n c tio n s

673

M IS C E L L A N E O U S L IB R A R Y F U N C T IO N S 2 6 .1

2 6 .2

2 6 .3

677

T h e < s t d a r g . h > H e a d e r : V a r ia b le A r g u m e n ts

677

C a llin g a F u n c tio n w ith a V a ria b le A rg u m e n t L ist

679

T h e v . . . p r i n t f F u n c tio n s T h e v ...s c a n f F u n c tio n s

680 681

T h e < s t d l i b . h > H e a d e r : G e n e r a l U t ilit ie s

682

N u m e ric C o n v e rs io n F u n c tio n s P ro g ra m : T e s tin g th e N u m e ric C o n v e rs io n F u n c tio n s

682 684

P s e u d o -R a n d o m S e q u e n c e G e n e ra tio n F u n c tio n s P ro g ra m : T e s tin g th e P s e u d o -R a n d o m S e q u e n c e G e n e ra tio n

686

F u n c tio n s

687

C o m m u n ic a tio n w ith th e E n v iro n m e n t

687

S e a rc h in g a n d S o rtin g U tilitie s P ro g ra m : D e te rm in in g A ir M ile a g e

689 690

In te g e r A rith m e tic F u n c tio n s

691

T h e < t i m e . h > H e a d e r : D a te a n d T im e

692

T im e M a n ip u la tio n F u n c tio n s T im e C o n v e rs io n F u n c tio n s

693 695

P ro g ra m : D is p la y in g th e D a te a n d T im e

698

X X ZYXWVUTSRQPONMLKJIHGFEDCBA C o n te n ts

2 7 aZYXWVUTSRQPONMLKJIHGFEDCBA A D D IT IO N A L C 9 9 S U P P O R T F O R M A T H E M A T IC S

27.1

2 7 .2

2 7 .3

2 7 .4

2 7 .5

2 7 .6

705

T h e < s t d i n t . h > H e a d e r (C 9 9 ): In te g e r T y p e s

705

< s td in t.h > T y p e s

706

L im its o f S p e c ifie d -W id th In te g e r T y p e s

707

L im its o f O th e r In te g e r T y p e s

708

M a c ro s fo r In te g e r C o n s ta n ts

708

T h e < i n t t y p e s . h > H e a d e r (C 9 9 ): F o rm a t C o n v e rs io n o f In te g e r T y p e s

709

M a c r o s f o r F o r m a t S p e c if ie r s

710

F u n c tio n s fo r G re a te s t-W id th In te g e r T y p e s

711

C o m p le x N u m b e r s ( C 9 9 )

712

D e fin itio n o f C o m p le x N u m b e rs

713

C o m p le x A r ith m e tic

714

C o m p le x T y p e s in C 9 9

714

O p e ra tio n s o n C o m p le x N u m b e rs

715

C o n v e rs io n R u le s fo r C o m p le x T y p e s

715

T h e < c o m p le x . h > H e a d e r (C 9 9 ): C o m p le x A r it h m e t ic

717

< c o m p le x . h > M a c r o s

717

T h e c x _ L iM iT E D _ R A N G E P r a g m a

718

< c o m p le x . h > F u n c t io n s

718

T r ig o n o m e tr ic F u n c tio n s

719

H y p e rb o lic F u n c tio n s

720

E x p o n e n tia l a n d L o g a rith m ic F u n c tio n s

721

P o w e r a n d A b s o lu te -V a lu e F u n c tio n s

721

M a n ip u la tio n F u n c tio n s

722

P ro g ra m : F in d in g th e R o o ts o f a Q u a d ra tic E q u a tio n

722

T h e < t g m a t h . h > H e a d e r ( C 9 9 ) : T y p e - G e n e r ic M a t h T y p e - G e n e r ic M a c ro s

723 724

In v o k in g a T y p e -G e n e ric M a c ro

725

T h e < f e n v . h > H e a d e r (C 9 9 ): F lo a tin g - P o in t E n v ir o n m e n t

726

F lo a tin g -P o in t S ta tu s F la g s a n d C o n tro l M o d e s

727

< f e n v . h > M a c ro s

727

T h e FENV_ACCESS P ra g m a

728

F lo a tin g -P o in t E x c e p tio n F u n c tio n s

729

R o u n d in g F u n c tio n s

730

E n v iro n m e n t F u n c tio n s

730

A p p e n d ix A

C O p e ra to rs

735

A p p e n d ix B

C 99 v e rs u s C 89

737

A p p e n d ix C

C 89 v e rs u s K & R C

743

A p p e n d ix D

S t a n d a r d L ib r a r y F u n c t io n s

747

A p p e n d ix E

A S C II C h a r a c t e r S e t

801

B ib lio g r a p h y

803

In d e x

807

1

In t r o d u c in g C

ZYXWVUTSRQPONMLKJIHGFEDCBA

‘7 w a n t a p r o g r a m m in g la n g u a g e in w h ic h W h e n s o m e o n e s a y s onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA I n e e d o n ly s a y w h a t I w is h d o n e , ” g iv e h im a lo llip o p . *

W hat is C? The simple answer— a widely used programming language developed in the early l970s at Bell Laboratories— conveys little of C\s special flavor. Before we become immersed in the details of the language, Iet?s take a look at where C came from, what it was designed for, and how it has changed over the years (Sec­ tion 1.1). W e’lI also discuss C ?s strengths and weaknesses and see how to get the most out of the language (Section 1.2).

1.1

H is to r y o fC Let's take a quick look al C ’s history, from its origins, to its com ing of age as a standardized language, to its influence on recent languages.dcbaZYXWVUTSRQPONMLKJIHGFEDCBA

O r ig in s C is a by-product of the UND< operating system, which was developed at Bell Lab­ oratories by Ken Thompson, Dennis Ritchie, and others. Thom pson single-hand­ edly wrote the original version ofU N IX , which ran on the DEC PDP-7 computer, an early minicom puter with only 8K words o fm a in memory (this was 1969, after all!). Like other operating systems of the time, UNIX was written in assem bly lan­ guage. Programs written in assembly language arc usually painful to debug and hard to enhance; UNE< was no exception. Thompson decided that a higher-level

*The epigrams ai the beginning o f each chapter arc froni “Epigrams on Program m ing” by Alan .1. Perlis cbaZYXWVUTSRQPONM (A C M S fG P L A N N o tic e s(S c p le m b c r, 1982): 7-13).

1

2 ZYXWVUTSRQPONMLKJIHGFEDCBA C h a p te r 1 In tr o d u c in g C

language was needed for the further developm ent o f U NIX, so he designed a small language named B. Thom pson based B on BCPL, a system s program m ing lan­ guage developed in (he m id-1960s. BCPL, in turn, traces its ancestry to Algol 60, one o fth e earliest (and most influential) program m ing languages. Ritchie so o n jo in ed the UNIX project and began program m ing in B. In 1970. Bell Labs acquired a P D P -1 1 for the UNIX project. Once B was up and running on the PDP-11, Thom pson rewrote a portion o f UNIX in B. By 1971, it becam e apparent that B was not w ell-suited to the P D P -1 1, so R itchie began to develop an extended version o fB . He called his language NB (“New B") at first, and then, as it bes^^j'an to d^*iverse m ore from B, he c4^h anged the nam e to C. TheU.' lan^*guage was stable enough by 1973 that UNIX could be rewritten in C. T he switch to C provided an im portant benefit: portability. By writing C com pilers for other com puters at Bell Labs, the team could get UNIX running on those m achines as well.dcbaZYXWVUTSRQPONMLKJIHG

S ta n d a r d iz a tio n C continued to evolve during the 1970s, especially betw een 1977 and 1979. It was during this period that the first book on C appeared. cbaZYXWVUTSRQPONMLKJIHGFEDCBA T h e C P rogram m ing L a n ­ guage. written by Brian Kernighan and Dennis Ritchie and published in 1978. quickly becam e the bible o f C program m ers. In the absence o fa n official standard for C. this book— known as K&R or the “W hite B ook v to aficionados— served as a de facto standard. During the l970s, there were relatively few C program m ers, and m ost o fth e m were UND< users. By the 1980s, however, C had expanded beyond the narrow con­ fines o f the UNIX world. C com pilers becam e available on a variety o f m achines running under different operating system s. In particular, C began to establish itself on the fast-grow ing IBM PC platform . With C ’s increasing popularity cam e problem s. Program m ers who wrote new C com pilers relied on K&R as a reference. Unfortunately, K & R was fuzzy about som e language features, so com pilers often treated these features differently. Also. K&R failed to make a clear distinction between which features belonged to C and which w ere part of UNDC. To make m atters worse, C continued to change after K&R was published, with new features being added and a few older features removed. The need for a thorough, precise, and up-to-date description o f the lan­ guage soon becam e apparent. W ithout such a standard, num erous dialects would have arisen, threatening the portability o fC program s, one o f the language’s m ajor strengths. The developm ent o f a U.S. standard for C began in 1983 under the auspices o f the A m erican National Standards Institute (A NSI). A fter m any revisions, the stan­ dard was com pleted in 1988 and form ally approved in D ecem ber 1989 as ANSI standard X 3.159-1989. In 1990, it was approved by the International O rganization for Standardization (ISO) as international standard lSO /IEC 9899:1990. This ver­ sion o f the language is usually referred to as C89 or C90, to distinguish it from the

1.1

H i s t o r y o f C hgfedcbaZYXWVUTSRQPONMLKJIHGFE 3 onmlkjihgfedcbaZYXWVU

original version of C, often called K&R C. Appendix C summarizes the major dif­ ferences between C89 and K&R C. The language underwent a few changes in 1995 (described in a document known as Amendm ent I). More significant changes occurred with the publication of a new standard, ISO/IEC 9899:1999, in 1999. The language described in this standard is commonly known as C99. The terms “ANSI C,” “ANSI/ISO C,” and “ISO C”— once used to describe C89— are now ambigV *^uous,} thanks to the existence of two standards. Because C99 isn't yet universal, and because o fth e need to maintain millions (ifnot billions) oflines ofcode written in older versions of C. I’ll use a special icon (shown in the left margin) to mark discussions of features that were added in C99. A com piler that doesn’t recognize these features isn’t “C99-compliant.” Ifh isto ry is any guide, it will be some years before all C compilers are C99-compliant, if they ever are. Appendix B lists the major differences between C99 and C89.dcbaZYXWVUTSRQPONMLKJIHGFEDCBA

C -B a s e d L a n g u a g e s C has had a huge influence on modern-day programming languages, many o f which borrow heavily from it. Of the many C-based languages, several are espe­ cially prominent: ■

C++ includes all the features o fC , but adds classes and other features to sup­ port object-oriented programming.cbaZYXWVUTSRQPONMLKJIHGFEDCBA ■ Ja v a is based on C++ and therefore inherits many C features.



C# is a more recent language derived from C+4- and Java.

■ P erl was originally a fairly simple scripting language; over time it has grown and adopted many o fth e features o fC .

Considering the popularity of these newer languages, it’s logical to ask whether it’s worth the trouble to learn C. 1 think it is, for several reasons. First, learning C can give you greater insight into the features ofC+4-, Java, C#, Perl, and the other C-based lan9g uagc?es. Programmers who leam G G* ^ one of these languages firstPONMLKJIHGFEDCBA often fail to master basic features that were inherited from C. Second, there are a lot of older C programs around; you may find yourself needing to read and main­ tain this code. Third, C is still widely used for developing new software, especially in situations where memory or processing power is limited or where the simplicity of C is desired. • If you haven’t already used one of the newer C-based languages, you’ll find that this book is excellent preparation for learning these languages. 11 em phasizes data abstraction, information hiding, and other principles that play a large role in object-oriented programming. C++ includes all the features o fC , so you’ll be able to use everything you learn from this book if you later tackle C++. Many of the features o fC can be found in the other C-based languages as well.

C h a p te r 1 In tr o d u c in g 4ZYXWVUTSRQPONMLKJIHGFEDCBA

1 .2

C aZYXWVUTSRQPONMLKJIHGFEDCBA

S t r e n g t h s a n d W e a k n e s s e s o f C onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA Like any other programming language, C has strengths and weaknesses. Both stem from the language’s original use (writing operating systems and other systems software) and its underlying philosophy:RQPONMLKJIHGFEDCBA To serve as a suitable language for systems pro­ gramming, C provides access to machine-level concepts (bytes and addresses, for example) that other programming languages try to hide. C also provides operations that correspond closely to a computer’s built-in instructions, so (hat programs can be fast. Since application programs rely on it for input/output. storage management, and numerous other services, an operating system can’t afford to be slow.

C is a lo w - le v e l la n g u a g e . ■

C provides a more limited set of features than many languages. (The reference manual in the second edition of K&R covers the entire language in 49 pages.) To keep the number of features small. C relies heavily on a “library” ofstandard functions. (A “function” is similar to what other programming languages might call a “procedure.” “subroutine,” or “m ethod”)

C is a s m a ll la n g u a g e . ■

C assumes that you know what you’re doing, so it allows you a wider degree of latitude than many languages. Moreover, C gv*uag found in other lanV C is a p e r m is s i v e la n g u a g e .



S tr e n g th s C ’s strengths help explain why the language has become so popular: Efficiency has been one of C ’s advantages from the beginning. Because C was intended for applications where assembly language had tradi­ tionally been used, it was crucial that C programs could run quickly and in limited amounts of memory.

E ffic ie n c y . ■

Although program portability wasn’t a primary goal of C, it has turned out to be one of the language’s strengths. When a program must run on computers ranging from PCs to supercomputers, it is often written in C. One reason for the portability o fC programs is that— thanks to C ’s early associa­ tion with UNIX and the later ANSI/ISO standaids— the language hasn’t splin­ tered into incompatible dialects. Another is that C compilers are small and easily written, which has helped make them widely available. Finally, C itself has features that support portability (although there’s nothing to prevent pro­ grammers from writing nonportable programs). P o r ta b ility .





C ’s large collection o fd ata types and operators help make it a power­ ful language. In C, it’s often possible to accomplish quite a bit with just a few lines of code. P ow er.

1 .2

S t r e n g t h s a n d W e a k n e s s e s o f C hgfedcbaZYXWVUTSRQPONMLKJIHGF 5 cbaZYXWVUTSRQPONM

Although C was originally designed for system s programming, it ■ F lexib ility. onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA has no inherent restrictions that limit it to this arena. C is now used for appli­ cations of all kinds, from embedded systems to commercial data processing. Moreover, C imposes very few restrictions on the use of its features; opera­ tions that would be illegal in other languages are often permitted in C. For example, C allows a character to be added to an integer value (or, for that mat­ ter, a floating-point number). This flexibility can make programming easier, although it may allow some bugs to slip through. ■ S ta n d a rd library. One o f C ’s great strengths is its standard library, which con­ tains hundreds of functions for input/output. string handling, storage alloca­ tion, and other useful operations. ■ In teg ra tio n with U N IX . C is particularly powerful in combination with UNIX (including the popular variant known as Linux). In fact, some UNLX tools assum e that the user knows C.dcbaZYXWVUTSRQPONMLKJIHGFEDCBA

W eak n esses C ’s weaknesses arise from the same source as many of its strengths: C ’s closeness to the machine. Here are a few of C ’s most notorious problems: ■

C pro g ra m s can be error-prone. C ’s flexibility makes it an error-prone lan­ guage. Programming mistakes that would be caught in many other languages can’t be detected by a C compiler. In this respect. C is a lot like assembly lan­ guage, where most errors aren't detected until the program is run. To make matters worse, C contains a number of pitfalls for the unwary. In later chap­ ters, w e’ll see how an extra semicolon can create an infinite loop or a missing & symbol can cause a program crash.

■ C p ro g ra m s can be d ifficu lt to understand. Although C is a small language by most measures, it has a number of features that aren't found in all program ­ ming languages (and that consequently are often misunderstood). These fea­ tures can be com bined in a great variety of ways, many o f which— although obvious to the original author of a program— can be hard for others to under­ stand. Another problem is the terse nature of C programs. C was designed at a time when interactive communication with computers was tedious at best. As a result, C was purposefully kept terse to minimize the time required to enter and edit programs. C ’s flexibility can also be a negative factor; program m ers who are too clever for their own good can make programs almost impossible to understand. ■ C p ro g ra m s can be d ifficu lt to m odify. Large programs written in C can be hard to change if they haven’t been designed with maintenance in mind. M od­ ern programming languages usually provide features such as classes and pack­ ages that support the division o f a large program into more manageable pieces. C, unfortunately, lacks such features.

C h a p te r 1 In tro d u c in g C RQPONMLKJIHGFEDCBA 6 ZYXWVUTSRQPONMLKJIHGFEDCBA

O b fu s c a te d C E v e n C ’s m o s t a rd e n t a d m ir e rs a d m it th a t C c o d e c a n b e h a rd to re a d . T h e a n n u a l In te rn a tio n a l O b fu s c a te d C C o d e C o n te s t a c tu a lly e n c o u ra g e s c o n te s ta n ts to w rite th e m o s t c o n fu s in g C p ro g ra m s p o s s ib le . T h e w in n e rs a re tru ly b a fflin g , a s 1 9 9 0 ’s “ B e s t S m a ll P ro g ra m ” s h o w s : kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA

v,i,j,k,l,s,a[99]; m a i n () for(scanf ("%d",&s) ;*a-s,-v=a [j*=v] -a [i] ,k = i < s ,j+= (v=j

is necessary to “include” information about C\s standard 1/0 (inpuU'output) library. The program 's executable code goes inside m a in , which represents the “main" program. The only line inside m a in is a com mand to display the desired message, p r i n t f is a function frpm the standard I/O library that can produce nicely for­ matted output. The \ n code tclls p r i n t f to advance to the next line after printing the message. The line return 0;

indicates that the program “returns” the value 0 to the operating system when it ter­ minates.dcbaZYXWVUTSRQPONMLKJIHGFEDCBA

C o m p ili n g a n d L in k in g Despite its brevity, getting p u n . c to run is more involved than you might expect. First, we need to create a file named p u n . c containing the program (any text edi­ tor will do). The name of the file doesn’t matter, but the . c extension is often required by compilers. Next, w e've got to convert the program to a form that the machine can exe­ cute. For a C program, that usually involves three steps: ■ P reprocessing, The program is first given to a preprocessor, which obeys commands that begin with # (known as directives). A preprocessor is a bit like an editor; it can add things to the program and make modifications. ■ C om piling. The modified program now goes to a com piler, which translates it into machine instructions (object code). The program isn’t quite ready to run yet, however. ■ L in k in g . In the final step, a lin k e r combines the object code produced by the com piler with any additional code needed to yield a com plete executable pro­ gram. This additional code includes library functions (like p r i n t f ) that are used in the program.

2 .1

11 onmlkjihgfedcbaZYXWV W ritin g a S im p le P ro g ra m hgfedcbaZYXWVUTSRQPONMLKJIHG

Fortunately, this process is often autom ated, so you w on't find it too onerous. In fact, the preprocessor is usually integrated with the compiler, so you probably w on’t even notice it at work. The com m ands necessary to com pile and link vary, depending on the com piler and operating system. Under UNIX, the C com piler is usually named c c . To com ­ pile and link the p u n . c program , enter the follow ing com m and in a term inal or com m and-line window:kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA % cc p u n .c

(The % character is the UNIX prom pt, not som ething that you need to enter.) Link­ ing is autom atic when using c c : no separate link com m and is necessary. After com piling and linking the program, c c leaves the executable program in a file named a . o u t by default, c c has many options; one o f them (the - o option) allows us to choose the name of the file containing the executable program . For exam ple, ifw e want the executable version o f p u n . c to be named p u n , we would enter the following command: % cc -0 pun pun.cdcbaZYXWVUTSRQPONMLKJIHGFEDCBA

T h e G C C C o m p ile r O n e o f th e m o s t p o p u la r C c o m p ile r s is th e G C C c o m p ile r, w h ic h is s u p p lie d w ith L in u x b u t is a v a ila b le fo r m a n y o th e r p la tfo rm s a s w e ll. U s in g th is c o m p ile r is s im ila r to u s in g th e tra d itio n a l U N IX c c c o m p ile r. F o r e x a m p le , to c o m p ile th e p u n . c p ro ­ g ra m , w e w o u ld u s e th e fo llo w in g c o m m a n d :

% gcc -o pun pun.c T h e Q & A s e c tio n a t th e e n d o f th e c h a p te r p ro v id e s m o re in fo rm a tio n a b o u t G C C . Q&A RQPONMLKJIHGFEDCBA

I n te g r a te d D e v e lo p m e n t E n v ir o n m e n ts So far, w e’ve assumed the use of a “com m and-line” com piler that's invoked by entering a com m and in a special window provided by the operating system . The alternative is to use an cbaZYXWVUTSRQPONMLKJIHGFEDCBA in te g ra te d d e v e lo p m e n t e n v ir o n m e n t (ID E ), a software package that allows us to edit, compile, link, execute, and even debug a program without leaving the environm ent. The com ponents o fa n ID E are designed to work together. For example, when the com piler detects an error in a program , it can arrange for the editor to highlight the line that contains the error. T here's a great deal of variation am ong IDEs, so 1 w on’t discuss them further in this book. H ow ­ ever, I would recom m end checking to see which IDEs are available for your plat­ form.

12 ZYXWVUTSRQPONMLKJIHGFEDCBA C h a p te r 2 C F u n d a m e n ta ls aZYXWVUTSRQPONMLKJIHGFEDCBA

2 .2

T h e G e n e r a l F o r m o f a S im p le P r o g r a m onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGF L et’s take a closer look at p u n . c and see how we can generalize it a bit. Simple C programs have the form PONMLKJIHGFEDCBA directiveskjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA int main(void) statements

In this template, and in sim ilar templates elsewhere in this book, items printed in C o u r i e r would appear in a C program exactly as shown: items in cbaZYXWVUTSRQPONMLKJIHGFEDC italics repre­ sent text to be supplied by the programmer. Notice how the braces show where m a in begins and ends. C uses { and } in much the same way that some other languages use words like b e g i n and e n d . This illustrates a general point about C: it relies heavily on abbreviations and spe­ cial symbols, one reason that C programs are concise (or— less charitably— crypQ&A

Even the sim plest C programs rely on three key language features: directives (editing commands that modify the program prior to com pilation), functions (named blocks ofexecutable code, ofw hich m a in is an example), and statements (com mands to be performed when the program is run). We’ll take a closer look at these features now.dcbaZYXWVUTSRQPONMLKJIHGFEDCBA

D ir e c tiv e s Before a C program is compiled, it is first edited by a preprocessor. Commands intended for the preprocessor are called directives. Chapters 14 and 15 discuss directives in detail. For now ? w e’re interested only in the # i n c l u d e directive. The p u n . c program begins with the line #include

h e a d e r s > 7 5 ,2

This directive states that the information in < s t d i o . h > is to be “included” into the program before it is compiled, < s t d i o . h > contains information about C 's standard 1/0 library. C has a number of h eaders like < s t d i o .h > ; each contains information about some part of the standard library. The reason w e’re including < s t d i o . h > is that C, unlike some programming languages, has no built-in “read” and “write” commands. The ability to perform input and output is provided instead by functions in the standard library. Directives always begin with a # character. which distinguishes them from other items in a C program. By default, directives are one line long: there’s no semicolon or other special marker at the end of a directive.

2 .2

T h e G e n e ra l F o rm o f a S im p le P ro g ra m hgfedcbaZYXWVUTSRQPONMLKJIHG 13 dcbaZYXWVUTSRQPO

F u n c tio n s RQPONMLKJIHGFEDCBA in other programmmg lan­ guages— they’re the building blocks from which programs are constructed. In fact, a C program is little more than a collection of functions. Functions fall into two categories: those written by the programmer and those provided as part of the C implementation. I'll refer to the latter as l i b r a r y f u n c t i o n s , since they belong to a “library” of functions that are supplied with the compiler. The term “function” comes from mathematics, where a function is a rule for computing a value when given one or more arguments:

are like “procedures’’ or “subroutines” F u n c t i o n s onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA

Av) = x + 1 g(y> z) = .y2 - ^ C uses the term “function” more loosely. In C. a function is simply a series of statements that have been grouped together and given a name. Some functions compute a value; some don’t. A function that computes a value uses the r e t u r n statement to specify what value it “returns.” For example, a function that adds l to its argument might execute the statementkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA return x + 1;

while a function that computes the difference of the squares of its arguments might execute the statement return y * y - z * z;

Although a C program may consist of many functions, only the m a in func­ tion is mandatory, m a in is special: it gets called automatically when the program is executed. Until Chapter 9, where we’lI learn how to write other functions, m a in will be the only function in our programs. The name m a in is critical; it can’t be b e g i n or s t a r t or even MAIN. T fm a in is a function, does it return a value? Yes: it returns a status code that is given to the operating system when the program terminates. Let's take another look at the p u n . c program: #include int main(void) printf("To C, or not to C: that is the question.\n"); return 0;

} The word i n t just before m a in indicates that the m a in function returns an inte­ ger value. The word v o i d in parentheses indicates that m a in has no arguments.

14 ZYXWVUTSRQPONMLKJIHGFEDCBA C h a p te r 2 C F u n d a m e n ta ls aZYXWVUTSRQPONMLKJIHGFEDCBA

The statementkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA return 0;

r e tu r n

has two effects: it causes the m a in function to term inate (thus ending the program) and it indicates that the m a i n function returns a value o f0 . W e’ll have more to say v a lu e o f m a i n BA > 9 .5dcbaZYXWVUTSRQPONMLKJIHGFEDCBA about m a i n ’s return value in a later chapter. For now, w e’ll always have m a in Q&A return the value 0, which indicates normal program termination. ■ Ifth e re ’s no r e t u r n statement at the end of the m a in function, the program will still terminate. However, many compilers will produce a warning message Q&A (because the function was supposed to return an integer but failed to).

S ta te m e n ts

[

A cbaZYXWVUTSRQPONMLKJIHGFEDCBA sta te m e n t is a com m and to be executed when the program runs. W e’ll explore statements later in the book, primarily in Chapters 5 and 6. The p u n . c program uses only two kinds of statements. One is the r e t u r n statement; the other is the fu n c tio n call. Asking a function to perform its assigned task is known as ca llin g the function. The p u n . c program, for example, calls the p r i n t f function to dis­ play a string on the screen: printf("To C, or not to C: that is the question.\n”);

c o m p o u n d s ta te m e n t >

C requires that each statem ent end with a semicolon. (As with any good rule, 5.2onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA there's one exception: the com pound statement, which w e’ll encounter later.) The semicolon shows the com piler where the statement ends; since statements can con­ tinue over several lines, it's not always obvious where they end. Directives, on the other hand, are normally one line long, and they d o n 't end with a semicolon.

P r in t in g S tr in g s p r i n t f is a powerful function that w e'll examine in Chapter 3. So far. w e’ve only used p r i n t f to display a strin g literal — a series ofcharacters enclosed in double quotation marks. When p r i n t f displays a string literal, it doesn’t show the quo­ tation marks. p r i n t f doesn’t autom atically advance to the next output line when it fin­ ishes printing. To instruct p r i n t f to advance one line, we must include \ n (the n ew -lin e character) in the string to be printed. W riting a new-line character term i­ nates the current output line; subsequent output goes onto the next line. To illus­ trate this point, consider the effect of replacing the statement printf("To C z or not to C: that is the question.\n");

by two calls o f p r i n t f : printf("To C, or not to C: ”); printf("that is the question.\n");

2 .3

15 C o m m e n ts onmlkjihgfedcbaZYXWVUTSRQPONMLK

The first call of p r i n t f writes To C z o r n o t t o C : . The second call writes t h a t i s t h e q u e s t i o n , and advances to the next line. The net effect is the same as the original p r i n t f — the u sercan ’t tell the difference. The new-line character can appear inore than once in a string literal. To dis­ play the message kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA Brevity is the soul of wit. --Shakespeare

we could write printf("Brevity is the soul of wit.\n

2 .3

--Shakespeare\n");aZYXWVUTSRQPONMLKJIHGFE

C o m m e n ts Our p u n . c program still lacks something important: documentation. Every pro­ gram should contain identifying information: the program name, the date written, the authOr, the purpose of the program, and so forth. Tn C, this information is c o m m e n t s . The symbol / * marks the beginning of a comment and the placed in RQPONMLKJIHGFEDCBA symbol * / marks the end: /* This is a comment */

Comments may appear almost anywhere in a program, either on separate lines or on the same lines as other program text. Here's what p u n . c might look like with comments added al the be ginning: C?

C^

/* Name: pun.c */ /* Purpose: Prints a bad pun. */ /* Author: K. N. King */ #include int main(void) C, or not to C: that is the question.\n"); printf(”To PONMLKJIHGFEDCBA return 0;

Comments may extend over more than one line: once it has seen the / * sym­ bol, the compiler reads (and ignores) whatever follows until it encounters the * / symbol. Ifw e like, we can combine a series o fshort comments into one long com­ ment: /* Name: pun.c Purpose: Prints a bad pun. Author: K. N. King */

A comment like this can be hard to read, though, because it’s not easy to see where

C F u n d a m e n ta ls onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA C h a p te r 2 16 ZYXWVUTSRQPONMLKJIHGFEDCBA

the comment ends. Putting * / on a line by itselfhelps:kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA /* Name: p u n .c Purpose: Prints a bad pun. Author: K. N. King

Even better, we can form a “box*’ around the comment to make it stand out: * * ★ * * ★ * ★ * ★ ★ ★ *RQPONMLKJIHGFEDCBA ★

*

*



*

★ ★

*



*



*

* *

* *

* k

*

* *

*



*



* *

*

*



* *



*

★ ★

*

*



* *



*

*

* * *

* N a m e : p u n .c * Purpose: Prints a bad pun. * Author: K. N. King ★ ★

*

★ ★

*



*

★ ★

* *

★ ★

*

★ ★

* *

*



*

*

★ ★

*



k



-k

★ ★ ★ ★ ★ ★ ★ ★ ★

*

★ ★

★ ★ ★

k

k

k k

k

k k

k

k k

k

k k

k

Programmers often simplify boxed comments by omitting three o fth e sides: * Name: p u n .c * Purpose: Prints a bad pun. * Author: K. N. King

A short comment can go on the same line with other program code: int main(void)

/* Beginning of main program */

A comment like this is sometimes called a “winged comment.” Forgetting to terminate a comment may cause the compiler to ignore part of your program. Consider the following example: printf("My "); printf("cat ”); printf("has ”); printf("fleas");

/* forgot to close this comment... /* so it ends here */

Because w e’ve neglected to terminate the first comment, the compiler ignores the middle two statements, and the example prints My f l e a s . C99 provides a second kind ofcom m eni, which begins with | | (two adjacent slashes): // This is a comment

This style ofcom m eni ends automatically at the end o fa line. To create a comment that’s more than one line long, we can either use the older comment style ( / * ... * / ) or else put | | at.the beginning of each comment line: // Name: pun.c // Purpose: Prints a bad pun. // Author: K. N. King

2 .4

17 V a ria b le s a n d A s s ig n m e n t onmlkjihgfedcbaZYXWVUTSRQPONMLK

The newer comment style has a couple of important advantages. First, because a comment automatically ends al the end of a Hne, there’s no chance that an unterm i­ nated comment will accidentally consume^part of a program. Second, multiline comments stand out better, thanks to the | | that’s required at the beginning of each line.aZYXWVUTSRQPONMLKJIHGFEDCBA

2 .4

V a r ia b le s a n d A s s ig n m e n t Few programs are as simple as the one in Section 2.1. Most programs need lo per­ form a series ofcalculations before producing output, and thus need a way to store data temporarily during program execution. In C, as in most programming lan­ variables. dcbaZYXWVUTSRQPONMLKJIHGFEDCBA guages, these storage locations are called cbaZYXWVUTSRQPONMLKJIHGFEDCBA

T ypes

ra n g e o f i n t

v a l u e s > 7 . BA 1

Q&A

Every variable must have a type, which specifies whal kind of data it will hold. C has a wide variety of types. For now, w e’ll Hmit ourselves to just two: i n t and f l o a t . Choosing the proper type is critical, since the type affects how the variable is stored and what operations can be performed on the variable. The type of a numeric variable determines the largest and sm allest numbers that the variable can store; it also determines whether or not digits are allowed after the decimal point. A variable of type i n t (short for integer) can store a whole number such as 0, I, 392, o r-2 5 5 3 . The range ofpossible values is limited, though. The largest i n t value is typically 2.147,483,647 but can be as small as 32,767. A variable of type f l o a t (short fo v flo a tin g -p o in f) can store much larger numbers than an i n t variable. Furthermore, a f l o a t variable can store numbers with digits after the decimal point, like 379.125. f l o a t variables have drawbacks, however. Arithmetic on f l o a t numbers may be slower than arithmetic on i n t numbers. Most significantly, the value of a f l o a t variable is o ftenjusi an approx­ imation of the number that was stored in it. If we store 0.1 in a f l o a t variable, we may later find that the variable has a value such as 0.09999999999999987, thanks to rounding© error.

D e c la r a tio n s Variables must be declared— described for the benefit of the com piler— before they can be used. To declare a variable, we first specify the type of the variable, then its nam e. (Variable names are chosen by the programmer, subject to the rules described in Section 2.7.) For example, we might declare variables h e i g h t and p r o f i t as follows:kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA int height; float profit;

18 ZYXWVUTSRQPONMLKJIHGFEDCBA C h a p te r 2 C F u n d a m e n ta ls onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA

The first declaration states that h e i g h t is a variable of type i n t , meaning that h e i g h t can store an integer value. The second declaration says that p r o f i t is a variable oftype f l o a t . Ifsevcral variables have the same type, their declarations can be combined:kjihgfedcbaZYX int height, length, width, volume; float profit, loss;

Notice that each complete declaration ends with a semicolon. Our first template for m a in didn’t include declarations. When m a in contains declarations, these must precede statements: int main(void)cbaZYXWVUTSRQPONMLKJIHGFEDCBA declarations statem ents

biocks>/0.3

As we’ll see in Chapter 9, this is true of functions in general, as well as blocks (statements that contain embedded declarations). As a matter of style, it’s a good idea to leave a blank line between the declarations and the statements. In C99, declarations don’t have to come before statements. For example, m a in might contain a declaration, then a statement, and then another declaration. For compatibility with older compilers, the programs in this book don’t take advantage of this rule. However, it’s common in C++ and Java programs not to declare variables until they’re first needed, so this practice can be expected to become popular in C99 programs as well.dcbaZYXWVUTSRQPONMLKJIHGFEDCBA

A s s ig n m e n t A variable can be given a value by means of aRQPONMLKJIHGFEDCBA s s i g n m e n t . For example, the state­ ments height = 8; length = 12; width = 10;

assign values to h e i g h t , l e n g t h , and w i d t h . The numbers 8, 12, and 10 are said to be c o n s t a n t s . Before a variable can be assigned a value— or used in any other way, for that matter— it must first be declared. Thus, we could write int height; height = 8;

but not height = 8; int height;

/*** WRONG ***/

2 .4

19 V a r ia b le s a n d A s s ig n m e n t onmlkjihgfedcbaZYXWVUTSRQPONML

A constant assigned to a f l o a t variable usually contains a decimal point. For example, i f p r o f i t is a f l o a t variable, we might write kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA profit = 2150.48;dcbaZYXWVUTSRQPONMLKJIHGFEDCBA Q&A

It's best to append the letter f (for “float”) to a constant that contains a decimal point if the number is assigned to a f l o a t variable: profit = 2150.48f;

Failing to include the f may cause a waming from the compiler. An i n t variable is normally assigned a value of type i n t , and a f l o a t vari­ able is normally assigned a value o fty p e f l o a t . Mixing types (such as assigning an i n t value to a f l o a t variable or assigning a f l o a t value lo an i n t variable) is possible but not always safe, as w e’ll see in Section 4.2. Once a variable has been assigned a value, it can be used to help compute the value of another variable: height = 8; length-= 12; width = 10; volume = height * length * width;

/* volume is now 960 */

In C. * represents the multiplication operator, so this statement multiplies the val­ ues stored in h e i g h t , l e n g t h , and w i d t h , then assigns the result to the vari­ able v o lu m e . In general, the right side of an assignment can be a formula (or cbaZYXWVUTSRQPON expression, in C terminology) involving constants, variables, and operators.

P r i n t i n g th e V a lu e o f a V a r ia b le

We can use p r i n t f to display the current value of a variable. For example, to write the message Height: h

where h is the current value o fth e h e i g h t variable, we’d use the following call of p rin tf: printf("Height: %d\n", height);

%d is a placeholder indicating where the value o f h e i g h t is to be filled in during printing. Note the placement of \ n j u s t after %d, so that p r i n t f will advance to the next line after printing the value o f h e i g h t . %d works only for i n t variables; to print a f l o a t variable, w e’d use %f instead. By default, %f displays a number with six digits after the decimal point. To force %f to display p digits after the decimal point, we can put ,p between % and f . For example, lo print the line Profit: $2150.48

2 0 ZYXWVUTSRQPONMLKJIHGFEDCBA C F u n d a m e n ta ls onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA C h a p te r 2

w e ?d call p x ^ i n t f as follows:kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA printf("Profit:

$%.2f\n” , profit);

There's no limit to the number of variables that can be printed by a single call of p r i n t f . To display the values o fb o th the h e i g h t and l e n g t h variables, we could use the following call of p r i n t f : printf("Height:

PROGRAM

%d

Length:

%d\n", height, length);

C o m p u tin g th e D im e n s io n a i W e ig h t o f a B o x

dcbaZYXWVUTSRQPONMLKJIHGFEDCBA

Shipping companies don’t especially like boxes that are large but very light, since they take up valuable space in a truck or airplane. In fact, com panies often charge extra for such a box,7 basing M . 3

PROGRAM

(1.0f / 3.14159f)

If it contains operators; the expression should be enclosed in parentheses. Notice that w e’ve used only upper-case letters in macro names. This is a con­ vention that most C program m ers follow, not a requirem ent o fth e language. (StiU, C programmers have been doing this for decades; you w ouldn’t want to be the first to deviate.)

C o n v e r tin g fr o m F a h r e n h e it to C e ls iu s

dcbaZYXWVUTSRQPONMLKJIHGFEDCBA

The following program prom pts the user to enter a Fahrenheit temperature; it then prints the equivalent Celsius temperature. The output o fth e program will have the following appearance (as usual, input entered by the user is underlined): Enter Fahrenheit temperature: 212 Celsius equivalent: 100.0

The program will allow tem peratures that aren’t integers; that’s why the Celsius tem perature is displayed as 1 0 0 . 0 instead of 1 0 0 . L et’s look first at the entire program, then see how it’s put together.BA celsius.c

/* Converts a Fahrenheit temperature to Celsius */ #include #define FREEZING_PT 32.0f #define SCALE_FACTOR (5.0f / 9.0f) int main(void) { float fahrenheit, celsius; printf(”Enter Fahrenheit temperature:

ti

2 ,7

Id e n tifie rs hgfedcbaZYXWVUTSRQPONMLKJIHGF 2 5 kjihgfedcbaZYXWVU

scanf("%f", &fahrenheit); celsius = (fahrenheit - FREEZING_PT) * SCALE_FACTOR; printf("Celsius equivalent: %.lf\n", celsius); return 0;onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA

The >statement celsius = (fahrenheit - FREEZING_PT) * SCALE_FACTOR;

converts the Fahrenheit temperature to Celsius. Since FREEZING_PT stands for 3 2 . 0 f and SCALE_FACTOR stands for ( 5 . 0 f / 9 . 0 f ) , the compiler sees this statement as celsius = (fahrenheit - 32.0f) * (5.0f / 9.0f);

Defining SCALE_FACTOR to be ( 5 . 0 f / 9 . 0 f ) instead of (5 / 9) is impor­ tant, because C truncates the result when two integers are divided. The value of (5 / 9 j would be 0, which definitely isnT what we want. The call o f p r i n t f writes the Celsius temperature: 7

25.4

Id e n t if ie r s As w e’re writing a program, w e’ll have to choose names for variables, functions, macros, and other entities. These names are called cbaZYXWVUTSRQPONMLKJIHGFEDCBA identifiers. In C, an identifier may contain letters, digits, and underscores, but must begin with a letter or underscore. (In C99, identifiers may contain certain “universal character names” as WC11.) Here are some examples of legal identifiers: timesl0

get_next_char

_done

The following are not legal identifiers: 10times

get-next-char

The symbol 1 0 t i m e s begins with a digit, not a letter or underscore, g e t - n e x t c h a r contains minus sigOns,7 not underscores. C is case-sensitive: it distinguishes between upper-case and lower-case letters in identifiers. For example, the following identifiers are all different: job

joB

jOb

jOB

Job

JoB

JOb

JOB

C F u n d a m e n ta ls onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA C h a p te r 2 2 6 ZYXWVUTSRQPONMLKJIHGFEDCBA

These eight identifiers could all be used simultaneously, each for a com pletely dif­ ferent purpose. (Talk about obfuscation!) Sensible programmers try to make identi­ fiers look different unless they’re somehow related. Since case matters in C, many programmers foLlow the convention of using only lower-case letters in identifiers (other than macros), with underscores inserted when necessary for legibility:kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA symbol_table

name_and_address

current_page

Other programmers avoid underscores, instead using an upper-case letter to begin each word within an identifier: ^ symbolTable

Q&A

currentPage

nameAndAddressdcbaZYXWVUTSRQPONMLKJIHGFEDCBA

(The first letter is sometimes capitalized as well.) Although the former style is common in traditional C, the latter style is becoming more popular thanks to its widespread use in Java and C# (and. to a lesser extent. C++). Other reasonable conventions exist; just be sure to capitalize an identifier the same way each time it appears in a program. C places no limit on the maximum length of an identifier, so don’t be afraid to use long, descriptive names. A name such as c u r r e n t _ p a g e is a lot easier to understand than a name like c p .

K eyw o rd s

T able 2.1 Keywords

keyw ords in Table 2.1 have special significance to C compilers and therefore The cbaZYXWVUTSRQPONMLKJIHGFEDCBA can’t be used as identifiers. Note that five keywords were added in C99.

auto break case char const continue default do double else j

enum extern float for goto if inline’ int long register

------ ;— f— restrict return short signed sizeof static struct switch typedef union

unsigned void volatile while _Bool' -Complex’ —Imaginary7

C99 only

Because o f C ’s case-sensitivity, keywords must appear in programs exactly as shown in Table 2.1, with all letters in lower case. Names o ffunctions in the stan­ dard library (such as p r i n t f ) contain only lower-case letters also. Avoid the plight of the unfortunate programmer who enters an entire program in upper case, only to find that the com piler can’t recognize keywords and calls of library func­ tions.

2 .8

2 7 kjihgfedcbaZYXWVUTS L a y o u t o f a C P r o g ra m hgfedcbaZYXWVUTSRQPONMLKJIHGFED

Watch out for other restrictions on identifiers. Some compilers treat certain identi­ fiers (asm , for example) as additional keywords. Identifiers that belong to the stan­ onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA dard library are restricted as well. Accidentally using one of these names can cause an error during compilation or linking. Identifiers that begin with an underscore > 2 1 .1 are also restricted.aZYXWVUTSRQPONMLKJIHGFEDCBA

A re s tric tio n s o n id e n tifie r s

2 .8

L a y o u t o f a C P ro g ra m ken s: groups of characters that can’t cbaZYXWVUTSRQPONMLKJIHGFEDCBA We can think of a C program as a series of to be split up without changing their meaning. Identifiers and keywords are tokens. So are operators like + and - , punctuation marks such as the com m a and sem ico­ lon, and string literals. For example, the statement

printf(''Height: %d\n", height);

consists of seven tokens: printf ©

( ©

"Height: %d\n" ®

, ®

height ®

) ®

; ®

Tokens © and ® are identifiers, token ® is a string^»* literal, and tokens ®. @. ©, and ® are punctuation. The amount of space between tokens in a program isn't critical in most cases. At one extreme, tokens can be crammed together with no space between them at all, except where this would cause two tokens to merge into a third token. For example, we could delete most of the space in the c e l s i u s . c program of Sec­ tion 2.6, provided that we leave space between tokens such as i n t and m a in and between f l o a t and f a h r e n h e i t : /* Converts a Fahrenheit temperature to Celsius */ #include #define FREEZING_PT 32.0f #define SCALE_FACTOR (5.0f/9.0f) int main(void){float fahrenheit,celsius;printf( "Enter Fahrenheit temperature: ");scanf("%f", &fahrenheit); celsius=(fahrenheit-FREEZING_PT)*SCALE_FACTOR; printf("Celsius equivalent: %.lf\n", celsius);return 0;)

ln fact, ifth e page were wider, we could put the entire m a in function on a single Line. We can’t put the whole program on one line, though, because each prepro­ cessing directive requires a separate line. Com pressing programs in this fashion isn’t a good idea. In fact, adding spaces and blank lines to a program can make it easier to read and understand. Fortunately,

C F u n d a m e n ta ls dcbaZYXWVUTSRQPONMLKJIHGFEDCBA C h a p te r 2 2 8 ZYXWVUTSRQPONMLKJIHGFEDCBA

C allows us to insert any am ount o fsp ace— blanks, tabs, and new-line characters— between tokens. This rule has several im portant consequences for program layout:cbaZYXWVU

■ Statem ents can be d ivid ed over any number of lines. The following statement, for example, is so long that it would be hard to squeeze it onto a single line:kjihgfedcbaZ

printf("Dimensional weight (pounds): %d\n", (volume + INCHES_PER_POUND - 1) / INCHES_PER_POUND); ■ Space betw een tokens makes it easier for the eye to separate them. For this reason, 1 usually put a space before and after each operator:

volume = height * length * width;

1 also put a space after each comma. Some programmers go even further, put­ ting spaces around parentheses and other punctuation. ■ Indentation can m ake nesting easier to spot. For example, we should indent declarations and statem ents to make it clear that they’re nested inside m a in . Q&A onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA ■ B lank lines can divide a program into logical units, making it easier for the reader to discern the program ’s structure. A program with no blank lines is as hard to read as a book with no chapters.

The c e l s i u s . c program of Section 2.6 illustrates several of these guide­ lines. L et’s take a closer look at the m a in function in that program: int main(void) float fahrenheit, celsius; printf("Enter Fahrenheit temperature: scanf("%f", &fahrenheit); celsius = (fahrenheit - FREEZING_PT)

”);

* SCALE_FACTOR;

printf("Celsius equivalent: %.lf\n'\ celsius); return 0;

First, observe how the space around =, - , and * makes these operators stand out. Second, notice how the indentation of declarations and statements makes it obvi­ ous that they all belong to m a in . Finally, note how blank lines divide m a i n into five parts: (1) declaring the f a h r e n h e i t and c e l s i u s variables; (2) obtaining the Fahrenheit temperature; (3) calculating the value of c e l s i u s ; (4) printing the Celsius temperature; and (5) returning to the operating system. W hile w e’re on the subject of program layout, notice how I’ve placed the { token underneath m a in () and put the matching } on a separate line, aligned with {. Putting } on a separate line lets us insert or delete statem ents at the end of the function; aligning it with { makes it easy to spot the end o f m a in . A final note: Although extra spaces can be added betw een tokens, it’s not pos-

29 Q & A onmlkjihgfedcbaZYXWVUTSRQPONMLKJIH sible ro add space cbaZYXWVUTSRQPONMLKJIHGFEDCBA wiihin a token without changing the meaning of the program or causing an error. Writing kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA fl oat fahrenheit, celsius;

/*** WRONG ***/

or fl oat fahrenheit, celsius;

/*** WRONG ***/

produces an error when the program is compiled. Putting a space inside a string lit­ eral is allowed, although it changes the meaning of the string. However, putting a new-line character in a string (in other words, splitting the string over two lines) is illegal: printf("To C, or not to C: that is the question.\n");

c o n iin u in g a s tr in g > 7 3 j

/*** WRONG ***/

Continuing a string from one line to the next requires a special technique that w e'llhgfedcbaZYXWVUTSRQ learn in a later chapter.aZYXWVUTSRQPONMLKJIHGFEDCBA

Q & A dcbaZYXWVUTSRQPONMLKJIHGFEDCBA Q:

W h a t d o e s G C C s ta n d fo r ? [p . 1 1 ]

A:

GCC originally stood for “GNU G compiler.” It now stands for “GNU G om piler Collection,” because the current version o fG C C compiles programs written in a variety oflanguages. including Ada. C. C++. Fortran. Java, and Objective-C.

Q:

O K , s o w h a t d o e s G N U s ta n d fo r ?

A:

GNU stands for “G N U ’s Not UNIX!'" (and is pronounced guh-NEW> by the way). GNU is a project of the Free Software Foundation, an organization set up by Rich­ ard M. Stallman as a protest against the restrictions of licensed UNIX software. According to its web site, the Free Software Foundation believes that users should be free to “run. copy, distribute, study, change and im prove” software. The GNU Project has rewritten much traditional UNIX software from scratch and made it publicly available at no charge. GCC and other GNU software are crucial to Linux. Linux itself is only the “kernel” of an operating system (the part that handles program scheduling and basic I/O services): the GNU software is necessary to have a fully functional oper­ ating system. For more information on the GNU Project, visit w w w .gm i.org.

Q:

W h a t ’s th e b ig d e a l a b o u t G C C , a n y w a y ?

A:

GCC is significant for many reasons, not least the fact that it’s free and capable of compiling a number of languages. It runs under many operating systems and gen­ erates code for many different CPUs, including all the widely used ones. GCC is

C h a p te r 2 C F u n d a m e n ta ls onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA 3 0 ZYXWVUTSRQPONMLKJIHGFEDCBA

the primary compiler for many UNIX-based operating systems, including Linux, BSD, and Mac OS X, and it’s used extensively for commercial software develop­ ment. For more information about GCC, visit cbaZYXWVUTSRQPONMLKJIHGFEDCBA gcc.g nu.o rg. dcbaZYXWVUTSRQPONMLKJIHGFEDCBA Q:

H o w g o o d is G C C a t fin d in g e r r o r s in p r o g r a m s ?

A:

GCC has various command-line options that control how thoroughly il checks pro­ grams. When these options are used, GCC is quite good at finding potential trouble spots in a program. Here are some of the more popular options: -W a ll

-W -p e d a n tic -a n si -s td = c 8 9 -s td = c 9 9

Causes the compiler to produce warning messages when it detects possible errors. (-W can be followed by codes for specific warnings; - W a l l means u all -W options.” ) Should be used in conjunction with - 0 for maximum effect. Issues additional warning messages beyond diose produced by - W a ll. Issues all warnings required by the C standard. Causes pro­ grams that use nonstandard features to be rejected. Turns off features of GCC that aren’t standard C and enables a few standard features that are normally disabled. Specifies which version of C the compiler should use to check the program.

These options are often used in combination:kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA % gcc -0 -Wall -W -pedantic -ansi -std=c99 -o pun pun.c Q:

W h y is C s o te r s e ? I t s e e m s a s th o u g h p r o g r a m s w o u ld b e m o r e r e a d a b le i f C u s e d b e g i n a n d e n d in s te a d o f { a n d } , i n t e g e r in s te a d o f i n t , a n d so fo r th , [p . 12]

A:

Legend has it that the brevity o fC programs is due to the environment that existed in Bell Labs at the tim e the language was developed. The first C com piler ran on a DEC P D P -11 (an early minicomputer); programmers used a teletype— essentially a typewriter connected to a computer— to enter programs and print listings. Because teletypes were very slow (they could print only 10 characters per second), minimizing the number ofcharacters in a program was clearly advantageous.

Q:

In s o m e C b o o k s , th e m a i n fu n c tio n e n d s w ith e x i t ( 0 ) in s te a d o f r e t u r n 0 . A r e th e s e th e s a m e ? [p . 1 4 ]

A:

When they appear inside m a in , these statements are indeed equivalent: both ter­ minate the program, returning the value 0 to the operating system. Which one lo use is mostly a matter of taste.

Q:

W h a t h a p p e n s i f a p r o g r a m r e a c h e s th e e n d o f th e m a i n fu n c tio n w it h o u t e x e ­ c u tin g a r e t u r n s ta te m e n t? [p . 14 ]

A:

The r e t u r n statement isn’t mandatory; if it’s missing, the program will still ter-

Q & A hgfedcbaZYXWVUTSRQPONMLKJIHGFED 31 aZYXWVUTSRQPONML

minate. ln C89, the value returned to the operating system is undefined. In C99, if ?C 9SL onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA m a in is declared to return an i n t (as in our examples), the program returns 0 to the operating system; otherwise, the program returns an unspecified value.dcbaZYXWVUTSRQPONMLKJIHGFED Q:

D o e s th e c o m p ile r r e m o v e a c o m m e n t e n tir e ly o r r e p la c e it w ith b la n k s p a c e ?

A:

Some old C compilers deleted all the characters in each comment, making it possi­ ble to write kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA a/**/b = 0;

and have the compiler interpret it as ab = 0 ;

According to thc C standard, however, the compiler must replace each comment by a single space character, so this trick doesn’t work. Instead, we’d end up with the following (illegal) statement: a b = 0; Q:

H o w c a n I tell if m y p r o g r a m h a s a n u n te r m in a te d c o m m e n t?

A:

If you’re lucky, the program w on't compile because the comment has rendered the program illegal. Ifth e program does compile, there are several techniques that you can use. Stepping through the program line by line with a debugger will reveal if any lines are being skipped. Some IDEs display comments in a distinctive color to distinguish them from surrounding code. Ify o u 're using such an environment, you can easily spot unterminated comments, since program text will have a different color if it's accidentally included in a comment. A program such as l i n t can also help.

iin t> /.2

Q:

Is it leg a l to n e s t o n e c o m m e n t in s id e a n o th e r ?

A:

Old-style comments ( / * ... * / ) can’t be nested. For instance, the following code is illegal: /* /*** WRONG ***/ */

The * / symbol on the second line matches the / * symbol on the first line, so the compiler will flag the * / symbol on the third line as an error. C ’s prohibition against nested comments can sometimes be a problem. Sup­ pose we’ve written a long program containing many short comments. To disable a portion of the program temporarily (during testing, say), our first impulse is to “comment out” the offending lines with / * and * / . Unfortunately, this method won’t work if the lines contain old-style comments. C99 comments (those begin­ ning with / / ) can be nested inside old-style comments, however— another advan­ ta& ge to usinos this kind ofcom m ent.

3 2 ZYXWVUTSRQPONMLKJIHGFEDCBA C h a p te r 2 C F u n d a m e n ta ls onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA

Ln any event, there’s a better way to disable portions of a program , as w e’ll see aZYXWV d is a b lin g c o d e >

M .4

latCl*.dcbaZYXWVUTSRQPONMLKJIHGFEDCBA

Q:

W h e r e d o e s th e f l o a t ty p e g e t its n a m e ? [p . 17]

A:

f l o a t is short for “floating-point,” a technique for storing numbers in which the decimal point “floats.” A f l o a t value is usually stored in two parts: the fraction (or mantissa) and the exponent. The number 12.0 might be stored as 1.5 x 2 3 , for example, where 1.5 is the fraction and 3 is the exponent. Some program m ing lan­ guages call this type r e a l instead of f l o a t .

Q:

W h y d o fl o a ti n g -p o in t c o n s t a n ts n e e d to e n d w ith th e le tt e r f ? [p . 19]

A:

For the fuU explanation, see Chapter 7. H ere’s the short answer: a constant that contains a decimal point but doesn’t end with f has type d o u b l e (short for “dou­ ble precision”), d o u b l e values are stored more accurately than f l o a t values. Moreover, d o u b l e values can bc larger than f l o a t values, which is why we need to add the letter f when assigning to a f l o a t variable. W ithout the f , a warning may be generated about the possibility of a number being stored into a f l o a t variable that exceeds the capacity o fth e variable.

*Q :

A:

e x te m a iiin k a g e > /s .2

Is it r e a lly tr u e th a t t h e r e ’s n o lim it o n th e le n g th o f an id e n t if ie r ? [p . 2 6 ]

Yes and no. The C89 standard says that identifiers may be arbitrarily long. How­ ever, com pilers are only required to rem em ber the first31 characters (63 characters in C99). Thus, if two names begin with the same 31 characters, a com piler might be unable to disting^^uish between them. To make matters even more complicated, there are special rules for identifiers with external linkage; most function names fall into this category. Since these names must be made available to the linker, and since some older linkers can han­ dle only short names, only the first six characters are significant in C89. Moreover, the case of letters may not matter. As a result. ABCDEFG and a b c d e f h might be treated as the same name. (In C99, the first 31 characters are significant, and the case of letters is taken into account.) Most compilers and linkers are more generous than the standard, so these rules aren’t a problem in practice. Don’t worry about making identifiers too long— worry about making them too short.

Q:

H o w m a n y s p a c e s s h o u ld I u se fo r in d e n ta tio n ? [p . 28 ]

A:

T hat’s a tough question. Leave too little space, and the eye has trouble detecting indentation. Leave too much, and lines run off the screen (or page). Many C pro­ grammers indent nested statements eight spaces (one tab stop), which is probably too much. Studies have shown that the optimum amount of indentation is three spaces, but many programmers feel uncomfortable with numbers that aren’t a power of two. Although 1 normally prefer to indent three or four spaces. I ’ll use two spaces in this book so that my programs will fit within the margins.

E x e rc is e s hgfedcbaZYXWVUTSRQPONMLKJIHG 3 3 aZYXWVUTSRQPON

E x e r c is e s S e c t i o n 2 .1

I.

Create and run Kemighan and Ritchie’s famous “hello, world" program:

onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA

# in c lu d e

< s td io .h >

i n t m a in (v o id ) { p rin tf(" h e llo ,

w o rld \n " );

Do you gct a warning message from the compiler? Ifso , what’s needed to make it go away? S e c tio n 2 .2

©

2.

C onsiderihefollow ingprogram : # in c lu d e

< s td io .h >

i n t m a in (v o id ) p r i n t f ( * 'P a r k i n s o n * s L a w :\n W o rk e x p a n d s s o a s p r i n t f ( " f i l l th e tim e \n " ) ; p r i n t f ( " a v a i l a b l e f o r i t s c o m p le tio n .\n " ) ; r e t u r n 0;

to

");

(a) Identify the directives and statements in this program. (b) What output does the program produce? S e c tio n 2 .4

S e c tio n 2 .7

©

3.

Condense the d w e i g h t . c program by (!) replacing the assignments to h e i g h t , l e n g t h , and w i d t h with initializers and (2) removing the w e i g h t variable, instead cal­ culating (v o lu m e 4 -1 6 5 ) / 1 6 6 within the l a s t p r i n t f .

©

4.

Write a program that declares several i n t and f l o a t variables— without initializing them— and then prints their values. Is there any pattern to the values? (Usually there isn’t.)

©

5.

Which of the following are not legal (a) (b) (c) (d)

C

identifiers?

1 0 0 _ b o ttle s _ 1 0 0 _ b o ttle s o n e __ h u n d r e d __ b o t t l e s b o ttle s _ b y _ th e _ h u n d re d _

6.

Why is it not a good idea for an identifier to contain more than one adjacent underscore (as in c u r r e n t b a l a n c e , for example)?

7.

Which of the following are keywords in C? (a) (b) (c) (d) (e)

fo r If m a in p rin tf w h ile

© A nsw er available on the Web at cbaZYXWVUTSRQPONMLKJIHGFEDCBA knking.coinfl>ooks/c2.

3 4 ZYXWVUTSRQPONMLKJIHGFEDCBA C h a p te r 2 C F u n d a m e n ta ls S e c tio n 2.8 onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA © 8. How many tokens are there in the following statem ent? kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA

answer=(3*q-p*p)/3; 9. 10.

Insert spaces between the tokens in Exercise 8 to make the statement easier to read. In the d w e i g h t . c program (Section 2.4). which spaces are essential? aZYXWVUTSRQPONMLKJIHGFEDCBA

P r o g r a m m in g P r o je c t s 1.

Write a program that uses p r i n t f to display the following picture on the screen: *

★ * *

* * * *

©

2.

W rite a program that computes the volume o f a sphere with a 10-meter radius, using the for­ mula v = 4/3jr/^. Write the fraction 4/3 as 4 . 0 f / 3 . 0 f . (Try writing it as 4 / 3 . W hat hap­ H int: C doesn't have an exponentiation operaton so you'll need to multiply r by itself pens?) cbaZYXWVUTSRQPONMLKJIHGFEDCBA twice to compute A

3.

Modify the program of Programming Project 2 so that it prompts the user to enter the radius o f the sphere.

4.

Write a program that asks the user to enter a dollars-and-cents amount, then displays the amount with 5% tax added:

Enter an amount: 100.00 With tax added: $105.00 5.

W rite a program that asks the user to enter a value for .v and then displays the value o f the following polynomial: 3.v5 + 2x 4 - 5.v3 - x 2 + 7 r - 6 H int: C doesn’t have an exponentiation operator, so you’ll need to multiply .v by itself repeatedly in order lo compute the powers o f i . (For exam ple, x * x * x is x cubed.)

6.

M odify the program of Program ming Project 5 so that the polynomial is evaluated using the following formula: ((((3A + 2).v-5)A *-I).r + 7 ).v -6 Note that the modified program perform s fewer multiplications. This technique for evaluat­ ing polynomials is known as H o r n e r ’s R u le .

7.

W rite a program that asks the user to enter a U.S. dollar amount and then shows how to pay that amount using the smallest num ber ofS 20, $10, $5, and $1 bills:

Enter a dollar amount: 93 $20 $10 $5 $1

bills: bills: bills: bills:

4 1 0 3

P r o g ra m m in g P ro je c ts hgfedcbaZYXWVUTSRQPONMLKJIHGFE 3 5 cbaZYXWVUTSRQPONMLK

Hint: onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA Divide the amount by 20 to determine the number of$ 2 0 bills needed, and then reduce the amount by the total value of the $20 bills. Repeat for the other bill sizes. Be sure to use integer values throughout, not floating-point numbers.

8.

W rite a program that calculates the remaining balance on a loan after the first, second, and third monthly payments:kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA

Enter amount of loan: 20000.00 Enter interest rate: 6.0 Enter monthly payment: 386.66 Balance remaining after first payment: $19713.34 Balance remaining after second payment: $19425.25 Balance remaining after third payment: $19135.71 Display each balance with two digits after the decimal point. Hint: Each month, the balance is decreased by the amount of the payment, but increased by lhc balance times the monthly interest rate. To find the monthly interest rate, convert the interest rate entered by the user to a percentage and divide it by 12.

3

F o r m a t te d ln p u t /O u t p u t

ZYXWVUTSRQPONMLKJIHGFEDCBA

In s e e k in g th e u n a tta in a b le , s im p lic it y o n ly g e t s in th e w a y onmlkjihgfedcbaZYXWVU

s c a n f and p r i n t f , which support form atted reading and writing, are two of the most frequently used functions in C. As this chapter shows, both are powerful but tricky to use properly. Section 3.1 describes p r i n t f , and Section 3.2 covers s c a n f . Neither section gives com plete details, which will have to wait until Chap­ ter 22.

3.1

T h e p r in tfF u n c tio n

The p r i n t f function is designed to display the contents o f a siring, known as the cbaZYXWVUTSRQPONML fo r m a t strin g, with values possibly inserted at specified points in the string. When it’s called, p r i n t f must be supplied with the format string, followed by any val­ ues that are to be inserted into the string during printing:hgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA p r in tf( s m n g ,

e x p r^

e x p r - i,

...);

The values displayed can be constants, variables, or more com plicated expressions. There's no limit on the number o f values that can be printed by a single call of p rin tf. The format string may contain both ordinary characters and co n version sp e ci­ fic a tio n s , which begin with the % character. A conversion specification is a place­ holder representing a value to be filled in during printing. The inform ation that follows the % character specifies how thc value is converted from its intemal form (binary) to printed form (characters)— that’s where the term ' ‘conversion specifica­ tion” comes from. For exam ple. the conversion specification %d specifies that p r i n t f is to convert an i n t value from binary to a string of decimal digits, while %f does the same for a f l o a t value.

37

C h a p te r 3 F o rm a tte d ln p u t/O u tp u t 38 ZYXWVUTSRQPONMLKJIHGFEDCBA

Ordinary characters in a format string are printed exactly as they appear in the string; conversion specifications are replaced by the values to be printed. Consider the following example:kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA int i , j ; float x, y; i j x y

= = = =

10; 20; 43.2892f; 5527.0f;

printf("i = %d, j = %d, x = %f, y = %f\n", i, j z x, y);

This call of p r i n t f produces the following output: i = 10, j = 20, x = 43.289200, y = 5527.000000

The ordinary characters in the format string are simply copied to the output line. The four conversion specifications are replaced by the values of the variables i , j , x . and y, in that order.

A

C com pilers aren’t required to check that the num ber o f conversion specifications in a form at string matches the num ber of output items. The following call of p r i n t f has more conversion specifications than values to be printed: printf("%d %d\n", i);

/*** WRONG ***/

p r i n t f will print the value of i correctly, then print a second (m eaningless) inte­ ger value. A call with too few conversion specifications has sim ilar problem s: printf("%d\n", i, j);

/*** WRONG ***/

ln this case, p r i n t f prints the value of i but doesn’t show the value of j . Furtherm ore, compilers aren’t required to check that a conversion specifica­ tion is appropriate for the type of item being printed. If the program m er uses an incorrect specification, the program will simply produce m eaningless output. Con­ sider the following call o f p r i n t f , in which the i n t variable i and the f l o a t variable x are in the wrong^* order: printf("%f %d\n", i, x);

/*** WRONG ***/

Since p r i n t f must obey the form at string, it will dutifully display a f l o a t value, followed by an i n t value. Unfortunately, both will be meaningless.dcbaZYXWVUTSRQPO

C o n v e r s io n S p e c ific a tio n s Conversion specifications give the program m er a great deal of control over the appearance ofoutput. On the other hand, they can be com plicated and hard to read. In fact, describing conversion specifications in com plete detail is too arduous a

3.1

3 9 onmlkjihgfedcbaZYXWVU The p r i n t f Function hgfedcbaZYXWVUTSRQPONMLKJIHGFE

task to tackle this early in the book. Instead, w e’llju st take a brief look at some of their more important capabilities. In Chapter 2, we saw that a conversion specification can include formatting information. In particular, we used %. I f to display a f l o a t value with one digit after the decimal point. More generally, a conversion specification can have the form % m .p X or % -/n.pX , where m and p are integer constants and X is a letter. Both m and p are optional; if p is omitted, the period that separates m and p is also dropped. In the conversion specification % 10. 2 f . m is 10, p is 2, and X is f . In the specification % 10f. ni is 10 and p (along with the period) is missing, but in the specification %. 2 f , p is 2 and in is missing. The m in iin u m fie ld width, ni. specifies the minimum number ofcharacters to print. If the value to be printed requires fewer than m characters, the value is rightjustified within the field. (In other words, extra spaces precede the value.) For example, the specification %4d would display the number 123 as « 1 2 3 . (In this chapter, I’ll use • to represent the space character.) If the value to be printed requires more than in characters, the field width automatically expands to the nec­ essary size. Thus, the specification %4d would display the number 12345 as 1 2 3 4 5 — no digits are lost. Putting a minus sign in front of m causes leftjustification; the specification % -4d would display 123 as 1 2 3 * . The meaning of the p recisio n , p, isnT as easily described, since it depends on the choice ofX , the conversion specifier, X indicates which conversion should be applied to the value before it’s printed. The most common conversion specifiers for numbers are:aZYXWVUTSRQPONMLKJIHGFEDCBA Q&A

d — Displays an integer in decimal (base 10) form, p indicates the minimum number of digits to display (extra zeros are added to the beginning of the num­ ber if necessary); if/? is omitted, it is assumed to have the value 1. (In other words, %d is the same as %. Id .) ■

e — Displays a floating-point number in exponential format (scientific nota­ tion). p indicates how many digits should appear after the decimal point (the default is 6). I f ^ is 0, the decimal point is not displayed. ■

■ f — Displays a floating-point number in “fixed decim al” format, without an exponent, p has the same meaning as for the e specifier. ■

g — Displays a floating-point number in either exponential format or fixed decimal format, depending on the num ber’s size, p indicates the maximum number of significant digits (not digits after the decimal point) to be dis­ played. Unlike the f conversion, the g conversion w on’t show trailing zeros. Furthermore, if the value to be printed has no digits after the decimal point, g doesn’t display the decimal point.

The g specifier is especially useful for displaying numbers whose size can’t be predicted when the program is written or that tend to vary widely in size. When used to print a moderately large or moderately small number, the g specifier uses fixed decimal format. But when used to print a very large or very small number, the g specifier switches to exponential format so that the number will require fewer characters.

C h a p te r 3 F o rm a tt e d ln p u t/O u tp u t aZYXWVUTSRQPONMLKJIHGFEDCBA 4 0 ZYXWVUTSRQPONMLKJIHGFEDCBA s p e c i f i e r s fo r I n t e g e r s > 7 . BA 1 s p e c ifie r s fo r flo a ts >

7.2

s p e c ifie r s fo r c h a r a c t e r s > 7 . 3 s p e c i f i e r s fo r s t r in g s >

13.3

There are many other specifiers besides %d. %e. %f. and %g. Tll gradually introduce many of them in subsequent chapters. For lhe full list, and for a complete explanation of the other capabilities of conversion specifications, consult Section 22.3.

P R O G R A M onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA U s in g p r i n t f to F o r m a t N u m b e r s

The following program illustrates the use o f p r i n t f to print integers and float­ ing-point numbers in various formats.kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA tprintf.cdcbaZYXWVUTSRQPONMLKJIHGFEDCBA /* Prints int and float values in various formats */ #include int main(void) int i ; float X; i = 40; x = 839.21f; printf(" %d|%5d %-5d|%5.3d|\n", i, i, i, i); printf(” %10.3f %10.3e|%-10g|\n", x, x, x); return 0; }

The | characters in the p r i n t f format strings are there merely to help show how much space each number occupies when printed; unlike % or \ , the | charac­ ter has no special significance to p r i n t f . The output of this program is: 40|

| 040| 40|40 839.210| 8.392e+02|839.21

Let’s take a closer look at the conversion specifications used in this program: ■ %d —

Displays i in decimal form, using a minimum amount of space.

■ % 5d — Displays i in decimal form, using a minimum of five characters. Since i requires only two characters, three spaces were added. ■ % - 5d — Displays i in decimal form, using a minimum of five characters; since the value of i doesn’t require five characters, the spaces are added after­ ward (that is, i is left-justified in a field of length five).





% 5. 3 d — Displays i in decimal form, using a minimum of five characters overall and a minimum of three digits. Since i is only two digits long, an extra zero was added to guarantee three digits. The resulting number is only three characters long, so two spaces were added, for a total of five characters ( i is right-justified). % 1 0 .3 f — Displays x in fixed decimal form, using 10 characters overall,

3.1

41 T he p r i n t f Function onmlkjihgfedcbaZYXWVUTSRQPONMLKJI

with three digits after the decim al point. Sincc x requires only seven charac­ ters (three before the decimal point, three after the decimal point, and one for the decimal point itself), three spaces precede x. ■ % 1 0. 3 e — Displays x in exponential form, using 10 characters overaU, with three digits after the decim al point, x requires nine characters altogether (including the exponent), so one space precedes x. ■

% -1 0 g — Displays x in either fixed decimal form or exponential form, using 10 characters overall, fn this case, p r i n t f chose to display x in fixed deci­ mal form. The presence of the minus sign forces left justification, so x is fol­ lowed by four spaces.

E scap e S eq uen ces The \ n code that we often use in format strings is called an escape se q u e n c e . Escape sequences enable strings lo contain characters that would otherw ise cause problem s for the compiler, including nonprinting (control) characters and charac­ ters that have a special meaning to the com piler (such as "). W e’ll provide a com ­ plete list ofescape sequences later; for now, here’s a sample: escape sequences> 7 .3 dcbaZYXWVUTSRQPONMLKJIHGFEDCBA Alert (bell) Backspace New line H orizontaltab

Q&A

\a \b \n \t

When they appear in p r i n t f format strings, these escape sequences represent actions to perform upon printing. Printing \ a causes an audible beep on most machines. Printing \ b moves the cursor back one position. Printing \ n advances the cursor to the be ^g**inning^>* of thiwe' next line. Printing \ t moves the cursor to the next tab stop. A string may contain any num ber ofescape sequences. Consider the following p r i n t f exam ple, in which the form at string contains six escape sequences:kjihgfedcbaZYXWVUTSRQPONMLK printf ("Item\tUnit\tPurchase\n\tPrice\tDate\n") ;

Executing this statement prints a tw o-line heading: Item

Unit Price

Purchase Date

Another common escape sequence is \ ” . which represents the ” character. Since the ” character marks the beginning and end of a string, it can’t appear within a string without the use of this escape sequence. H ere’s an example: printf(”\ "Hello!\"");

This statement produces the following output: "Hello!"

F o rm a tte d ln p u t/O u tp u t onmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA C h a p te r 3 4 2 ZYXWVUTSRQPONMLKJIHGFEDCBA

Incidentally, you can’t just put a single \ character in a string; the com piler will assume that it’s the beginning of an escape sequence. To print a single \ char­ acter, put two \ characters in the string:kjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA printf("\\");

3 .2

/* prints one \ character */aZYXWVUTSRQPONMLKJIHGFEDCBA

T h e s c a n f F u n c t io n Just as p r i n t f prints output in a specified format, s c a n f reads input according lo a particular format. A s c a n f format string, like a p r i n t f format string, may contain both ordinary characters and conversion specifications. The conversions allowed with s c a n f are essentially the same as those used with p r i n t f . In many cases, a s c a n f format string will contain only conversion specifica­ tions, as in the following example: int i , j ; float x, y; scanf("%d%d%f%f",

&i, &j, &x, &y);

Suppose that the user enters the following input line: 1 -20 .3 -4.0e3

s c a n f will read the line, converting its characters to the numbers they represent, and then assign 1, -2 0 , 0.3, and ^4000.0 to i , j , x , and y, respectively. “Tightly packed” format strings like "% 2 2 . 3

43 T h e s c a n f F u n c tio n onmlkjihgfedcbaZYXWVUTSRQPONMLK

Calling s c a n f is a powerful but unforgiving way to read data. Many profes­ sional C programmers avoid s c a n f , instead reading all data in characterform and converting it to numeric form later. W e’ll use s c a n f quite a bit, especially in the early chapters of this book, because it provides a simple way to read numbers. Be aware, however, that many of our programs won’t behave properly if the user enters unexpected input. As w e’ll see later, it’s possible to have a program testhgfedcbaZYXWVUTSR whether s c a n f successfully read the requested data (and attempt to recover if it didn’t). Such tests are impractical for the programs in this book— they would add too many statements and obscure the point of the examples.dcbaZYXWVUTSRQPONMLKJIHGFEDCBA H ow s c a n f W orks

s c a n f can actually do much more than I've indicated so far. It is essentially a “pattern-m atching’’ function that tries to match up groups of input characters with conversion specifications. Like the p r i n t f function, s c a n f is controlled by the format string. When it is called, s c a n f begins processing the information in the string, starting at the left. For each conversion specification in the format string, s c a n f tries to locate an item o fth e appropriate type in the input data, skipping blank space if necessary, s c a n f then reads the item, stopping when it encounters a character that can’t pos­ sibly belong to the item. If the item was read successfully, s c a n f continues pro­ cessing the rest of the format string. If any item is not read successfully, s c a n f returns immediately without looking at the rest of the format string (or the remain­ ing input data). As it searches for the beginning of a number, s c a n f ignores RQPONMLKJIHGFEDCBA w h ite - s p a c e c h a r a c t e r s (the space, horizontal and vertical tab, form-feed, and new-line charac­ ters). As a result, numbers can be put on a single line or spread out over several lines. Consider the following call of s c a n f : scanf("% d% d% f% f", & i, & j, &x, &y); Suppose that the user enters three lines of input: 1 - 20

.3 - 4. 0e3

s c a n f sees one continuous stream ofcharacters: • • i a- 20