C in a Nutshell
Total Page:16
File Type:pdf, Size:1020Kb
www.it-ebooks.info www.it-ebooks.info C IN A NUTSHELL www.it-ebooks.info 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’ll also find links to news, 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’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries. We specialize in documenting the latest tools and systems, translating the 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- scribers can zero in on answers to time-critical questions 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. www.it-ebooks.info C IN A NUTSHELL Peter Prinz and Tony Crawford Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo www.it-ebooks.info 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. 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 (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected]. 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 trademarks of O’Reilly Media, Inc. The In a Nutshell series designations, C in a Nutshell, the image of a cow, 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 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] www.it-ebooks.info Chapter 1 Table of Contents Preface . xi Part I. 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 © 2012www.it-ebooks.info 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 © 2012www.it-ebooks.info 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 © 2012www.it-ebooks.info O’Reilly & Associates, Inc. All rights reserved. Part II. 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 Part III. 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 © 2012www.it-ebooks.info O’Reilly & Associates, Inc. All rights reserved. 19. Using make to Build C Programs . 512 Targets, Prerequisites, and Commands 512 The Makefile 513 Rules 513 Comments 520 Variables 520 Phony Targets 527 Other Target Attributes 528 Macros 529 Functions 530 Directives 534 Running make 537 20. Debugging C Programs with GDB . 545 Installing GDB 546 A Sample Debugging Session 546 Starting GDB 550 Using GDB Commands 554 Index . 577 Table of Contents | ix This is the Title of the Book, eMatter Edition Copyright © 2012www.it-ebooks.info O’Reilly & Associates, Inc. All rights reserved. www.it-ebooks.info Chapter 2 Preface 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, reli- able 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++. 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. xi This is the Title of the Book, eMatter Edition Copyright © 2012 O’Reillywww.it-ebooks.info & Associates, Inc. All rights reserved. Part I 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 struc- tured and compiled.