user2714823
user2714823

Reputation: 605

Declaring a global and a static variable

I was studying about static variable and i came across this code on wikipedia . The static int x stores it value throughout the programme. And even when we declare it globally it will store the previous values. Does this mean static and global variables are treated as same .And if not ,how does the both work ?

#include <stdio.h>

 void func() {
       static int x=0;
        // x is initialized only once across three calls of func()
        printf("%d\n", x); // outputs the value of x
        x = x + 1;
}

 int main(int argc, char *argv[]) {
        func(); // prints 0
        func(); // prints 1
        func(); // prints 2
        return 0;
}

Upvotes: 3

Views: 10843

Answers (7)

Michael Kusch
Michael Kusch

Reputation: 340

The specifier static has a slightly different meaning depending where you use it. You have 3 options to declare a variable whose value you want to be preserved throughout the lifetime of the program:

1) When declaring a variable as static within a function (that is a local variable) means that its value will be preserved between function calls throughout the lifetime of the program. If you initialize the variable to a value, this is the starting value of that variable on the first function call; all subsequent function calls will have the value of the variable preserved instead of re-initialized.

2) When declaring a global variable (without the static specifier), its value is preserved throughout the lifetime of the program as if it were a local static variable, but the difference is that a global variable is visible (to be read and written to) by any function of any file of the program.

3) When declaring a global static variable, its value is again preserved throughout the lifetime of the program, but in contrast to plain global variables, it can only be accessed by other functions within the same file.

In conclusion, all these three types of variables have their value preserved; the difference lies in their scope, meaning which functions have access to this variable.

All 3 variables of the following code preserve their value thoughout the life of the program.

int x = 100;         // visible to any function within any .c file of this program
static int y = 5000;  // visible from any function within this .c
int func (int i)
{
  static int z = 0;      // invisible to all other functions
  x += i;
  y += i;
  z += i;
  return z;
}

A thorough explanation can be found here.

Upvotes: 9

Furquan
Furquan

Reputation: 678

This is a classic question of scope and lifetime of a variable.

Scope of a variable is the span across which that variable can be referenced or is accessible. Scope could be within a particular function or a whole file or a whole program.

On the other hand, lifetime of a variable is how long does the variable hold its space in memory. Lifetime could be limited to a function call or the entire execution of program.

Based on these factors, the location of the variable changes as well. For example, a local variable occupies space on the stack. However, if this local variable has the specifier static, then this variable occupies space in the data section, however the linkage (or visibility) is only within the function where it is declared. So, when the function call is completed, it does not lose out on its current value, but can be accessed only when within the function.

On the other hand, global variable has a scope of the entire file in which it is defined and occupies space in the data section.

So, as you see above, both static and global variables occupy space in the data section, but their linkage to the program is different i.e. function and whole file respectively. Hope that helps!

The best reference for this question would be "The C Programming Language".

Upvotes: 1

JackCColeman
JackCColeman

Reputation: 3807

Have modified your code to have both global and static variables.

#include <stdio.h>

 int z;              // can be accessed by other modules
 static int y;       // can only be used by this source module

 void func() {
       static int x=0;
        // x is initialized only once across three calls of func()
        printf("%d %d\n", x); // outputs the value of x
        x++;
        y++;
}

 int main(int argc, char *argv[]) {
        y = 3;
        func(); // prints: 0 3
        func(); // prints: 1 4
        func(); // prints: 2 5
        return 0;
}

y persists and is accessible from both main and func. x persists but can only be read from func. z can be read from other code in other source modules if they declare: extern int z;.

Upvotes: 2

Jonathan Leffler
Jonathan Leffler

Reputation: 753475

There are similarities between a static variable in a function and a global variable. Both have a lifetime that is the same as that of the program as a whole. This means that any changes made to the static variable in the function are preserved between calls to the function.

The big difference, though, is that the static variable in the function can only be accessed by name from within that function. If the function makes a pointer available to other code, then it can be accessed via the pointer. But otherwise, it is hidden inside the function, and other functions can have a static variable with the same name, and there could be a file scope variable with the same name too (which would be hidden inside the function).

By contrast, a global variable is accessible by name anywhere it is declared — potentially in multiple source files.

Upvotes: 2

TGH
TGH

Reputation: 39248

Global vs local is more about accessibility. Static vs instance variables is different because it deals with how the data is stored, Static data is stored at the type level which means it's shared between all variables of a certain type. Instance variables on the other hand are associated with a specific instance, and unique to the instance.

Upvotes: 0

luser droog
luser droog

Reputation: 19484

Yes, a static variable is stored just like a global variable. Its value will persist throughout the lifetime of the program.

The static keyword also affect the scope of the variable if it's declared outside of a function. The variable cannot be accessed by name from another source file.

Upvotes: 0

nonsensickle
nonsensickle

Reputation: 4528

Yes, they both are pretty much the same. Variables declared static inside a function can only be referenced/used by that function.

This is good for limiting the scope of a variable that only that particular function uses. There is no point in making a global variable that is used by only one function. It makes your program less clear.

Upvotes: 0

Related Questions