Introduction to Variable in CPlusPlus (Declaration & Scope)
Programming is not just writing some lines of codes to print a statement like “Hello World!”. Indeed, it is much complex. In order to write programs that are really useful and help us save time, we need to go a little far and introduce the concept of variable.
Variable is a name given to a specific memory location in the storage device which the program can manipulate. Variables can store different data types and the value stored in it can change during the execution of the program.
A variable name can be made up of letters, digits and underscore. Upper case and lower case letters are also distinct in C++. As C++ is a case sensitive programming language.
Basic Types of Variables in CPlusPlus:
The data types which are given to variable while their declaration in the program actually specifies the size of the memory for that variable. For example when you define a variable as (char) it will only occupy 1 byte of memory. But if you define the same variable as (int) it will occupy 2 bytes or more. There are further larger types of data types too. Such as double and float and etc.
Some of the variable types in C++ are described below:
|1||bool||Stores true or false value|
|2||char||Stores only one Alpha-Numeric letter|
|3||int||Stores whole numbers|
|4||float||Stores a decimal point number|
|5||void||It is NULL and doesn’t store anything|
Note: CPlusPlus defines further complex types of data too. They are such as pointer variables, arrays, references, data structures and classes.
See Also: [button link=”http://codejow.com/different-data-types/” size=”default” icon=”fa-th-list” side=”left” target=”” color=”b72354″ textcolor=”ffffff”]Data Types in Programming Languages[/button]
How to Declare a Variable?
A variable in C++ can be declared at any point of the program. But it should be declared before its use in the program. Following is the syntax of How you can declare a variable;
You can also initialize your variable at the same time of declaration.
int a = 8;
If you do not initialize your variable at the time of declaration; the system will give it a garbage value also called the default value.
What is important in declaring a variable in C++?
- The name of variables should either start with an alphabet or an underscore (_).
- The variables name cannot have any space in it.
- The name of the variables should not match any keyword of the language.
- No other symbol except (_) can be used in the name of the variables.
a = 10;
int x, y, z;
char Initial = B;
char Name ;
A Program to demonstrate the concept of Variables:
using namespace std;
int main ()
// Variable declaration
int m, n;
// variable initialization
m = 14;
n = 4;
a = m + n;
cout << a << endl ;
b = m/n;
cout << b << endl ;
See Also: [button link=”http://codejow.com/array-data-structure-explanation-c/” size=”default” icon=”fa-cog” side=”left” target=”” color=”2bb70f” textcolor=”ffffff”]Array Data Structure[/button]
Scope of the Variables:
Every variable has a boundary of functioning. Out of that boundary the variables is of no use. This boundary is called the scope of the variables. According to the scope of variables, they are divided into two major categories.
It is a variable which is declared or even initialized outside the main () function. It can be accessed by any function or class in the program. Even if it is initialized at the time of the declaration, you can reassign it with any other value.
It is a variable which is declared inside a function or a class and can only be accessed by the function or class (inside a pair of curly brace). Out of that the variable does not mean anything.