Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is default storage class for global variables?

What is default storage class of a global variable?

While searching on web I found, some sites say it is static. But, static means internal linkage and the variable can not be available outside the file scope i.e it should not be available to other object files. But, they still can be accessed to other files using declarations like extern int i.

And, if I explicitly mention static to global variable then it is not available outside the file scope.

Then, what is correct default storage class for the global variables?

like image 510
Vinod T. Patil Avatar asked Jul 19 '10 14:07

Vinod T. Patil


People also ask

Is global is a storage class?

There's no "default storage class" for what is commonly known as "global" variables. When a variable is defined in namespace scope it always has static storage duration.

Which is the default storage class in C?

auto: This is the default storage class for all the variables declared inside a function or a block. Hence, the keyword auto is rarely used while writing programs in C language.

In which storage class the scope of the variable is global?

In this tutorial, you will learn about scope and lifetime of local and global variables. Also, you will learn about static and register variables.


1 Answers

The default storage duration is static, but default linkage is external. You're not the only one to find it a bit confusing. The C Book (always a good reference) says:

"You'll probably find the interactions between these various elements to be both complex and confusing: that's because they are!"

The section with that quote, Declarations, Definitions and Accessibility, has a helpful table (8.1). The last row describes the case you're interested in. As it notes, data objects with no storage class specifier have external linkage and static duration.

like image 191
Matthew Flaschen Avatar answered Oct 19 '22 21:10

Matthew Flaschen