Sep. 27, 2021
$75\%$ of students are new to programming.
C is a computer programming language (PL).
You communicate your ideas to computers via PLs.
Programming is NOT (only) about languages.
Programming is not about C.
You learn C to express YOUR IDEAS.
"无他, 但手熟尔"
How many bugs have you ever produced?
Brian W. Kernighan (1942 ~)
Dennis M. Ritchie (1941 ~ 2011)
编程练习: 每周基础训练 (40 分) + 学期项目 (30 分; 2 个)
"你有你的选择, 而我选择 CLion"
You do not have to become a language lawyer.
Turing Award (1983)
You think. I will type it for you.
$s \qquad f(s) \qquad f(f(s)) \qquad \ldots$
pseudo-random number sequence
Braces
Tabs vs. Spaces
int
, double
)前两次各扣 10 分, 第三次总分降为 60 分 $\;(\times\; 60\%)$
6735 99232 2021-C-PL
发布课件、资料、调查问卷等
Oct. 11, 2021
Variables (变量) Constants (常量)
Data Types (数据类型)
Operators (运算符) Expressions (表达式)
Assignment Statements (赋值语句)
I/O (Input/Output; 输入输出)
circle.c sphere.c mol.c admin.c
Given a radius ($10$) of a circle,
to compute its circumference and area.
$L = 2\pi r$ $S = \pi r^2$
int radius = 10;
radius
.radius
later.radius
is int
(integer).radius
is initialized (初始化) to 10
.radius
.radius
refers to a location (&radius
) in memory.int radius = 10;
is also a definition.
Any definitions are declarations.
All declarations are definitions (at least for now).
int radius = 10;
The name radius
is an identifier.
surface_area
vs. surfaceArea
double circumference = 2 * PI * radius;
double circumference = 0;
circumference = 2 * PI * radius;
Given a radius ($100$) of a sphere,
to compute its surface area and volume.
$A = 4 \pi r^2$ $V = \frac{4}{3} \pi r^3$
_______________ : surface_area
_______________ : volume
$6$ 克氧气的物质的量是多少?
$Q = 6 / 32 \times 6.02 \times 10^{23}$
两种格式输出, 结果均使用科学计数法表示
int
($\approx \mathbb{Z}$)double
($\approx \mathbb{R}$)char
(Character; 字符)C string
(char array; 字符数组)int
$\approx \mathbb{Z}$INT_MIN
INT_MAX
printf("INT_MIN = %d \t INT_MAX = %d\n", INT_MIN, INT_MAX);
char
isdigit isalpha isalnum
islower isupper tolower toupper
isspace
(including , \n, \t
)C string
char first_name[] = "Tayu";
A C string is an array of characters.
'\0': terminating null character
'T', 'a', 'y', 'u', '\0'
char first_name[5] = "Tayu";
char first_name[10] = "Tayu";
char first_name[2] = "Tayu";
printf
int printf(const char *format, ...);
format
: format string (格式串)...
: variable argument list (可变长参数列表)printf
int printf(const char *format, ...);
The format
string consists of
printf
int printf(const char *format, ...);
Escape sequence (转义序列)
\n
: Newline\t
: Horizontal Tab\"
: Double quotation mark\'
: Single quotation mark\\
: Backslash\b
: Backspaceprintf
int printf(const char *format, ...);
%specifier | Argument | Output |
---|---|---|
%d (%i) | int | decimal ([-]dddd) |
%f | double | decimal ([-]ddd.ddd) |
%e (%E) | double | decimal ([-]d.ddde[+-]dd) |
%g (%G) | double | %f or %e |
printf
int printf(const char *format, ...);
%specifier | Argument | Output |
---|---|---|
%c | int | character |
%s | pointer to a char array | string |
%% | % |
printf
"It is up to you to ensure that
the type of the actual argument
matches the type expected by conversion specifiers."
printf
%[flags][width][.precision]specifier
int printf(const char *format, ...);
flags
-
: left-justified (otherwise, right-justified)+
: always begin with a plus or minus signprintf
%[flags][width][.precision]specifier
int printf(const char *format, ...);
width
printf
%[flags][width][.precision]specifier
int printf(const char *format, ...);
scanf
int scanf(const char *format, ...);
format
: format string (格式串)...
: variable argument list (可变长参数列表)scanf
int scanf(const char *format, ...);
The format
string consists of
scanf
int scanf(const char *format, ...);
scanf
int scanf(const char *format, ...);
%specifier | Matched Item | Argument |
---|---|---|
%d | skip white-spaces; matches an int |
pointer to int |
%le, %lf, %lg | skip white-spaces; matches a double |
pointer to double |
%e, %f, %g | skip white-spaces; matches a float |
pointer to float |
scanf
%specifier | Matched Item | Argument |
---|---|---|
%c | a character | pointer to a char |
%s | a sequence of non-white-spaces | pointer to a char array |
%[abc] | scanlist | pointer to a char array |
%[^abc] | not in scanlist | pointer to a char array |
%% | % |
scanf
%[$\star$][width]specifier
int scanf(const char *format, ...);
scanf
"It is up to you to ensure that
the type of each actual argument pointer
matches the type expected by conversion specifiers."
Do not use scanf
.
Use $\dots$ instead.
Oct. 18, 2021
Variables Constants Data Types
Operators Expressions Assignment Statements
I/O (Input/Output)
int
: 42
double
: 3.14
char
: 'c'
string
: "Hello World"
Literal constants (字面常量)
const double PI = 3.14159;
PI
is still a variable.
min leap
sum.c min.c
Given two integers $a$ and $b$,
to compute their minimum.
$\mathit{min} = \min\{a, b\}$
if
The else
part is optional.
if
Multiple declarations and statements surrounded by {}
{}
!>=
<=
>
<
==
(equal to)!=
(not equal to)Relational expressions have values 0 (false
) or 1 (true
).
In C, non-zero numbers are treated as 1 (true
).
?:
min = a >= b ? b : a;
Conditional Expression (条件表达式)
Ternary Operator (三目运算符)
Given three integers $a$, $b$, and $c$,
to compute their minimum.
$\mathit{min} = \min\{a, b, c\}$
if
Given a set $A$ of integers,
to compute their minimum.
$\mathit{min} = \min A$
if
if
else-if
A year is a leap year if
Logical expressions have values 0
(false
) or 1
(true
).
year = 25
year = 80
Given an integer $n \ge 0$, to compute $\sum\limits_{i = 1}^{n} i$.
++
, --
)i++
i
after its value has been used++i
i
before its value is used++
, --
)i = 2;
j = i * i++;
Undefined Behavior (未定义行为)
Given a set $A$ of integers,
to compute their minimum.
$\mathit{min} = \min A$
#define NUM 5
Symbolic Constants (符号常量)
#define
is a pre-processing directive (预处理指令).
int numbers[NUM] = {0};
has a constant size.
NUM
is known at compiler time.int numbers[NUM] = {0};
int numbers[NUM] = {1};
{1, 0, 0, 0, 0}
int numbers[] = {0};
{0}
int numbers[NUM] = {[2] = 1};
{0, 0, 1, 0, 0}
int numbers[NUM] = {0};
int numbers[NUM] = {};
C99
(Unfortunately)int numbers[NUM];
int numbers[];
[]
: subscript operator (下标运算符)
Oct. 25, 2021
break
Statementstars.c
prime.c primes.c
selection-sort.c
palindrome.c
binary-search.c
digits-while.c digits-do-while.c
counting-if.c counting-switch.c
Nov. 01, 2021
第一次作业抄袭现象触目惊心
助教们敦促蚂老师召开了紧急磋商会议
会上大家对作业进行了反思, 并制定了严厉的抄袭处罚措施
助教与第一次作业(疑似)抄袭的同学进行了亲切友好的交谈
口头警告, 算作一次抄袭记录, 不作总评扣分处理
break
continue, goto
merge.c
bubble-sort.c
John Horton Conway ($1937 \sim 2020$)
game-of-life.c
counting-if.c counting-switch.c
break continue goto
common.c continue.c
Nov. 08, 2021
Re-writing Programs using Functions
Nov. 15, 2021
A function that calls (调用) itself.
It is a looooooog way to go to master recursion!!!
min-re.c
sum-re.c
$0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, \ldots$
fib-re.c fib-array.c fib-iter.c
gcd-euclid-re.c gcd-euclid-iter.c
$\text{gcd}(a, b) = \text{gcd}(b, a \;\%\; b)$
gcd-euclidean-re.c gcd-euclidean-iter.c
bsearch-re.c
Nov. 22, 2021
A function that calls itself.
Recursion (More Examples)
static
bsearch-re.c
merge-sort.c
The type of a variable determines
int
double
char
Signed (有符号数)
short int
int
long
long long
Unsigned (无符号数)
bool
(stdbool.h
)unsigned short int
unsigned int
unsigned long
unsigned long long
char
(unsigned/signed
)
"Arithmetic operators do not accept types smaller than int
as arguments, and integral promotions are automatically applied."
float y = 5.0F
%f
%f
double x = 5.0
%lf
%f
long double z = 5.0L
%Lf
%Lf
float.h
(Section 23.1)
significance.c
"Many applications don't need floating-point arithmetic at all."
Use math.h
(Section 23.3) whenever possible.
Nov. 29, 2021
Pointers and Arrays Pointers and C Strings
"A pointer is a variable that
contains the address of a variable."
int radius = 10;
radius
refers to a location (&radius
) in memory.&
: Address-of Operator ("取地址"运算符)
printf("%p\n", &radius);
int *ptr_radius = &radius;
ptr_radius
is "pointer to int".double circumference = 2 * 3.14 * radius;
radius = 20;
A variable behaves as an lvalue or a rvalue.
左值 右值
int *ptr_radius = &radius;
*ptr_radius
behaves just like radius
does.
*
: Indirection/Dereferencing ("间接寻址/解引用"运算符)double circumference = 2 * 3.14 * (*ptr_radius);
*ptr_radius = 20;
int *ptr_radius = &radius;
ptr_radius
is also a variable.selection-sort.c
Compute both the min and the max
of an array of integers.
min-max.c
scanf.c
int *numbers = malloc(len * sizeof *numbers);
void *malloc(size_t size);
void free(void *ptr);
selection-sort.c
stdlib.h
malloc.h
numbers[i]
*(numbers + i)
i[numbers]
&numbers[i]
numbers + i
numbers++
char msg[20] = "Hello World!";
char *msg = "Hello World!";
strlen.c
strlen
strlen_s
strcmp.c
strcmp
strncmp
strcpy.c
strcpy
strcpy_s
strncpy
strncpy_s
Chapter 14: string.h
selection-sort-strings.c
Dec. 06, 2021
Pointers and Arrays Pointers and C Strings
string.h
Pointer Arrays Pointers and 2D Arrays
Function Pointers
Dec. 13, 2021
string.h
Pointer Arrays Pointers and 2D Arrays
Program Arguments Function Pointer
struct
union
enum
Dec. 20, 2021
Program Arguments Function Pointer
struct
union
enum
struct
musician.c
Singly Linked List
Doubly Linked List
Circular Linked List