Difference between 'global' and 'static global'

后端 未结 7 1323
野性不改
野性不改 2020-12-12 15:44

A global variable\'s scope is in all the files, while a static global variable\'s scope is just the file where it is declared. Why

7条回答
  •  没有蜡笔的小新
    2020-12-12 16:13

    We use static attribute to hide variable and function declarations inside modules, much as we would use public and private declarations in Java and C++. C source files play the role of modules. Any global variable or function declared with the static attribute is private to that module. Similarly, any global variable or function declared without the static attribute is public and can be accesses by any other module. Though it is a good practice to define as variables or functions with the 'global' keyword.

    Both these variables are stored in .data or .bss section of a relocatable file.

提交回复
热议问题