Skip to content
HOME - C++ - what are variables in programming

what are variables in programming



Variables are a memory location i.e, a place to store information inside a memory. Variables are used to store values that can be changed during program execution. It is a combination of 2 words vary and able which means changeable. So, a variable may store different values but not at the same time.

As it is a name of memory location so we can store all kinds of data in it. Either it is a numeric value or alphabets. The variable name remains the same during the program execution but the value stored in it may change.


When a variable is created in a program, some bytes in the computer memory are allocated to it. These bytes are used to store the value assigned to a variable. These bytes vary according to the datatype of the variable. For example, a variable containing char datatype takes 1 byte in computer memory and int datatype takes 4 bytes. Similarly, float and double datatype take 4 bytes and 8 bytes respectively.

DataTypeNumber of bytes
char 1 byte
int 4 bytes
float 4 bytes
double 8 bytes

Variable Name: Variable name is the identifier that is assigned to a specific slot ( location ) in the computer memory for storing value.

Variable Content: Variable content refers to value stored in the memory location associated with the variable.

Variable Address: Variable address refers to the address of memory location associated with the variable.


There are some rules for naming a variable that is followed to declare or initialize variables.

Rules for naming variables

  1. The first character of a variable name must be an alphabet. It can’t be any digit or symbols except underscore ( _ ).


  1. A variable name can only consist of letters, digits, and the underscore character.


  1. The first character of a variable name cannot be a digit.


  1. Blank spaces are not allowed in variable names.


  1. Spacial characters for example & , ^ , $ , etc cannot be used in variable names.



int number ; ✔ double s&t ; ✘
char _alpha ; ✔ float h98 ; ✔

float d ; ✔ char 99abc ; ✘
int 8c ; ✘ double &g ; ✘
int 99 ; ✘float _978 ; ✔

int my_name ; ✔ int my name ; ✘
  1. A variable name can only be declared for only one datatype in a program.




int  x ;

float  x ;


This will generate an error because x can only be declared with only 1 datatype.


  1. C language , C++, Java etc are case sensitive languages. Variable names counter and COUNTER are two different variables.


Int  counter  =  2;       counter variable has value 2

int  COUNTER  =  4;       COUNTER variable has value 4


  1. Reserved keywords cannot be used as variable names.


  1. Normally lower case letters are used for variable names and upper case letters for constants.


  1. Variable names can be up to 31 characters long. The compiler ignores the characters after 31st


Variable declaration


The variable declaration tells the compiler the name and type of value( datatype) stored in a variable. C language, C++, Java, etc, are strongly typed languages i.e, variable must e declared before using it in a program. When a variable is declared, a certain number of bytes ( see the above table ) are allocated to the variable in memory.

It means declaration not only declares but also defines a variable in the memory. Compilers generate an error if undeclared variable is used in the program. You can declare more than one variable at a time by separating them with a comma.




datatype  variable_name ;




int num;                 here int a datatype and num is the variable name.

char b ;                    here char is a datatype and b is the variable name.

float cam ;               here float is a datatype and cam is the variable name.

double s ;                here double is a datatype and s is a variable name.


Variable initialization


Assigning a value to the variable at the time of declaration is termed as variable initialization.




datatype  variable_name = value ;




int num = 20 ;          num is the variable name having value 20.

char b = ‘ S ’;           b is the variable name having character value S.

float cam = 3.14 ;           cam is the variable name having value 3.14.

double s = 6.7653357;         s is a variable name having a value 6.7653357.


Garbage Value


When a variable is declared, the compiler reserves the memory for it. If we do not initialize it then it may store meaningless data is called garbage value. With the involvement of such variables may cause unexpected and wrong results. So, to avoid this, all the variables must be initialized.


to watch a video related to variables click here

1 thought on “what are variables in programming”

  1. Pingback: What are the data types in programming languages | logicalmaster

Leave a Reply

Your email address will not be published. Required fields are marked *

en English