Table of Contents
| Ch. 1 | Getting ready | 1 |
| Ch. 2 | Introducing C | 23 |
| Ch. 3 | Data and C | 49 |
| Ch. 4 | Character strings and formatted input/output | 89 |
| Ch. 5 | Operators, expressions, and statements | 129 |
| Ch. 6 | C control statements : looping | 169 |
| Ch. 7 | C control statements : branching and jumps | 221 |
| Ch. 8 | Character input/output and input validation | 271 |
| Ch. 9 | Functions | 303 |
| Ch. 10 | Arrays and pointers | 347 |
| Ch. 11 | Character strings and string functions | 399 |
| Ch. 12 | Storage classes, linkage, and memory management | 453 |
| Ch. 13 | File input/output | 499 |
| Ch. 14 | Structures and other data forms | 535 |
| Ch. 15 | Bit fiddling | 597 |
| Ch. 16 | The C preprocessor and the C library | 629 |
| Ch. 17 | Advanced data representation | 681 |
Forewords & Introductions
C Primer PlusC Primer PlusPreface
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 veryportable language, but you may find differences between how a program works on your system and how it works on ours. Experimentchange 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.
© Copyright Pearson Education. All rights reserved.
Read an Excerpt
Chapter 1: Getting Ready
In this chapter, you learn about C's history and features, examine the steps needed to write programs, learn a bit about compilers and linkers, and look at 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 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 the modern-day language 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. l). Let's preview a few of them now.
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 users to use 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 abilities 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 assembly language. If you choose, you can fine-tune your programs for maximum speed or most efficient use of memory.
Portability
C is a portable language. That means C programs written on one system can be run with little or no modification on other systems. If modifications are necessary, they can often be made by simply changing a few entries in a header file accompanying the main program. Of course, 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 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 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 VGA monitor-or special features of an operating system, such as Windows or System 7.1, typically are not portable.
You Can Take C Home
Because C is portable, you can take your UNIX C programs home to use on a personal computer. Several C compilers are available now to enable you to do that, or you can take your home-grown programs to a UNIX system.
Power and Flexibility
C is powerful and flexible (two favorite words in computer literature). For example, most of the powerful, flexible UNIX operating system is written in C. Many compilers and interpreters for other languages-such as FORTRAN, APL, Pascal, LISP, Logo, and BASIC-have been written in C. Therefore, when you use FORTRAN on a UNIX machine, ultimately a C program has done the work of producing the final executable program. C programs have been used for solving physics and engineering problems and even for animating special effects for movies such as Return of the jedi.
Programmer Oriented
C is oriented toward the needs of programmers. It gives you access to hardware, and it enables you to manipulate individual bits in memory It has a rich selection of operators that allow you to express yourself succinctly. C is less strict than, say, Pascal, in limiting what you can do. This flexibility is both an advantage and a danger. The advantage is that many tasks, such as converting forms of data, are much simpler in C. The danger is that with C, you can make mistakes that are impossible in some languages. C gives you more freedom, but it also puts more responsibility on you.
Also, most C implementations have a large library of useful C functions. These functions deal with many needs commonly facing the programmer...