您的位置:首页 > 编程语言 > C语言/C++

C语言详解 - 表达式和语句

2012-04-27 15:55 711 查看
本文转载http://www.cnblogs.com/JCSU/articles/1303491.html

1. 什么是表达式?

C语言中的表达式一种有值的语法结构,它由运算符将变量、常量、函数调用返回值结合而成。

1.1 变量

变量名本身是一个表达式,表达式的值是变量当前的值。复杂的表达式由[],
->
,
.
, 和单目运算符*构成。

1.2 常量

常量名本身是一个表达式,字面常量也是表达式。对于这两者,表达式的值是常量当前的值。

1.3 函数调用

对于返回值不为void的函数,对它的正确调用也是表达式。表达式的值为函数的返回值。

1.4 操作符

运算符用于连接表达式中的值。以下是C语言中的运算符,运算符的优先级,及运算符的结合顺序

Order

Category

Operator

Operation

Associativity

1

Highest precedence

( )
[ ]

: :
.

Function call

L → R
Left to Right

2

Unary

!
~
+
-
++
- -
&
*
Size of

Logical negation (NOT)
Bitwise 1’s complement
Unary plus
Unary minus
Pre or post increment
Pre or post decrement
Address
Indirection
Size of operant in bytes

R → L
Right -> Left

3

Member Access

.*
→*

Dereference
Dereference

L → R

4

Multiplication

*
/
%

Multiply
Divide
Modulus

L → R

5

Additive

+
-

Binary Plus
Binary Minus

L → R

6

Shift

<<
>>

Shift Left
Shift Right

L → R

7

Relational

<
<=
>
>=

Less than
Less than or equal to
Greater than
Greater than or equal to

L → R

8

Equality

==
!=

Equal to
Not Equal to

L → R

9

Bitwise AAND

&

Bitwise AND

L → R

10

Bitwise XOR

^

Bitwise XOR

L → R

11

Bitwise OR

|

Bitwise OR

L → R

12

Logical AND

&&

Logical AND

L → R

14

Conditional

? :

Ternary Operator

R → L

15

Assignment

=
*=
%=
/=
+=
-=
&=
^=
|=
<<=
>>=

Assignment
Assign product
Assign reminder
Assign quotient
Assign sum
Assign difference
Assign bitwise AND
Assign bitwise XOR
Assign bitwise OR
Assign left shift
Assign right shift

R → L

16

Comma

,

Evaluate

L → R

1.5 实例

/* 变量常量表达式 */
a
sum
1
0.5
PI

/* 算术表达式 */
a + b
a - b + c
a * b + 'A'
a / b - c + 10
i++ + i++ + ++i

/* 赋值表达式 */
a = b
a *= b += 20
a = b = c = 10
a = (b=4) / (c=2)

/* 逗号表达式 */
1+2, 3+4
(10, a*5), a+10

/* 关系表达式 */
x == y
x <= y
x != y

/* 逻辑表达式 */
10 && 20
0 || 1
(a>b) && (!0)

/* 复合表达式 */
x = ( y = (a + b), z=10)

/* 表达式的应用 */
if( !100 ) //!100为逻辑表达式
printf("expression show!");

for(int i=10; i<10; i++)//for包含3个表达式,分别为i=10 i<10 i++
printf("expression show!");

while( 1 ) //1也是一个表达式
{
printf("death loop!");
}

2. 语句

语句指的是当程序运行时执行某个动作的语法结构。它改变变量的值,产生输出,或处理输入。C语言包括4类语句:

2.1 表达式语句

表达式语句是最简单的一种语句,在表达式的末尾加分号就形成了一个表达式语句。表达式语句有以下形式:

expression;

最常用的表达式语句是函数调用语句和赋值语句。函数调用语句也属于表达式语句,因为函数调用(如sin(x) )也属于表达式的一种。赋值语句的作用是将等号左边变量的值改成等号右边表达式的值。赋值语句最常用的形式为:

variable = expression;

实例:

x = 10;
y = 20;
x *= 10;

1;
x + y; //这些语句也是合法的。语句被执行时,表达式求值,但结果并不保存于任何地方
getchar(); //读取输入中的下一个字符,但接着便将其丢弃!

x++;
printf("hello \n"); //printf函数的返回值通常并不关心。所谓语句“没有效果”只是表示表达式的值被忽略。

2.2 语句块

可以用{ }将一系列语句括起来使其大功能上相当于一条语句,这就是语句块。语句块中可以有变量声明,声明必须位于块的开始。

实例:

int x;
int y;
x = 6;

{
int z = 100;
y = z/x;
printf("%d \n", y);
}

2.3 空语句

即只有一个分号的语句,它什么也不做。当用在循环体中时,表示循环体什么也不做。

实例:

; //空语句

for( int i=0; i<10; i++ )
; //循环体为空

2.4 控制语句

控制语句分类3类:循环语句,选择/条件语句,特殊语句

Repetition
While Loops-pretest loops
For Loops-pretest loops
Do-While Loops-posttest loops
/* While Loops */
while (conditional-expression) {
nested-statements
}

/* For Loops */
for (initialization; conditional-expression; increment) {
nested-statements
}

/* Do-While Loops */
do {
nested-statements
} while (conditional-expression);

Conditional Execution And Selection

If Statements-conditional execution of a single statement
If-Else Statements-conditional selection among two statements
Switch Statements-conditional selection among several statements
Extended If Statements-conditional selection among several statements
/* If Statements */
if (conditional-expression) {
then-clause
}

/* If-Else Statements */
if (conditional-expression) {
then-clause
}
else {
else-clause
}

/* Switch Statements */
switch (control-expression) {
case constant-expression-1:
statements-1
.
.
.
case constant-expression-n:
statements-n
default:
default-statements
}

/* Extended If Statements */
if (conditional-expression-1) {
statements-1
}
else if (conditional-expression-1) {
statements-1
.
.
.
}
else if (conditional-expression-n) {
statements-n
}
else {
default-statements
}

Special Control Statements

Return Statements-return values from and terminate functions
Continue Statements-skip the remaining statements in an iteration of a loop
Break Statements-exit a loop or switch statement
/* Return Statements */
return;
return expression;

/* Continue Statements */
continue;

/* Break Statements */
break;

实例略
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: