- How to spread the visibility of a global variable to two files?
- Can an extern variable be declared in the block to expand the scope of the global variable?
- Is it possible to change the value of a global variable inside a block?
- Can the names of a global variable and a variable defined inside a block match?
- Can multiple files have a global variable with the same name?
Closed due to the fact that the question is too general for the participants αλεχολυτ , user194374, ermak0ff , Harry , Vadizar 5 Mar '17 at 0:41 .
Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .
- onePlease ask one question in one publication (question). - Nicolas Chabanovsky ♦
1 answer
How to spread the visibility of a global variable to two files?
You need to place the declaration of a variable with the extern qualifier in the header file, and include this header in two files with code. In this case, the variable is determined only in one file.
Can an extern variable be declared in the block to expand the scope of the global variable?
In C ++, you can use a qualified variable name by specifying the name of the namespace in front of the variable name, where the variable is declared. For example, if the variable x declared in the global namespace, then you can use the name ::x to refer to it.
You can also declare an external variable in C and C ++ in a code block, supplying the declaration with the extern qualifier.
Is it possible to change the value of a global variable inside a block?
If the variable is not declared with the c / v qualifier, then you can change it.
Can the names of a global variable and a variable defined inside a block match?
As already mentioned, in C ++ you can use a qualified name for a global variable, so it will differ from the name of a local variable. Otherwise, the local variable name will hide the global variable name.
Can multiple files have a global variable with the same name?
If a variable has an internal binding, then in each translation unit this variable will represent a separate object. If it has an external binding, then all variables declared with the same name and external binding will denote the same object.
In the end, an example of a simple program in which the behavior of variables declared with the extern specifier in C and C ++ coincides
#include <iostream> extern int x = 10; int main() { std::cout << "x = " << x << std::endl; { int x = 20; std::cout << "x = " << x << std::endl; { extern int x; x = 30; std::cout << "x = " << x << std::endl; } std::cout << "x = " << x << std::endl; } std::cout << "x = " << x << std::endl; return 0; } Output of the program to the console
x = 10 x = 20 x = 30 x = 20 x = 30 - “If a variable is not declared with a c / v qualifier” - it seems like only c, v should not interfere? - VladD
- @VladD For a volatile variable, as a client, you can read its values, but not assign values. - Vlad from Moscow
- Can any link to any documentation? And then, to my shame, I must confess that I heard about it for the first time. For example, I did not find anything about it. - VladD
- @VladD Usually, how an object with a volatile qualifier changes is unknown to the client using this variable. Therefore, changing this object to the client can have an unpredictable effect. - Vlad from Moscow
- Yeah, now I understand, thanks! - VladD