Static and global

If I have a C file as shown below, what is the difference between i and j ?

 #include <stdio.h> #include <stdlib.h> static int i; int j; int main () { //Some implementation } 
+45
c static global
Feb 16 '10 at 9:52
source share
4 answers

i has an internal connection, so you cannot use the name i in other source files (strictly translation units) to refer to the same object.

j has an external connection, so you can use j to refer to this object if you declare it extern in another translation unit.

+41
Feb 16 2018-10-16
source share

i not displayed outside the module; j is available worldwide.

That is, another module that is associated with it can execute

 extern int j; 

and then you can read and write the value to j . The same other module cannot access i , but can declare its own instance, even a global one, which is not displayed for the first module.

+16
Feb 16 '10 at 9:55
source share

The difference is that i has an internal connection, and j has an external connection. This means that you can access j from other files that you link to, while i is only available in the file where it is declared.

+3
Feb 16 '10 at 9:55
source share

i will have a static connection , i.e. the variable is available only in the current file.

j should be defined as extern , i.e.

 extern int j; 

in another header file ( .h ), and then it will have an external link and can be accessed through files.

+3
Feb 16 '10 at 10:02
source share



All Articles