Static variable defined globally

Feb 22, 2012 at 5:10pm
Hi,
im a beginner in C++. if a static variable is defined globally , what will be the scope of the variable in any of the functions used ... will the scope of the variable will be local to that function?
Feb 22, 2012 at 8:55pm
defined globally
then global scope, if you define a variable with the same name in any scope, the variable that is in scope will be used instead.
Feb 22, 2012 at 9:04pm
Unless you specifically use the global scope.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

const int number = 10;

using std::cout;
using std::endl;

int main()
{
    int number = 5;
    cout << number << endl;
    cout << ::number << endl;
    return 0;
}

Output:
5
10
Last edited on Feb 22, 2012 at 9:05pm
Feb 22, 2012 at 9:19pm
Globally defined variables belong to the global namespace. They will be visible if in any inner scope variables with the same names will not be declared.
Feb 22, 2012 at 9:21pm
hidden != overridden
There are methods to access things in a specific scope, even if you overrode them.
Feb 22, 2012 at 9:44pm
Unless you specifically use the global scope.
Touche, had to use this today in some nested namespace work in a library I'm working on.
Last edited on Feb 22, 2012 at 9:45pm
Feb 23, 2012 at 5:37pm
Thanks a lot guys for helping me ..

I have a doubt here.


#include<iostream>
using namespace std;
static int x=5;
void function1()
{
x=25;
cout<< x;
}

int main()
{
x=10;
cout<< x;
function1();
cout<<x;
}

In the above example, i declare variable x as static int globally.
I declare x=10 in main , now the scope of the variable is in main function , it will be showing x=10.
now i call function1() , and modify the value of x and its scope in function1 is x=25.
when i return back to the main function and output the value of x, i see x=25.

my question is " what is the use of defining a variable as static which has been declared globally..even global variable has a lifetime until the end of the program"

Feb 23, 2012 at 5:59pm
static for global variables/functions means something very different.

If a global variable is static, it means that is not externally linked. It's difficlut to explain in words... so here's an example:

1
2
3
4
//one.cpp
int a;
static int b;
int c;

1
2
3
4
//two.cpp
extern int a;
static int b;
int c;


Here, 'a' is a variable that exists in both cpp files. Changes made to 'a' in one.cpp will be visible in two.cpp because they both use the same var. That var is instantiated in one.cpp, and the 'extern' keyword in two.cpp tells it to look for a variable that has already been instantiated elsewhere (without instantiating it again)

'b' is declared as static, therefore one.cpp and two.cpp have their own copy of the 'b' variable. Changes made to 'b' in one.cpp will not be visible in two.cpp and vice versa because they are both accessing different vars.

'c' will cause a linker error because the same variable is being instantiated multiple times, and the linker doesn't know which variable you wanted to use.
Feb 23, 2012 at 6:02pm
So it doesn't pollute the project.
When you've got several cpp and declare global variables in them.

I declare x=10 in main
No, you assign a value to the global variable.
Topic archived. No new replies allowed.