0% found this document useful (0 votes)
343 views

C The Complete Reference Index PDF

C The Complete Reference Index Pdf

Uploaded by

ewtrergr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
343 views

C The Complete Reference Index PDF

C The Complete Reference Index Pdf

Uploaded by

ewtrergr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Page i

C
The Complete Reference
Fourth Edition
Page ii

ABOUT THE AUTHOR


Herbert Schildt is the world's leading programming author. He is an authority on the C and C++
languages, a master Windows programmer, and an expert on Java. His programming books have
sold more that 2.5 million copies worldwide and have been translated into all major foreign
languages. He is the author of numerous bestsellers, including C++: The Complete Reference,
Teach Yourself C, Teach Yourself C++, C++ from the Ground Up, Windows 2000 Programming
from the Ground Up, and Java: The Complete Reference. Schildt holds a master's degree in
computer science from the University of Illinois. He can be reached at his consulting office at (217)
586-4683.
Page iii

C
The Complete Reference
Fourth Edition

Herbert Schildt
Page iv

Copyright © 2000 by The McGraw-Hill Companies. All rights reserved. Manufactured in the United
States of America. Except as permitted under the United States Copyright Act of 1976, no part of
this publication may be reproduced or distributed in any form or by any means, or stored in a
database or retrieval system, without the prior written permission of the publisher.

0-07-213295-7

The material in this eBook also appears in the print version of this title: 0-07-212124-6.

All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after
every occurrence of a trademarked name, we use names in an editorial fashion only, and to the
benefit of the trademark owner, with no intention of infringement of the trademark. Where such
designations appear in this book, they have been printed with initial caps.

McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales
promotions, or for use in corporate training programs. For more information, please contact George
Hoare, Special Sales, at [email protected] or (212) 904-4069.

TERMS OF USE

This is a copyrighted work and The McGraw-Hill Companies, Inc. (''McGraw-Hill") and its
licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as
permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work,
you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works
based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it
without McGraw-Hill's prior consent. You may use the work for your own noncommercial and
personal use; any other use of the work is strictly prohibited. Your right to use the work may be
terminated if you fail to comply with these terms.

THE WORK IS PROVIDED "AS IS". McGRAW-HILL AND ITS LICENSORS MAKE NO
GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR
COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK,
INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA
HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its
licensors do not warrant or guarantee that the functions contained in the work will meet your
requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its
licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of
cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for
the content of any information accessed through the work. Under no circumstances shall McGraw-
Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or
similar damages that result from the use of or inability to use the work, even if any of them has been
advised of the possibility of such damages. This limitation of liability shall apply to any claim or
cause whatsoever whether such claim or cause arises in contract, tort or otherwise.

DOI: 10.1036/0-07-213295-7
Page v

CONTENTS

Preface xxiii

Part I
Foundational C

1 3
An Overview of C

A Brief History of C 4

C Is a Middle-Level Language 5

C Is a Structured Language 6

C Is a Programmer's Language 8

Compilers Vs. Interpreters 9

The Form of a C Program 10

The Library and Linking 11

Separate Compilation 12

Compiling a C Program 13

C's Memory Map 13

C Vs. C++ 14

Review of Terms 15
Page vi

2 17
Expressions

The Basic Data Types 18

Modifying the Basic Types 18

Identifier Names 20

Variables 21

Where Variables Are Declared 21

Local Variables 22

Formal Parameters 25

Global Variables 26

The Four C Scopes 27

Type Qualifiers 28

const 28

volatile 30

Storage Class Specifiers 30

extern 31

static Variables 33

register Variables 35

Variable Initializations 36

Constants 37

Hexadecimal and Octal Constants 38

String Constants 38

Backslash Character Constants 39


Operators 40

The Assignment Operator 40

Arithmetic Operators 43

The Increment and Decrement Operators 44

Relational and Logical Operators 46

Bitwise Operators 48

The ? Operator 53

The & and * Pointer Operators 53

The Compile-Time Operator sizeof 55

The Comma Operator 56

The Dot (.) and Arrow (–>) Operators 56

The [ ] and ( ) Operators 57

Precedence Summary 58

Expressions 58

Order of Evaluation 58

Type Conversion in Expressions 59

Casts 60

Spacing and Parentheses 61

3 63
Statements

True and False in C 64


Page vii

Selection Statements 64

if 64

Nested ifs 66

The if-else-if Ladder 67

The ? Alternative 69

The Conditional Expression 72

switch 72

Nested switch Statements 75

Iteration Statements 76

The for Loop 76

for Loop Variations 77

The Infinite Loop 82

for Loops with No Bodies 82

Declaring Variables within a for Loop 83

The while Loop 83

The do-while Loop 86

Jump Statements 87

The return Statement 87

The goto Statement 88

The break Statement 89

The exit( ) Function 90

The continue Statement 91

Expression Statements 93
Block Statements 93

4 95
Arrays and Strings

Single-Dimension Arrays 96

Generating a Pointer to an Array 97

Passing Single-Dimension Arrays to Functions 98

Strings 99

Two-Dimensional Arrays 101

Arrays of Strings 106

Multidimensional Arrays 107

Indexing Pointers 108

Array Initialization 110

Unsized Array Initializations 112

Variable-Length Arrays 113

A Tic-Tac-Toe Example 114

5 119
Pointers

What Are Pointers? 120

Pointer Variables 121

The Pointer Operators 121


Page viii

Pointer Expressions 122

Pointer Assignments 122

Pointer Conversions 123

Pointer Arithmetic 124

Pointer Comparisons 126

Pointers and Arrays 128

Arrays of Pointers 129

Multiple Indirection 130

Initializing Pointers 131

Pointers to Functions 134


Y
FL
C's Dynamic Allocation Functions 138
AM

Dynamically Allocated Arrays 140

restrict-Qualified Pointers 142


TE

Problems with Pointers 143

6 147
Functions

The General Form of a Function 148

Understanding the Scope of a Function 148

Function Arguments 149

Call by Value, Call by Reference 149

Creating a Call by Reference 150

Calling Functions with Arrays 152

argc and argv— Arguments to main( ) 155

Team-Fly®
The return Statement 158

Returning from a Function 158

Returning Values 160

Returning Pointers 162

Functions of Type void 163

What Does main( ) Return? 164

Recursion 164

Function Prototypes 166

Old-Style Function Declarations 168

Standard Library Function Prototypes 169

Declaring Variable Length Parameter Lists 169

The ''Implicit int" Rule 170

Old-Style Vs. Modern Function Parameter Declarations 171

The inline Keyword 172

7 173
Structures, Unions, Enumerations, and typedef

Structures 174

Accessing Structure Members 176

Structure Assignments 177


Page ix

Arrays of Structures 178

A Mailing List Example 178

Passing Structures to Functions 186

Passing Structure Members to Functions 186

Passing Entire Structures to Functions 187

Structure Pointers 188

Declaring a Structure Pointer 189

Using Structure Pointers 189

Arrays and Structures within Structures 192

Unions 193

Bit-Fields 195

Enumerations 198

An Important Difference between C and C++ 200

Using sizeof to Ensure Portability 201

typedef 203

8 205
Console I/O

Reading and Writing Characters 206

A Problem with getchar( ) 207

Alternatives to getchar( ) 208

Reading and Writing Strings 209

Formatted Console I/O 212

printf( ) 212
Printing Characters 213

Printing Numbers 214

Displaying an Address 215

The %n Specifier 216

Format Modifiers 216

The Minimum Field Width Specifier 216

The Precision Specifier 218

Justifying Output 219

Handling Other Data Types 219

The * and # Modifiers 220

scanf( ) 221

Format Specifiers 221

Inputting Numbers 221

Inputting Unsigned Integers 223

Reading Individual Characters Using scanf 223


()

Reading Strings 223

Inputting an Address 224

The %n Specifier 224

Using a Scanset 224

Discarding Unwanted White Space 225


Page x

Non-White-Space Characters in the Control String 226

You Must Pass scanf( ) Addresses 226

Format Modifiers 226

Suppressing Input 227

9 229
File I/O

C vs. C++ File I/O 230

Standard C Vs. Unix File I/O 230

Streams and Files 230

Streams 231

Files 231

File System Basics 232

The File Pointer 232

Opening a File 232

Closing a File 235

Writing a Character 235

Reading a Character 236

Using fopen( ), getc( ), putc( ), and fclose( ) 236

Using feof( ) 238

Working with Strings: fputs( ) and fgets( ) 239

rewind( ) 240

ferror( ) 241

Erasing Files 243


Flushing a Stream 244

fread( ) and fwrite( ) 245

Using fread( ) and fwrite( ) 245

fseek( ) and Random-Access 253

fprintf( ) and fscanf( ) 254

The Standard Streams 256

The Console I/O Connection 257

Using freopen( ) to Redirect the Standard Streams 258

10 261
The Preprocessor and Comments

The Preprocessor 262

#define 262

Defining Function-like Macros 264

#error 265

#include 265

Conditional Compilation Directives 266

#if, #else, #elif, and #endif 266

#ifdef and #ifndef 269

#undef 270

Using defined 270

#line 271
Page xi

#pragma 272

The # and ## Preprocessor Operators 272

Predefined Macro Names 273

Comments 274

Single-Line Comments 275

Part II
The C99 Standard

11 279
C99

C89 Vs. C99: An Overview 280

Features Added 280

Features Removed 281

Features Changed 281

restrict-Qualified Pointers 282

inline 282

New Built-in Data Types 284

_Bool 284

_Complex and _Imaginary 284

The long long Integer Types 285

Array Enhancements 285

Variable-Length Arrays 285

Use of Type Qualifiers in an Array Declaration 286

Single-Line Comments 286

Interspersed Code and Declarations 286


Preprocessor Changes 287

Variable Argument Lists 287

The _Pragma Operator 288

Built-in Pragmas 288

Additional Built-in Macros 289

Declaring Variables within a for Loop 289

Compound Literals 290

Flexible Array Structure Members 291

Designated Initializers 291

Additions to the printf( ) and scanf( ) Family of Functions 292

New Libraries in C99 293

The _ _func_ _ Predefined Identifier 293

Increased Translation Limits 294

Implicit int No Longer Supported 294

Implicit Function Declarations Have Been Removed 296

Restrictions on return 296

Extended Integer Types 297

Changes to the Integer Promotion Rules 297


Page xii

Part III
The C Standard Library

12 301
Linking, Libraries, and Headers

The Linker 302

Separate Compilation 302

Relocatable Vs. Absolute Code 303

Linking with Overlays 303

Linking with DLLs 304

The C Standard Library 305

Library Files Vs. Object Files 305

Headers 305

Macros in Headers 307

Redefinition of Library Functions 308

13 309
I/O Functions

clearerr 310

fclose 312

feof 313

ferror 313

fflush 314

fgetc 315

fgetpos 316

fgets 317
fopen 318

fprintf 320

fputc 321

fputs 321

fread 322

freopen 323

fscanf 324

fseek 325

fsetpos 326

ftell 327

fwrite 328

getc 329

getchar 330

gets 331

perror 332

printf 332

Format Modifiers for printf( ) Added by C99 335

putc 336

putchar 337

puts 337
Page xiii

remove 338

rename 339

rewind 340

scanf 340

Format Modifiers for scanf( ) Added by C99 344

setbuf 345

setvbuf 345

snprintf 346

sprintf 347

sscanf 347
Y
FL
tmpfile 348
AM

tmpnam 349

ungetc 350
TE

vprintf, vfprintf, vsprintf, and vsnprintf 351

vscanf, vfscanf, and vsscanf 352

14 353
String and Character Functions

isalnum 354

isalpha 355

isblank 356

iscntrl 357

isdigit 358

isgraph 358

Team-Fly®
islower 359

isprint 360

ispunct 361

isspace 362

isupper 362

isxdigit 363

memchr 364

memcmp 365

memcpy 366

memmove 367

memset 368

strcat 368

strchr 369

strcmp 370

strcoll 371

strcpy 372

strcspn 372

strerror 373

strlen 373

strncat 374

strncmp 375

strncpy 376
Page xiv

strpbrk 377

strrchr 377

strspn 378

strstr 379

strtok 380

strxfrm 381

tolower 381

toupper 382

15 383
Mathematical Functions

acos 386

acosh 387

asin 387

asinh 388

atan 388

atanh 389

atan2 390

cbrt 391

ceil 391

copysign 392

cos 392

cosh 393

erf 394
erfc 394

exp 395

exp2 395

expm1 395

fabs 396

fdim 397

floor 397

fma 398

fmax 398

fmin 398

fmod 399

frexp 399

hypot 400

ilogb 400

ldexp 401

lgamma 402

llrint 402

llround 402

log 403

log1p 404
Page xv

log10 404

log2 405

logb 405

lrint 406

lround 406

modf 406

nan 407

nearbyint 407

nextafter 408

nexttoward 408

pow 409

remainder 409

remquo 410

rint 410

round 411

scalbln 411

scalbn 412

sin 412

sinh 413

sqrt 414

tan 414

tanh 415

tgamma 416
trunc 416

16 417
Time, Date, and Localization Functions

asctime 418

clock 419

ctime 420

difftime 421

gmtime 422

localeconv 423

localtime 425

mktime 426

setlocale 427

strftime 428

time 431

17 433
Dynamic Allocation Functions

calloc 434

free 435

malloc 436

realloc 437
Page xvi

18 439
Utility Functions

abort 440

abs 441

assert 441

atexit 442

atof 443

atoi 444

atol 445

atoll 446

bsearch 446

div 448

exit 449

_Exit 450

getenv 450

labs 451

llabs 451

ldiv 452

lldiv 453

longjmp 453

mblen 454

mbstowcs 455

mbtowc 456
qsort 456

raise 458

rand 459

setjmp 459

signal 460

srand 460

strtod 461

strtof 463

strtol 463

strtold 464

strtoll 465

strtoul 465

strtoull 466

system 467

va_arg, va_copy, va_start, and va_end 467

wcstombs 469

wctomb 470

19 471
Wide-Character Functions

Wide-Character Classification Functions 472

Wide-Character I/O Functions 474

Wide-Character String Functions 477


Page xvii

Wide-Character String Conversion Functions 478

Wide-Character Array Functions 479

Multibyte/Wide-Character Conversion Functions 480

20 483
Library Features Added by C99

The Complex Library 484

The Floating-Point Environment Library 488

The <stdint.h> Header 488

Integer Format Conversion Functions 490

Type-Generic Math Macros 490

The <stdbool.h> Header 493

Part IV
Algorithms and Applications

21 497
Sorting and Searching

Sorting 498

Classes of Sorting Algorithms 498

Judging Sorting Algorithms 499

The Bubble Sort 500

Sorting by Selection 504

Sorting by Insertion 505

Improved Sorts 506

The Shell Sort 506

The Quicksort 508


Choosing a Sort 511

Sorting Other Data Structures 511

Sorting Strings 512

Sorting Structures 513

Sorting Random-Access Disk Files 515

Searching 518

Searching Methods 519

The Sequential Search 519

The Binary Search 519

22 521
Queues, Stacks, Linked Lists, and Trees

Queues 522

The Circular Queue 528

Stacks 531

Linked Lists 536

Singly Linked Lists 536

Doubly Linked Lists 541

A Mailing List Example 546

Binary Trees 553


Page xviii

23 563
Sparse Arrays

Understanding the Need for Sparse Arrays 564

The Linked-List Sparse Array 565

Analysis of the Linked-List Approach 568

The Binary-Tree Approach to Sparse Arrays 569

Analysis of the Binary-Tree Approach 571

The Pointer-Array Approach to Sparse Arrays 572

Analysis of the Pointer-Array Approach 575

Hashing Y 575

Analysis of Hashing 579


FL

Choosing an Approach 580


AM

24 581
Expression Parsing and Evaluation
TE

Expressions 582

Dissecting an Expression 584

Expression Parsing 586

A Simple Expression Parser 588

Adding Variables to the Parser 595

Syntax Checking in a Recursive-Descent Parser 604

25 605
AI-Based Problem Solving

Representation and Terminology 606

Combinatorial Explosions 608

Team-Fly®
Search Techniques 610

Evaluating a Search 610

A Graphic Representation 611

The Depth-First Search 613

Analysis of the Depth-First Search 624

The Breadth-First Search 625

Analysis of the Breadth-First Search 626

Adding Heuristics 626

The Hill-Climbing Search 628

Analysis of Hill Climbing 635

The Least-Cost Search 635

Analysis of the Least-Cost Search 636

Choosing a Search Technique 636

Finding Multiple Solutions 637

Path Removal 638

Node Removal 639

Finding the ''Optimal" Solution 645

Back to the Lost Keys 652


Page xix

Part V
Software Development Using C

26 659
Building a Windows 2000 Skeleton

Windows 2000 Programming Perspective 660

The Desktop Model 661

The Mouse 661

Icons, Bitmaps, and Graphics 661

Menus, Controls, and Dialog Boxes 661

The Win32 Application Programming Interface 662

Components of a Window 662

How Windows and Your Program Interact 663

Some Windows 2000 Application Basics 664

WinMain( ) 664

The Window Procedure 664

Window Classes 665

The Message Loop 665

Windows Data Types 665

A Windows 2000 Skeleton 666

Defining the Window Class 669

Creating a Window 672

The Message Loop 674

The Window Function 675

Definition File No Longer Needed 676


Naming Conventions 676

27 679
Software Engineering Using C

Top-Down Design 680

Outlining Your Program 680

Choosing a Data Structure 682

Bulletproof Functions 682

Using MAKE 685

Using Macros in MAKE 689

Using an Integrated Development Environment 689

28 691
Efficiency, Porting, and Debugging

Efficiency 692

The Increment and Decrement Operators 692

Using Register Variables 693

Pointers Vs. Array Indexing 694

Use of Functions 694


Page xx

Porting Programs 698

Using #define 698

Operating-System Dependencies 699

Differences in Data Sizes 699

Debugging 700

Order-of-Evaluation Errors 700

Pointer Problems 701

Interpreting Syntax Errors 703

One-Off Errors 704

Boundary Errors 705

Function Prototype Omissions 706

Argument Errors 708

Stack Overruns 708

Using a Debugger 708

Debugging Theory in General 709

Part VI
A C Interpreter

29 713
A C Interpreter

The Practical Importance of Interpreters 714

The Little C Specifications 715

Some Little C Restrictions 716

Interpreting a Structured Language 718

An Informal Theory of C 719


C Expressions 720

Evaluating Expressions 721

The Expression Parser 722

Reducing the Source Code to Its Components 723

The Little C Recursive-Descent Parser 730

The Little C Interpreter 744

The Interpreter Prescan 745

The main( ) Function 748

The interp_block( ) Function 749

Handling Local Variables 766

Calling User-Defined Functions 767

Assigning Values to Variables 771

Executing an if Statement 772

Processing a while Loop 773

Processing a do-while Loop 774

The for Loop 775

The Little C Library Functions 776

Compiling and Linking the Little C Interpreter 780


Page xxi

Demonstrating Little C 780

Improving Little C 785

Expanding Little C 786

Adding New C Features 786

Adding Ancillary Features 787

Index 789

You might also like