There are 12 modules in this textbook, including: C programming overview; types, operators and expressions; input and output; decision making; loops; arrays; functions; preprocessors; pointers; structures, unions and enums; bitwise operators; files.
This textbook was developed in response to the growing scale of Sino-Foreign education cooperation and the growing demand for English and bilingual textbooks. It focuses on students participating in English or bilingual classes in higher vocational education, and emphasizes the combination of theory and practice.
This textbook provides a variety of resources, such as the Chinese annotation of the corresponding chapters, answers to the exercises, source codes, teaching coursewares, teaching plans and other conventional teaching resources. Students can scan the QR code to gain these resources.
The C language is a popular computer programming language. It is a very important basic course for students of electronics, computers, automation, communication and other majors. The C language is a compulsory course for students majoring in electronics and computer applications. Internationalization and Sino-Foreign cooperation in higher education have been important trends in recent decades. In this context, there is a growing demand for English and bilingual textbooks, and it is difficult to locate English textbooks suitable for Chinese students in the market. This textbook was produced for students from foreign cooperative education and bilingual courses in higher vocational education, fully considering the learning characteristics of higher vocational students, and focusing on the combination of theory and practice.
Features of this textbook are as follows:
1) Written in English and proofread by foreign teachers.
Due to developing of Sino-Foreign cooperation and the internationalization of higher education through foreign teaching, all English classes and bilingual classes are increasingly widely-offered in colleges.?Higher vocational education requires progressively more English and bilingual textbooks. This textbook is written in English, and its contents were supervised and reviewed by professional foreign teachers, which ensured the accuracy and professionalism of the English communication of the contents.
2) Emphasis on practice.
By considering the learning characteristics of higher vocational students, the contents of this textbook have been arranged in a way that promotes “l(fā)earning by doing, doing by learning”. Each example has a detailed analysis, so that the knowledge of the theory can be strengthened. Students can understand how to analyze and solve practical problems, and gradually cultivate good programming habits.
3) Logical and well-ordered content.
In order to enable students to learn the C language effectively, the contents of the textbook have been designed to be structurally logically, and ordered in such a way that the level of learning follows a scaffolding approach. According to the characteristics of the C language, the chapters have been arranged?from an elementary?level to?an advanced level. For each chapter, the theoretical knowledge has been used as a guide, followed by an example analysis and programming practice, and finally, the knowledge is extended through the presentation of more complex theory.
4) Dual teaching resources.
This textbook provides a variety of resources, such as the Chinese an notation of the corresponding chapters for students with a less-developed English level. Additionally, exercises answers and example source code for students’ self-study, e-textbook courseware and e-teaching plans for teachers’ have been provided. Students can scan the QR code to gain these resources.
There are 12 modules in this textbook. The contents of each chapter are written in English, including: C programming overview; types, operators and expressions; input and output; decision making; loops; arrays; functions; preprocessors; pointers; structures, unions and enums; bitwise operators and files.
All of the teachers who participated in the production of this textbook have many years of professional experience. Modules 1, 2, 6, 9 and 10 were written by Liao Renxiu; Modules 3, 4 and 5 by Wang Weibin; Module 7 by Lin Jie; Module 8 by Zhang Ying; and Modele 11 by Zhu Suhang; Module 12 by Chen Guilan. Liao Renxiu and Wang Weibin were responsible for the organization and compilation of the contents. Lloyd Michael Hasson provided proofreading for the whole manuscript to ensure the accuracy and professionalism of the English language used in this edition.
However, due to various limitations and a constrained development time, there will inevitably be omissions in the textbook. We hope that readers will provide corrections if necessary.
Module 1 C Programming Overview 1
1.1 History of the C Language 1
1.2 Features of the C Language 2
1.3 C Program Development Process 4
1.4 Environment Setup 5
1.5 Simple C Example 8
1.6 Summary 11
1.7 Exercises 11
Module 2 Types, Operators and Expressions 13
2.1 Keywords and Identifiers 13
2.1.1 Keywords 13
2.1.2 Identifiers 13
2.2 Data Types of The C Language 14
2.3 Constants and Variables 16
2.3.1 Constants and Symbolic Constants 16
2.3.2 Variables 18
2.3.3 Data Type Conversion 19
2.4 Operators and Expressions 21
2.4.1 Arithmetic Operators and Arithmetic
Expressions 22
2.4.2 Assignment Operators and Assignment
Expressions 26
2.4.3 Comma Operator and Comma
Expression 28
2.5 Summary 29
2.6 Exercises 29
Module 3 Input and Output 31
3.1 Program Flow Chart 31
3.2 Formatting Input and Output 35
3.2.1 Formatted Output Function 35
3.2.2 Formatted Input Function 38
3.3 Character Input and Output 39
3.3.1 putchar( ) Function 39
3.3.2 getchar( ) Function 40
3.4 Summary 41
3.5 Exercises 41
Module 4 Decision Making 42
4.1 Logical Operators and Logical Expressions 42
4.1.1 Logical Operators 42
4.1.2 Logical Expressions 43
4.2 Relational Operators and Relational
Expressions 45
4.2.1 Relational Operators 45
4.2.2 Relational Expressions 46
4.3 Conditional Operators and Conditional
Expressions 47
4.4 Branch Structure 49
4.4.1 Single Branch Structure 49
4.4.2 Double Branch Structure 51
4.4.3 Multi-Branch Structure 54
4.5 Summary 60
4.6 Exercises 60
Module 5 Loops 63
5.1 For Loop 63
5.2 While Loop 67
5.3 Do-While Loop 70
5.4 Nested Loop 74
5.5 Loop Control Statements 77
5.5.1 Break Statement 77
5.5.2 Continue Statement 79
5.6 Summary 81
5.7 Exercises 81
Module 6 Arrays 85
6.1 Array Overview 85
6.2 Single-Dimensional Arrays 86
6.2.1 Declaring Single-Dimensional Arrays 86
6.2.2 Initializing Single-Dimensional Arrays 86
6.2.3 Accessing Single-Dimensional Array
Elements 87
6.3 Two-Dimensional Arrays 90
6.3.1 Declaring Two-Dimensional Arrays 90
6.3.2 Initializing Two-Dimensional Arrays 90
6.3.3 Accessing Two-Dimensional Array Elements 91
6.4 Character Arrays and the Application of Strings 93
6.4.1 Declaring Character Arrays 93
6.4.2 Initializing Character Arrays 94
6.4.3 Accessing Character Array Elements 94
6.4.4 Input and Output of A Character Array 95
6.4.5 Frequently Used String Processing
Functions 97
6.5 Sorting Arrays 101
6.5.1 Bubble Sorting 101
6.5.2 Selection Sorting 103
6.6 Summary 105
6.7 Exercises 106
Module 7 Functions 108
7.1 Definition of Function 108
7.1.1 Definition of Nonparametric Function 108
7.1.2 Definition of Parametric Function 109
7.2 Calling A Function 110
7.2.1 The Syntax of Calling A Function 110
7.2.2 Formal and Actual Parameters 110
7.2.3 Return Value of A Function 112
7.2.4 Function Declaration 112
7.3 Nested Calling of A Function 113
7.4 Recursive Call 115
7.5 Array as a Function Parameter 117
7.6 Scope and Storage Type of the Variable 119
7.6.1 Scope of the Variable 119
7.6.2 Storage Type of the Variable 125
7.7 Internal and External Functions 133
7.7.1 Internal Function 133
7.7.2 External Function 134
7.8 Summary 134
7.9 Exercises 134
Module 8 Preprocessors 138
8.1 Macro Definition 138
8.1.1 Macro Definition with No Parameters 138
8.1.2 Macro Definition with Parameters 139
8.2 Include Directive 140
8.3 Conditional Compilation 141
8.3.1 #ifdef Form 141
8.3.2 #ifndef Form 142
8.3.3 #if Form 142
8.4 Summary 142
8.5 Exercises 143
Module 9 Pointers 146
9.1 Concept of Pointers 146
9.2 Definition and Reference of Pointer
Variables 147
9.2.1 Definition of Pointer Variables 147
9.2.2 Reference of Pointer Variables 148
9.2.3 Pointer Variables as Function
Arguments 150
9.3 Pointer of Arrays and Pointer Variables to
Arrays 152
9.3.1 Pointers to Array Elements 152
9.3.2 References to Array Elements through
Pointers 152
9.3.3 Array Names as Function Parameters 155
9.4 Pointer of A String and A Pointer Variable
to A String 156
9.5 Pointer Variables Pointing to Functions 157
9.6 Summary 159
9.7 Exercises 159
Module 10 Structures, Unions and Enums 162
10.1 Structures 162
10.1.1 Definition of Structure Type 162
10.1.2 Definition and Initialization of
Structure Variables 163
10.1.3 Accessing Structure Variables 165
10.1.4 Structure Array 168
10.1.5 Application of Structures in Functions 170
10.2 Unions 173
10.2.1 Definition of Union Type 174
10.2.2 Definition of Union Variables 174
10.2.3 Accessing Union Variables 175
10.3 Enumerations 178
10.3.1 Definition of Enumeration Types 178
10.3.2 Definition of Enumeration Variables 179
10.4 Typedef 181
10.5 Summary 182
10.6 Exercises 182
Module 11 Bitwise Operators 185
11.1 Representation of Values in Computers 185
11.1.1 Binary Code 185
11.1.2 Inverse Code 186
11.1.3 Complement Code 186
11.2 Bitwise Operators 187
11.2.1 AND Operator 187
11.2.2 OR Operator 188
11.2.3 XOR Operator 189
11.2.4 NOT Operator 191
11.2.5 Left Shift Operator 191
11.2.6 Right Shift Operator 192
11.3 Summary 193
11.4 Exercises 193
Module 12 Files 195
12.1 Overview of Files 195
12.1.1 Ordinary Files and Device Files 195
12.1.2 ASCII File and Binary File 196
12.2 Opening and Closing of Files 196
12.2.1 Definition of File Pointers 196
12.2.2 Opening and Closing of Files 196
12.3 Reading and Writing of Files 199
12.3.1 Character Read-Write Functions:
fgetc( ) and fputc( ) 199
12.3.2 String Read-Write Functions: fgets( )
and fputs( ) 203
12.3.3 Block Data Reading-Writing
Functions: fread( ) and fwrite( ) 206
12.3.4 Formatting Read-Write Functions:
fscanf( ) and fprintf( ) 208
12.4 File Location and Random
Reading-Writing 210
12.4.1 Random Reading-Writing of
Documents 210
12.4.2 File Detection Function 213
12.5 Summary 214
12.6 Exercises 214
Appendix A ASCII Code 216
Appendix B Priority and Associativity of
Operator 218
Appendix C Common Library Functions 220
References 224