English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

The Correct Method to Print size_t Using printf in ++What is the Difference Between int and long in

A reference variable is an alternative name for an existing variable. It cannot be changed to refer to another variable and must be initialized at the time of declaration and cannot be NULL. The operator '&' is used to declare a reference variable.

The following is the syntax of a reference variable.

datatype variable_name; // variable declaration
datatype& refer_var = variable_name; // reference variable

Here,

datatype-The data type of the variable, such as int, char, float, etc.

variable_name-This is the variable name given by the user.

Refer_var-The name of the reference variable.

The following is an example of a reference variable.

Example

#include <iostream>
using namespace std;
int main() {
   int a = 8;
   int& b = a;
   cout << "The variable a : " << a;
   cout << "\nThe reference variable r : " << b;
   return 0;
}

Output Result

The variable a : 8
The reference variable r : 8

In the above program, an integer type variable is declared and initialized with a value.

int a = 8;

Variable b is declared, which refers to variable a.

int& b = a;
SQL Tutorial