Table Of ContentC
IN A NUTSHELL
Other resources from O’Reilly
Related titles C Pocket Reference Programming with GNU
Practical C Programming Software
Secure Programming Objective-C Pocket
Cookbook Reference
for C and C++ Prefactoring
Programming Embedded Practical Development
Systems with C Environments
and C++
oreilly.com oreilly.com is more than a complete catalog of O’Reilly
books.You’llalsofindlinkstonews,events,articles,web-
logs, sample chapters, and code examples.
oreillynet.com is the essential portal for developers inter-
ested in open and emerging technologies, including new
platforms, programming languages, and operating systems.
Conferences O’Reillybringsdiverseinnovatorstogethertonurturethe
ideasthatsparkrevolutionaryindustries.Wespecializein
documentingthelatesttoolsandsystems,translatingthe
innovator’s knowledge into useful skills for those in the
trenches. Visit conferences.oreilly.com for our upcoming
events.
Safari Bookshelf (safari.oreilly.com) is the premier online
reference library for programmers and IT professionals.
Conduct searches across more than 1,000 books. Sub-
scriberscanzeroinonanswerstotime-criticalquestions
in a matter of seconds. Read the books on your Book-
shelf from cover to cover or simply flip to the page you
need. Try it today for free.
C
IN A NUTSHELL
Peter Prinz and Tony Crawford
Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo
C in a Nutshell
by Peter Prinz and Tony Crawford
Copyright © 2006 O’Reilly Media, Inc. All rights reserved.
Printed in the United States of America.
PublishedbyO’ReillyMedia,Inc.,1005GravensteinHighwayNorth,Sebastopol,CA95472.
O’Reillybooksmaybepurchasedforeducational,business,orsalespromotionaluse.Online
editions are also available for most titles (safari.oreilly.com). For more information, contact
our corporate/institutional sales department: (800) 998-9938 orcorporate@oreilly.com.
Editor: Jonathan Gennick
Production Editor: A. J. Fox
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Printing History:
December 2005: First Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered
trademarksofO’ReillyMedia,Inc.TheInaNutshellseriesdesignations,CinaNutshell,the
image of a cow, and related trade dress are trademarks of O’Reilly Media, Inc.
Manyofthedesignationsusedbymanufacturersandsellerstodistinguishtheirproductsare
claimed as trademarks. Where those designations appear in this book, and O’Reilly Media,
Inc. was aware of a trademark claim, the designations have been printed in caps or initial
caps.
While every precaution has been taken in the preparation of this book, the publisher and
authors assume no responsibility for errors or omissions, or for damages resulting from the
use of the information contained herein.
ISBN: 978-0-596-00697-6
[LSI] [2012-05-11]
Chapter1
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xi
PartI. Language
1. Language Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Characteristics of C 3
The Structure of C Programs 4
Source Files 6
Comments 7
Character Sets 8
Identifiers 13
How the C Compiler Works 16
2. Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Typology 20
Integer Types 21
Floating-Point Types 26
Complex Floating-Point Types (C99) 28
Enumerated Types 29
The Type void 30
3. Literals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Integer Constants 32
Floating-Point Constants 33
v
This is the Title of the Book, eMatter Edition
Copyright © 2012 O’Reilly & Associates, Inc. All rights reserved.
Character Constants 34
String Literals 37
4. Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Conversion of Arithmetic Types 41
Conversion of Nonarithmetic Types 48
5. Expressions and Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
How Expressions Are Evaluated 56
Operators in Detail 59
Constant Expressions 81
6. Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Expression Statements 83
Block Statements 84
Loops 85
Selection Statements 89
Unconditional Jumps 92
7. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Function Definitions 96
Function Declarations 103
How Functions Are Executed 104
Pointers as Arguments and Return Values 104
Inline Functions 106
Recursive Functions 107
Variable Numbers of Arguments 108
8. Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Defining Arrays 111
Accessing Array Elements 113
Initializing Arrays 114
Strings 116
Multidimensional Arrays 117
Arrays as Arguments of Functions 120
9. Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Declaring Pointers 122
Operations with Pointers 125
Pointers and Type Qualifiers 129
Pointers to Arrays and Arrays of Pointers 132
Pointers to Functions 136
vi | Table of Contents
This is the Title of the Book, eMatter Edition
Copyright © 2012 O’Reilly & Associates, Inc. All rights reserved.
10. Structures, Unions, and Bit-Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Structures 139
Unions 149
Bit-Fields 151
11. Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
General Syntax 153
Type Names 160
typedef Declarations 161
Linkage of Identifiers 163
Storage Duration of Objects 164
Initialization 165
12. Dynamic Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Allocating Memory Dynamically 168
Characteristics of Allocated Memory 169
Resizing and Releasing Memory 170
An All-Purpose Binary Tree 171
Characteristics 172
Implementation 172
13. Input and Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Streams 182
Files 183
Opening and Closing Files 186
Reading and Writing 188
Random File Access 205
14. Preprocessing Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Inserting the Contents of Header Files 210
Defining and Using Macros 211
Conditional Compiling 218
Defining Line Numbers 220
Generating Error Messages 221
The #pragma Directive 221
The _Pragma Operator 222
Predefined Macros 223
Table of Contents | vii
This is the Title of the Book, eMatter Edition
Copyright © 2012 O’Reilly & Associates, Inc. All rights reserved.
PartII. Standard Library
15. The Standard Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Using the Standard Headers 227
Contents of the Standard Headers 230
16. Functions at a Glance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Input and Output 252
Mathematical Functions 253
Character Classification and Conversion 260
String Processing 262
Multibyte Characters 263
Converting Between Numbers and Strings 264
Searching and Sorting 264
Memory Block Handling 265
Dynamic Memory Management 265
Date and Time 266
Process Control 267
Internationalization 268
Nonlocal Jumps 269
Debugging 269
Error Messages 270
17. Standard Library Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
PartIII. Basic Tools
18. Compiling with GCC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
The GNU Compiler Collection 491
Obtaining and Installing GCC 492
Compiling C Programs with GCC 493
C Dialects 501
Compiler Warnings 502
Optimization 503
Debugging 507
Profiling 507
Option and Environment Variable Summary 508
viii | Table of Contents
This is the Title of the Book, eMatter Edition
Copyright © 2012 O’Reilly & Associates, Inc. All rights reserved.