Variables in C++
Variables and their Types
In programming, a variable is a value that can change, depending on conditions or on information passed to the program. Typically, a program consists of instruction s that tell the computer what to do and data that the program uses when it is running.
For example: num=20;
How to Declare Variable in C++ SYNTAX?
datatype variable_name = value;
For example:
int number= 10;
Multi Variable Declaration
datatype variable_name1 = value1 ,variable_name2=value2;
For example:
int number1= 10 , number2 =12;
Types of Variable
Types
|
Length
|
Range
|
unsigned
char
|
8 bits
|
0 to 255
|
char
|
8 bits
|
-128 to 127
|
unsigned
int
|
32 bits
|
0 to 4,294,967,295
|
short int
|
16 bits
|
-32,768 to 32,767
|
int
|
32 bits
|
-2,147,283,648 to
2,147,283,647
|
unsigned
long
|
32 bits
|
0 to 4,294,967,295
|
enum
|
16 bits
|
-2,147,283,648 to
2,147,283,647
|
long
|
32 bits
|
-2,147,283,648 to 2,147,283,647
|
float
|
32 bits
|
3.4 x 10^-38 to 3.4 x
10^ +38
|
double
|
64 bits
|
1.7 x 10^-308 to 1.7 x 10^ +308
|
long double
|
80 bits
|
3.4 x 10^4932 to 3.4 x
10^ +4932
|
far
|
32 bits
|
Types of variables based of their Scope:
Variable scope is a location in a program where the value of the variable is available. Every variable has its scope. According to scope there are two types of variables in C++ local and global.
Local variable is a variable declared within a function. The scope of local variables is the body of the function, in which the variable is declared. Local variable can be initialized by a constant or an expression corresponding to its type.
Global variable is a variable declared beyond all functions. The scope of global variables is the entire program. A global variable can be initialized only by a constant corresponding to its type (and not expression). Global variables are initialized only once before stating the execution of special functions.
Local variable is a variable declared within a function. The scope of local variables is the body of the function, in which the variable is declared. Local variable can be initialized by a constant or an expression corresponding to its type.
Global variable is a variable declared beyond all functions. The scope of global variables is the entire program. A global variable can be initialized only by a constant corresponding to its type (and not expression). Global variables are initialized only once before stating the execution of special functions.
Example of Global variables:
#include<iostream>
using namepsace std;
int value= 10;
int main(){
int value2 =20;
cout<< value <<"\n"<< value2 <<endl;
return 0;
}
Output:
10 12
Example of local Variables
#include<iostream>
using namespace std;
int funct(){
int val =10;
}
int main(){
cout << val<<endl;
return 0;
}
Output
Compile time error, because we are trying to access the variable val outside of its scope. The scope of val is limited to the body of function Funcnt(), inside those braces.using namespace std;
int funct(){
int val =10;
}
int main(){
cout << val<<endl;
return 0;
}
Post a Comment