This book is a complete reference to the C programming language and the C runtime library. As a Nutshell book, its purpose is to serve as a convenient, reliable companion for C programmers in their day-to-day work. It describes all the elements of the language and illustrates their use with numerous examples.
The present description of the C language is based on the 1999 international C standard, ISO/IEC 9899:1999, including the Technical Corrigenda, TC1 of 2001 and TC2 of 2004. This standard, widely known as C99, is an extension of the ISO/IEC 9899:1990 standard and the 1995 Normative Addendum 1 (ISO/IEC 9899/AMD1:1995). The 1990 ISO/IEC standard corresponds to the ANSI standard X3.159, which was ratified in late 1989 and is commonly called ANSI C or C89.
The new features of the 1999 C standard are not yet fully supported by all compilers and standard library implementations. In this book we have therefore labeled 1999 extensions, such as new standard library functions that were not mentioned in earlier standards, with the abbreviation C99.
This book is not an introduction to programming in C. Although it covers the fundamentals of the language, it is not organized or written as a tutorial. If you are new to C, we assume that you have read at least one of the many introductory books, or that you are familiar with a related language, such as Java or C++.
This book is divided into three parts. The first part describes the C language in the strict sense of the term; the second part describes the standard library; and the third part describes the process of compiling and testing programs with the popular tools in the GNU software collection.
Part I, which deals with the C language, includes Chapters 1 through 14. After Chapter 1, which describes the general concepts and elements of the language, each chapter is devoted to a specific topic, such as types, statements, or pointers. Although the topics are ordered so that the fundamental concepts for each new topic have been presented in an earlier chapter—types, for example, are described before expressions and operators, which come before statements, and so on—you may sometimes need to follow references to later chapters to fill in related details. For example, some discussion of pointers and arrays is necessary in Chapter 5 (which covers expressions and operators), even though pointers and arrays are not described in full detail until Chapters 8 and 9.
- Chapter 1, Language Basics
Describes the characteristics of the language and how C programs are structured and compiled. This chapter introduces basic concepts such as the translation unit, character sets, and identifiers.
- Chapter 2, Types
Provides an overview of types in C and describes the basic types, the type
void, and enumerated types.
- Chapter 3, Literals
Describes numeric constants, character constants, and string literals, including escape sequences.
- Chapter 4, Type Conversions
Describes implicit and explicit type conversions, including integer promotion and the usual arithmetic conversions.
- Chapter 5, Expressions and Operators
Describes the evaluation of expressions, all the operators, and their compatible operands.
- Chapter 6, Statements
Describes C statements such as blocks, loops, and jumps.
- Chapter 7, Functions
Describes function definitions and function calls, including recursive and inline functions.
- Chapter 8, Arrays
Describes fixed-length and variable-length arrays, including strings, array initialization, and multidimensional arrays.
- Chapter 9, Pointers
Describes the definition and use of pointers to objects and functions.
- Chapter 10, Structures, Unions, and Bit-Fields
Describes the organization of data in these user-defined derived types.
- Chapter 11, Declarations
Describes the general syntax of a declaration, identifier linkage, and the storage duration of objects.
- Chapter 12, Dynamic Memory Management
Describes the standard library’s dynamic memory management functions, illustrating their use in a sample implementation of a generalized binary tree.
- Chapter 13, Input and Output
Describes the C concept of input and output, with an overview of the use of the standard I/O library.
- Chapter 14, Preprocessing Directives
Describes the definition and use of macros, conditional compiling, and all the other preprocessor directives and operators.
- Chapter 15, The Standard Headers
Describes contents of the headers and their use. The headers contain all of the standard library’s macros and type definitions.
- Chapter 16, Functions at a Glance
Provides an overview of the standard library functions, organized by areas of application, such as “Mathematical Functions,” “Time and Date Functions,” and so on.
- Chapter 17, Standard Library Functions
Describes each standard library function in detail, in alphabetical order, and contains examples to illustrate the use of each function.
The third part of this book provides the necessary knowledge of the C programmer’s basic tools: the compiler, the make utility, and the debugger. The tools described here are those in the GNU software collection.
- Chapter 18, Compiling with GCC
Describes the principal capabilities that the widely used compiler offers for C programmers.
- Chapter 19, Using make to Build C Programs
Describes how to use the make program to automate the compiling process for large programs.
- Chapter 20, Debugging C Programs with GDB
Describes how to run a program under the control of the GNU debugger and how to analyze programs’ runtime behavior to find logical errors.
In addition to works mentioned at appropriate points in the text, there are a number of resources for readers who want more technical detail than even this book can provide. The international working group on C standardization has an official home page at http://www.open-std.org/jtc1/sc22/wg14, with links to the latest version of the C99 standard and current projects of the working group.
For readers who are interested in not only the what and how of C, but also the why, the WG14 site also has a link to the “C99 Rationale”: this is a nonnormative but current document that describes some of the motivations and constraints involved in the standardization process. The C89 Rationale is online at http://www.lysator.liu.se/c/rat/title.html. Furthermore, for those who may wonder how C “got to be that way” in the first place, the originator of C, Dennis Ritchie, has an article titled “The Development of the C Language” as well as other historical documents on his Bell Labs web site, http://cm.bell-labs.com/cm/cs/who/dmr.
Readers who want details on floating-point math beyond the scope of C may wish to start with David Goldberg’s thorough introduction, “What Every Computer Scientist Should Know About Floating-Point Arithmetic,” currently available online at http://docs.sun.com/source/806-3568/ncg_goldberg.html.
The following typographical conventions are used in this book:
Highlights new terms; indicates filenames, file extensions, URLs, directories, and Unix utilities.
Indicates all elements of C source code: keywords, operators, variables, functions, macros, types, parameters, and literals. Also used for console commands and options, and the output from such commands.
Constant width bold
Highlights the function or statement under discussion in code examples. In compiler, make, and debugger sessions, this font indicates command input to be typed literally by the user.
Constant width italic
Indicates parameters in function prototypes, or placeholders to be replaced with your own values.
- Plain text
Indicates keys such as Return, Tab, and Ctrl.
This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "C in a Nutshell by Peter Prinz and Tony Crawford. Copyright 2006 O’Reilly Media, Inc., 0-596-00697-7.”
If you feel that your use of code examples falls outside fair use or the permission given here, feel free to contact us at email@example.com.
When you see a Safari® Enabled icon on the cover of your favorite technology book, that means the book is available online through the O’Reilly Network Safari Bookshelf.
Safari offers a solution that’s better than e-books. It’s a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you need the most accurate, current information. Try it for free at http://safari.oreilly.com.
Please address comments and questions concerning this book to the publisher:
|O’Reilly Media, Inc.|
|1005 Gravenstein Highway North|
|Sebastopol, CA 95472|
|(800) 998-9938 (in the United States or Canada)|
|(707) 829-0515 (international or local)|
|(707) 829-0104 (fax)|
We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:
To comment or ask technical questions about this book, send email to:
For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our web site at:
Both of us want to thank Jonathan Gennick, our editor, for originally bringing us together and starting us off on this book, and for all his guidance along the way. We also thank our technical reviewers, Matt Crawford, David Kitabjian, and Chris LaPre, for their valuable criticism of our manuscript, and we’re grateful to our production editor, Abby Fox, for all her attention to making our book look good.
I would like to thank Tony first of all for the excellent collaboration. My heartfelt thanks also go to all my friends for the understanding they showed again and again when I had so little time for them. Last but not least, I dedicate this book to my daughters, Vivian and Jeanette—both of them now students of computer science—who strengthened my ambition to carry out this book project.