R-Value in Expression : C Programming



In the previous chapter we have learnt about the Lvalue of an expression in this chapter we are going to learn R-value of an expression.

What is R-Value ?

  1. R Value stands for Right value of the expression.
  2. In any Assignment statement R-Value must be anything which is capable of returning Constant Expression or Constant Value.
  3. R Value Can be anything of following -
Examples of R-Value
VariableConstant
FunctionMacro
Enum ConstantAny other data type

Diagram Showing Lvalue :

Example of RValue :

#include<stdio.h>
int main()
{
int num;
num = 5;
return(0);
}

In the above example, Constant Value 5 is assigned to the variable will be considered as right Value of the variable.

Important Tips of Using RValue :

Tip 1 : R Value may be Constant or Constant expression

num = 20;      //Constant RValue
num = 20 + 20; //Constant Expression as RValue
  1. In the example, we have assigned constant value to the variable.
  2. Constant value “20” is considered as RValue.
  3. In the next line we have assigned constant expression to the variable. “20 + 20” will be evaluated first and then result of expression will be assigned to an Variable.

Tip 2 : R Value may be MACRO

#define MAX 20
main() {
   int num = MAX;
}

If we have defined a MACRO then we can use MACRO as right value. In the above example we have assigned the 20 to the variable “num”.

In case if we defined the MACRO value of different data type then it may results into compile error.

Tip 3 : R Value may be Variable

#define MAX 20
main()
{
   int flag = 0;
   int num  = flag;
}

For more information , Visit MSDN library.