ebook img

C in a Nutshell PDF

961 Pages·2015·7.227 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview C in a Nutshell

SECOND EDITION C in a Nutshell Peter Prinz and Tony Crawford C in a Nutshell, Second Edition by Peter Prinz and Tony Crawford Copyright © 2010 Peter Prinz, Tony Crawford. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribookson line.com). For more information, contact our corporate/institutional sales depart‐ ment: 800-998-9938 or <[email protected]>. Editors: Andy Oram and Rachel Roume‐ Copyeditor: FIX ME! liotis Proofreader: FIX ME! Production Editor: FIX ME! Indexer: FIX ME! Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Rebecca Demarest January 2015: Second Edition Revision History for the Second Edition: 2015-02-13 First Early release 2015-04-14 Second early release revision 2015-06-16 Third early release revision 2015-08-03 Fourth early release revision See http://oreilly.com/catalog/errata.csp?isbn=0636920033844 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are regis‐ tered trademarks of O’Reilly Media, Inc. !!FILL THIS IN!! and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are 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 publish‐ er and authors assume no responsibility for errors or omissions, or for damages re‐ sulting from the use of the information contained herein. ISBN: 063-6-920-03384-4 [?] Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii • Part I. Language 1. Language Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Characteristics of C 3 The Structure of C Programs 4 Source Files 6 Comments 8 Character Sets 9 Wide Characters and Multibyte Characters 10 Universal Character Names 13 Digraphs and Trigraphs 14 Identifiers 15 Identifier Name Spaces 17 Identifier Scope 18 How the C Compiler Works 20 The C Compiler’s Translation Phases 21 Tokens 23 2. Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Typology 25 Integer Types 26 Integer Types Defined in Standard Headers 31 Floating-Point Types 33 Complex Floating-Point Types (C99) 35 Enumerated Types 36 The Type void 37 void in Function Declarations 38 Expressions of Type void 38 Pointers to void 38 iii The Alignment of Objects in Memory 39 3. Literals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Integer Constants 41 Floating-Point Constants 42 Decimal Floating-Point Constants 42 Hexadecimal Floating-Point Constants 43 Character Constants 44 Types and Values of Character Constants 44 Escape Sequences 46 String Literals 48 4. Type Conversions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Conversion of Arithmetic Types 54 Hierarchy of Types 54 Integer Promotion 55 Usual Arithmetic Conversions 56 Other Implicit Type Conversions 58 The Results of Arithmetic Type Conversions 59 Conversion of Nonarithmetic Types 63 Array and Function Designators 63 Explicit Pointer Conversions 65 Implicit Pointer Conversions 67 Conversions Between Pointer and Integer Types 70 5. Expressions and Operators. . . . . . . . . . . . . . . . . . . . . 73 How Expressions Are Evaluated 74 Generic Selections (C11) 74 Lvalues 75 Side Effects and Sequence Points 77 Operator Precedence and Associativity 78 Operators in Detail 79 Arithmetic Operators 80 Assignment Operators 83 Increment and Decrement Operators 86 Comparative Operators 88 Logical Operators 90 Bitwise Operators 91 Memory Addressing Operators 95 Other Operators 99 Constant Expressions 106 Integer Constant Expressions 106 Other Constant Expressions 107 6. Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 iv Table of Contents Expression Statements 109 Block Statements 110 Loops 111 while Statements 111 for Statements 112 do…while Statements 114 Nested Loops 115 Selection Statements 116 if Statements 116 switch Statements 117 Unconditional Jumps 119 The break Statement 119 The continue Statement 120 The goto Statement 121 The return Statement 123 7. Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Function Definitions 125 Functions and Storage Class Specifiers 126 K&R-Style Function Definitions 127 Function Parameters 128 Arrays as Function Parameters 128 The main() Function 130 Function Declarations 133 Declaring Optional Parameters 134 Declaring Variable-Length Array Parameters 134 How Functions Are Executed 135 Pointers as Arguments and Return Values 135 Inline Functions 137 Non-returning Functions 139 Recursive Functions 139 Variable Numbers of Arguments 141 8. Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Defining Arrays 143 Fixed-Length Arrays 144 Variable-Length Arrays 144 Accessing Array Elements 145 Initializing Arrays 146 Writing Initialization Lists 147 Initializing Specific Elements 148 Strings 149 Multidimensional Arrays 151 Matrices 151 Declaring Multidimensional Arrays 152 Table of Contents v Initializing Multidimensional Arrays 152 Arrays as Arguments of Functions 153 9. Pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Declaring Pointers 157 Null Pointers 159 void Pointers 159 Initializing Pointers 160 Operations with Pointers 161 Using Pointers to Read and Modify Objects 161 Modifying and Comparing Pointers 163 Pointers and Type Qualifiers 165 Constant Pointers and Pointers to Constant Objects 166 Restricted Pointers 167 Pointers to Arrays and Arrays of Pointers 169 Array Pointers 170 Pointer Arrays 171 Pointers to Functions 174 10. Structures and Unions and Bit-Fields. . . . . . . . . . . . . 177 Structures 177 Defining Structure Types 178 Structure Objects and typedef Names 179 Incomplete Structure Types 179 Accessing Structure Members 180 Initializing Structures 182 Initializing Specific Members 183 Structure Members in Memory 184 Flexible Structure Members 185 Pointers as Structure Members 186 Unions 189 Defining Union Types 189 Initializing Unions 191 Anonymous Structures and Unions 191 Bit-Fields 192 11. Declarations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Object and Function Declarations 196 Examples 197 Storage Class Specifiers 199 Type Qualifiers 200 Declarations and Definitions 202 Complex Declarators 203 Type Names 205 typedef Declarations 206 vi Table of Contents _Static_assert Declarations 208 Linkage of Identifiers 209 External Linkage 209 Internal Linkage 209 No Linkage 210 Storage Duration of Objects 211 Static Storage Duration 211 Thread Storage Duration 211 Automatic Storage Duration 211 Initialization 212 Implicit Initialization 212 Explicit Initialization 212 12. Dynamic Memory Management. . . . . . . . . . . . . . . . . 215 Allocating Memory Dynamically 216 Characteristics of Allocated Memory 217 Resizing and Releasing Memory 218 An All-Purpose Binary Tree 220 Characteristics 220 Implementation 221 Generating an Empty Tree 223 Inserting New Data 224 Finding Data in the Tree 225 Removing Data from the Tree 226 Traversing a Tree 229 A Sample Application 230 13. Input and Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Streams 234 Text Streams 234 Binary Streams 235 Files 235 File Position 236 Buffers 237 The Standard Streams 237 Opening and Closing Files 238 Opening a File 238 Access Modes 239 Closing a File 240 Reading and Writing 241 Byte-Oriented and Wide-Oriented Streams 241 Error Handling 242 Unformatted I/O 243 Formatted Output 249 Formatted Input 256 Table of Contents vii Random File Access 262 Obtaining the Current File Position 262 Setting the File Access Position 263 14. Multithreading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Threads 268 Creating Threads 269 Other Thread Functions 271 Accessing Shared Data 273 Mutual Exclusion 274 Atomic Objects 276 Atomic Operations 276 Memory Ordering 278 Fences 280 Communication Between Threads: Condition Variables 280 Thread-local Objects and Thread-specific Storage 286 Using Thread-local Objects 286 Using Thread-specific Storage 287 15. Preprocessing Directives. . . . . . . . . . . . . . . . . . . . . . 291 Inserting the Contents of Header Files 292 How the Preprocessor Finds Header Files 293 Nested #include Directives 293 Defining and Using Macros 294 Macros Without Parameters 295 Macros with Parameters 296 Using Macros Within Macros 300 Macro Scope and Redefinition 302 Type-generic Macros 302 Conditional Compiling 303 The #if and #elif Directives 304 The defined Operator 304 The #ifdef and #ifndef Directives 305 Defining Line Numbers 305 Generating Error Messages 306 The #pragma Directive 306 The _Pragma Operator 307 Predefined Macros 308 Conditionally Defined Macros 309 • Part I. Standard Library 1. The Standard Headers. . . . . . . . . . . . . . . . . . . . . . . . 315 Using the Standard Headers 316 viii Table of Contents Execution Environments 316 Function and Macro Calls 316 Reserved Identifiers 319 Functions with Bounds-Checking 320 Availability 320 Run-time Constraints 321 Contents of the Standard Headers 322 assert.h 322 complex.h 322 ctype.h 323 errno.h 324 fenv.h 325 float.h 326 inttypes.h 330 iso646.h 331 limits.h 332 locale.h 333 math.h 334 setjmp.h 336 signal.h 337 stdalign.h 338 stdarg.h 338 stdatomic.h 339 stdbool.h 341 stddef.h 342 stdint.h 343 stdio.h 345 stdlib.h 347 stdnoreturn.h 348 string.h 348 tgmath.h 349 threads.h 350 time.h 352 uchar.h 353 wchar.h 353 wctype.h 354 2. Functions at a Glance. . . . . . . . . . . . . . . . . . . . . . . . . 357 Input and Output 357 Mathematical Functions 359 Mathematical Functions for Integer Types 359 Floating-Point Functions 359 Function-like Macros 361 Pragmas for Arithmetic Operations 362 Table of Contents ix

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.