Banahan, Brady, & Doran - The C Book Featuring the ANSI C Standard.pdf
(
1107 KB
)
Pobierz
TheCBook
1
Mike Banahan
Declan Brady
Mark Doran
January 1991
1
Conversion to LaTeX by Ward van Wanrooij. Any layout issues are caused by
my conversion script and do not reect on the authors. Also available on-line at
http://publications.gbdirect.co.uk/c_book/
CONTENTS
i
Contents
Preface
1
About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
The Success of C
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
2
Standards
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
Hosted and Free-Standing Environments
. . . . . . . . . . . . . . .
5
Typographical conventions . . . . . . . . . . . . . . . . . . . . . . .
6
Order of topics
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
Example programs
. . . . . . . . . . . . . . . . . . . . . . . . . . .
7
Deference to Higher Authority . . . . . . . . . . . . . . . . . . . . .
7
Address for the Standard . . . . . . . . . . . . . . . . . . . . . . . .
7
1
An Introduction to C
9
1.1
The form of a C program . . . . . . . . . . . . . . . . . . . . .
9
1.2
Functions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
1.3
A description of Example 1.1
. . . . . . . . . . . . . . . . . .
12
1.3.1
What was in it
. . . . . . . . . . . . . . . . . . . . . .
12
1.3.2
Layout and comment . . . . . . . . . . . . . . . . . . .
12
1.3.3
Preprocessor statements
. . . . . . . . . . . . . . . . .
13
1.3.3.1
Dene statements . . . . . . . . . . . . . . . .
14
1.3.3.2
Summary
. . . . . . . . . . . . . . . . . . . .
14
1.3.4
Function declaration and denition
. . . . . . . . . . .
15
1.3.4.1
Declaration
. . . . . . . . . . . . . . . . . . .
15
1.3.4.2
Denition . . . . . . . . . . . . . . . . . . . .
15
1.3.4.3
Summary
. . . . . . . . . . . . . . . . . . . .
16
1.3.5
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
1.3.6
The main function
. . . . . . . . . . . . . . . . . . . .
18
1.3.7
Declarations . . . . . . . . . . . . . . . . . . . . . . . .
18
1.3.8
Assignment statement
. . . . . . . . . . . . . . . . . .
19
1.3.9
The while statement
. . . . . . . . . . . . . . . . . . .
19
1.3.10 The return statement . . . . . . . . . . . . . . . . . . .
20
1.3.10.1
Summary
. . . . . . . . . . . . . . . . . . . .
21
ii
CONTENTS
1.3.11 Progress so far
. . . . . . . . . . . . . . . . . . . . . .
21
1.4
Some more programs . . . . . . . . . . . . . . . . . . . . . . .
21
1.4.1
A program to nd prime numbers . . . . . . . . . . . .
22
1.4.2
The division operators
. . . . . . . . . . . . . . . . . .
24
1.4.3
An example performing input
. . . . . . . . . . . . . .
24
1.4.4
Simple arrays
. . . . . . . . . . . . . . . . . . . . . . .
25
1.4.5
Summary
. . . . . . . . . . . . . . . . . . . . . . . . .
27
1.5
Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . .
27
1.6
Summary
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28
1.7
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28
2
Variables and Arithmetic
31
2.1
Some fundamentals . . . . . . . . . . . . . . . . . . . . . . . .
31
2.2
The alphabet of C
. . . . . . . . . . . . . . . . . . . . . . . .
31
2.2.1
Basic Alphabet
. . . . . . . . . . . . . . . . . . . . . .
32
2.2.2
Trigraphs
. . . . . . . . . . . . . . . . . . . . . . . . .
33
2.2.3
Multibyte Characters . . . . . . . . . . . . . . . . . . .
34
2.2.4
Summary
. . . . . . . . . . . . . . . . . . . . . . . . .
35
2.3
The Textual Structure of Programs
. . . . . . . . . . . . . . .
36
2.3.1
Program Layout . . . . . . . . . . . . . . . . . . . . . .
36
2.3.2
Comment
. . . . . . . . . . . . . . . . . . . . . . . . .
37
2.3.3
Translation phases
. . . . . . . . . . . . . . . . . . . .
38
2.4
Keywords and identiers . . . . . . . . . . . . . . . . . . . . .
38
2.4.1
Keywords
. . . . . . . . . . . . . . . . . . . . . . . . .
38
2.4.2
Identiers
. . . . . . . . . . . . . . . . . . . . . . . . .
39
2.5
Declaration of variables . . . . . . . . . . . . . . . . . . . . . .
40
2.6
Real types . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
41
2.6.1
Summary of real arithmatic
. . . . . . . . . . . . . . .
44
2.6.2
Printing real numbers
. . . . . . . . . . . . . . . . . .
44
2.7
Integral types
. . . . . . . . . . . . . . . . . . . . . . . . . . .
45
2.7.1
Plain integers . . . . . . . . . . . . . . . . . . . . . . .
45
2.7.2
Character variables . . . . . . . . . . . . . . . . . . . .
46
2.7.3
More complicated types
. . . . . . . . . . . . . . . . .
49
2.7.4
Summary of integral types . . . . . . . . . . . . . . . .
51
2.7.5
Printing the integral types . . . . . . . . . . . . . . . .
51
2.8
Expressions and arithmetic . . . . . . . . . . . . . . . . . . . .
52
2.8.1
Conversions . . . . . . . . . . . . . . . . . . . . . . . .
53
2.8.1.1
Integral promotions
. . . . . . . . . . . . . .
54
2.8.1.2
Signed and unsigned integers
. . . . . . . . .
54
2.8.1.3
Floating and integral . . . . . . . . . . . . . .
55
2.8.1.4
The usual arithmetic conversions
. . . . . . .
55
CONTENTS
iii
2.8.1.5
Wide characters
. . . . . . . . . . . . . . . .
57
2.8.1.6
Casts
. . . . . . . . . . . . . . . . . . . . . .
60
2.8.2
Operators
. . . . . . . . . . . . . . . . . . . . . . . . .
62
2.8.2.1
The multiplicative operators . . . . . . . . . .
62
2.8.2.2
Additive operators
. . . . . . . . . . . . . . .
62
2.8.2.3
The bitwise operators
. . . . . . . . . . . . .
63
2.8.2.4
The assignment operators
. . . . . . . . . . .
65
2.8.2.5
Increment and decrement operators . . . . . .
66
2.8.3
Precedence and grouping . . . . . . . . . . . . . . . . .
69
2.8.4
Parentheses
. . . . . . . . . . . . . . . . . . . . . . . .
72
2.8.5
Side Eects
. . . . . . . . . . . . . . . . . . . . . . . .
73
2.9
Constants
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
73
2.9.1
Integer constants
. . . . . . . . . . . . . . . . . . . . .
73
2.9.2
Real constants . . . . . . . . . . . . . . . . . . . . . . .
77
2.10 Summary
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
78
2.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
79
3
Control of Flow and Logical Expressions
81
3.1
The Task ahead . . . . . . . . . . . . . . . . . . . . . . . . . .
81
3.1.1
Logical expressions and Relational Operators . . . . . .
81
3.2
Control of ow
. . . . . . . . . . . . . . . . . . . . . . . . . .
83
3.2.1
The if statement
. . . . . . . . . . . . . . . . . . . . .
83
3.2.2
The while and do statements . . . . . . . . . . . . . . .
85
3.2.2.1
Handy hints . . . . . . . . . . . . . . . . . . .
86
3.2.3
The for statement . . . . . . . . . . . . . . . . . . . . .
87
3.2.4
A brief pause
. . . . . . . . . . . . . . . . . . . . . . .
89
3.2.5
The switch statement . . . . . . . . . . . . . . . . . . .
89
3.2.5.1
The major restriction
. . . . . . . . . . . . .
91
3.2.5.2
Integral Constant Expression
. . . . . . . . .
91
3.2.6
The break statement
. . . . . . . . . . . . . . . . . . .
92
3.2.7
The continue statement
. . . . . . . . . . . . . . . . .
92
3.2.8
goto and labels
. . . . . . . . . . . . . . . . . . . . . .
93
3.2.9
Summary
. . . . . . . . . . . . . . . . . . . . . . . . .
94
3.3
More logical expressions
. . . . . . . . . . . . . . . . . . . . .
95
3.4
Strange operators . . . . . . . . . . . . . . . . . . . . . . . . .
96
3.4.1
The ?: operator . . . . . . . . . . . . . . . . . . . . . .
97
3.4.2
The comma operator . . . . . . . . . . . . . . . . . . .
98
3.5
Summary
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
99
3.6
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
iv
CONTENTS
4
Functions
101
4.1
Changes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
4.1.1
Footnotes
. . . . . . . . . . . . . . . . . . . . . . . . . 101
4.2
The type of functions . . . . . . . . . . . . . . . . . . . . . . . 102
4.2.1
Declaring functions . . . . . . . . . . . . . . . . . . . . 102
4.2.2
The return statement . . . . . . . . . . . . . . . . . . . 104
4.2.3
Arguments to functions . . . . . . . . . . . . . . . . . . 105
4.2.4
Function prototypes
. . . . . . . . . . . . . . . . . . . 107
4.2.5
Argument Conversions
. . . . . . . . . . . . . . . . . . 110
4.2.6
Function denitions . . . . . . . . . . . . . . . . . . . . 111
4.2.6.1
Summary
. . . . . . . . . . . . . . . . . . . . 112
4.2.7
Compound statements and declarations . . . . . . . . . 113
4.2.8
Footnotes
. . . . . . . . . . . . . . . . . . . . . . . . . 114
4.3
Recursion and argument passing . . . . . . . . . . . . . . . . . 115
4.3.1
Call by value
. . . . . . . . . . . . . . . . . . . . . . . 115
4.3.2
Call by reference
. . . . . . . . . . . . . . . . . . . . . 116
4.3.3
Recursion
. . . . . . . . . . . . . . . . . . . . . . . . . 117
4.3.4
Footnotes
. . . . . . . . . . . . . . . . . . . . . . . . . 120
4.4
Linkage
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
4.4.1
Eect of scope . . . . . . . . . . . . . . . . . . . . . . . 124
4.4.2
Internal static . . . . . . . . . . . . . . . . . . . . . . . 125
4.4.3
Footnotes
. . . . . . . . . . . . . . . . . . . . . . . . . 126
4.5
Summary
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
4.5.1
Footnotes
. . . . . . . . . . . . . . . . . . . . . . . . . 128
4.6
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
4.6.1
Footnotes
. . . . . . . . . . . . . . . . . . . . . . . . . 129
5
Arrays and Pointers
131
5.1
Opening shots . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
5.1.1
So why is this important?
. . . . . . . . . . . . . . . . 131
5.1.2
Eect of the Standard
. . . . . . . . . . . . . . . . . . 132
5.2
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
5.2.1
Multidimensional arrays
. . . . . . . . . . . . . . . . . 133
5.3
Pointers
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
5.3.1
Declaring pointers
. . . . . . . . . . . . . . . . . . . . 135
5.3.2
Arrays and pointers . . . . . . . . . . . . . . . . . . . . 141
5.3.2.1
Summary
. . . . . . . . . . . . . . . . . . . . 143
5.3.3
Qualied types
. . . . . . . . . . . . . . . . . . . . . . 143
5.3.4
Pointer arithmetic
. . . . . . . . . . . . . . . . . . . . 145
5.3.5
void, null and dubious pointers
. . . . . . . . . . . . . 146
5.4
Character handling . . . . . . . . . . . . . . . . . . . . . . . . 148
Plik z chomika:
Yohoho25
Inne pliki z tego folderu:
Banahan, Brady, & Doran - The C Book Featuring the ANSI C Standard.pdf
(1107 KB)
Heterogeneous Computing with OpenCL.epub
(5099 KB)
Peter Van Der Linden - Expert C Programming Deep C Secrets.pdf
(2206 KB)
Richard Reese - Understanding and Using C Pointers.pdf
(7520 KB)
Steve Oualline - Practical C Programming 3rd Edition.pdf
(6008 KB)
Inne foldery tego chomika:
Zgłoś jeśli
naruszono regulamin