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;
實例略
Tag標簽: 表達式,語句
jcsu
關注 - 1
粉絲 - 1
關注博主