A variable provides us with named storage that our programs can manipulate.
- 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.
- In C++, all the variables must be declared before use.
Variables Definitions
A simple variable definition consists of:
- A type specifier, followed by
- A list of one or more variable names separated by commas, and ends with
- A semicolon.
Ex:
1 2 3 | int num1; float num2, num3; int num4 = 1; |
Declaring Variables
Syntax:
// Declaring a single variable
type variable_name;
// Declaring multiple variables:
type variable1_name, variable2_name, variable3_name;
datatype: Type of data that can be stored in this variable.
variable_name: Name given to the variable.
value: It is the initial value stored in the variable.
Rules for Declaring Variables
- The name of the variable contains letters, digits, and underscores.
- The name of the variable is case sensitive (ex Arr and arr both are different variables).
- The name of the variable does not contain any whitespace and special characters (ex #,$,%,*, etc).
- All the variable names must begin with a letter of the alphabet or an underscore(_).
- We cannot used C++ keyword(ex float,double,class)as a variable name.
Valid variable names:
1 2 3 | int x; //can be letters int _yz; //can be underscores int z40;//can be letters |
Invalid variable names:
1 2 3 | int 89; //Should not be a number int a b; //Should not contain any whitespace int double;// C++ keyword CAN NOT BE USED |
Types of Variables
There are three types of variables based on the scope of variables in C++
- Local Variables
- Instance Variables
- Static Variables
Local Variables
Variables defined within a function or block are said to be local to those functions.
- Anything between ‘{‘ and ‘}’ is said to inside a block.
- Local variables do not exist outside the block in which they are declared, i.e. they can not be accessed or used outside that block.
- Declaring local variables: Local variables are declared inside a block.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include<iostream> using namespace std; void func() { // this variable is local to the // function func() and cannot be // accessed outside this function int age=20; cout<<age; } int main() { cout<<"Age is: "; func(); return 0; } |
Output:
1 | Age is: 18 |
Global Variables
As the name suggests, Global Variables can be accessed from any part of the program.
- They are available through out the life time of a program.
- They are declared at the top of the program outside all of the functions or blocks.
- Declaring global variables: Global variables are usually declared outside of all of the functions and blocks, at the top of the program. They can be accessed from any portion of the program.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include<iostream> using namespace std; // global variable int global = 5; // global variable accessed from // within a function void display() { cout<<global<<endl; } // main function int main() { display(); // changing value of global // variable from main function global = 10; display(); } |
Output:
1 2 | 5 10 |