C++ Variable Types

    In this tutorial, we will discuss the variable in C++. We will also discuss the types of variables with the help of some concrete examples.

    What is a Variable in C++?

    Using the concept of Variables in C++, we give a name to data so we can access it any time we want throughout the program under its scope (Visibility area of a variable). When we define a variable or write the variable name first time, we also define its data type along with it.

    The data type of the variable describes how much space will be occupied by the variable in the memory and what set of operations can be applied to that variable. For example, if we declare an integer type Variable, then it would occupy 2 bytes in memory, and Arithmetic operations apply to it. We can define a variable using letters, digits, and underscore.

    There are some rules to declaring a variable name.

    • A variable name should start with a letter or underscore.
    • A variable name can be composed of Letters, underscore, and digits.
    • There should not be a white space between the variable name.
    • A variable name could be of any length.
    • C++ is case-sensitive, so two variables with different cases would be treated as two different variables.

    Types of Variables

    A variable is described by its data type, and in C++, we have some basic primitive data types which define the type of variable.

    Data Type Description
    bool Store boolean value either true or false
    char Store single character value
    int Integer, store integer type value with a specific range
    float Store floating type value
    void Store void value
    double Store double-precision floating values
    wchar_t Store wide-character type

    These are some basic data types that define the type of a variable. Apart from these, C++ also has some other data types such as Enumeration, Pointer, Array, and Classes.

    Variable in C++

    To use a variable in C++, we have to follow two steps:

    • Variable Definition.
    • Variable Declaration.

    Variable Definition in C++:

    This is the first step of using a Variable. Before we use a variable or assign a value to that variable, we need to define it with an appropriate Data Type. In Variable Definition, we create a variable or write the variable name with its data type.

    Syntax to define a variable:

    Data_type variable_name;

    The Data_type and variable_name should be valid. We can also define more than one variable at once, to do so we have to put a comma after each variable name.


    #include <iostream>
    using namespace std;
    int main()
         int integer_1, integer_2;  //defining two integer variables
         char alphabet_1, alphabet_2;  // defining two char variables

    Behind the code:

    In the above example, we define two types of variables one is int, and the other is char. Here int data type has two variables, integer_1 and integer_2, and char data type also has two variables, alphabet_1 and alphabet_2.

    Variable Declaration

    A Variable declaration is similar to the variable definition. In the Variable declaration, we tell the compiler about the type and size of the variable. Though when we define a variable in C++, it automatically stores some garbage value in it, so the declaration property is violet there. In the variable declaration, the compiler ensures the existence of the variables but does not provide it with any space in the memory until the variables get defined. In C++, we use the extern keyword to declare variables.


    #include <iostream>
    using namespace std;
    extern int integer_1, integer_2; ; // variable declaratoion
    int main()
             int integer_1, integer_2; // Variable defination              
             integer_1=10; // variable Initialization
             integer_2 = 20; //variable initialization

    Behind the Code:

    In the above example, first, we declare two integer types variables, integer_1 and integer_2, then we define those two variables. We also initialize both variables by assigning values 10 and 20 to integer_1 and integer_2, respectively.

    Lvalues and Rvalues:


    In C++, if an expression refers to a value or a memory location, then it would be considered an Lvalue. An Lvalue could be written on either side of the assignment operator. For example, a variable could be an Lvalue.

    int x=10, y =20;
    x=y; // here x and y both are Lvalues

    The Rvalue mostly refers to the data or value stored in the memory and is referred to by the variables and identifiers. An Rvalue always resides on the right side of the assignment operator. For example:

    int x;
    x = 10; // here x is the Lvalue and y is Rvalue


    • Variable is the name that refers to the value.
    • To define a variable, we have some rules.
    • The variable name can start with any letter and underscore except digits.
    • Variables are case-sensitive.
    • In C++, we have variable declaration and variable Definition
    • A variable declaration declares the variables without occupying the memory.
    • A variable definition is the combination of Variable declaration and occupying memory space.
    • Lvalue stores the data.
    • Rvalues are the data.

    People are also reading: