c++ icon

A little about C++ variables

In C++

  • A variable is the name of the memory location.
  • The variables are used to store data.
  • Its value could be altered and it may be reused repeatedly.
  • The value saved in a variable can be altered during program implementation.
  • A variable is merely a name given to some memory location, all of the operations done on the variable effects that memory location.
  • In C++, all of the variables have to be declared before usage.
How to declare a variable in C++

This is how a variable is declared in C++:

type variable_name;

For example:

int var_x;

float var_x;

char var_x;

Declaring multiple variables:

int var_1, var_2, var_3;

float var_1, var_2, var_3;

char var_1, var_2, var_3;


A variable declaration tells the compiler there is just one variable present with the specified type and name so that compiler goes for more compilation without having complete detail regarding the variable.

A variable declaration is helpful once you’re using several files and you specify your variable in one of those files that will be accessible in the time of connecting of this program.

Further Reading:  What is C++

You may use extern keyword to declare a variable at any given location. Although you are able to declare variable multiple times on your C++ program, it may be defined just once in a file, a function or a block of code.

Rules of declaring variables in C++
  • C++ keywords can’t be utilized as variable names.
  • A variable name may include Capital letters A-Z, lowercase letters a-z, digits 0-9, and the underscore character.
  • The initial character must be a letter or underscore.
  • Blank distances can’t be utilized in variable names.
  • Special characters such as #, $ are not permitted.
  • Variable titles are case-sensitive.
  • A variable name could be comprising 31 characters just if we declare a variable over one character, the compiler will dismiss after 31 characters.
  • Variable type can be bool, char, int, float, double, void, or wchar_t.
Assigning values to the variable at declaration

You may also assign values to the variables at declaration time.

int a=1,b=10;

float c=55.8;

char d='X';


Further Reading:  What is C++

Important: The C++ language is a “case sensitive” language. That means that an identifier written in capital letters isn’t equal to a different one using the identical name but written in small letters. Therefore, by Way of Example, the RollNo variable Isn’t the same as the rollno variable or the rollNo variable. These are 3 distinct variables identifying three different variables.

Types of variables in C++

Following are the types of variables in C++:

  • Local Variables
  • Instance Variables
  • Static Variables
Local variables

A variable defined inside a block or method or constructor is known as local variable.

An example of local variable:

#include <iostream>

using namespace std;

void Numf()


//A local variable

int num = 10;

num = num + 15;

cout << "The number is : " << num;


int main()