What is difference between auto global and static global variable

Thread Starter

@vajra

Joined May 2, 2018
150
Hi forum

I do not understand difference between between global and static global variable. I know global variable can be access anywhere in program where as local variable is only accessible in the function and static is storage class
C:
#include <stdio.h>

int x = 0;

static int y= 0;
 
void foo()
{
   static int z = 0;
      printf("%d \n", z);
}
 
int main()
{
    foo();
    foo();
    
    printf("%d \n", x);

    printf("%d \n", y);
  
    return 0;
}
In my program, x is global variable and y is static global variable I do not understand when I will need a static variable because I don't understand much difference between auto global and static global variable. they both will exit until program run. They both are accessible anywhere in program
 

WBahn

Joined Mar 31, 2012
25,224
The keyword "static" is used in two very different ways in C, which I find very unfortunate as it leads to needless confusion since people almost inevitably learn what it means in one use case and then, quite reasonably, assume that it means the same thing in the other.

When used with a local variable, it distinguishes between automatic and static storage, thus controlling the lifetime (i.e., storage duration) of the local variable. But when used with a function or a global variable it means something quite different. In that case, it restricts visibility of the function or variable to the containing file, thus controlling the scope (i.e., storage class) of the function or global variable.
 

Thread Starter

@vajra

Joined May 2, 2018
150
When used with a local variable, it distinguishes between automatic and static storage, thus controlling the lifetime (i.e., storage duration) of the local variable. But when used with a function or a global variable it means something quite different. In that case, it restricts visibility of the function or variable to the containing file, thus controlling the scope (i.e., storage class) of the function or global variable.
Variable duration means as long as it stays alive and Scope shows where its available to use. The static variable and the global variable remain alive as long as the program runs

In code, both X and Z has the same life time. they remain alive as long as the program run and we can access both from anywhere. I do not understand theirs difference
 

WBahn

Joined Mar 31, 2012
25,224
Variable duration means as long as it stays alive and Scope shows where its available to use. The static variable and the global variable remain alive as long as the program runs

In code, both X and Z has the same life time. they remain alive as long as the program run and we can access both from anywhere. I do not understand theirs difference
What are X and Z? I can't comment on their lifetime or their scope without knowing what they are and since I'm not a mind reader and my crystal ball is in the shop you need to tell me what X and Z are.

But what did I say about the effect of the static modifier on global variables and functions?

I said, "But when used with a function or a global variable it means something quite different. In that case, it restricts visibility of the function or variable to the containing file, thus controlling the scope (i.e., storage class) of the function or global variable."

So a static global variable is NOT accessible from anywhere, it is only accessible from within the file in which it was declared.

Have you even tried to Google something like: static global variable in C and reading what comes up on the results page?
 
Top