VARIABLES IN C++
A variable is a name which is associated with a value that can be changed. For example when I write
int num=20; here variable name is num which is associated with value 20, int is a data type that
represents that this variable can hold integer values.
Syntax of declaring a variable in C++
data_type
For example:
int num1=20, num2=100;
We can also write it like this:
int num1,num2;
num1=20;
num2=100;
TYPES OF VARIABLES
Variables can be categorized based on their data type. For example, in the above example we have seen
integer type variables. Following are the types of variables available in C++.
int: These type of variables holds integer value.
char: holds character value like ‘c’, ‘F’, ‘B’, ‘p’, ‘q’ etc.
bool: holds boolean value true or false.
double: double-precision floating point value.
float: Single-precision floating point value.
TYPES OF VARIABLES BASED ON THEIR SCOPE
Before going further let’s discuss what is scope first. When we discussed the Hello World Program, we
have seen the curly braces in the program like this:
Any variable declared inside these curly braces have scope limited
int main { within these curly braces, if you declare a variable in main() functionand
try to use that variable outside main() function then you will get
//Some code compilation error.
}
Now that we have understood what is scope. Let’s move on to the types of variables based on the scope.
These are the:
A variable is a name which is associated with a value that can be changed. For example when I write
int num=20; here variable name is num which is associated with value 20, int is a data type that
represents that this variable can hold integer values.
Syntax of declaring a variable in C++
data_type
For example:
int num1=20, num2=100;
We can also write it like this:
int num1,num2;
num1=20;
num2=100;
TYPES OF VARIABLES
Variables can be categorized based on their data type. For example, in the above example we have seen
integer type variables. Following are the types of variables available in C++.
int: These type of variables holds integer value.
char: holds character value like ‘c’, ‘F’, ‘B’, ‘p’, ‘q’ etc.
bool: holds boolean value true or false.
double: double-precision floating point value.
float: Single-precision floating point value.
TYPES OF VARIABLES BASED ON THEIR SCOPE
Before going further let’s discuss what is scope first. When we discussed the Hello World Program, we
have seen the curly braces in the program like this:
Any variable declared inside these curly braces have scope limited
int main { within these curly braces, if you declare a variable in main() functionand
try to use that variable outside main() function then you will get
//Some code compilation error.
}
Now that we have understood what is scope. Let’s move on to the types of variables based on the scope.
These are the: