Variable:
- When we run the program, memory will be allocated to store the information of that program.
- We can give identities to memory locations to store data called “Variables”
- Variable is a “Named memory location”
- The value stored in a variable can be changed during program execution.
- A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.
- Variables must be declared before its usage in the program.
Syntax: We can declare the variables in many ways as show below
data_type identity; // declaration
data_type identity = value ; // definition
data_type identity;
identity=value; //assignment
Declaring multiple variables:
data_type var1, var2, var3;
Rules for defining variables:
- A variable can have alphabets, digits and underscore.
- A variable name can start with alphabet and underscore only. It can’t start with digit.
- No white space is allowed within variable name.
- A variable name must not be any reserved word or keyword e.g. char, float etc.
- Valid variable names:
- int a;
- int _ab;
- int a30;
- Invalid variable names:
- int 4;
- int x y;
- int double;
Simple code to execute:
#include <iostream>
using namespace std;
int main()
{
int a;
a = 15;
cout << a;
return 0;
}
Types of variables: Variables classified into
- Local Variables
- Global variables
- Instance Variables
- Static Variables
Local Variables:
- A variable defined within a block or method or constructor is called local variable.
- These variables are created when the block in entered or the function is called and destroyed after exiting from the block.
- The scope of these variables exists only within the block in which the variable is declared. i.e. we can access these variable only within that block.
- Initialization of Local Variable is Mandatory.
Global Variables: Global variables are declared outside the functions. Any functions i.e. both local function and global function can change the value of global variables.
int y = 10; //global variable
int main()
{
int x = 5; //local variable
}
Instance Variables:
- Instance variables are non-static variables and are declared in a class outside any method, constructor or block.
- Unlike local variables, we may use access specifiers for instance variables. If we do not specify any access specifier then the default access specifier will be used.
- Initialization of Instance Variable is not Mandatory.
- Instance Variable can be accessed only by creating objects.
Static Variables:
- Static variables are also known as Class variables.
- These variables are declared similarly as instance variables, the difference is that static variables are declared using the static keyword within a class outside any method constructor or block.
- Unlike instance variables, we can only have one copy of a static variable per class irrespective of how many objects we create.
- Static variables are created at the start of program execution and destroyed automatically when execution ends.
- Initialization of Static Variable is not Mandatory. Its default value is 0
Syntax for static and instance variables:
class Example
{
static int a; // static variable
int b; // instance variable
}