PREV NEXT
What are variables?
The variables are basically used to store the values which can be modified later on in the program. The variables can be declared in many ways and every way with different memory requirements. The variable is used to represent a name of memory location that is allocated by the compiler depending upon the data type of the variable. The variable defined within a program has a specific type which determines the size and the layout of the variable memory. At the time of declaration the variables are composed of letters, digits and an underscore character. It can start with either a letter or an underscore. In C++, the uppercase and lowercase letters are unique because C++ is case sensitive.
For example :
How to define variables in C++?
As you define the variable it let the compiler know that how much and where to create the storage for the variable. They are declared before they are taken in use but in C++ you can declare them in the middle of the program but before using them. The variable defined in the program specifies the data type and contains the list of one or more variables of the same data type.
Let us have a look at the syntax:
type variable-list;
In this syntax, type is a data type like int, char, float, double, etc. and variable-list is that which contains one or more identifiers names separated by commas. Like
1 2 3 4 5 |
int a, b, c; float x, number; double m; |
A variable declaration is also useful when you are using multiple files and you define your variable in one of those files which will be available at the time of linking of the program. So, in this case extern keyword can be used to declare a variable at any place.
How to initialize variable in C++?
In this the variables are initialized and the initial value can be assigned along with the declaration. As you can see in the above examples the variables are declared but not initialized with any value.
Let us have a look at the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> using namespace std; /* variable declaration*/ extern int x,y; extern int z; int main () { /*variable definition*/ int x, y; int z; // actual initialization x = 10; y = 20; z = x + y; cout << z << endl ; return 0; } |
What are the rules to declare variables?
- First character must be letter or underscore.
- Keywords in C++ cannot be used in variable name.
- Variable names are case sensitive in C++.
- It can consist of capital letter A-Z, lowercase letters a-z, digits 0-9 and underscore character.
- You cannot use blank spaces in variable names.
Scope of Variables in C++
The variables have a boundary of functioning and out of that boundary they don’t hold their value and this boundary are known as scope of variables. In most of the cases it’s between the curly braces where the variable is declared and it exists but not outside the curly braces.
The variables can be divided in to two main types.
They are:
- Local variables
- Global variables
Local Variables
The local variables are those variables which exist in between the curly braces only where they are declared. But outside the curly braces they are unavailable and lead to compile time error.
Let us have a look at the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> using namespace std; int main() { int i=15; if(i<30) // if condition starts { int n=50; // Local variable declared and initialized } // if condition ends cout << n; // compile time error because n is not declared here } |
Global Variables
The global variables are those variables which are once declared can be used throughout the program by any class or function. They are declared outside the main function.
Let us have a look at the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include<iostream> using namespace std; int x; // Global variable declaration int main () { int a, b; // Local variable declaration // actual initialization a = 20; b = 30; x = a + b; cout << x; return 0; } |