Đây là bộ sách tiếng anh cho dân công nghệ thông tin chuyên về bảo mật,lập trình.Thích hợp cho những ai đam mê về công nghệ thông tin,tìm hiểu về bảo mật và lập trình.
C Primer Plus, Fifth Edition By Stephen Prata Publisher : Sams Pub Date : November 23, 2004 ISBN : 0-672-32696-5 Pages :792 A lean revision of a computer industry classic that has sold over 500,000 copies in previous editions. Fifth Edition contains over 20 new programming exercises and newly improved examples. C and C++ account for 30% of developers, about three million, and is a $22 million book market per year. Updates the third edition by integrating new ANSI/ISO standard, C99. Classic that teaches the fundamentals of programming. PREFACE C was a relatively little-known language when the first edition of C Primer Plus was written in 1984. Since then, the language has boomed, and many people have learned C with the help of this book. In fact, over 500,000 people have purchased C Primer Plus throughout its various editions. As the language has grown from the early informal K&R standard through the 1990 ISO/ANSI standard to the 1999 ISO/ANSI standard, so has this book matured through this, the fifth edition. As with all the editions, my aim has been to create an introduction to C that is instructive, clear, and helpful. Approach and Goals My goal is for this book to serve as a friendly, easy-to-use, self-study guide. To accomplish that objective, C Primer Plus employs the following strategies: Programming concepts are explained, along with details of the C language; the book does not assume that you are a professional programmer. Many short, easily typed examples illustrate just one or two concepts at a time, because learning by doing is one of the most effective ways to master new information. Figures and illustrations clarify concepts that are difficult to grasp in words alone. Highlight boxes summarize the main features of C for easy reference and review. Review questions and programming exercises at the end of each chapter allow you to test and improve your understanding of C. To gain the greatest benefit, you should take as active a role as possible in studying the topics in this book. Don't just read the examples, enter them into your system, and try them. C is a very portable language, but you may find differences between how a program works on your system and how it works on ours. Experiment—change part of a program to see what the effect is. Modify a program to do something slightly different. Ignore the occasional warnings and see what happens when you do the wrong thing. Try the questions and exercises. The more you do yourself, the more you will learn and remember. I hope that you'll find this newest edition an enjoyable and effective introduction to the C language. ABOUT THE AUTHOR Stephen Prata teaches astronomy, physics, and programming at the College of Marin in Kentfield, California. He received his B.S. from the California Institute of Technology and his Ph.D. from the University of California, Berkeley. His association with computers began with the computer modeling of star clusters. Stephen has authored or coauthored over a dozen books, including C++ Primer Plus and Unix Primer Plus. ACKNOWLEDGMENTS I wish to thank Loretta Yates of Sams Publishing for getting this project underway and Songlin Qiu of Sams Publishing for seeing it through. Also, thank you Ron Liechty of Metrowerks and Greg Comeau of Comeau Computing for your help with new C99 features and your noteworthy commitment to customer service. Table of content Chapter 1. Getting Ready 1 Whence C? 1 Why C? 1 Whither C? 3 What Computers Do 4 High-level Computer Languages and Compilers 5 Using C: Seven Steps 6 Programming Mechanics 9 Language Standards 15 How This Book Is Organized 16 Conventions Used in This Book 16 Summary 18 Review Questions 18 Programming Exercise 19 Chapter 2. Introducing C 19 A Simple Example of C 19 The Example Explained 20 The Structure of a Simple Program 30 Tips on Making Your Programs Readable 31 Taking Another Step in Using C 32 While You're at It—Multiple Functions 33 Introducing Debugging 35 Keywords and Reserved Identifiers 38 Key Concepts 38 Summary 39 Review Questions 39 Programming Exercises 40 Chapter 3. Data and C 41 A Sample Program 42 Data Variables and Constants 44 Data: Data-Type Keywords 44 Basic C Data Types 47 Using Data Types 70 Arguments and Pitfalls 70 One More Example: Escape Sequences 72 Key Concepts 74 Summary 74 Review Questions 75 Programming Exercises 77 Chapter 4. Character Strings and Formatted Input/Output 77 Introductory Program 78 Character Strings: An Introduction 79 Constants and the C Preprocessor 83 Exploring and Exploiting printf() and scanf() 88 Key Concepts 108 Summary 109 Review Questions 110 Programming Exercises 112 Chapter 5. Operators, Expressions, and Statements 113 Introducing Loops 113 Fundamental Operators 115 Some Additional Operators 124 Expressions and Statements 132 Type Conversions 138 Function with Arguments 141 A Sample Program 142 Key Concepts 144 Summary 144 Review Questions 145 Programming Exercises 147 Chapter 6. C Control Statements: Looping 149 Revisiting the while Loop 150 The while Statement 152 Which Is Bigger: Using Relational Operators and Expressions 156 Indefinite Loops and Counting Loops 164 The for Loop 165 More Assignment Operators: +=, -=, *=, /=, %= 171 The Comma Operator 171 An Exit-Condition Loop: do while 176 Which Loop? 179 Nested Loops 180 Introducing Arrays 181 A Loop Example Using a Function Return Value 184 Key Concepts 188 Summary 188 Review Questions 189 Programming Exercises 193 Chapter 7. C Control Statements: Branching and Jumps 195 The if Statement 196 Adding else to the if Statement 197 Let's Get Logical 211 A Word-Count Program 216 The Conditional Operator: ?: 219 Loop Aids: continue and break 221 Multiple Choice: switch and break 225 The goto Statement 232 Key Concepts 235 Summary 236 Review Questions 236 Programming Exercises 239 Chapter 8. Character Input/Output and Input Validation 241 Single-Character I/O: getchar() and putchar() 241 Buffers 242 Terminating Keyboard Input 243 Redirection and Files 247 Creating a Friendlier User Interface 251 Input Validation 256 Menu Browsing 261 Key Concepts 266 Summary 266 Review Questions 266 Programming Exercises 267 Chapter 9. Functions 269 Reviewing Functions 269 ANSI C Function Prototyping 281 Recursion 285 Compiling Programs with Two or More Source Code Files 291 Finding Addresses: The & Operator 295 Altering Variables in the Calling Function 296 Pointers: A First Look 298 Key Concepts 305 Summary 305 Review Questions 305 Programming Exercises 307 Chapter 10. Arrays and Pointers 307 Arrays 308 Multidimensional Arrays 316 Pointers and Arrays 320 Functions, Arrays, and Pointers 323 Pointer Operations 328 Protecting Array Contents 332 Pointers and Multidimensional Arrays 336 Variable-Length Arrays (VLAs) 344 Compound Literals 347 Key Concepts 349 Summary 350 Review Questions 351 Programming Exercises 353 Chapter 11. Character Strings and String Functions 355 Representing Strings and String I/O 355 String Input 364 String Output 369 The Do-It-Yourself Option 372 String Functions 374 A String Example: Sorting Strings 389 The ctype.h Character Functions and Strings 392 Command-Line Arguments 393 String-to-Number Conversions 396 Key Concepts 399 Summary 399 Review Questions 399 Programming Exercises 402 Chapter 12. Storage Classes, Linkage, and Memory Management 404 Storage Classes 404 Storage-Class Specifiers 418 Storage Classes and Functions 420 A Random-Number Function and a Static Variable 421 Roll 'Em 424 Allocated Memory: malloc() and free() 428 ANSI C Type Qualifiers 433 Key Concepts 439 Summary 439 Review Questions 440 Programming Exercises 442 Chapter 13. File Input/Output 444 Communicating with Files 444 Standard I/O 446 A Simple-Minded File-Condensing Program 451 File I/O: fprintf(), fscanf(), fgets(), and fputs() 453 Adventures in Random Access: fseek() and ftell() 456 Behind the Scenes with Standard I/O 460 Other Standard I/O Functions 461 Key Concepts 468 Summary 469 Review Questions 470 Programming Exercises 471 Chapter 14. Structures and Other Data Forms 473 Sample Problem: Creating an Inventory of Books 474 Setting Up the Structure Declaration 475 Defining a Structure Variable 476 Arrays of Structures 479 Nested Structures 483 Pointers to Structures 485 Telling Functions About Structures 487 Saving the Structure Contents in a File 502 Structures: What Next? 506 Unions: A Quick Look 506 Enumerated Types 509 typedef: A Quick Look 513 Fancy Declarations 515 Functions and Pointers 517 Key Concepts 523 Summary 523 Review Questions 524 Programming Exercises 526 Chapter 15. Bit Fiddling 529 Binary Numbers, Bits, and Bytes 530 Other Number Bases 532 C's Bitwise Operators 534 Bit Fields 543 Key Concepts 554 Summary 554 Review Questions 554 Programming Exercises 556 Chapter 16. The C Preprocessor and the C Library 558 First Steps in Translating a Program 558 Manifest Constants: #define 559 Using Arguments with #define 564 Macro or Function? 570 File Inclusion: #include 571 Other Directives 575 Inline Functions 583 The C Library 585 The Math Library 587 The General Utilities Library 589 The Assert Library 596 memcpy() and memmove() from the string.h Library 597 Variable Arguments: stdarg.h 599 Key Concepts 601 Summary 601 Review Questions 601 Programming Exercises 603 Chapter 17. Advanced Data Representation 605 Exploring Data Representation 606 Beyond the Array to the Linked List 608 Abstract Data Types (ADTs) 615 Getting Queued with an ADT 629 Simulating with a Queue 641 The Linked List Versus the Array 645 Binary Search Trees 649 Other Directions 671 Key Concepts 671 Summary 672 Review Questions 672 Programming Exercises 673 Appendix A. Answers to the Review Quesions 674 Answers to Review Questions for Chapter 1 674 Answers to Review Questions for Chapter 2 675 Answers to Review Questions for Chapter 3 677 Answers to Review Questions for Chapter 4 679 Answers to Review Questions for Chapter 5 682 Answers to Review Questions for Chapter 6 685 Answers to Review Questions for Chapter 7 688 Answers to Review Questions for Chapter 8 692 Answers to Review Questions for Chapter 9 693 Answers to Review Questions for Chapter 10 695 Answers to Review Questions for Chapter 11 698 Answers to Review Questions for Chapter 12 701 Answers to Review Questions for Chapter 13 702 Answers to Review Questions for Chapter 14 705 Answers to Review Questions for Chapter 15 708 Answers to Review Questions for Chapter 16 709 Answers to Review Questions for Chapter 17 711 Appendix B. Reference Section 715 Section I: Additional Reading 715 Section II: C Operators 718 Section III: Basic Types and Storage Classes 724 Section IV: Expressions, Statements, and Program Flow 729 Section V: The Standard ANSI C Library with C99 Additions 735 Section VI: Extended Integer Types 776 Section VII: Expanded Character Support 779 Section VIII: C99 Numeric Computational Enhancements 784 Section IX: Differences Between C and C++ 787 C Primer Plus 5th Edition 1 1 Chapter 1. Getting Ready You will learn about the following in this chapter: C's history and features The steps needed to write programs A bit about compilers and linkers C standards Welcome to the world of C—a vigorous, professional programming language popular with amateur and commercial programmers alike. This chapter prepares you for learning and using this powerful and popular language, and it introduces you to the kinds of environments in which you will most likely develop your C-legs. First, we look at C's origin and examine some of its features, both strengths and drawbacks. Then we look at the origins of programming and examine some general principles for programming. Finally, we discuss how to run C programs on some common systems. Whence C? Dennis Ritchie of Bell Labs created C in 1972 as he and Ken Thompson worked on designing the Unix operating system. C didn't spring full-grown from Ritchie's head, however. It came from Thompson's B language, which came from… but that's another story. The important point is that C was created as a tool for working programmers, so its chief goal is to be a useful language. Most languages aim to be useful, but they often have other concerns. The main goal for Pascal, for instance, was to provide a sound basis for teaching good programming principles. BASIC, on the other hand, was developed to resemble English so that it could be learned easily by students unfamiliar with computers. These are important goals, but they are not always compatible with pragmatic, workaday usefulness. C's development as a language designed for programmers, however, has made it one of the modern-day languages of choice. Why C? During the past three decades, C has become one of the most important and popular programming languages. It has grown because people try it and like it. In the past decade, many have moved from C to the more ambitious C++ language, but C is still an important language in its own right, as well a migration path to C++. As you learn C, you will recognize its many virtues (see Figure 1.1). Let's preview a few of them now. Figure 1.1. The virtues of C. 2 C Primer Plus 5th Edition 2 Design Features C is a modern language incorporating the control features found desirable by the theory and practice of computer science. Its design makes it natural for top-down planning, structured programming, and modular design. The result is a more reliable, understandable program. Efficiency C is an efficient language. Its design takes advantage of the capabilities of current computers. C programs tend to be compact and to run quickly. In fact, C exhibits some of the fine control usually associated with an assembly language. (An assembly language is a mnemonic representation of the set of internal instructions used by a particular central processing unit design; different CPU families have different assembly languages.) If you choose, you can fine- tune your programs for maximum speed or most efficient use of memory. Portability C is a portable language, which means that C programs written on one system can be run on other systems with little or no modification. If modifications are necessary, they can often be made by simply changing a few entries in a header file accompanying the main program. Most languages are meant to be portable, but anyone who has converted an IBM PC BASIC program to Apple BASIC (and they are close cousins) or has tried to run an IBM mainframe FORTRAN program on a Unix system knows that porting is troublesome at best. C is a leader in portability. C compilers (programs that convert your C code into the instructions a computer uses internally) are available for about 40 systems, running from 8-bit microprocessors to Cray supercomputers. Note, however, that the portions of a program written specifically to access particular hardware devices, such as a display monitor, or special features of an operating system, such as Windows [...]... which appends the EXE extension to the original source code basename For example, compiling and linking a source code file called concrete .c produces a file called concrete.exe Some compilers provide an option to create an executable named concrete.com instead In either case, you can run the program by typing the basename at the command line: C> concrete C on the Macintosh The best known Macintosh C/ C++... numbers Characters, such as the alphabetical characters you use in a text document, are stored as numbers; each character has a numeric code The instructions that a computer loads into its registers are stored as numbers; each instruction in the instruction set has a numeric code Second, computer programs ultimately have to be expressed in this numeric instruction code, or what is called machine language... Typically, the installation makes cc an alias for gcc, so you can use cc in the command line instead of gcc if you like You can obtain further information about gcc, including information about new releases, at http://www.gnu.org/software/gcc/gcc.html Integrated Development Environments (Windows) C compilers are not part of the standard Windows package, so you may need to obtain and install a C compiler... platforms, including IBM compatibles and Macintoshes Preparing C programs on Linux is much the same as for Unix systems, except that you would use the public domain C compiler, called gcc, that's provided by GNU The compile command would look like this: C Primer Plus 5th Edition 13 gcc inform .c Note that installing gcc may be optional when installing Linux, so you (or someone) might have to install gcc if... to 63 characters, except for external identifiers (see Chapter 12, "Storage Classes, Linkage, and Memory Management"), for which only 31 characters need to be recognized This is a substantial increase from the C9 0 requirement of 31 characters and six characters, respectively, and older C compilers often stopped at eight characters max Actually, you can use more than the maximum number of characters,... including the extension; other Unix systems allow longer names, up to 255 characters Linux, Windows, and the Macintosh OS also allow long names So that we'll have something concrete to refer to, let's assume we have a source file called concrete .c containing the C source code in Listing 1.2 Listing 1.2 The concrete .c Program #include int main(void) { printf("Concrete contains gravel and cement.\n");... the source file to the project Because the Windows IDEs typically handle both C and C+ +, you need to indicate that you want a C program With some products, such as Metrowerks CodeWarrior, you use the project type to indicate that you want to use C With other products, such as Microsoft Visual C+ +, you use the c file extension to indicate that you want to use C rather than C+ + However, most C programs... source code file shown in Listing 1.2; you'll learn about them in Chapter 2 Object Code Files, Executable Files, and Libraries The basic strategy in C programming is to use programs that convert your source code file to an 9 10 C Primer Plus 5th Edition executable file, which is a file containing ready-to-run machine language code C implementations do this in two steps: compiling and linking The compiler.. .C Primer Plus 5th Edition 3 XP or OS X, typically are not portable Because of C' s close ties with Unix, Unix systems typically come with a C compiler as part of the packages Linux installations also usually include a C compiler Several C compilers are available for personal computers, including PCs running various versions of Windows, and Macintoshes So whether you are using a home computer,... Win32 C Stationery and then select C Console App or the WinSIOUX C App (the latter has a nicer user interface) For other systems, look for an option using terms such as DOS EXE, Console, or Character Mode executable These modes will run your executable program in a console-like window After you have the correct project type, use the IDE menu to open a new source code file For most products, you can do . 776 Section VII: Expanded Character Support 779 Section VIII: C9 9 Numeric Computational Enhancements 784 Section IX: Differences Between C and C+ + 787. the capabilities of current computers. C programs tend to be compact and to run quickly. In fact, C exhibits some of the fine control usually associated