<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # C 作用域規則 任何一種編程中,作用域是程序中定義的變量所存在的區域,超過該區域變量就不能被訪問。C 語言中有三個地方可以聲明變量: 1. 在函數或塊內部的**局部**變量 2. 在所有函數外部的**全局**變量 3. 在**形式**參數的函數參數定義中 讓我們來看看什么是**局部**變量、**全局**變量和**形式**參數。 ## 局部變量 在某個函數或塊的內部聲明的變量稱為局部變量。它們只能被該函數或該代碼塊內部的語句使用。局部變量在函數外部是不可知的。下面是使用局部變量的實例。在這里,所有的變量 a、b 和 c 是 main() 函數的局部變量。 ``` #include <stdio.h> int main () { /* 局部變量聲明 */ int a, b; int c; /* 實際初始化 */ a = 10; b = 20; c = a + b; printf ("value of a = %d, b = %d and c = %d\n", a, b, c); return 0; } ``` ## 全局變量 全局變量是定義在函數外部,通常是在程序的頂部。全局變量在整個程序生命周期內都是有效的,在任意的函數內部能訪問全局變量。 全局變量可以被任何函數訪問。也就是說,全局變量在聲明后整個程序中都是可用的。下面是使用全局變量和局部變量的實例: ``` #include <stdio.h> /* 全局變量聲明 */ int g; int main () { /* 局部變量聲明 */ int a, b; /* 實際初始化 */ a = 10; b = 20; g = a + b; printf ("value of a = %d, b = %d and g = %d\n", a, b, g); return 0; } ``` 在程序中,局部變量和全局變量的名稱可以相同,但是在函數內,局部變量的值會覆蓋全局變量的值。下面是一個實例: ``` #include <stdio.h> /* 全局變量聲明 */ int g = 20; int main () { /* 局部變量聲明 */ int g = 10; printf ("value of g = %d\n", g); return 0; } ``` 當上面的代碼被編譯和執行時,它會產生下列結果: ``` value of g = 10 ``` ## 形式參數 函數的參數,形式參數,被當作該函數內的局部變量,它們會優先覆蓋全局變量。下面是一個實例: ``` #include <stdio.h> /* 全局變量聲明 */ int a = 20; int main () { /* 在主函數中的局部變量聲明 */ int a = 10; int b = 20; int c = 0; printf ("value of a in main() = %d\n", a); c = sum( a, b); printf ("value of c in main() = %d\n", c); return 0; } /* 添加兩個整數的函數 */ int sum(int a, int b) { printf ("value of a in sum() = %d\n", a); printf ("value of b in sum() = %d\n", b); return a + b; } ``` 當上面的代碼被編譯和執行時,它會產生下列結果: ``` value of a in main() = 10 value of a in sum() = 10 value of b in sum() = 20 value of c in main() = 30 ``` ## 初始化局部變量和全局變量 當局部變量被定義時,系統不會對其初始化,您必須自行對其初始化。定義全局變量時,系統會自動對其初始化,如下所示: | 數據類型 | 初始化默認值 | | --- | --- | | int | 0 | | char | '\0' | | float | 0 | | double | 0 | | pointer | NULL | 正確地初始化變量是一個良好的編程習慣,否則程序可能會產生意外的結果,因為未初始化的變量會導致一些在內存位置中已經可用的垃圾值。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看