1. Introduction to the if statement
( 1 ) if ( expression )
{
statement list 1 ;
}
else
{
statement list 2;
}
If the expression evaluates to true, execute statement list 1 , otherwise execute statement list 2 .
( 2 ) if( expression 1)
{
statement list 1 ;
}
else if( expression2 )
{
statement list 2;
}
else
{
statement list ;
}
If the result of expression 1 is true, execute statement list 1 , if the result of expression 2 is true, execute statement list 2 , otherwise execute statement list 3 .
Note: else will be paired with the most recent unpaired if . When writing code, be careful not to write a dangling else
2. Comparison of floating point numbers, pointers, integers, boolean variables and zero
( 1 ) floating point number with zero
Set an acceptable precision exp to ensure that the error is within the allowable range.
( 2 ) Pointer with zero
When you don't know what value to assign to the pointer, assign NULL ;
After assigning null, it must be judged that it can only be used when it is not NULL .
( 3 ) Integer and zero
== means judgment; = means assignment
( 4 ) Boolean variable with zero
Boolean variables generally use true to represent true, and false to represent false.
If you define an integer variable a , when a is 0 , it means false, and when it is not 0 , it means true.
3. Introduction to switch statement
switch( constant expression )
{
statement item;
}
switch and case must be followed by a constant expression;
There is only one default statement in each switch statement;
The case statement has no sequence;
真正实现分支的其实是break。
4. goto语句介绍
可以实现在函数中任意跳转。建议少用,容易出错。在深层嵌套的结构的处理过程中,可以使用使得代码简化。
5. 各种操作符的介绍
(1)算术操作符
+ - * / %
1)除了%操作符之外,其他的几个操作符可以作用于整数和浮点数
2)对于/操作符,若两个操作数都为整数,执行整数除法。而只要有浮点数参与操作,执行的就是浮点数除法
3)%操作符的两个操作数必须为整数,返回整除之后的余数
(2)移位操作符
<< 左移操作符(左边抛弃、右边补0)
>> 右移操作符(逻辑移位:左边补0,右边丢弃;算术移位:左边补符号位,右边丢弃)
注:对于移位操作符,不要移动负数位
(3)位操作符
& 按位与
| 按位或
^ 按位异或
注:操作数必须是整数
(4)赋值操作符
=
+= -= *= /= %= >>= <<= &= |= ^=(复合赋值操作符)
(5)单目操作符
! 逻辑反操作
- 负值
+ 正值
& 取地址
sizeof 操作数的类型长度(以字节为单位)
~ 对一个数的二进制按位取反
-- 前置、后置--
++ 前置、后置++
* 间接访问操作符(解引用操作符)
(类型) 强制类型转换
(6)关系操作符
< > == <= >= !=
(7)逻辑操作符
&& 逻辑与
|| 逻辑或
得到的结果是真或假,要注意区分它们与位操作符
(8)条件操作符
exp1 ? exp2 : exp3
若exp1的结果为真,执行exp2,否则执行exp3
(9)逗号表达式
exp1, exp2, exp3 … expN
从左向右依次执行,整个表达式的结果是最后一个表达式的结果
(10)下标引用、函数调用和结构成员
1)下标引用[ ]
操作数:一个数组名 + 一个索引值
2)函数调用操作符()
接受一个或者多个操作数:第一个操作数是函数名,剩余的操作数就是传递给函数的参数。
3)访问一个结构的成员
. 结构体.成员名
-> 结构体指针->成员名
6. 表达式的求值
表达式求值的顺序是由操作符的优先级和结合性决定。同样,有些表达式的操作数在求值的过程中可能需要转换为其他类型。
(1) 隐式类型转换
(2) 算术转换
7. 影响表达式求值的因素
操作符的优先级、结合性,以及是否控制求值顺序
注:不能写出依赖求值顺序的表达式,因为它是不可移植的
8. 整型提升
C的整型算术运算总是至少以缺省整型类型的精度来进行的。为了获得这个精度,表达式中的字符和短整型操作数在使用之前被转换为普通整型,这种转换称为整型提升。
9. 算术转换
如果某个操作符的各个操作数属于不同的类型,那么除非其中一个操作数的转换为另一个操作数的类型,否则操作就无法进行。
下面的层次体系称为寻常算术转换(从下向上转化)
long double
double
float
unsigned long int
long int
unsigned int
int