Variables in C++

Variables are the memory locations that can be used to save data/values multiple time. It can remove the existing value and save new value during the execution of program. Variable declaration statement always ends with semicolon.

Variable Properties

In C++ a variable has the following properties

Name

Every variable must have a name/identifier by which value is store and retrived.

Type

In C++ each variable has some type, that define which type of data can be save in variable.

Address

A variable is created in memory (RAM). The location where it is created is called it's address. Variable can also be accessed by the use of its address.

Value

Each variable has stored value. Variable must be provided valid value at the time of declaration, otherwise it will contain null value. In case of integer variable it will contains 0 by default.

Variable Declaration

variable declaration is the manifestation that a memory location is named and can be used in entire program. Variable declaration can be written anywhere in program.

Syntax

data_type variable_name ;

Example

#include<iostream>
using namespace std;

int main() {

  // variable declaration with name age of type int
  int age;

  return 0;

}

Variable Initialization

Variable Initialization specify variable name, it's type as well as it's intial value. Variable Initialization is the good practice in programming.

Syntax

data_type variable_name = value ;

Example

#include<iostream>
using namespace std;

int main() {

  // variable Initialization with name age of type int and value 18
  int age = 18;

  return 0;

}

Variable Storage Class

Storage class differentiate variables according to their allocated memory location, accessibility and lifetime. There are four different storage classes used for variables as.

  1. Local Variable

  2. Global Variable

  3. Static Variable

  4. Register Variable

 

Comments
Login to TRACK of Comments.