All
Variable Types in C++
- Data types are the entities that define which/what type of data we are currently working int upon, whether it is an integer, float(decimal), or character.
- Variables are containers with a unique name/id that along with the data types store information
#include <iostream>
using namespace std;
int main(){
int myvar = 50;
}
- As we can see in the above program int here refers to the integer data type, myvar refers to our variable name and 50 is the value that we have assigned to our variable
-----------------------------------------------------------------------------------------------------------
Now we will look into types of variables extensively:
- Auto variables: These are simple variables and their scope is limited to the function in which they were defined. Even if we do not write auto, the compiler assumes it to be an automatic variable.So from now on we won't be explicitly writing auto to declare variable names.
This is the syntax of C language :
This is the syntax of the C++ language :
#include <iostream>
using namespace std;
int main()
{
// same as writing int a = 20;
auto a = 20;
cout<<a<<endl;
return 0;
}
___________________________________________________________________
Output:
20
- Global variables: These variables have a global scope in our program and can be used throughout our program and their scope is not restricted as they are defined outside our main function. Global variables that have not been assigned any value are by default assigned the value as 0 by the compiler.
#include <iostream>
using namespace std;
// global variables a and b
int a;
int b = 30;
int main()
{
cout<<"Value of a is "<<a<<endl;
cout<<"Value of b is "<<b<<endl;
}
___________________________________________________________________
Output:
Value of a is 0
Value of b is 30
- Static variables: These variables are pretty similar to global variables bu the only difference is that the static variables do not have a global scope just like global variables only a single copy of these variables is maintained in the system. The difference between auto and static variables can be illustrated with the following example:
Output when using conventional(auto) variables:
#include<iostream>
using namespace std;
void fun(){
int a = 1;
a = a+1;
cout<<a<<endl;
}
int main()
{
fun();
fun();
fun();
return 0;
}
___________________________________________________________________
Output:
2
2
2
Output when using static variables:
#include<iostream>
using namespace std;
void fun(){
static int a = 1;
a = a+1;
cout<<a<<endl;
}
int main()
{
fun();
fun();
fun();
return 0;
}
___________________________________________________________________
Output:
2
3
4
- As we can see, we made a function here that increments the value of variable 'a'. Now when we use the auto variable the variable after the function was called was removed from the system's memory so as a result every time we called the function a new copy of the variable 'a' was created.
- Whereas since static variables are created only once in the system's memory we get an incremented value of a after each function call as the same copy of the variables is incremented every time.
- Register variables: These variables are created to assign them to the register memory in the system so that we can have faster access to these variables. Such variables are declared to give them a priority for quicker access. Here is an example of how we can declare register variables:
#include<iostream>
using namespace std;
int main()
{
register int a = 10;
cout<<a<<endl;
return 0;
}
___________________________________________________________________
Output:
10
- External variables: These variables are created when we are working with different files and when we have to import a variable that we created in one file to another.
- Constant variables: These values stored in these variables are permanent and cannot be modified hence these variables once defined cannot be modified again. These variables are declared using the const keyword. Here we can see an example of a constant variable:
#include<iostream>
using namespace std;
int main()
{
const int a = 30;
a = 45;
cout<<a<<endl;
return 0;
}
___________________________________________________________________
Output:
error: cannot assign to variable 'a' with const-qualified type '
const int'
a = 45;
~ ^
basics.cpp:5:15: note: variable 'a' declared const here
const int a = 30;
~~~~~~~~~~^~~~~~
- The above example clearly shows us that constant variables once defined cannot be modified.
0 Comments