Reputation: 4633
If I write something like this:
#include <iostream>
int main()
{
using namespace std;
{int n;n=5;} cout<<n;
system("pause");
return 0;
}
The compiler tells me that n is undeclared. Then I tried making it static, but again, the compiler tells me that it is undeclared. Doesn't a variable declated static have program scope? If not, how do I use n in this program?
Upvotes: 7
Views: 15268
Reputation: 341
Consider below example for global static scope in terms of accessibility
#include <iostream>
using namespace std;
static int y;
class A {
public:
void increment() {
++y;
}
};
class B {
public:
void increment() {
++y;
}
};
int main()
{
A a;
a.increment();
cout << y << endl;
A b;
b.increment();
cout << y << endl;
B c;
c.increment();
cout << y;
return 0;
}
1 2 3
Here global static variable access is within both class A and B.
Consider below example for class static scope in terms of accessibility
#include <iostream>
using namespace std;
class A {
public:
static int y;
void increment() {
++y;
}
};
class B {
public:
static int x;
void increment() {
++x;
}
};
int A::y = 1;
int B::x = 1;
int main()
{
A a;
a.increment();
cout << a.y << endl;
A b;
b.increment();
cout << b.y << endl;
B c;
c.increment();
cout << c.x;
return 0;
}
2 3 2
Here static variable y scope is with class A and x scope is with class B. If you will try to access static variable y with class B object then it will return error. (B b -> b.y)
Life time of both static variable x and y remains till the main ends.
Upvotes: 0
Reputation: 1
Please don't be confuse between scope and life time of a static variable. Scope means where can you access the variable. Life time of variable is duration for which variable stay in memory. In your case, Scope of x varible is within the curly braces. Life time of x would be program scope.
Upvotes: 0
Reputation: 165
Here the compiler gives error n is undeclared because here "{int n;n=5;}" it is declared in the braces. And braces tells us about the scope of the variable. When ever the scope ends the variable is deleted from the memory.
And for Static and local.
Static : The variable is same like global variable but its value remains constant throughout the application. And the static variable cannot be used on the other page using extern.
Local : The local variables are stored in the stack and they are deleted when they get out of scope.
Upvotes: 1
Reputation: 3180
A variable declared static in the global scope has its scope limited to the translation unit. A variable declared static within a function has its lifetime set to be the same as the program's, but in this case does not affect its scope. You will have to put cout
in the same scope as n
was declared in order to use it.
Upvotes: 1
Reputation: 328
The scope of n is just between the brackets:
{int n;n=5;}
so outside of the block, you have no n variable.
Making it static just makes it's value retain even after you exit the block so that the next time you enter that block again, you can retrieve it's value from the last time you executed that block, but still it's scope is still within the brackets.
Upvotes: 7
Reputation: 64672
how do I use n in this program?
using namespace std;
int main()
{
int n; // declare n as int
n=5; // assign it a value
cout << n; // display it.
system("pause");
return 0;
}
Upvotes: 0
Reputation: 103693
You're confusing scope with lifetime. Static variables have a lifetime equal to the program's lifetime, but they still follow scoping rules based on where they are declared.
Upvotes: 22