Variables in C++

In C++, a variable is something that can be used to store some value. The variables can be of different types to store different types of values.

For example-

An ‘int’ type variable stores an integer value whereas ‘char’ stores character type value.

A variable is actually a name given to a memory location and is the basic unit of storage in a program. When you declare a variable and assign it some value, some memory has been allocated to that variable.

Variable declaration in C++

A variable in C++ must be declared before use. The declaration means creating a variable. The syntax of creating a variable is given below.

datatype variable_name;

For example, if you want to declare a variable to store an integer, then its syntax should be like this-

int x;

where int is the data type of the variable and x is the name of the variable.

We can declare multiple variables in a single line as long as they have the same data type.

For example-

int x, y, z;

Here we have declared three variables named x, y, and z which can store integer type values.

Defining a variable in C++

Defining a variable means to store some value in it. To store a value, we use the assignment operator.

For example-

int x;
x = 1;

First, we have created a variable named x and then assigned it a value of 1.

It should be noted that the assignment always takes place from right to left.

We can also define a variable the moment we created it-

To define a variable at the time of declaration, follow this:

int x = 12;

It should be noted that the variable name can be a letter or a whole word. It can contain letters along with numbers.

For example-

char section = 'A';
int age1 = 21;

We can also declare and define multiple variables at the same time or in a single line as long as they have the same data type.

For example-

int x = 1, y = 4, z = 7;

Value inside a variable can be changed anytime

A variable can have only one value at a time, however, we can change the value inside a variable by some new value at any moment. The old value get replaced by the new one.

For example-

#include<iostream>
using namespace std;
int main()
{
    int x = 4;
    cout<<"The value inside x is "<<x;
    x = 7;
    cout<<"The value inside x is now "<<x;

    return 0;
}

Output

The value inside x is 4

The value inside x is now 7

Types of variables in C++

  • int: To store integer type values.
  • float: To store decimal type values with single precision.
  • double: To store decimal type values with double precision.
  • char: To store character type values.
  • bool: To store boolean value. A boolean value can be either ‘true’ or ‘false’.
  • void: It represents an absence of datatype.

For example:

int x = 1;
float y = 70.0
double z = 1.2345;
char section = 'B';
bool car = true;
void a;

Rules for variable declaration in C++

  • A variable can only start with a letter or underscore. It cannot start with a digit.
  • A variable can have letters, digits, and underscore.
  • A variable name cannot be a reserved keyword.
  • If a variable contains more than one word, the words should not be separated with space. However, you can separate them with an underscore.

Some Invalid variable names:

int 1age; // A variable should not be started with a number.
char my section; //Variable names cannot have space within
int float; //Float is a reserved keyword

Some valid variable names:

int age1;
char my_section;
int marks;