Is it possible to use extern static. I have wrote a program where i used extern static. This was firing a compiler error. But the same i compiled in another compiler but it compiled successfully.
Can anyone please tell whether it is right to use extern static?
Is it possible to use static exter ?....
storage type 'extern' means the variable declared in another file.
storage type 'static' means the value of the variable is static with respect to the scope of the variable. When the program reenter the scope you can retrieve the value. The scope can a function or a file or a class. For example if you define at the top of a fle
staticint i=9;
every time you enter that file you can retrieve i=9. It is like global
now if you want to use these together, the you need declare seperately
For example:
In h1.h file you declare
externint x; // this means actual x is defined somewhere else
Then in h2.h file you declare
1 2 3
#include "h1.h"
staticint x=25; // this means whereever we include h2.h the value for x
// can be retrieved
aEx.cpp
1 2 3 4 5 6 7 8 9 10
#include "h2.h"
#include <iostream>
using std::cout;
int display(){
cout<<"x="<<x<<"\n";
x++;
return 0;
}
main.cpp
1 2 3 4 5 6 7
int main(){
display();
display();
display();
return 0;
}