本書由在計算機程序設計方面有著豐富教學和實踐經驗的中外作者合作編寫。共14章內容,由淺入深全面介紹C程序設計方法,包括基本數據類型和基本輸出輸出方式、各種控制結構和語句、指針和數組、字符串、函數、結構、文件輸入和輸出等內容,*后討論了C預處理器。本書所有實例經過精心挑選、貼近生活,尤其強調讀者的親自參與意識。每章都為初學者提供了常見錯誤分析,所選習題可提高讀者上機編程的興趣。本書采用中英文對照混排,既方便初學者熟悉相關概念和內容,也便于英文非母語的讀者熟悉英文專業詞匯。
國內首次出版的中英文對照混排式雙語版C 程序設計教材的姐妹篇。
內容通俗易懂,例子貼近生活,尤其強調讀者的親自參與意識。
每章都為初學者提供了常見錯誤分析,每章結尾有很多有趣的習題。
Preface
Learn C through English and Chinese assumes no previous programming knowledge and has been carefully written to teach the fundamentals of programming through the medium of the C programming language. Although the book is primarily intended for use in a first year undergraduate Computer Science course, it will be equally useful to experienced programmers who intend to learn C programming on their own.
The book presents a thorough introduction to programming, using working programs to demonstrate the key features of the C language in a step-by-step logical sequence.
Using their professional teaching experience, the authors have carefully chosen each example program to carefully explain an important programming concept, confidently leading the learner to competence in C and programming in general. Emphasis is placed on key programming concepts and features of programming in C, while keeping explanations as simple and clear as possible.
Key concepts are re-enforced throughout the book by the use of Quick Syntax Reference and Programming Pitfalls sections at the end of each chapter. These are useful reference points for learners while writing their own programs.
Although mainly written in English, this book includes additional explanatory annotations in Chinese. This bi-lingual approach will be appreciated by Chinese learners and will help them focus on the learning task in hand, without being over-burdened by English technical terminology.
Despite C being available on a wide variety of platforms, this book is not specific to any particular machine, compiler, or operating system. All programs are designed to be portable with little or no modification to a wide variety of platforms.
Learn C through English and Chinese
Is a comprehensive introduction to C programming.
Uses practical examples to explain theoretical concepts.
Uses an active learning approach with detailed explanations of working programming examples.
Uses additional explanatory annotations written in Chinese.
Each chapter contains
A Quick syntax reference section.
A Programming pitfalls section.
End-of-chapter exercises, allowing the learner to test and re-enforce their understanding of the programming concepts covered in the chapter.
Is accompanied by a web site containing the example programs used in the book in addition to solutions to selected end-of-chapter exercises.
Typographic conventions used in this book.
The line numbers to the left of the program examples are for reference purposes only and are not part of the C language.
Keyboard input to a program is printed in this typeface.
When a C keyword is first introduce it is in bold and in this typeface.
Program examples, screen output, reference to part of a C statement, a variable, a value in a program or a C keyword are in this typeface.
When a new term is introduced it is in italic type.
前 言
為了適應國內高等教育逐步與國際接軌的發展趨勢,英語教學和雙語教學越來越受到人們的重視。尤其是以國際化、工業化為辦學理念并注重國際化、工業化人才培養的國家示范性軟件學院的部分課程,還常常邀請一些外籍教師來國內進行全英語授課。但是由于辦學經費有限,國內學生的英語水平又參差不齊,導致全英語教學目前還無法普及。因此,利用國內的優秀師資(包括留學回國人員)進行雙語教學成為首選。針對目前許多雙語課程教學缺少雙語版教材的問題,我們組織國內和國外大學教師合作編寫了本書。
本書的第一作者是愛爾蘭都柏林工業大學(DIT)的高級講師Paul Kelly。他長期從事程序設計類課程的教學工作,在程序設計類課程教學方面具有豐富的教學實踐經驗,在國外已先后出版多本程序設計語言類書籍,還曾數次被哈爾濱工業大學軟件學院作為外聘教師應邀來校講授程序設計方面的課程。Kelly對中國學生比較了解,針對其在教學中發現的問題,即初學者面臨著既不熟悉專業術語和基本概念、又不熟悉英文專用詞匯的雙重困難,提出了出版中英文對照混排式雙語版教材的思路,幫助學生在克服語言障礙的同時,能夠更快、更好地熟悉和掌握計算機程序設計方面的基礎知識,為國內的雙語教學提供了一種最佳的解決方案。
本書內容共分14章,由淺入深、全面介紹了C語言程序設計方法。本書的特點如下:
1. 使用實際生活中的例子和直觀的圖示,通俗易懂地講解難于理解的概念。
2. 采用案例驅動和循序漸進的方式,從一個應用實例出發,先利用現有知識編寫出一個較為簡單的程序,然后在此基礎上不斷擴充,在擴充的過程中引入一個新的概念和知識點,逐漸編寫出一個較大的程序。
3. 每個例程都有詳細的講解,重點內容和段落給出了中文注解,適合以C作為入門語言的讀者對照閱讀,既方便初學者熟悉相關概念和內容,也便于母語不是英語的讀者熟悉英文的專業詞匯,尤其適合雙語教學。
4. 每章后面都有一節介紹初學者編程時易犯的錯誤,以幫助初學者在程序設計中避免這些錯誤。
5. 每章后面都有快速語法參考,總結本章內容,便于讀者快速查詢相關內容。
6. 每章后面都有精心設計的、有趣的習題,便于讀者測試和強化對相關內容的理解。
7. 有相關的教學網站(華信教育資源網,網址http://www.hxedu.com.cn)和教材網站(http://book.sunner.cn),方便讀者下載示例的源代碼和教學課件等資料。
本書是繼國內首次出版的中英文對照混排式雙語版教材《雙語版C 程序設計》之后出版的第二本雙語版程序設計教材。Paul Kelly是一位治學非常嚴謹的教師,本書的第二作者蘇小紅在與他合著過程中,經常為一個細節內容的編寫進行交流與討論,書稿完成后又進行了多次校對工作。本著對所有讀者負責的精神,我們真誠地歡迎讀者對教材提出寶貴意見,可以通過發送電子郵件或在網站(http://book.sunner.
Paul Kelly,愛爾蘭都柏林工業大學(DIT)的高級講師Paul Kelly。Kelly 老師長期從事程序設計類課程的教學工作,在程序設計類課程教學方面教學實踐經驗豐富,在國外已先后出版多本程序設計語言類書籍。蘇小紅,哈爾濱工業大學計算機學院博士生導師,計算機應用技術專家,研究領域主要是色彩匹配,信息融合,空間計算,人工神經網絡,進化算法,計算機圖形學,灰色預測,彩色圖像處理等。
Chapter One Introduction to C(引言)1
1.1 Brief history of C(C 語言簡史)1
1.2 Why programmers use C(為什么程序員愛用C語言)1
1.2.1 C is portable1
1.2.2 C is a structured programming language2
1.2.3 C is efficient2
1.2.4 C is flexible2
1.2.5 C is powerful2
1.2.6 C is concise3
1.3 Developing a C program(開發C 程序)3
1.4 Suggestions for learning C programming(學習C 語言程序設計的建議)4
Chapter Two C Data Types(C數據類型)6
2.1 Constants(常量)6
2.2 Variables(變量)6
2.3 Simple output to the screen(簡單的屏幕輸出)8
2.4 Comments(注釋)9
2.5 Data types(數據類型)10
2.5.1 Short integer data types10
2.5.2 Long integer data types11
2.5.3 Unsigned integer data types11
2.5.4 Double floating-point data type11
2.6 Data type sizes(數據類型的大。11
Programming pitfalls13
Quick syntax reference13
Exercises14
Chapter Three Simple Arithmetic Operations and Expressions(簡單的算術運算和表達式)16
3.1 C operators(C 運算符)16
3.1.1 The assignment operator16
3.1.2 Arithmetic operators17
3.1.3 Increment and decrement operators19
3.1.4 Combined operators21
3.2 Operator precedence(運算符優先級)22
3.3 Type conversions and casts(類型轉換與強制類型轉換)24
Programming pitfalls26
Quick syntax reference27
Exercises27
Chapter Four Keyboard Input and Screen Output(鍵盤輸入和屏幕輸出)30
4.1 Simple keyboard input(簡單的鍵盤輸入)30
4.2 Using a width and precision specification in printf()
[在函數printf( )中使用域寬和精度說明]31
4.3 Single-character input and output(單個字符的輸入和輸出)33
Programming pitfalls35
Quick syntax reference35
Exercises36
Chapter Five Control Statements: if and switch(控制語句:if 和switch)38
5.1 The if statement(if 語句)38
5.2 The if-else statement(if-else 語句)39
5.3 Logical operators(邏輯運算符)41
5.4 Nested if statements(嵌套的if 語句)42
5.5 The switch statement(switch 語句)44
5.6 The conditional operator ?:(條件運算符)46
Programming pitfalls47
Quick syntax reference48
Exercises48
Chapter Six Iterative Control Statements: while, do-while, and for
(循環控制語句:while、do-while和for)51
6.1 The while statement(while 語句)51
6.2 The do-while loop(do-while 循環)52
6.3 The for statement(for 語句)54
6.4 Nested loops(嵌套的循環)56
Programming pitfalls58
Quick syntax reference59
Exercises59
Chapter Seven Arrays(數組)61
7.1 Introduction to arrays(引言)61
7.2 Initialising arrays(數組初始化)66
7.3 Two-dimensional arrays(二維數組)67
7.4 Initialising two-dimensional arrays(二維數組的初始化)68
7.5 Multi-dimensional arrays(多維數組)69
Programming pitfalls70
Quick syntax reference70
Exercises70
Chapter Eight Pointers(指針)73
8.1 Variable addresses(變量的地址)73
8.2 Pointer variables(指針變量)74
8.3 The dereference operator *(解引用運算符*)75
8.4 Why use pointers? (為什么使用指針)76
Programming pitfalls77
Quick syntax reference77
Exercises77
Chapter Nine Pointers and Arrays(指針和數組)79
9.1 Pointers and one-dimensional arrays(指針和一維數組)79
9.2 Pointers and multi-dimensional arrays(指針和多維數組)81
9.3 Dynamic memory allocation(動態內存分配)82
9.3.1 The malloc() function82
9.3.2 The calloc() function85
9.3.3 The realloc() function86
9.3.4 Allocating memory for multi-dimensional arrays87
Programming pitfalls90
Quick syntax reference90
Exercises90
Chapter Ten Strings(字符串)93
10.1 String literals(字符串)93
10.2 Long character strings(長字符串)94
10.3 Strings and arrays(字符串和數組)94
10.4 Displaying a string(顯示一個字符串)95
10.5 The puts() function[puts( )函數]97
10.6 The gets() function[gets( )函數]98
10.7 Accessing individual characters of a string(訪問字符串中的單個字符)99
10.8 Assigning a string to a pointer (用字符串為字符指針賦值)100
10.9 String functions(字符串處理函數)101
10.9.1 Finding the length of a string101
10.9.2 Copying a string102
10.9.3 String concatenation102
10.9.4 Comparing strings102
10.9.5 Other string functions103
10.10 Converting numeric strings to numbers (數值字符串向數值的轉換)103
10.11 Arrays of strings(字符串數組)105
Programming pitfalls108
Quick syntax reference109
Exercises109
Chapter Eleven Functions(函數)112
11.1 Introduction(引言)112
11.2 Function arguments(函數參數)114
11.3 Returning a value from a function (從函數返回一個值)116
11.4 Passing arguments by value (按值傳參)118
11.5 Passing arguments by reference(按引用傳參)119
11.6 Changing arguments in a function (在函數中改變實參的值)120
11.7 Passing a one-dimensional array to a function (向函數傳遞一維數組)121
11.8 Passing a multi-dimensional array to a function (向函數傳遞多維數組)123
11.9 Storage classes(變量的存儲類型)124
11.9.1 auto124
11.9.2 static125
11.9.3 extern126
11.9.4 register128
11.10 Command line arguments (命令行參數)128
11.11 Mathematical functions(數學函數)130
11.11.1 Some commonly used trigonometric functions130
11.11.2 Other common mathematical functions131
11.11.3 Pseudo-random number functions132
11.11.4 Some time-related functions132
11.12 Recursion133
Programming pitfalls136
Quick syntax reference137
Exercises137
Chapter Twelve Structures(結構體)141
12.1 Defining a structure(定義結構體)141
12.2 Pointers to structures(結構體指針)144
12.3 Initialising a structure variable (結構體變量的初始化)145
12.4 Passing a structure to a function (向函數傳遞結構體變量)147
12.5 Nested structures(嵌套的結構體)149
12.6 Including a structure template from a file (從文件中引用結構體模板)150
12.7 The typedef statement (typedef 語句)151
12.8 Arrays of structures(結構體數組)152
12.9 Enumerated data types(枚舉數據類型)158
Programming pitfalls160
Quick syntax reference161
Exercises162
Chapter Thirteen File Input and Output(文件的輸入和輸出)165
13.1 Binary and ASCII (text) files [二進制文件和ASCII(文本)文件]165
13.2 Opening and closing files (文件的打開和關閉)166
13.3 Reading a character from a file using fgetc()
[使用函數fgetc( )從文件中讀字符]168
13.4 Writing a character to a file using fputc()
。凼褂煤瘮礷putc( )向文件中寫字符]170
13.5 Reading a string of characters from a file using fgets()
。凼褂煤瘮礷gets( )從文件中讀字符串]171
13.6 Writing a string of characters to a file using fputs()
[使用函數fputs( )向文件中寫字符串]172
13.7 Formatted input-output to a file using fscanf() and fprintf()173
。凼褂煤瘮礷scanf( )和fprintf( )進行文件的格式化讀寫]173
13.8 The standard files(標準文件)174
13.9 Block input-output using fread() and fwrite()175
。凼褂煤瘮礷read( )和fwrite( )進行塊讀寫]175
13.10 Rewinding a file using rewind()[使用函數rewind( )對文件重定位]177
13.11 Random access of files using fseek() [使用函數fseek( )隨機訪問文件]179
13.12 Finding the position in a file using ftell()
。凼褂煤瘮礷tell( )查找文件的當前位置]184
13.13 Deleting a file using remove()[使用函數remove( )刪除文件]184
Programming pitfalls185
Quick syntax reference186
Exercises187
Chapter Fourteen The C Preprocessor(C編譯預處理)190
14.1 Including files(包含文件)190
14.2 Defining macros(定義宏)191
14.3 Macro parameters(帶參數的宏)192
14.4 Macros and functions(宏和函數)194
14.5 Some useful macros(一些有用的宏)195
14.6 Conditional directive