LR2

下面來回顧一下嵌套循環例子。
複製代碼
Action()
{
    int  i,j;   //生命兩個變量
    for (i=1;i<=5;i++)   //第一重循環,循環5次
    {
        if (i==3)    
            break;   //當i等於3時,跳出本重循環
        else
             lr_output_message("i=%d",i);  //否則,輸入i的值

        for (j=1;j<=5;j++)  //第二重循環,循環5次
        {
            if (j==2)   
                break;   //當j等於2時,跳出本重循環
            else
             lr_output_message("j=%d",j);  //否則,輸入j的值
        }
    }
}
複製代碼


運行結果:

複製代碼
Starting iteration 1.
Starting action Action.
Action.c(9): i=1
Action.c(16): j=1
Action.c(9): i=2
Action.c(16): j=1
Ending action Action.
Ending iteration 1.
複製代碼

 

函數                                                                                

複製代碼
void SsyHello()  //打招呼函數
{
    lr_output_message("hello %s",lr_get_host_name());
}

int GetBigger(int x,int y)  //得到最大值函數
{
    if (x>y) {
        return x;
    }
    else{
        return y;
    }

}

Action(){
    int x=10,y=20, result;   //聲明變量

    SsyHello();  //無形參,無返回值函數
    result = GetBigger(x,y);
    lr_output_message("GetBigger(%d,%d)=%d",x,y,result);  //帶形參,帶返回值函數

    return 0;
}
複製代碼

 

上面的程序加註解了,簡單來說就是前面定義了兩個函數SsyHello() 和 GetBigger(),主函數Action()對前面兩個函數進行調用。

運行結果:

複製代碼
Starting iteration 1.
Starting action Action.
Action.c(4): hello 2011-20120624YO
Action.c(23): GetBigger(10,20)=20
Ending action Action.
Ending iteration 1.
複製代碼

 

 

動態存儲方式與靜態存儲方式                                               

  

  我們在定義變量是,根據定義的位置不同,分爲全局變量與局部變量。我出生在一個叫“舞陽”的小縣城,在這個縣城中也有人名“舞陽”,前一個作用於整個縣城,後一個只作用於他個人。那麼從變量值的存在生存期角度,又可分爲靜態存儲方式和動態存儲方式兩類。

靜態存儲方式:是指在程序運行期間分配固定的存儲空間方式。

動態存儲方式:是在程序運行期間根據需要進行動態的分配存儲空間的方式。

 

用戶存儲空間可分三部分:

1、程序區

2、靜態存儲區

3、動態存儲區

  全局變量全部存放在靜態存儲區,在程序開始執行時給全局變量分配存儲區,程序運行完畢就釋放,在程序執行過程中它們佔據固定的存儲單元,而不動態地進行分配和釋放。

   動態存儲區存放以下數據

(1)函數形式參數

(2)自動變量(未加static聲明的局部變量)

(3)函數調用時的現場保護和返回地址

上面這些數據,在函數開始調用時分配動態空間,函數結果時釋放這些空間。

C語言中,每個變量和函數有兩個屬性:數據類型和數據的存儲類別

自動(auto)變量

函數中的局部變量,如不專門的聲明爲static存儲類別,都是動態地分配存儲空間的。

靜態(static)聲明局部變量

有時希望函數中的局部變量的值在函數調用結束後不消失而保留,這時就應該指定局部變量爲“靜態局部變量”,用static關鍵字。

*  寄存器(register)變量

爲了提高效率,C語言允許把局部變量的值放在CPU中的寄存器中,這種變量叫“寄存器變量”,用關鍵字register變量。

複製代碼
static int c;

int prime(register int number)   //判斷是否爲素數
{
    register int flag=1;
    auto int n;
    for (n=2;n<number/2 && flag==1;n++) {
        if (number % n==0) flag=0;
    return(flag);    
        
    }
}

demo(int a)    //static、auto變量的演示函數
{
    auto int b=0;
    int d;
    static c=3;
    b=b+1;
    c=c+1;
    lr_output_message("demo()函數中的d=%d",d);
    lr_output_message("demo()函數中的static c=%d",c);
    return a+b+c;

}
Action(){
    int a=2,i;   //變量聲明

    for (i=0;i<3;i++) {
        lr_output_message("demo()函數部分第%d運行情況如下:",i+1);
        lr_output_message("函數demo運行結果爲:%d",demo(a));
        lr_output_message("-------------------\n\r");
    }

    //判斷13是否爲素數,並輸出提示信息
    if (prime(13)==0)
        lr_output_message("13不是素數!");
    else
        lr_output_message("13是素數!");

    lr_output_message("c=%d",c);  //輸入變理的值

     
    return 0;
}
複製代碼

  素數:指大於1的自然數,除了1和它本身不能被其它數整除的數。prime()函數部分主要用來判斷傳入的數是否是素數。

  demo()函數用來做staticauto類型的變量演示。Action()函數用於調用與輸入結果。

運行結果

複製代碼
Starting iteration 1.
Starting action Action.
Action.c(31): demo()函數部分第1運行情況如下:
Action.c(22): demo()函數中的d=51446257
Action.c(23): demo()函數中的static c=4
Action.c(32): 函數demo運行結果爲:7
Action.c(33): -------------------

Action.c(31): demo()函數部分第2運行情況如下:
Action.c(22): demo()函數中的d=51446257
Action.c(23): demo()函數中的static c=5
Action.c(32): 函數demo運行結果爲:8
Action.c(33): -------------------

Action.c(31): demo()函數部分第3運行情況如下:
Action.c(22): demo()函數中的d=51446257
Action.c(23): demo()函數中的static c=6
Action.c(32): 函數demo運行結果爲:9
Action.c(33): -------------------

Action.c(40): 13是素數!
Action.c(42): c=0
Ending action Action.
Ending iteration 1.
複製代碼

 

 

 

指針                                                                                       

 

  指針是C語言中廣泛使用的一種數據類型,指針可以使我們的程序變得非常靈活,但也讓不少程序員頭痛,一不小心就會使程序出錯。

  指針一般指向一個函數或一個變量。在使用一個指針時,一個程序既可以直接使用這個指針所儲存的內存地址,又可以使用這個地址裏儲存的變量或函數的值。 

  有一本很厚小說,爲了便於讀者找到某一段內容,我們會給某一段內容起一個小標題並標註上頁數。這樣找起來就非常方便了。那在內存中,小標題頁數就相當於內存單元的指針,具體的小說內容就是內存單元的內容。

複製代碼
Action(){
    int score[5]={100,98,78,55};   //一維數組
    int *p=score;         //一維數組指針
    int sixnum[2][3]={{1,2,3},{4,5,6}};  //二維數組
    int (*p1)[3];        //二維數組指針
    int i,j;      //定義兩個變量

    for (i=0;i<=4;i++) {
        lr_output_message("score[%d]=%d",i,score[i]);  //以下標形式標識數組
        lr_output_message("*(p++)=%d",*(p++));   //以指針方式輸出數組
    }
    lr_output_message("--------------------------");

    p=score;
    for (i=0;i<=4;i++) {
        lr_output_message("score[%d]=%d",i,score[i]);  //以下標形式標識數組
        lr_output_message("*(p+%d)=%d",*(p+i));   //以指針方式輸出數組
    }
    lr_output_message("--------------------------");

    p1=sixnum;
    for (i=0;i<=1;i++) {
        for (j=0;j<=2;j++) {
             lr_output_message("sixnum[%d][%d]=%d",i,j,sixnum[i][j]);   //以下標形式標識數組
             lr_output_message("*(*(p1+%d)+%d)=%d",*(*(p1+i)+j));  //以指針方式輸出數組
        }

    }
    
    return 0;
}
複製代碼

運行結果

複製代碼
Starting iteration 1.
Starting action Action.
Action.c(11): score[0]=100
Action.c(12): *(p++)=100
Action.c(11): score[1]=98
Action.c(12): *(p++)=98
Action.c(11): score[2]=78
Action.c(12): *(p++)=78
Action.c(11): score[3]=55
Action.c(12): *(p++)=55
Action.c(11): score[4]=0
Action.c(12): *(p++)=0
Action.c(14): --------------------------
Action.c(18): score[0]=100
Action.c(19): *(p+100)=0
Action.c(18): score[1]=98
Action.c(19): *(p+98)=0
Action.c(18): score[2]=78
Action.c(19): *(p+78)=0
Action.c(18): score[3]=55
Action.c(19): *(p+55)=0
Action.c(18): score[4]=0
Action.c(19): *(p+0)=0
Action.c(21): --------------------------
Action.c(26): sixnum[0][0]=1
Action.c(27): *(*(p1+1)+0)=54385392
Action.c(26): sixnum[0][1]=2
Action.c(27): *(*(p1+2)+0)=54385392
Action.c(26): sixnum[0][2]=3
Action.c(27): *(*(p1+3)+0)=54385392
Action.c(26): sixnum[1][0]=4
Action.c(27): *(*(p1+4)+0)=54385392
Action.c(26): sixnum[1][1]=5
Action.c(27): *(*(p1+5)+0)=54385392
Action.c(26): sixnum[1][2]=6
Action.c(27): *(*(p1+6)+0)=54385392
Ending action Action.
Ending iteration 1.
複製代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章