Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global variables in C are static or not?

Are global variables in C static or extern by default?
If global variables are by default static then it means that we would be able to access them in a single file, but we can use global variables in different files as well.
Does this imply that they have extern storage by default?

like image 699
Mishthi Avatar asked Nov 21 '10 19:11

Mishthi


People also ask

Are global variables static in C?

In C, a global variable which doesn't have an initializer or any storage class specifiers is a tentative definition of a variable with static storage duration and external linkage.

Is global variable always static?

If global variable is to be visible within only one . c file, you should declare it static. If global variable is to be used across multiple . c files, you should not declare it static.

Are variables static in C?

Static variables are initialized only once. The compiler persists with the variable till the end of the program. Static variables can be defined inside or outside the function.

What is global variable in C?

The variables that are declared outside the given function are known as global variables. These do not stay limited to a specific function- which means that one can use any given function to not only access but also modify the global variables.


3 Answers

If you do not specify a storage class (that is, the extern or static keywords), then by default global variables have external linkage. 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.

So even if you don't specify the extern keyword, globals can still be accessed by other source files (so-called translation units), because they can still have an extern declaration for the same variable. If you use the static keyword to specify internal linkage, then even in the presence of an extern declaration for the same variable name in another source file, it will refer to a different variable.

like image 199
Adam Rosenfield Avatar answered Oct 04 '22 08:10

Adam Rosenfield


In C, a global variable which doesn't have an initializer or any storage class specifiers is a tentative definition of a variable with static storage duration and external linkage.

In a translation unit all tentative definitions and up to one non-tentative definition (e.g. from a declaration with an initializer) are collapsed into a single definition for a variable. Although it's not allowed to have a definition of the same variable in multiple translation units it is a common extension to allow "common" variables, i.e. tentative definitions of the same variable in multiple translation units.

like image 24
CB Bailey Avatar answered Oct 04 '22 08:10

CB Bailey


Global variables in C are by default extern.. (i.e) they have external linkage..

To restrict the external linkage, 'static' storage class specifier can be used for the global variable.. if static specifier is used, then the variable has file scope.. You cannot link it in an other file using the 'extern' keyword..

Specifying 'static' depends on your usage of the program..

like image 25
Raghu Srikanth Reddy Avatar answered Oct 04 '22 07:10

Raghu Srikanth Reddy