SOLVED - Static global variable

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
What would be the advantage of using a static storage class in front of a global variable in a C program?

A global variable is a variable that can be accessed in the source file where it is declared.

static variable is a variable that indicates the lifetime of the variable

C:
static int variable_name;

int main ( ) {
......
}
It is advantage to use static storage class inside a function.

I followed several tutorials on websites and youtube but I don't understand what can be the advantage of using static storage classes in front of global variables. Do you use static global variables in your program?
 

Dave Lowther

Joined Sep 8, 2016
138
1638964124177.png

The advantage is that it can be used to hide the variable from other C files, prevent unintentional access to the variable from other files and if you did have the same variable name used in two or more different C files they would be separate variables. I did use this in the past before I started using C++ classes.
 

ApacheKid

Joined Jan 12, 2015
618
What would be the advantage of using a static storage class in front of a global variable in a C program?

A global variable is a variable that can be accessed in the source file where it is declared.

static variable is a variable that indicates the lifetime of the variable

C:
static int variable_name;

int main ( ) {
......
}
It is advantage to use static storage class inside a function.

I followed several tutorials on websites and youtube but I don't understand what can be the advantage of using static storage classes in front of global variables. Do you use static global variables in your program?
C and C++ let you declare local static variables. Thus if the variable is modified during the execution of the function the value remains and will be the value seen next time the function gets invoked. As Dave above says the variable is "hidden" and there's no risk of it being unexpectedly changed (ordinarily anyway) by other functions.

Static variables declared outside of any functions can be seen by any function at any time, if not also declared "const" it can be changed too.

Use them with caution, even simple software can behave non intuitively when static variables are being used, especially in cases where there are multiple threads of execution.
 

MrChips

Joined Oct 2, 2009
25,931
As Dave says, if you have other files using the same global variable name there could be some unexpected consequences.
 

ApacheKid

Joined Jan 12, 2015
618
That's what confuses me, I don't understand any difference between a global variable and a static global variable . Both have the same lifetime and scope. I don't think there is any advantage to write static storage class in front of global variable.
Show us some actual code declarations, if something is "global" then by definition it (in some way other) is also static. Static variables reside in a static "region" that's built (usually) when code is linked, the addresses of static or global anythings are fixed at link time (that's where the term "static" originates, actually the term was first used in this way in the PL/1 language).

If you post actual code (that is show what you mean by static vs global) examples it will be easier to discuss, I'm no C++ expert myself so can't speak with great confidence about the conventions used in that language.
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
Show us some actual code declarations, if something is "global" then by definition it (in some way other) is also static. Static variables reside in a static "region" that's built (usually) when code is linked, the addresses of static or global anythings are fixed at link time (that's where the term "static" originates, actually the term was first used in this way in the PL/1 language).

If you post actual code (that is show what you mean by static vs global) examples it will be easier to discuss, I'm no C++ expert myself so can't speak with great confidence about the conventions used in that language.
The name of this source file is main.c
C:
#include <stdio.h>

static int x;

void foo()
{
  x = 0;
  x++;
  printf("%d\n", x);  // outputs the value of x
}

int main() {
  foo();  // prints 1
  foo();  // prints 1
  foo();  // prints 1

  return 0;
}
This is a program with x static global variables
it gives output 1 1 1

even if i remove the static storage class from the program i still get the same output 1 1 1

That's why I don't see any difference between global variable and static global variable.
 

michael8

Joined Jan 11, 2015
278
A static global variable is NOT included in the external linkage information for that source module, while non-static global variables are included. So some other source module which is linked with this one can "see" (and possibly change) a regular global variable. A static global variable is invisible (the name isn't defined outside this module).

This is similar to the static function definiition.
 

ApacheKid

Joined Jan 12, 2015
618
The name of this source file is main.c
C:
#include <stdio.h>

static int x;

void foo()
{
  x = 0;
  x++;
  printf("%d\n", x);  // outputs the value of x
}

int main() {
  foo();  // prints 1
  foo();  // prints 1
  foo();  // prints 1

  return 0;
}
This is a program with x static global variables
it gives output 1 1 1

even if i remove the static storage class from the program i still get the same output 1 1 1

That's why I don't see any difference between global variable and static global variable.
Well the function foo begins by assigning zero to x, then increments it and prints it, that will always print simply 1 no matter how often it is called.

The x exists as static (as opposed to stack/heap) and is accessible to any function in the source file. If foo did not reset it to zero each time you'd get 1 then 2 then 3.

Consider these two alternatives:

C:
#include <stdio.h>

void foo()
{
  static int x = 0;  // this will actually set x (the storage where x is) to zero before the program even starts.
  x++;
  printf("%d\n", x);  // outputs the value of x
}

int main() {
  foo();  // prints 1
  foo();  // prints 1
  foo();  // prints 1

  return 0;
}
and

C:
#include <stdio.h>

void foo()
{
  int x = 0;
  x = 0;
  x++;
  printf("%d\n", x);  // outputs the value of x
}

int main() {
  foo();  // prints 1
  foo();  // prints 1
  foo();  // prints 1

  return 0;
}
Run each of them and see what happens.
 
Top