怎樣才能知道循環是否提前結束了 循環通常依賴于一個或多個變量,你可以在循環外檢查這些變量,以確定循環被正确執行。請看下例:
int x
char * cp[REQUESTED_BLOCKS]
/ * Attempt (in vain, I must add... )to
allocate 512 10KB blocks in memory. * /
for (x = 0; x<REQUESTED_ BLOCKS ; x++ )
{
cpi[x]= (char * ) malloc (10000,1)
if (cp[x]= = (char * ) NULL)
break
}
/ * If x is less than REQUESTED-BLOCKS,
the loop has ended prematurely. * /
if (x<REQUESTED_BLOCKS)
printf ("Bummer ! My loop ended prematurely ! \n" );
注意:如果上述循環執行成功,它一定會循環512次。緊接着循環的if語句用來測試循環次數,進而判斷循環是否提前結束。如果變量x的值小于512,就說明循環出錯了。
在C語言中 除了for語句中之外,在哪些情況下還要使用逗号運算符
逗号運算符通常用來分隔變量說明、函數參數、表達式以及for語句中的元素。
下例給出了使用逗号的多種方式:
#include <stdio.h>
#include <stdlib.h>
void main(void);
void main ()
/ * Here, the comma operator is used to separate
three variable declarations. * /
int i, j, k;
/ * Notice how you can use the comma operator to perform
multiple initializations on the same line. * /
i=0, j=1, k=2;
printf("i= %d, j=%d, k= %d\n", i, j, k);
/ * Here, the comma operator is used to execute three expressions
in one line: assign k to i, increment j, and increment k.
The value that i receives is always the rigbtmost expression. * /
i= ( j++, k++ );
printf("i=%d, j=%d, k=%d\n", i, j, k);
/ * Here, the while statement uses the comma operator to
assign the value of i as well as test it. * /
while (i=(rand() % 100), i !=50)
printf("i is %d, trying again... \n", i)
printf ("\nGuess what? i is 50!\n" )
請注意下述語句:
i:(j++,k++)
這條語句一次完成了三個動作,依次為:
- 把k值賦給i。這是因為左值(lvaule)總是等于最右邊的參數,本例的左值等于k。注意,本例的左值不等于k++,因為k++是一個字尾自增表達式,在把k值賦給j之後k才會自增。如果所用的表達式是++k,則++k的值會被賦給i,因為++k是一個字首自增表達式,k的自增發生在指派操作之前。
- j自增。
- k自增。
此外,還要注意看上去有點奇怪的while語句:
while (i=(rand() % 100), i !=50)
printf("i is %d, trying again... \n");
這裡,逗号運算符将兩個表達式隔開,while語句的每次循環都将計算這兩個表達式的值。逗号左邊是第一個表達式,它把0至99之間的一個随機數賦給i;第二個表達式在while語句中更常見,它是一個條件表達式,用來判斷i是否不等于50。while語句每一次循環都要賦予i一個新的随機數,并且檢查其值是否不等于50。最後,i将被随機地指派為50,而while語句也将結束循環。
請參見:
1、運算符的優先級總能保證是“自左至右”或“自右至左”的順序嗎?
2、++var和var++有什麼差別?
什麼是C語言局部程式塊(local block) 局部程式塊是指一對大括号({})之間的一段C語言程式。一個C函數包含一對大括号,這對大括号之間的所有内容都包含在一個局部程式塊中。if語句和swich語句也可以包含一對大括号,每對大括号之間的代碼也屬于一個局部程式塊。
此外,你完全可以建立你自己的局部程式塊,而不使用C函數或基本的C語句。
- 你可以在局部程式塊中說明一些變量,這種變量被稱為局部變量,它們隻能在局部程式塊的開始部分說明,并且隻在說明它的局部程式塊中有效。
- 如果局部變量與局部程式塊以外的變量重名,則前者優先于後者。
下面是一個使用局部程式塊的例子:
void main()
/ * Begin local block for function main() * /
int test_ var = 10;
printf("Test variable before the if statement: %d\n", test_var);
if (test_var>5)
{
/ * Begin local block for "if" statement * /
int test_ var = 5;
printf("Test variable within the if statement: %d\n",
test_var);
{
/ * Begin independent local block (not tied to
any function or keyword) * /
int test_var = 0;
printf (
"Test variable within the independent local block: %d\n",
test_var)
}
/ * End independent local block * /
printf ("Test variable after the if statement: %d\n", test_var);
/*End local block for function main () * /
上例産生如下輸出結果:
Test variable before the if statement: 10
Test variable within the if statement: 5
Test variable within the independent local block:0
Test variable after the if statement: 10
注意:在這個例子中,每次test_var被定義時,它都要優先于前面所定義的test_var變量。此外還要注意,當if語句的局部程式塊結束時,程式重新進入最初定義的test_var變量的作用範圍,此時test_var的值為10。