Statistics
71
Views
0
Downloads
0
Donations
Uploader

高宏飞

Shared on 2025-11-16
Support
Share

AuthorPeter Prinz, Ulla Kirch-Prinz

C is one of the oldest programming languages and still one of the most widely used. Whether you're an experienced C programmer or you're new to the language, you know how frustrating it can be to hunt through hundreds of pages in your reference books to find that bit of information on a certain function, type or other syntax element. Or even worse, you may not have your books with you. Your answer is the C Pocket Reference. Concise and easy to use, this handy pocket guide to C is a must-have quick reference for any C programmer. It's the only C reference that fits in your pocket and is an excellent companion to O'Reilly's other C books.Ideal as an introduction for beginners and a quick reference for advanced programmers, the C Pocket Reference consists of two parts: a compact description of the C language and a thematically structured reference to the standard library. The representation of the language is based on the ANSI standard and includes extensions introduced in 1999. An index is included to help you quickly find the information you need.This small book covers the following:C language fundamentals Data types Expressions and operators C statements Declarations Functions Preprocessor directives The standard library O'Reilly's Pocket References have become a favorite among programmers everywhere. By providing a wealth of important details in a concise, well-organized format, these handy books deliver just what you need to complete the task at hand. When you've reached a sticking point in your work and need to get to a solution quickly, the new C Pocket Reference is the book you'll want to have.

Tags
No tags
ISBN: 0596004362
Publisher: O'Reilly Media
Publish Year: 2002
Language: 中文
Pages: 142
File Format: PDF
File Size: 1.3 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
(This page has no text content)
C Pocket Reference Peter Prinz and Ulla Kirch-Prinz Translated by Tony Crawford Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo ,TITLE.21695 Page 3 Sunday, June 18, 2006 8:42 PM
C Pocket Reference by Peter Prinz and Ulla Kirch-Prinz Copyright © 2003 O’Reilly Media, Inc. All rights reserved. Printed in the United States of America. This book was originally published as C kurz & gut, Copyright © 2002 by O’Reilly Verlag GmbH & Co. KG. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly Media, Inc. 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: Jane Ellin Cover Designer: Pam Spremulli Interior Designer: David Futato Printing History: November 2002: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. The Pocket Reference series designations, C Pocket Reference, 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. 0-596-00436-2 [C] [6/06] COPYRIGHT Page 1 Thursday, May 25, 2006 5:13 PM
v Contents Introduction 1 Fundamentals 2 C Program Structure 3 Character Sets 4 Identifiers 6 Categories and Scope of Identifiers 7 Basic Types 9 Integer Types 9 Real and Complex Floating Types 11 The Type void 13 Constants 14 Integer Constants 14 Floating Constants 16 Character Constants and String Literals 16 Expressions and Operators 18 Arithmetic Operators 20 Assignment Operators 21 Relational Operators 22 Logical Operators 23 Bitwise Operators 24 Memory Accessing Operators 25 Other Operators 27
vi | Contents Type Conversions 29 Integer Promotion 29 Usual Arithmetic Conversions 30 Type Conversions in Assignments and Pointers 30 Statements 31 Block and Expression Statements 32 Jumps 33 Loops 35 Unconditional Jumps 37 Declarations 39 General Syntax and Examples 39 Complex Declarations 40 Variables 41 Storage Classes 41 Initialization 42 Derived Types 43 Enumeration Types 43 Structures, Unions, and Bit-Fields 45 Arrays 49 Pointers 52 Type Qualifiers and Type Definitions 55 Functions 57 Function Prototypes 58 Function Definitions 59 Function Calls 61 Functions with Variable Numbers of Arguments 62 Linkage of Identifiers 64 Preprocessing Directives 65
Contents | vii Standard Library 73 Standard Header Files 73 Input and Output 74 Error Handling for Input/Output Functions 76 General File Access Functions 76 File Input/Output Functions 79 Numerical Limits and Number Classification 87 Value Ranges of Integer Types 87 Range and Precision of Real Floating Types 88 Classification of Floating-Point Numbers 90 Mathematical Functions 91 Mathematical Functions for Integer Types 91 Mathematical Functions for Real Floating Types 92 Optimizing Runtime Efficiency 94 Mathematical Functions for Complex Floating Types 95 Type-Generic Macros 96 Error Handling for Mathematical Functions 97 The Floating-Point Environment 98 Character Classification and Case Mapping 101 String Handling 103 Conversion Between Strings and Numbers 105 Multibyte Character Conversion 107 Searching and Sorting 108 Memory Block Management 109 Dynamic Memory Management 110 Time and Date 111
viii | Contents Process Control 113 Communication with the Operating System 113 Signals 114 Non-Local Jumps 115 Error Handling for System Functions 116 Internationalization 116 Index 121
1 C Pocket Reference Introduction The programming language C was developed in the 1970s by Dennis Ritchie at Bell Labs (Murray Hill, New Jersey) in the process of implementing the Unix operating system on a DEC PDP-11 computer. C has its origins in the typeless pro- gramming language BCPL (Basic Combined Programming Language, developed by M. Richards) and in B (developed by K. Thompson). In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard. C is a highly portable language oriented towards the architec- ture of today’s computers. The actual language itself is rela- tively small and contains few hardware-specific elements. It includes no input/output statements or memory manage- ment techniques, for example. Functions to address these tasks are available in the extensive C standard library. C’s design has significant advantages: • Source code is highly portable • Machine code is efficient • C compilers are available for all current systems The first part of this pocket reference describes the C lan- guage, and the second part is devoted to the C standard library. The description of C is based on the ANSI X3.159 standard. This standard corresponds to the international
2 | C Pocket Reference standard ISO/IEC 9899, which was adopted by the Interna- tional Organization for Standardization in 1990, then amended in 1995 and 1999. The ISO/IEC 9899 standard can be ordered from the ANSI web site; see http://webstore.ansi. org/. The 1995 standard is supported by all common C compilers today. The new extensions defined in the 1999 release (called “ANSI C99” for short) are not yet implemented in many C compilers, and are therefore specially labeled in this book. New types, functions, and macros introduced in ANSI C99 are indicated by an asterisk in parentheses (*). Font Conventions The following typographic conventions are used in this book: Italic Used to introduce new terms, and to indicate filenames. Constant width Used for C program code as well as for functions and directives. Constant width italic Indicates replaceable items within code syntax. Constant width bold Used to highlight code passages for special attention. Fundamentals A C program consists of individual building blocks called functions, which can invoke one another. Each function per- forms a certain task. Ready-made functions are available in the standard library; other functions are written by the pro- grammer as necessary. A special function name is main(): this designates the first function invoked when a program starts. All other functions are subroutines.
Fundamentals | 3 C Program Structure Figure 1 illustrates the structure of a C program. The pro- gram shown consists of the functions main() and showPage(), and prints the beginning of a text file to be specified on the command line when the program is started. The statements that make up the functions, together with the necessary declarations and preprocessing directives, form the source code of a C program. For small programs, the source code is written in a single source file. Larger C programs Figure 1. A C program /* Head.c: This program outputs the beginning of a * * text file to the standard output. * * Usage : Head <filename> */ #include <stdio.h> #define LINES 22 void showPage( FILE * ); // prototype int main( int argc, char **argv ) { FILE *fp; int exit_code = 0; if ( argc != 2 ) { fprintf( stderr, "Usage: Head <filename>\n" ); exit_code = 1; } else if ( ( fp = fopen( argv[1], "r" )) == NULL ) { fprintf( stderr, "Error opening file!\n" ); exit_code = 2; } else { showPage( fp ); fclose( fp ); } return exit_code; } void showPage( FILE *fp ) // Output a screen page { int count = 0; char line[81]; while ( count < LINES && fgets( line, 81, fp ) != NULL ) { fputs( line, stdout ); ++count; } } Comments Preprocessor directives Funtion main() Other functions
4 | C Pocket Reference consist of several source files, which can be edited and com- piled separately. Each such source file contains functions that belong to a logical unit, such as functions for output to a terminal, for example. Information that is needed in several source files, such as declarations, is placed in header files. These can then be included in each source file via the #include directive. Source files have names ending in .c; header files have names ending in .h. A source file together with the header files included in it is called a translation unit. There is no prescribed order in which functions must be defined. The function showPage() in Figure 1 could also be placed before the function main(). A function cannot be defined within another function, however. The compiler processes each source file in sequence and decomposes its contents into tokens, such as function names and operators. Tokens can be separated by one or more whitespace characters, such as space, tab, or newline charac- ters. Thus only the order of tokens in the file matters. The layout of the source code—line breaks and indentation, for example—is unimportant. The preprocessing directives are an exception to this rule, however. These directives are com- mands to be executed by the preprocessor before the actual program is compiled, and each one occupies a line to itself, beginning with a hash mark (#). Comments are any strings enclosed either between /* and */, or between // and the end of the line. In the preliminary phases of translation, before any object code is generated, each comment is replaced by one space. Then the preprocess- ing directives are executed. Character Sets ANSI C defines two character sets. The first is the source character set, which is the set of characters that may be used
Fundamentals | 5 in a source file. The second is the execution character set, which consists of all the characters that are interpreted dur- ing the execution of the program, such as the characters in a string constant. Each of these character sets contains a basic character set, which includes the following: • The 52 upper- and lower-case letters of the Latin alpha- bet: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z • The ten decimal digits (where the value of each character after 0 is one greater than the previous digit): 0 1 2 3 4 5 6 7 8 9 • The following 29 graphic characters: ! " # % & ' ( ) * + , – . / : ; < = > ? [ \ ] ^ _ { | } ~ • The five whitespace characters: space, horizontal tab, vertical tab, newline, form feed In addition, the basic execution character set contains the following: • The null character \0, which terminates a character string • The control characters represented by simple escape sequences, shown in Table 1, for controlling output devices such as terminals or printers Table 1. The standard escape sequences Escape sequence Action on display device Escape sequence Action on display device \a Alert (beep) \' The character ' \b Backspace \" The character " \f Form feed \? The character ? \n Newline \\ The character \ \r Carriage return \o \oo \ooo (o = octal digit) The character with this octal code
6 | C Pocket Reference Any other characters, depending on the given compiler, can be used in comments, strings, and character constants. These may include the dollar sign or diacriticals, for example. How- ever, the use of such characters may affect portability. The set of all usable characters is called the extended charac- ter set, which is always a superset of the basic character set. Certain languages use characters that require more than one byte. These multibyte characters may be included in the extended character set. Furthermore, ANSI C99 provides the integer type wchar_t (wide character type), which is large enough to represent any character in the extended character set. The modern Unicode character encoding is often used, which extends the standard ASCII code to represent some 35,000 characters from 24 countries. C99 also introduces trigraph sequences. These sequences, shown in Table 2, can be used to input graphic characters that are not available on all keyboards. The sequence ??!, for example, can be entered to represent the “pipe” charac- ter |. Identifiers Identifiers are names of variables, functions, macros, types, etc. Identifiers are subject to the following formative rules: \t Horizontal tab \xh.. (h..= string of hex digits) The character with this hexadecimal code\v Vertical tab Table 2. The trigraph sequences Trigraph ??= ??( ??/ ??) ??' ??< ??! ??> ??- Meaning # [ \ ] ^ { | } ~ Table 1. The standard escape sequences (continued) Escape sequence Action on display device Escape sequence Action on display device
Fundamentals | 7 • An identifier consists of a sequence of letters (A to Z, a to z), digits (0 to 9), and underscores (_). • The first character of an identifier must not be a digit. • Identifiers are case-sensitive. • There is no restriction on the length of an identifier. However, only the first 31 characters are generally signifi- cant. Keywords are reserved and must not be used as identifiers. Following is a list of keywords: External names—that is, identifiers of externally linked func- tions and variables—may be subject to other restrictions, depending on the linker: in portable C programs, external names should be chosen so that only the first eight charac- ters are significant, even if the linker is not case-sensitive. Some examples of identifiers are: Valid: a, DM, dm, FLOAT, _var1, topOfWindow Invalid: do, 586_cpu, zähler, nl-flag, US_$ Categories and Scope of Identifiers Each identifier belongs to exactly one of the following four categories: auto enum restrict(*) unsigned break extern return void case float short volatile char for signed while const goto sizeof _Bool(*) continue if static _Complex(*) default inline(*) struct _Imaginary(*) do int switch double long typedef else register union
8 | C Pocket Reference • Label names • The tags of structures, unions, and enumerations. These are identifiers that follow one of the keywords struct, union, or enum (see “Derived Types”). • Names of structure or union members. Each structure or union type has a separate name space for its members. • All other identifiers, called ordinary identifiers. Identifiers of different categories may be identical. For exam- ple, a label name may also be used as a function name. Such re-use occurs most often with structures: the same string can be used to identify a structure type, one of its members, and a variable; for example: struct person {char *person; /*...*/} person; The same names can also be used for members of different structures. Each identifier in the source code has a scope. The scope is that portion of the program in which the identifier can be used. The four possible scopes are: Function prototype Identifiers in the list of parameter declarations of a func- tion prototype (not a function definition) have function prototype scope. Because these identifiers have no mean- ing outside the prototype itself, they are little more than comments. Function Only label names have function scope. Their use is lim- ited to the function block in which the label is defined. Label names must also be unique within the function. The goto statement causes a jump to a labelled state- ment within the same function. Block Identifiers declared in a block that are not labels have block scope. The parameters in a function definition also have block scope. Block scope begins with the
Basic Types | 9 declaration of the identifier and ends with the closing brace (}) of the block. File Identifiers declared outside all blocks and parameter lists have file scope. File scope begins with the declaration of the identifier and extends to the end of the source file. An identifier that is not a label name is not necessarily visible throughout its scope. If an identifier with the same category as an existing identifier is declared in a nested block, for example, the outer declaration is temporarily hidden. The outer declaration becomes visible again when the scope of the inner declaration ends. Basic Types The type of a variable determines how much space it occu- pies in storage and how the bit pattern stored is interpreted. Similarly, the type of a function determines how its return value is to be interpreted. Types can be either predefined or derived. The predefined types in C are the basic types and the type void. The basic types consist of the integer types and the floating types. Integer Types There are five signed integer types: signed char, short int (or short), int, long int (or long), and long long int(*) (or long long(*)). For each of these types there is a correspond- ing unsigned integer type with the same storage size. The unsigned type is designated by the prefix unsigned in the type specifier, as in unsigned int. The types char, signed char, and unsigned char are formally different. Depending on the compiler settings, however, char is equivalent either to signed char or to unsigned char. The prefix signed has no meaning for the types short, int, long,
10 | C Pocket Reference and long long(*), however, since they are always considered to be signed. Thus short and signed short specify the same type. The storage size of the integer types is not defined; however, their width is ranked in the following order: char <= short <= int <= long <= long long(*). Furthermore, the size of type short is at least 2 bytes, long at least 4 bytes, and long long at least 8 bytes. Their value ranges for a given imple- mentation are found in the header file limits.h. ANSI C99 also introduces the type _Bool to represent Bool- ean values. The Boolean value true is represented by 1 and false by 0. If the header file stdbool.h has been included, then bool can be used as a synonym for _Bool and the mac- ros true and false for the integer constants 1 and 0. Table 3 shows the standard integer types together with some typical value ranges. Table 3. Standard integer types with storage sizes and value ranges Type Storage size Value range (decimal) _Bool 1 byte 0 and 1 char 1 byte -128 to 127 or 0 to 255 unsigned char 1 byte 0 to 255 signed char 1 byte -128 to 127 int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295 short 2 bytes -32,768 to 32,767 unsigned short 2 bytes 0 to 65,535 long 4 bytes -2,147,483,648 to 2,147,483,647 unsigned long 4 bytes 0 to 4,294,967,295 long long(*) 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 unsigned long long(*) 8 bytes 0 to 18,446,744,073,709,551,615
Basic Types | 11 ANSI C99 introduced the header file stdint.h(*), which defines integer types with specific widths (see Table 4). The width N of an integer type is the number of bits used to rep- resent values of that type, including the sign bit. (Generally, N = 8, 16, 32, or 64.) For example, int16_t is an integer type that is exactly 16 bits wide, and int_fast32_t is the fastest integer type that is 32 or more bits wide. These types must be defined for the widths N = 8, 16, 32, and 64. Other widths, such as int24_t, are optional. For example: int16_t val = -10; // integer variable // width: exactly 16 bits For each of the signed types described above, there is also an unsigned type with the prefix u. uintmax_t, for example, rep- resents the implementation’s widest unsigned integer type. Real and Complex Floating Types Three types are defined to represent non-integer real num- bers: float, double, and long double. These three types are called the real floating types. The storage size and the internal representation of these types are not specified in the C standard, and may vary from one compiler to another. Most compilers follow the IEEE 754-1985 standard for binary floating-point arithmetic, how- ever. Table 5 is also based on the IEEE representation. Table 4. Integer types with defined width Type Meaning intN_t Width is exactly N bits int_leastN_t Width is at least N bits int_fastN_t The fastest type with width of at least N bits intmax_t The widest integer type implemented intptr_t Wide enough to store the value of a pointer
12 | C Pocket Reference The header file float.h defines symbolic constants that describe all aspects of the given representation (see “Numeri- cal Limits and Number Classification”). Internal representation of a real floating-point number The representation of a floating-point number x is always composed of a sign s, a mantissa m, and an exponent exp to base 2: x = s * m * 2exp, where 1.0 <= m < 2 or m = 0 The precision of a floating type is determined by the number of bits used to store the mantissa. The value range is deter- mined by the number of bits used for the exponent. Figure 2 shows the storage format for the float type (32-bit) in IEEE representation. The sign bit S has the value 1 for negative numbers and 0 for other numbers. Because in binary the first bit of the mantissa is always 1, it is not represented. The exponent is stored with a bias added, which is 127 for the float type. For example, the number –2.5 = –1 * 1.25 * 21 is stored as: S = 1, Exponent = 1+127 = 128, Mantissa = 0.25 Table 5. Real floating types Type Storage size Value range (decimal, unsigned) Precision (decimal) float 4 bytes 1.2E-38 to 3.4E+38 6 decimal places double 8 bytes 2.3E-308 to 1.7E+308 15 decimal places long double 10 bytes 3.4E-4932 to 1.1E+4932 19 decimal places Figure 2. IEEE storage format for the 32-bit float type S Exponent Mantissa Bit position: 31 30 23 22 0
The above is a preview of the first 20 pages. Register to read the complete e-book.