Variables in C++ store data in a memory location, which can be modified or used during the program's execution. This tutorial describes C++ variables.

Variables play a significant role in constructing a program, storing values in memory, and dealing with them. Variables are required in various functions of every program. For example, variables are required when we check for conditions to execute a block of statements. Again, for iterating or repeating a block of the statements several times, a counter variable is set along with a condition, or simply if we store an employee's age, we need an integer type variable. So in every respect, the variable is used. In this tutorial, you will learn how variables are declared in C++, assign values to them, and use them within a C++ program.

What are Variables in C++?

Variables are used in C++, where you need to store any type of value within the program and whose value can be changed during program execution. In a program, variables can be declared differently, each with varying memory and storage capacity requirements. Variables are the names of memory locations allocated by the C++ compiler, and allocation is done based on the data type used to declare the variable.

Variable Definition in C++

A variable definition means that the programmer writes some instructions to tell the compiler to create the storage in a memory location. The syntax for defining variables is:

Syntax:

data_type variable_name;
data_type variable_name, variable_name, variable_name;

Here the data_type means the valid C++ Data Types, including int, float, double, char, wchar_t, bool, etc., and the list of variable names to be declared, separated by commas.

Example:

/* variable definition */
int width, height, age;
char letter;
float area;
double d;

Variable Initialization in C++

Variables are declared in the above example, but none have been assigned any value. Variables can be initialized, and initial values can be set along with their declaration.

Syntax:

data_type variable_name = value;

Example:

/* variable definition and initialization */
int width, height = 5, age = 32;
char letter = 'A';
float area;
double d;

/* actual initialization */
width = 10;
area = 26.5;

Some rules must be in your knowledge to work with C++ variables.

Rules of Declaring Variables in C++

  • A variable name can consist of Capital letters A-Z, lowercase letters a-z digits 0-9, and the underscore character.
  • The first character must be a letter or underscore.
  • Blank spaces cannot be used in variable names.
  • Special characters like # and $ are not allowed.
  • C++ keywords cannot be used as variable names.
  • Variable names are case-sensitive.
  • A variable name can consist of 31 characters only if we declare a variable more than one character compiler will ignore after 31 characters.
  • Variable type can be bool, char, int, float, double, void, or wchar_t.

Here's a Program to Show the Usage of Variables in C++:

Example:

#include <iostream> 
using namespace std;

int main()
{
    int x = 5;
    int y = 2;
    int Result;
    Result = x * y;
    cout << Result;
}

Another program showing how Global variables are declared and used within a program:

Example:

#include <iostream> 
using namespace std;

// Global Variable declaration:
int x, y;
float f;

int main()
{
    // Local variable
    int tot;
    float f;
    x = 10;
    y = 20;
    tot = x + y;

    cout << tot;
    cout << endl;
    f = 70.0 / 3.0;
    cout << f;
    cout << endl;
}