How to Declare Variables?

A typical variable declaration is of the form: 

// Declaring a single variable
type variable_name;

// Declaring multiple variables:
type variable1_name, variable2_name, variable3_name;

A variable name can consist of alphabets (both upper and lower case), numbers, and the underscore ‘_’ character. However, the name must not start with a number. 

Initialization of a variable in C++

In the above diagram,  

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.  

Examples:  

// Declaring float variable
float simpleInterest; 

// Declaring integer variable
int time, speed; 

// Declaring character variable
char var;  

We can also provide values while declaring the variables as given below:

int a=50,b=100;  //declaring 2 variable of integer type    
float f=50.8;  //declaring 1 variable of float type     
char c='Z';    //declaring 1 variable of char type   

C++ Variables

Variables in C++ is a name given to a memory location. It is the basic unit of storage in a program. 

  • 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.

Similar Reads

How to Declare Variables?

A typical variable declaration is of the form:...

Rules For Declaring Variable

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....

Difference Between Variable Declaration and Definition

The variable declaration refers to the part where a variable is first declared or introduced before its first use. A variable definition is a part where the variable is assigned a memory location and a value. Most of the time, variable declaration and definition are done together.See the following C++ program for better clarification:...

Types of Variables

...

Instance Variable Vs Static Variable

There are three types of variables based on the scope of variables in C++...