Transcription of The C Programming Language Ritchie & Kernighan
1 1. 2. 6. Preface to the first edition .. 8. Chapter 1 - A Tutorial Introduction .. 9. Getting 9. Variables and Arithmetic Expressions .. 11. The for 16. Symbolic 17. Character Input and Output .. 18. File 18. Character Counting .. 20. Line 21. Word 22. Arrays .. 23. Functions .. 25. Arguments - Call by 28. Character Arrays .. 29. External Variables and Scope .. 31. Chapter 2 - Types, Operators and Expressions .. 35. Variable Names .. 35. Data Types and Sizes .. 35. Constants .. 36. 39. Arithmetic 40. Relational and Logical 40. Type 41. Increment and Decrement 44. Bitwise Operators .. 46. Assignment Operators and 47. Conditional 49. Precedence and Order of 49. Chapter 3 - Control Flow .. 52. Statements and Blocks .. 52. If-Else .. 52. Else-If .. 53. 54. Loops - While and For .. 56. Loops - 58. Break and 59. Goto and 60. Chapter 4 - Functions and Program 62.
2 Basics of Functions .. 62. Functions Returning Non-integers .. 65. External Variables .. 67. Scope Rules .. 72. Header 73. Static Variables .. 75. Register Variables .. 75. Block 76. Initialization .. 76. 78. The C Preprocessor .. 79. 3. File 79. Macro Substitution .. 80. Conditional Inclusion .. 82. Chapter 5 - Pointers and Arrays .. 83. Pointers and 83. Pointers and Function Arguments .. 84. Pointers and Arrays .. 87. Address Arithmetic .. 90. Character Pointers and 93. Pointer Arrays; Pointers to Pointers .. 96. Multi-dimensional Arrays .. 99. Initialization of Pointer 101. Pointers vs. Multi-dimensional 101. Command-line Arguments .. 102. Pointers to 106. Complicated Declarations .. 108. Chapter 6 - 114. Basics of Structures .. 114. Structures and Functions .. 116. Arrays of Structures .. 118. Pointers to Structures .. 122. Self-referential Structures.
3 124. Table Lookup .. 127. 129. Unions .. 131. 132. Chapter 7 - Input and 135. Standard Input and Output .. 135. Formatted Output - printf .. 137. Variable-length Argument 138. Formatted Input - 140. File Access .. 142. Error Handling - Stderr and Exit .. 145. Line Input and Output .. 146. Miscellaneous Functions .. 147. String 147. Character Class Testing and Conversion .. 148. Ungetc .. 148. Command Execution .. 148. Storage 148. Mathematical Functions .. 149. Random Number generation .. 149. Chapter 8 - The UNIX System 151. File Descriptors .. 151. Low Level I/O - Read and 152. Open, Creat, Close, Unlink .. 153. Random Access - Lseek .. 155. Example - An implementation of Fopen and 156. Example - Listing Directories .. 159. Example - A Storage Allocator .. 163. Appendix A - Reference Manual .. 168. Introduction .. 168. 4. Lexical 168.
4 Tokens .. 168. 168. 168. 169. Constants .. 169. String Literals .. 171. Syntax 171. Meaning of Identifiers .. 171. Storage Class .. 171. Basic Types .. 172. Derived 173. Type 173. Objects and Lvalues .. 173. Conversions .. 173. Integral 174. Integral 174. Integer and 174. Floating Types .. 174. Arithmetic 174. Pointers and Integers .. 175. Void .. 176. Pointers to 176. 176. Pointer Conversion .. 177. Primary 177. Postfix Expressions .. 177. Unary Operators .. 179. Casts .. 181. Multiplicative 181. Additive Operators .. 182. Shift Operators .. 182. Relational 183. Equality 183. Bitwise AND 183. Bitwise Exclusive OR Operator .. 184. Bitwise Inclusive OR Operator .. 184. Logical AND 184. Logical OR Operator .. 184. Conditional 184. Assignment 185. Comma Operator .. 185. Constant Expressions .. 186. 186. Storage Class Specifiers .. 187. Type 188.
5 Structure and Union Declarations .. 188. 191. 192. Meaning of Declarators .. 193. 196. Type 198. 5. 199. Type Equivalence .. 199. Statements .. 199. Labeled 200. Expression Statement .. 200. Compound Statement .. 200. Selection 201. Iteration 201. Jump statements .. 202. External Declarations .. 203. Function 203. External Declarations .. 204. Scope and Linkage .. 205. Lexical Scope .. 205. 206. 206. Trigraph Sequences .. 207. Line Splicing .. 207. Macro Definition and Expansion .. 207. File 209. Conditional Compilation .. 210. Line Control .. 211. Error 211. Pragmas .. 212. Null 212. Predefined names .. 212. 212. Appendix B - Standard 220. Input and Output: < > .. 220. File Operations .. 220. Formatted 222. Formatted Input .. 223. Character Input and Output 225. Direct Input and Output Functions .. 225. File Positioning Functions .. 226.
6 Error Functions .. 226. Character Class Tests: < > .. 226. String Functions: < >.. 227. Mathematical Functions: < >.. 228. Utility Functions: < > .. 229. Diagnostics: < >.. 231. Variable Argument Lists: < > .. 231. Non-local Jumps: < >.. 232. Signals: < > .. 232. Date and Time Functions: < >.. 233. Implementation-defined Limits: < > and < >.. 234. Appendix C - Summary of Changes .. 236. 6. Preface The computing world has undergone a revolution since the publication of The C. Programming Language in 1978. Big computers are much bigger, and personal computers have capabilities that rival mainframes of a decade ago. During this time, C has changed too, although only modestly, and it has spread far beyond its origins as the Language of the UNIX. operating system. The growing popularity of C, the changes in the Language over the years, and the creation of compilers by groups not involved in its design, combined to demonstrate a need for a more precise and more contemporary definition of the Language than the first edition of this book provided.
7 In 1983, the American National Standards Institute (ANSI) established a committee whose goal was to produce ``an unambiguous and machine-independent definition of the Language C'', while still retaining its spirit. The result is the ANSI standard for C. The standard formalizes constructions that were hinted but not described in the first edition, particularly structure assignment and enumerations. It provides a new form of function declaration that permits cross-checking of definition with use. It specifies a standard library, with an extensive set of functions for performing input and output, memory management, string manipulation, and similar tasks. It makes precise the behavior of features that were not spelled out in the original definition, and at the same time states explicitly which aspects of the Language remain machine-dependent.
8 This Second Edition of The C Programming Language describes C as defined by the ANSI. standard. Although we have noted the places where the Language has evolved, we have chosen to write exclusively in the new form. For the most part, this makes no significant difference;. the most visible change is the new form of function declaration and definition. Modern compilers already support most features of the standard. We have tried to retain the brevity of the first edition. C is not a big Language , and it is not well served by a big book. We have improved the exposition of critical features, such as pointers, that are central to C Programming . We have refined the original examples, and have added new examples in several chapters. For instance, the treatment of complicated declarations is augmented by programs that convert declarations into words and vice versa.
9 As before, all examples have been tested directly from the text, which is in machine-readable form. Appendix A, the reference manual, is not the standard, but our attempt to convey the essentials of the standard in a smaller space. It is meant for easy comprehension by programmers, but not as a definition for compiler writers -- that role properly belongs to the standard itself. Appendix B is a summary of the facilities of the standard library. It too is meant for reference by programmers, not implementers. Appendix C is a concise summary of the changes from the original version. As we said in the preface to the first edition, C ``wears well as one's experience with it grows''. With a decade more experience, we still feel that way. We hope that this book will help you learn C and use it well. 7. We are deeply indebted to friends who helped us to produce this second edition.
10 Jon Bently, Doug Gwyn, Doug McIlroy, Peter Nelson, and Rob Pike gave us perceptive comments on almost every page of draft manuscripts. We are grateful for careful reading by Al Aho, Dennis Allison, Joe Campbell, Emlin, Karen Fortgang, Allen Holub, Andrew Hume, Dave Kristol, John Linderman, Dave Prosser, Gene Spafford, and Chris van Wyk. We also received helpful suggestions from Bill Cheswick, Mark Kernighan , Andy Koenig, Robin Lake, Tom London, Jim Reeds, Clovis Tondo, and Peter Weinberger. Dave Prosser answered many detailed questions about the ANSI standard. We used Bjarne Stroustrup's C++ translator extensively for local testing of our programs, and Dave Kristol provided us with an ANSI C. compiler for final testing. Rich Drechsler helped greatly with typesetting. Our sincere thanks to all. Brian W. Kernighan Dennis M. Ritchie 8. Preface to the first edition C is a general-purpose Programming Language with features economy of expression, modern flow control and data structures, and a rich set of operators.
