Reference Variables in C++ Explained

In this tutorial, you will learn how reference variables are used in the C++ programming language and their usage. A reference variable is a variable that stores reference to its data or object.

For example:

int i = 0;
int& r = i;

Below is a simple program of how reference variables work in C++.

C++ reference variables

#include <iostream>

using namespace std;

int main()
    int a=2;
    int &b = a; 
    cout << endl << "The value of a is: " << a;
    cout << endl << "The value of b is: " << b << endl;
    return 0;

The value of a is: 2 The value of b is: 2
Since Refence variable are alias of already existing variables, they do not take up memory space.

Happy Coding!

Leave a Reply

Your email address will not be published.