C 變數如何作用域化


我們將在此看到 C 變數如何作用域化。C 中的變數始終是靜態作用域的。變數的繫結可由程式文字確定。這些都不依賴於執行時函式呼叫堆疊。

讓我們舉一個例子來了解這個概念。

示例

# include <stdio.h>
int x = 0;
int my_function() {
   return x;
}
int my_function2() {
   int x = 1;
   return my_function();
}
int main(){
   printf("The value is: %d\n", my_function2());
}

輸出

The value is: 0

結果為 0。因為 my_function() 返回的值不依賴於呼叫該函式的函式。該函式始終返回全域性變數 x 的值。

更新於:2019 年 7 月 30 日

71 次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.