A variable is an identifier that refers to the data item stored at a particular memory location. This data item can be accessed in the program simply by using the variable name. The value of a variable can be changed by assigning different values to it at various places in a program.
A variable name should be carefully chosen by the programmer so that its use is reflected in a useful way in the entire program. Variable names are case sensitive.
Examples of variable names are: Sun, number, Salary, Emp_name, average etc.
Any variable declared in a program should confirm to the following points:
1. They must always begin with a letter, although some systems permit underscore as the first character.
2. The length of a variable must not be more than 8 characters.
3. White space is not allowed and
4. A variable should not be a Keyword
5. It should not contain any special characters.
Examples of Invalid Variable names are: 123, (area), 6th, %abc etc.
We’ll be covering the following topics in this tutorial:
Declaration of Variables
Definition: “Variables are those quantities whose value can vary during the execution of the program”
Variables must be declared in a program before they are used. Variables can be declared at the start of any block of code, but most are found at the start of each function. Most local variables are created when the function is called, and are destroyed on return from that function Variables uses the primary storage area.
Variables are basically memory locations which are given names and these locations are refereed in the program by variable names to read and write data in it. Variables are used for identification for entering the information or data. In variable you can enter integer value or real or character value (string value) according to the requirement. So we can say that variable are either of integers or float type or string type according to the data type used in C++ programming. Variables are used in C++ programs for reading data from the keyboard or from the file, process some formula and printing the information. In C++ language variables have no length for declaration, but some compilers allow either 31 character or 8 character long variables. It contains the name of a valid identifier. The syntax for declaring a variable is
data_type variable_name;
For example, a variable a of type int can be declared using this statement.
int a;
At the time of the variable declaration, more than one variable of the same data type can be declared in a single statement. For example, consider this statement.
int x, y, z;
Note that in C++, it is not necessary to declare the variables in the beginning of a program as required in C. It can be declared at the place where it is used first.
Initialization of Variables
Declaration of variables allocates sufficient memory for variables. However, it does not store any data in the variables. To store data in variables, they need to be initialized at the time of declaration. For example, consider this statement.
int i=10;
In this statement, a variable i of the integer type is declared and initialized with a value of 10. We can assign a value to a variable by using assignment operator (=).
Variables can either be initialized at the compile-time or at the run-time initialization at the compile-time using a constant is known as static initialization. However, variables can also be initialized at the run-time using expressions initialization of variables at the run-time is known as dynamic initialization.
To understand the concept of static initialization and dynamic initialization, consider this example.
Example 1:A program to demonstrate static and dynamic initialization
#include<iostream>
using namespace std;
int main ()
{
int num1 = 5, num2 = 6; //static initialization using
I I constant
int num3 = num1 + num2; // Dynamic initialization
II using expression
cout<<num3;
return 0;
}
The const qualifier: When the qualifier const precedes a data type in the declaration of a variable, it implies that the value of the variable cannot be changed throughout the program. For example, consider this statement.
const float pie_val = 3.14;
The qualifier const with variable pie_val ensures that the program cannot alter its value inadvertently. If any attempt to alter the value of pie_val is made within a program, a compile-time error is generated. Moreover, if any data type is not specified with the const qualifier, by default it is int. For example, consider these statements.
const int max = 20; II Both these statements
const max = 20; II are equivalent.
Note that C also allows to create canst values. However, there are some differences in the implementation of const values in C and C++, which are discussed here.
• In C++, the variable declared as a constant must be initialized at the time of its declaration. While in C, const value is automatically initialized to 0 if it is not initialized by the user.
• In C++, const can be used in expressions. For example, consider these statements.
const int max =20;
char name [max] ;
However, it is invalid in C.
• In C++, by default, a const value is local to the file in which it is declared. However, it can be made global by explicitly defining it as an extern variable, as given here.
extern canst max=10;
In C, by default, canst value is recognized globally, that is, it is also visible outside the file in which it is declared. However, it can be made local by declaring it as static.
Reference Variables
A Reference variable, a new kind of variable that is introduced in C++ provides an
alias for an already defined variable. Like other variables, reference variables must
be declared in a program before they are used. The syntax for declaring a reference variable is
data_type & refname = varname;
where,
data_type = any valid C++ data type
& = reference operator
refname = the name of the reference variable being declared
varname = the name of the variable whose reference is being declared
To understand the concept of reference variable, consider this example.
Example 2: A Program to demonstrate the use of reference variable
#include<iostream>
using namespace std;
int main ( )
{
float cost = 200;
float &price = cost;
II Declaration of Reference variable
Cout<<“price: ” <<price<<“\n”;
price = price+20;
cout<<“cost: ” <<cost<<“\n”;
cout<<“price: ” <<price;
return 0;
}
The output of the program is
price: 200
cost: 220
price: 220
In this example, price is declared as a reference variable for the variable cost. Since both the variables refer to the same memory location, the statement price=price + 20 changes the value of both price and cost.
Storage Class Specifiers
In addition to data type, a storage class is also associated with a variable which determines the scope and life-time of a variable. The scope refers to the part of the program that can access that variable and life-time refers to the duration for which a variable stays in existence. The syntax for declaring a variable with the storage class is
storage_class data_type variable;
There are four storage class Specifiers supported by c++ namely, auto, extern, static and register, which are discussed here.
Automatic Variable: A variable defined within a block or a function is known as local variable and can be accessed within that block or function only. In C++, this local variable is referred to as automatic variables. A variable is made automatic by prefixing its declaration with the keyword auto.
An automatic variable is created only when the function or block in which it is declared is called and is automatically destroyed when the function returns. In addition, it needs to be initialized explicitly with a valid value; otherwise, it will be initialized with a garbage value.
External Variable: A variable that is accessible to all the functions in the file in which it is declared is known as global variable. However, if a global variable has to be accessed by some other file, it has to be declared as an external variable in that file by prefixing its declaration using the keyword extern. The keyword extern informs the compiler that the variable is defined in the same file or another file. An external variable remains in existence throughout the life of the program and its value is retained between the function calls. In addition, an external variable is automatically initialized to zero.
Static Variable: A variable that is recognized only inside the function in which it is declared, but remains in existence throughout the life of program is known as static variable. A variable is declared static by prefixing its declaration by the keyword static. Both local and global variables can be declared as static.
When a local variable is declared as static, it is known as static local variable. A static local variable retains its value between the function calls. Moreover, it can be initialized with the constants only at the time of declaration and its value can be changed later at any point within the function. However, it is initialized only once, that is, at the time of creation.
When a global variable is declared as static, it is known as static global variable. A static global variable can be accessed by all the functions of the program in the same file in which it is declared. However, functions in other files cannot access it. The lifetime and initialization of static global variable is the same as that of static local variable.
Register Variable A variable that is stored in the register (a special storage area within the central processing unit) instead of the main memory where other variables are stored is known as a register variable. A register variable speeds up the execution of a program as it does not require a memory access like normal variables. The register variable is declared with the help of the keyword register.
The scope and life-time of the register variable is the same as that of the automatic variable. Note that, the register storage Specifiers is applied only to pointers and to variables of type int and char. In addition, only local variables and the arguments which are passed to the functions can be declared as register variables. Moreover, declaring a variable as register does not guarantee that it will be a treated as register variable. In that case, it will be treated as an automatic variable.