Is the global variable in C static or?

By default, what is the C static or external global variable?
If global variables are static by default, then it means that we can access them in a single file, but we can also use global variables in different files.
Does this mean they have external by default storage?
If no storage class (i.e. extern or static keyword) is specified, then global variables have external links by default. From the C99 standard:

§6.2.2 Linkages of identifiers

3) If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.

5) If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern. If the declaration of an identifier for an object has file scope and no storage-class specifier , its linkage is external.

Therefore, even if you do not specify the extern keyword, you can still access global variables through other source files (so-called conversion units) because they can still be used for the same variable. Make an extern declaration. If you use the static keyword to specify internal links, even if an extern declaration with the same variable name exists in another source file, it will refer to another variable.

< p>By default, what is the C static or external global variable?
If global variables are static by default, then it means that we can access them in a single file, but we can also use global variables in different files.
Does this mean they have external by default storage?

If the storage class (ie extern or static keyword) is not specified, then the global variable has external links by default. From the C99 standard:

< p>

§6.2.2 Linkages of identifiers

3) If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.

5) If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern. If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.

Therefore, even if you do not specify the extern keyword, you can still access global variables through other source files (so-called conversion units), because they can still make extern declarations for the same variable. If you use the static keyword to specify internal linkage , Then even if there is an extern declaration with the same variable name in another source file, it will refer to another variable.

Leave a Comment

Your email address will not be published.