Open In App

Types of Values in C++

Last Updated : 16 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, values represent data that variables hold or expressions produce during a program's execution. These values can be categorized based on how they behave, how they are stored, and how they can be used. Understanding the types of values helps write more efficient, safe, and readable code.

1. Lvalue (Left Value)

An lvalue (locator value) refers to an object that occupies some identifiable location in memory. You can take the address of an lvalue using the & operator, and it can appear on the left-hand side or right-hand side of an assignment.

Example:

C++
#include <iostream>
using namespace std;

int main() {

    // x is an lvalue
    int x = 10;
    
    // valid: lvalue on left
    x = 20;         

    cout << x;
    return 0;
}

Output
20

The lvalues has a persistent address in memory and can be assigned a value (if not const).

2. Rvalue (Right Value)

An rvalue is a value that does not have a persistent memory address (or it's not safe/allowed to take its address). Rvalues typically appear on the right-hand side of an assignment.

Example:

C++
#include <iostream>
using namespace std;

int main() {

    // 5 + 3 is an rvalue
    int y = 5 + 3;
    
    // y is lvalue, but its value is rvalue
    int z = y;
    cout << y << " " << z;

    return 0;
}

Output
8 8

The rvalues represents temporary values. Cannot appear on the left-hand side of assignment.

3. Prvalue (Pure Rvalue)

A prvalue (pure rvalue) is a type of rvalue that represents temporary values that don’t have an identity (like literals or the result of most expressions).

Example:

C++
#include <iostream>
using namespace std;

int main() {

    // 100 is prvalue
    int a = 100;
    
    // (a + 20) is prvalue
    int b = a + 20;

    cout << a << " " << b;

    return 0;
}

Output
100 120

The prvalues are used to initialize objects. They represents literal values, temporary results of expressions, or returned values from functions that return by value.

Xvalue (Expiring Value)

An xvalue (expiring value) is a type of rvalue that represents an object near the end of its lifetime. It typically refers to resources that can be moved from.

Example

C++
#include <iostream>
using namespace std;

int main() {
    
    // num is glvalue
    int num = 42;

    cout << num << endl;
    
    return 0;
}

Output
Hello

The xvalues are used in move semantics that allows transferring resources instead of copying.

Glvalue (Generalized Lvalue)

A glvalue (generalized lvalue) refers to any value that has an identifiable location in memory (includes both lvalues and xvalues).

Example:

C++
#include <iostream>
using namespace std;

int main() {
    
    // num is glvalue
    int num = 42;

    cout << num << endl;
    
    return 0;
}

Output
42

The glvalue has memory identity and can be either lvalue or xvalue.


Next Article
Article Tags :
Practice Tags :

Similar Reads