8

In a typical C program, both static variable and global variable reside on the data segment. Still the scope of static variable is limited to file. On the contrary, the global variable can be accessed from anywhere. Why does it happen, although both reside in the same memory?

Varaquilex
  • 3,447
  • 7
  • 40
  • 60
dexterous
  • 6,422
  • 12
  • 51
  • 99
  • That's the way it was defined, way back in the mists of time. The developers of C didn't want to waste a new keyword on "local" or "nonglobal" or whatever, so they recycled "static". – Hot Licks Sep 03 '13 at 17:43
  • 1
    The term "variable" is not very well defined in C. In fact, it's not used at all in the normative text of the section "Language". There are objects (regions of storage) and identifiers (names). The problem here is: an identifier has scope and linkage, an object has a storage duration. `static` doesn't change the storage duration of an object (for "global variables"), it just changes the linkage of the identifier. – dyp Sep 03 '13 at 18:04

4 Answers4

9

By design.

static at global scope is the keyword you use to mean "I want these variables limited in scope; I do not want to have to care what other modules have declared variables of the same name." The reason using this keyword does a different thing to not using it is in fact exactly the reason for its existence.

Note the keyword means different things in different contexts; at function scope static means "the contents of this variable should persist between function calls".

The actual arrangement of data in memory that results is an implementation detail, and will vary between compilers and platforms.

moonshadow
  • 86,889
  • 7
  • 82
  • 122
3
Why does it happen, although both resides in the same memory?

Short answer:-

From the C11 standard ( 6.2.2 Linkages of identifiers) para 4 :

If the declaration of a file scope identifier for an object or a function contains the storageclass specifier static, the identifier has internal linkage.

Internal linkage means that its visible only inside its translational unit.

Detailed answer:

A global variable(without static) has external linkage which means it is visible to other translational units.

When you declare static variables with file scope it has internal linkage but when you declare it with block scope it has no linkage.

Lets understand few terms specifically .( inspired from C keywords (static))

A C variable has one of the following linkages:

  • no linkage :- Variables with block scope have no linkage.It means they are private to the block in which they are defined . All variables with automatic, thread and dynamic storage durations have this linkage, as well as variables declared static at block scope. A variable with file scope can have either internal or external linkage.
  • internal linkage :- The variable can be referred to from all scopes in the current translation unit. All variables which are declared at file scope have this linkage, including variables declared static at file scope.
  • external linkage :- The variable can be referred to from any other translation units in the entire program. All variables which are declared either extern or const with no explicit storage-class specifier, but not static, have this linkage.

e.g-

int i = 5; // file scope, external linkage
static int j = 3; // file scope, internal linkage
...
...
int main()
{
...
...
}
int func ()
{
static int num;// block scope – no linkage
. . .
}

By declaring a variable static on file level (static within function has a different meaning) you forbid other units to access it, e.g. if you try to use the variable inside another unit (declared with extern), linker won't find this symbol.

Emphasis mine :)

0decimal0
  • 3,884
  • 2
  • 24
  • 39
2

The idea of static variables is that they're not global and don't 'pollute' the global namespace. It means I can use the variable 'count' in 5 different functions and they won't clash. Scope confines variables to their context.

Cramer
  • 1,785
  • 1
  • 12
  • 20
1

Technically you can access anything. The compiler just wouldn't give you a memory address - you would have to get it yourself. Somehow.

keltar
  • 17,711
  • 2
  • 37
  • 42
  • Technically, whether you can access “everything” is implementation dependent. Not all C implementations use a simple, flat memory model. And, even in simple memory models, not all objects in the C computation model exist in the final implementation after optimization. – Eric Postpischil Sep 03 '13 at 11:34