語句
陳述式是 C 程式中按順序執行的片段。任何函數的主體都是複合陳述式,而複合陳述式本身則是一連串的陳述式與宣告。
int main(void) { // start of a compound statement int n = 1; // declaration (not a statement) n = n+1; // expression statement printf("n = %d\n", n); // expression statement return 0; // return statement } // end of compound statement, end of function body
共有五種類型的陳述式:
|
屬性說明符序列(attr-spec-seq)可以應用於無標籤的陳述式;在這種情況下(運算式陳述式除外),這些屬性會被應用於該陳述式。 |
(C23 起) |
目錄 |
[編輯] 標籤
任何陳述式都可以被加上標籤,方法是在陳述式本身之前提供一個名稱,後接冒號。
attr-spec-seq(可選)(C23 起) 識別字 : |
(1) | ||||||||
attr-spec-seq(可選)(C23 起) case 常數運算式 : |
(2) | ||||||||
attr-spec-seq(可選)(C23 起) default : |
(3) | ||||||||
任何陳述式(宣告除外)前面都可以加上任意數量的標籤,每個標籤都將 識別字 宣告為標籤名稱,該名稱在包含它的函數內必須是唯一的(換句話說,標籤名稱具有函數作用域)。
標籤宣告本身沒有作用,不會改變控制流,也不會以任何方式修改其後陳述式的行為。
|
標籤後必須跟隨一個陳述式。 |
(C23 之前) |
|
標籤可以單獨出現而沒有後續陳述式。如果標籤在區塊中單獨出現,其行為就如同後面跟隨了一個空陳述式。 可選的 attr-spec-seq 會應用於該標籤。 |
(C23 起) |
[編輯] 複合陳述式
複合陳述式(或稱區塊)是由大括號括起來的陳述式與宣告序列。
{ 陳述式 | 宣告...(可選) } |
(C23 之前) | ||||||||
attr-spec-seq(可選) { 非標籤陳述式 | 標籤 | 宣告...(可選) } |
(C23 起) | ||||||||
複合陳述式允許將一組宣告和陳述式分組為一個單位,該單位可用於任何預期出現單個陳述式的地方(例如在 if 陳述式或迭代陳述式中)。
if (expr) // start of if-statement { // start of block int n = 1; // declaration printf("%d\n", n); // expression statement } // end of block, end of if-statement
每個複合陳述式都會引入其自己的區塊作用域。
在區塊內宣告的具有自動儲存期變數的初始化式,以及 VLA 宣告子,會在控制流按順序經過這些宣告時執行,就如同它們是陳述式一樣。
int main(void) { // start of block { // start of block puts("hello"); // expression statement int n = printf("abc\n"); // declaration, prints "abc", stores 4 in n int a[n*printf("1\n")]; // declaration, prints "1", allocates 8*sizeof(int) printf("%zu\n", sizeof(a)); // expression statement } // end of block, scope of n and a ends int n = 7; // n can be reused }
[編輯] 運算式陳述式
後跟分號的運算式即為一個陳述式。
運算式(可選) ; |
(1) | ||||||||
attr-spec-seq 運算式 ; |
(2) | (C23 起) | |||||||
典型 C 程式中的大多數陳述式都是運算式陳述式,例如賦值或函數呼叫。
沒有運算式的運算式陳述式稱為空陳述式。它常用於為 for 或 while 迴圈提供空的本體。它也可用於在複合陳述式的末尾或宣告之前承載標籤。
puts("hello"); // expression statement char *s; while (*s++ != '\0') ; // null statement
|
可選的 attr-spec-seq 會應用於該運算式。 attr-spec-seq 後跟 |
(C23 起) |
[編輯] 選擇陳述式
選擇陳述式根據運算式的值,在多個陳述式中選擇其中之一執行。
attr-spec-seq(可選)(C23 起) if ( 運算式 ) 陳述式 |
(1) | ||||||||
attr-spec-seq(可選)(C23 起) if ( 運算式 ) 陳述式 else 陳述式 |
(2) | ||||||||
attr-spec-seq(可選)(C23 起) switch ( 運算式 ) 陳述式 |
(3) | ||||||||
[編輯] 迭代陳述式
迭代陳述式用於重複執行某個陳述式。
attr-spec-seq(可選)(C23 起) while ( 運算式 ) 陳述式 |
(1) | ||||||||
attr-spec-seq(可選)(C23 起) do 陳述式 while ( 運算式 ) ; |
(2) | ||||||||
attr-spec-seq(可選)(C23 起) for ( 初始化子句 ; 運算式(可選) ; 運算式(可選) ) 陳述式 |
(3) | ||||||||
[編輯] 跳躍陳述式
跳躍陳述式用於無條件轉移控制流。
attr-spec-seq(可選)(C23 起) break ; |
(1) | ||||||||
attr-spec-seq(可選)(C23 起) continue ; |
(2) | ||||||||
attr-spec-seq(可選)(C23 起) return 運算式(可選) ; |
(3) | ||||||||
attr-spec-seq(可選)(C23 起) goto 識別字 ; |
(4) | ||||||||
[編輯] 參考文獻
- C23 標準 (ISO/IEC 9899:2024)
- 6.8 陳述式與區塊 (頁: 待定)
- C17 標準 (ISO/IEC 9899:2018)
- 6.8 陳述式與區塊 (頁: 106-112)
- C11 標準 (ISO/IEC 9899:2011)
- 6.8 陳述式與區塊 (頁: 146-154)
- C99 標準 (ISO/IEC 9899:1999)
- 6.8 陳述式與區塊 (頁: 131-139)
- C89/C90 標準 (ISO/IEC 9899:1990)
- 3.6 陳述式
[編輯] 參見
| C++ 文件 關於 陳述式
|