This book is a complete reference to the C programming language and the C runtime library. As an “In 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 2011 international C standard, ISO/IEC 9899:2011, widely known as C11. This standard supersedes the C99 standard, ISO/IEC 9899:1999, and its Technical Corrigenda, TC1 of 2001, TC2 of 2004, and TC3 of 2007. The first international C standard, ISO/IEC 9899:1990, was published in 1990 and supplemented in 1995 by 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 2011 C standard are not yet fully supported by all compilers and standard library implementations. In this book, we have therefore labeled 2011 features—such as multithreading, type-generic macros, and new standard library functions—with the abbreviation C11. Extensions that were introduced by the C99 standard are labeled 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++.
How This Book Is Organized
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 15. 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, “Multithreading”
Describes the use of the C11 multithreading features, including atomic operations, communication between threads, and thread-specific storage.
- Chapter 15, “Preprocessing Directives”
Describes the definition and use of macros, conditional compiling, and all the other preprocessor directives and operators.
- Chapter 16, “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 17, “Functions at a Glance”
Provides an overview of the standard library functions, organized by areas of application (e.g., mathematical functions, date and time functions, etc.).
- Chapter 18, “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, which includes Chapters 19 through 20, 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. Finally, the use of these tools in an integrated development environment (IDE) for C is described using the Eclipse IDE as an example.
- Chapter 19, “Compiling with GCC”
Describes the principal capabilities that the widely used compiler offers for C programmers.
- Chapter 20, “Using make to Build C Programs”
Describes how to use the make program to automate the compiling process for large programs.
- Chapter 21, “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.
- Chapter 22, “Using an IDE with C”
Describes the use of an integrated development environment (IDE) for unified, convienient access to all the tools for developing C programs.
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 C 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 offers links to some of its drafts and rationales. These documents describe some of the motivations and constraints involved in the standardization process. Furthermore, for those who may wonder how C “got to be that way” in the first place, the originator of C, the late Dennis Ritchie, wrote an article titled “The Development of the C Language”. This and other historical documents are still available on his Bell Labs website, https://www.bell-labs.com/usr/dmr/www/index.html.
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.
Conventions 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 element signifies a tip or suggestion.
This element signifies a general note.
This element indicates a warning or caution.
Using Code Examples
Supplemental material (code examples, exercises, etc.) is available for download at https://github.com/oreillymedia/c-in-a-nutshell-2E.
This book is here to help you get your job done. In general, if example code is offered with this book, you may use it 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, 2nd Edition by Peter Prinz and Tony Crawford (O’Reilly). Copyright 2016 Peter Prinz, Tony Crawford, 978-1-491-90475-6.”
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at firstname.lastname@example.org.
Safari® Books Online
Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.
Members have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online.
How to Contact Us
- 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 http://bit.ly/C_Nutshell_2e.
To comment or ask technical questions about this book, send email to email@example.com.
For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
Both of us want to thank everyone at O’Reilly for their fantastic work on our book, and especially our editors, Rachel Roumeliotis and Katie Schooling, for all their guidance along the way. We also thank our technical reviewers, Matt Crawford, David Kitabjian, Chris LaPre, John C. Craig, and Loïc Pefferkorn, for their valuable criticism of our manuscript, and we’re grateful to our production editor, Kristen Brown, and our copyeditor, Gillian McGarvey, for all their attention to making our book look good and bringing our style up to date. Finally, thanks to Jonathan Gennick for setting the whole project in motion all those years ago.
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 PhDs in computer science—who strengthened my ambition to carry out this book project.
I thank Peter for letting me take all the space I could fill in this project.