在要求输入邮箱的文本域,请填写真实的邮件地址。非真实邮件地址,将收不到回复信息。

C语言简单入门

电子书 清风 734℃ 0评论

很早以前在网上看到的一本书,讲的关于C语言的。个人感觉还不错,当然不是什么高深的内容,当时觉得这样的国外书籍比国内的还是要好上很多。英文名字是:How to Think Like a Computer Scientist C Version

C语言简单入门-第0张图片

1 The way of the program 1
1.1 What is a programming language? . . . . . . . . . . . . . . . . . 1
1.2 What is a program? . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 What is debugging? . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3.1 Compile-time errors . . . . . . . . . . . . . . . . . . . . . 4
1.3.2 Run-time errors . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.3 Logic errors and semantics . . . . . . . . . . . . . . . . . 4
1.3.4 Experimental debugging . . . . . . . . . . . . . . . . . . . 5
1.4 Formal and natural languages . . . . . . . . . . . . . . . . . . . . 5
1.5 The first program . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.6 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2 Variables and types 13
2.1 More output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2 Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.5 Outputting variables . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.6 Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.7 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.8 Order of operations . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.9 Operators for characters . . . . . . . . . . . . . . . . . . . . . . . 20
ii Contents
2.10 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.11 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3 Function 25
3.1 Floating-point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.2 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.3 Converting from double to int . . . . . . . . . . . . . . . . . . . 27
3.4 Math functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.5 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.6 Adding new functions . . . . . . . . . . . . . . . . . . . . . . . . 29
3.7 Definitions and uses . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.8 Programs with multiple functions . . . . . . . . . . . . . . . . . . 31
3.9 Parameters and arguments . . . . . . . . . . . . . . . . . . . . . 32
3.10 Parameters and variables are local . . . . . . . . . . . . . . . . . 33
3.11 Functions with multiple parameters . . . . . . . . . . . . . . . . . 34
3.12 Functions with results . . . . . . . . . . . . . . . . . . . . . . . . 34
3.13 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.14 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4 Conditionals and recursion 39
4.1 Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . 39
4.2 The modulus operator . . . . . . . . . . . . . . . . . . . . . . . . 40
4.3 Alternative execution . . . . . . . . . . . . . . . . . . . . . . . . . 40
4.4 Chained conditionals . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.5 Nested conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.6 The return statement . . . . . . . . . . . . . . . . . . . . . . . . 42
4.7 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.8 Infinite recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.9 Stack diagrams for recursive functions . . . . . . . . . . . . . . . 45
4.10 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Contents iii
5 Fruitful functions 49
5.1 Return values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
5.2 Program development . . . . . . . . . . . . . . . . . . . . . . . . 51
5.3 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
5.4 Boolean values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
5.5 Boolean variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
5.6 Logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
5.7 Bool functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.8 Returning from main() . . . . . . . . . . . . . . . . . . . . . . . 57
5.9 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
6 Iteration 63
6.1 Multiple assignment . . . . . . . . . . . . . . . . . . . . . . . . . 63
6.2 Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
6.3 The while statement . . . . . . . . . . . . . . . . . . . . . . . . . 64
6.4 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
6.5 Two-dimensional tables . . . . . . . . . . . . . . . . . . . . . . . 68
6.6 Encapsulation and generalization . . . . . . . . . . . . . . . . . . 68
6.7 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
6.8 More encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . 70
6.9 Local variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
6.10 More generalization . . . . . . . . . . . . . . . . . . . . . . . . . 71
6.11 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
6.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
7 Arrays 77
7.1 Increment and decrement operators . . . . . . . . . . . . . . . . . 78
7.2 Accessing elements . . . . . . . . . . . . . . . . . . . . . . . . . . 78
7.3 Copying arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
7.4 for loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
iv Contents
7.5 Array length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
7.6 Random numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
7.7 Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
7.8 Array of random numbers . . . . . . . . . . . . . . . . . . . . . . 83
7.9 Passing an array to a function . . . . . . . . . . . . . . . . . . . . 84
7.10 Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
7.11 Checking the other values . . . . . . . . . . . . . . . . . . . . . . 85
7.12 A histogram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
7.13 A single-pass solution . . . . . . . . . . . . . . . . . . . . . . . . 87
7.14 Random seeds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
7.15 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
7.16 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
8 Strings and things 91
8.1 Containers for strings . . . . . . . . . . . . . . . . . . . . . . . . 91
8.2 String variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
8.3 Extracting characters from a string . . . . . . . . . . . . . . . . . 92
8.4 Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
8.5 Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
8.6 Finding a character in a string . . . . . . . . . . . . . . . . . . 94
8.7 Pointers and Addresses . . . . . . . . . . . . . . . . . . . . . . . . 95
8.8 String concatenation . . . . . . . . . . . . . . . . . . . . . . . . . 96
8.9 Assigning new values to string variables . . . . . . . . . . . . . 96
8.10 strings are not comparable . . . . . . . . . . . . . . . . . . . . . 97
8.11 Character classification . . . . . . . . . . . . . . . . . . . . . . . . 98
8.12 Getting user input . . . . . . . . . . . . . . . . . . . . . . . . . . 99
8.13 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
8.14 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Contents v
9 Structures 103
9.1 Compound values . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
9.2 Point objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
9.3 Accessing member variables . . . . . . . . . . . . . . . . . . . . . 104
9.4 Operations on structures . . . . . . . . . . . . . . . . . . . . . . . 105
9.5 Structures as parameters . . . . . . . . . . . . . . . . . . . . . . . 105
9.6 Call by value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
9.7 Call by reference . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
9.8 Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
9.9 Structures as return types . . . . . . . . . . . . . . . . . . . . . . 109
9.10 Passing other types by reference . . . . . . . . . . . . . . . . . . 110
9.11 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
9.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
A Coding Style 113
A.1 A short guide on style . . . . . . . . . . . . . . . . . . . . . . . . 113
A.2 Naming conventions and capitalization rules . . . . . . . . . . . . 114
A.3 Bracing style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
A.4 Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
B ASCII-Table… 117

Think-C_v1.08



转载请注明:清风亦平凡 » C语言简单入门

喜欢 (1)or分享 (0)
支付宝扫码打赏 支付宝扫码打赏 微信打赏 微信打赏
头像
发表我的评论
取消评论

CAPTCHA Image
Reload Image
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址