Thông tin tài liệu
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
Practical C Programming
Secure Programming
Cookbook
for C and C++
Programming Embedded
Systems with C
and C++
Programming with GNU
Software
Objective-C Pocket
Reference
Prefactoring
Practical Development
Environments
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 corporate@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
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
v
This is the Title of the Book, eMatter Edition
Copyright © 2012 O’Reilly & Associates, Inc. All rights reserved.
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
www.it-ebooks.info
vi
|
Table of Contents
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
www.it-ebooks.info
Table of Contents | vii
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
www.it-ebooks.info
viii
|
Table of Contents
This is the Title of the Book, eMatter Edition
Copyright © 2012 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
www.it-ebooks.info
[...]... for a lowercase alpha In multibyte character sets, each character is coded as a sequence of one or more bytes Both the source and execution character sets may contain multibyte characters If they do, then each character in the basic character set occupies only one byte, and no multibyte character except the null character may contain any byte in which all bits are 0 Multibyte characters can be used in. .. horizontal tab, vertical tab, new line, and form feed The basic execution character set also includes four nonprintable characters: the null character, which acts as the termination mark in a character string; alert; backspace; and carriage return To represent these characters in character and 8 | Chapter 1: Language Basics This is the Title of the Book, eMatter Edition www.it-ebooks.info Copyright... divided into two source files Example 1-2 The first source file, containing the main( ) function // circle .c: Prints the areas of circles // Uses circulararea .c for the math #include double circularArea( double r ); int main( ) { /* As in Example 1-1 */ } Example 1-3 The second source file, containing the circularArea( ) function // circulararea .c: Calculates the areas of circles // Called... executed, the execution environment Accordingly, C defines two character sets: the source character set is the set of characters that may be used in C source code, and the execution character set is the set of characters that can be interpreted by the running program In many C implementations, the two character sets are identical If they are not, then the compiler converts the characters in character... O’Reilly & Associates, Inc All rights reserved | 9 Language Basics string literals, type the corresponding escape sequences beginning with a backslash: \0 for the null character, \a for alert, \b for backspace, and \r for carriage return See Chapter 3 for more details The escape sequence beginning with \x indicates a character code in hexadecimal notation to be stored in the variable in this case, the code... character constants and string literals in the source code into the corresponding elements of the execution character set Each of the two character sets includes both a basic character set and extended characters The C language does not specify the extended characters, which are usually dependent on the local language The extended characters together with the basic character set make up the extended character... character constants and string literals are converted into the corresponding characters in the execution character set 6 Adjacent string literals are concatenated into a single string 7 The actual compiling takes place: the compiler analyzes the sequence of tokens and generates the corresponding machine code 8 The linker resolves references to external objects and functions, and generates the executable... character set is UTF-16, then you need to define the variable as a wide character: wchar_t alpha = '\u03B1'; In this case, the character code value assigned to alpha is hexadecimal 3B1, the same as the universal character name Not all compilers support universal character names Digraphs and Trigraphs C provides alternative representations for a number of punctuation marks that are not available on all... & Associates, Inc All rights reserved The actual numeric values of characters—the character codes—may vary from one C implementation to another The language itself imposes only the following conditions: • Each character in the basic character set must be representable in one byte • The null character is a byte in which all bits are 0 • The value of each decimal digit after 0 is greater by one than... internationally, a variety of multibyte character encoding schemes have been in use for decades to represent non-Latin alphabets and the nonalphabetic Chinese, Japanese, and Korean writing systems In 1994, with the adoption of “Normative Addendum 1,” ISO C standardized two ways of representing larger character sets: wide characters, in which the same bit width is used for every character in a character . function calls, including recursive and
inline functions.
Chapter 8, Arrays
Describes fixed-length and variable-length arrays, including strings, array
initialization,. 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
Ngày đăng: 23/03/2014, 04:20
Xem thêm: C in a Nutshell ppt, C in a Nutshell ppt