编程技术是信息技术中最重要的技能和工具之一,它是连接信息和计算机的桥梁。掌握并熟练使用编程语言已成为所有大学生必备的技能。 C语言是高级编程语言的先驱,也是最重要和最流行的语言之一。本书的目标是引导初学者进入程序设计的殿堂,内容涉及编程基础知识包括运算符和表达式、数据输入和输出、选择结构、循环结构、数组、函数、指针、结构体、链表和文件读写等。主要侧重于C语言的基本语法和编程的基本技能。我们将通过大量简洁的程序示例指导您构建编程思维并编写程序。本书旨在帮助学生掌握C语言编程技能,并为其他课程奠定基础,如C++、数学建模、操作系统、计算机图形学、系统仿真等。本书既可以作为大学本科计算机专业和非计算机专业的程序设计基础课程教材,也可以用作大专院校授课教材,亦是一本不可多得的自学教材。
汪芳,硕士,副教授。1995-1999,西北工业大学,本科;2001-2004,西北工业大学,硕士;2005-2006,德国IFW研究所,公派访问学者;2014-2015,美国莱斯大学,公派访问学者。参加的学术组织及任职:中国高校计算机大赛移动应用创新赛执委会成员;ICPC国际大学生程序设计竞赛亚洲赛站共同主任。个人/集体荣誉:西北工业大学最满意教师;西北工业大学十育人先进个人,教师代表;《Fundamentals of C programming》获省级一流本科课程(线上),中国高校计算机教育 MOOC 联盟优秀课程,负责人;《C++程序设计》,获国家级精品课程,国家级一流本科课程,重要成员。个人/集体荣誉:(1)校级最满意教师、校级"三育人先进个人”、校级先进工作者多次;(2)省级一流课程负责人:2门。承担过的重点科研项目:① 基于全球胜任力的程序设计(英)在线教学研究与实践,西北工业大学教育教学改革研究项目,第一完成人;② 程序设计基础(英语),西北工业大学研究型教学示范课程建设项目,第一完成人;③ 基于华为全栈AI的智能科学系列课程建设,教育部产学合作协同育人项目,主要完成人。教学成果获奖情况:① 参与时空一体、知行并进、学做融创的计算机基础教学改革实践,校级优秀教学成果奖一等奖;②在程序设计课程中加强学生自主学习的改革与实践,校级二等教学成果奖;③ 程序设计基础(英语)国际化改革与实践,校级二等教学成果奖。主要著作出版情况:①C++程序设计,西安电子科技大学出版社,2009年;②软件工程,西北工业大学出版社,2010年;③大学计算机实验教程,西北工业大学出版社,2013年④大学计算机基础学习辅导与实训,西北工业大学出版社,2013年;⑤ C程序设计习题与解析,清华出版社,2011年。
Contents
Chapter 1 Introducing C 1
1.1 Why programming is important 1
1.2 Why C 2
1.3 History of C 3
1.4 Strengths and Weaknesses of C 4
1.5 How to learn C? 6
1.5.1 Code::Blocks 6
1.5.2 XCODE 13
1.5.3 online compiler 16
1.5.4 How to sign in to ICOURSE (MOOC) and XUETANG 17
1.5.5 Can’t find GNU GCC Compiler 20
1.5.6 I can’t debug 21
quiz 1. 23
Chapter 2 Data Types and Expressions 24
2.1 The General Form of a Simple Program 24
2.2.1 The General Form 27
2.2.2 Variables and Assignment 31
2.2 Character set and Keywords 33
2.2.1 Character set 33
2.2.2 Keywords 34
2.2.3 Identifiers 34
2.3 Data Types 35
2.3.1 integer types 37
2.3.2 floating types 38
2.3.3 Character types 39
2.3.4 void type 41
2.3.5 Variables 42
2.4 Constants and Variables 45
2.4.1 Constants 45
2.5 Operators and Expressions 50
2.5.1 Arithmetic Operators 52
2.5.2 Assignment Operators 54
2.5.3 Increment and Decrement Operators 57
2.5.4 The sizeof Operator 58
2.5.5 The Comma Operator 59
2.5.6 The conditional operator 59
2.5.7 Bitwise Operators 60
2.5.8 Expression Statement 61
2.5.9 Type Conversion 61
quiz 2. 63
Chapter 3 Program control structure 64
3.1 Statement 64
3.2 Input and output 65
3.2.1 Single Character Input/output 66
3.2.2 Formatted Input and Output 68
3.2 The scanf Function 83
3.3 The sequence structure 92
3.4 The selection structure 95
3.4.1 Relational Expressions 97
3.4.2 Logical Expressions 99
3.4.3 The if Statement 102
3.4.5 The switch Statement 112
3.5 The loop structure 119
3.5.1 While loop 120
3.5.2 do while loop 124
3.5.3 for loop 129
3.5.4 Exiting from a Loop 133
Quiz 3 137
Chapter 4 The Preprocessor 141
4.1 Preprocessor Directives 141
4.2 Macro Definitions 142
4.3 File Inclusion 145
Quiz 4 146
Answer 4 146
Chapter 5 Arrays 147
5.1 One-Dimensional Arrays 147
5.1.1 The declaration of a one-dimensional array 147
5.1.2 Access array elements 149
5.1.3 Index 150
5.1.4 Initialization 151
5.1.5 Variable-Length Arrays 152
5.2 Multidimensional Arrays 152
5.2.1 Declaration of a two-dimensional array 152
5.2.2 Variable-Length 2D arrays 154
5.2.3 Initializing a 2D Array 157
5.2.4 Application of the 2D Array 157
Quiz 5. 158
Answer 158
Chapter 6 Functions 160
6.1 Introduction 160
6.2 Defining and Calling Functions 162
6.3 Arguments 168
6.4 The return Statement 170
6.5 Declaration 175
6.6 Array arguments 176
6.7 Recursion 179
6.8 Sort algorithm 180
6.9 Program Organization 188
6.9.1 Scope 188
6.9.2 Local variables and Global variables 189
Quiz 6 195
Answer 196
Chapter 7 Pointers 197
7.1 Pointer Variables 197
7.2 The Address and Indirection Operators 198
7.3 Pointers as Arguments 199
7.4 Pointers as Return Values 201
7.5 Using Pointers for Array 201
7.6 Array Arguments 204
7.9 Pointers and Multidimensional Arrays 205
7.10 Pointers to Functions 207
Quiz 7 208
Answer 209
Chapter 8 Strings 210
8.1 String Constants and Variables 210
8.1.1 String Constants 210
8.1.2 String Variables 211
8.2 Reading and Writing Strings 212
8.2.1 Writing Strings 212
8.2.2 Reading Strings 213
8.3 Accessing the Characters in a String 214
8.4 Using the C String Library 215
8.4.1 strcpy and strncpy 215
8.4.2 strlen 216
8.4.3 strcat and strncat 216
8.4.4 strcmp 217
8.5 sprintf and sscanf 218
8.6 Array of Strings 219
8.7 Array of Pointers to Strings 220
Quiz 8. 221
Answer 222
Chapter 9 Structures, Unions, and Enumerations 223
9.1 Structure Variables 223
9.1.1 Structure Variables 223
9.1.2 Structures as Arguments and Return Values 228
9.1.3 Arrays of Structures 230
9.1.4 Pointers to structs 231
9.2 Unions 231
9.3 Enumerations 233
Quiz 9 234
Answer 236
Chapter 10 LinkedList 237
10.1 Introduction 237
10.3 creating a linked list 239
10.4 Insert a node 242
10.5 Search a node 245
10.6 delete a node 245
Chapter 11 Files 247
11.1 Streams 247
11.2 File Operations 248
11.2.1 Opening a file 249
11.2.2 Closing a file 251
11.2.3 remove and rename functions 251
11.2.4 Detecting End-of-File and Error Conditions 252
11.3 Input/Output operation on File 253
11.3.1 Character Input/Output Functions 253
11.3.2 Line Input/Output Functions 255
11.3.3 Formatted Input/Output Functions 257
11.3.4 Block Input/Output Functions 259
11.4 File Position 260
Quiz 11 261
Answer 11 261