I just learn about static and extern keywords. Everything make a sense for me BUT there is one thing which doesn't. Most of articles, forums etc wrote that the static keyword change linkage to internal(the static variable or function have only file scope - not visible for other files more precisely compilation unit). I do a test: I have a header file with static int variable. When I include this header in my main.cpp I see this variable so whats going on? So pls guys help me to understand.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//header.h
staticint var; // internal linkage, visible only in this file scope
//main.cpp
#include "header.h"
int main (){
int i = var; // var is visible here even if it static
std::cout << i << std::endl;
}
Hi. Ok thanks for quick replay. BUT there is another question for you:
You said every compilation unit has its own copy of var but when you change your main.cpp to folowing:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "header.h"
#include <iostream>
var = 12; // now I make definition of var here = ERROR REDEFINITION
int main ()
{
// var = 12; used to be here
set_other_var(88);
std::cout << "main var: " << var << std::endl;
std::cout << "other var: " << get_other_var() << std::endl;
}
now I get redefinition error even if, static int var has file scope. How is it possible that in the block of the function is work but outside it doesn't. Can you explain ?
You can never assign to variables outside functions like that so I assume you mean line 4 should be staticint var = 12;.
"file scope" is a bit of a misnomer because the scope extends until the end of the whole translation unit.
A translation unit is the content of a source file + the content of all the headers that it includes. Each translation unit is compiled in separation without knowledge of other translation units. When all translation units has been compiled they can be linked together to produce the executable. That is why if var had been defined without static in my code (giving it external linkage) you would still been able to compile main.cpp and other.cpp, but the linking would fail because var has been defined in multiple translation units.
Ok. And if you want to have a var as global variable ? I mean ok, maybe its a dumb question. You said I can never assign to variables outside function but there are global variables which works like that . What if I want to have static global variable. Pls expend that part of why I cant never assign to variables outside especially if is it static.
And the second thing just make it simple
This is only declaration right ?
1 2 3
// header.h
staticint var;
and watch this
1 2 3 4 5 6 7
// main.cpp
var = 12; // get redefinition error , but it was not defined at all yet ..so what i MISS OMG PLS TELL ME !!
int main(){
....
}
You can always initialize the variable when you define it but if you want to later assign another value to it you would have to do that inside a function.
Static global variables are mostly used for constants (const have internal linkage by default). You cannot modify a constant so you normally not notice that each translation unit has its own copy.
Mutable global variables are usually considered bad practice, but if you insist you can declare them with the extern keyword in the header and then define them as normal inside a source file. This ensures the variables are only defined in one translation unit.