Since we learned basic input function at programming with C++, we need to talk a little more about the variables.
Variables are simply naming some data location in your pc. With variables you can store a value.
Variables can be global, local or block based. Actually, you can use the same variable name in these locations at the same time.
- #include <iostream>
- using namespace std;
- int sameVar=1; //defining a global variable at header location
- int main(){
- int sameVar=2; //defining a local variable inside int main
- cout<<“local variable:”<<sameVar; //printing a local variable in int main
- {
- int sameVar=3; //here we define a variable in any block.
- cout<<“block variable:”<<sameVar; //printing the block variable
- }
- //reaching a global variable inside program
- cout<<“global variable:”<<::sameVar; //we print global variable in a program
- }
Output;
Here the most important thing we need to talk is the
variable = value logic.
Always variable is on the left and the value is on the right of the equivalent sign.