L-Value of Expression : C Programming



In the previous chapter we have learnt about C Programming Expressions, in this chapter we will be learning about L-Value of Expression.

What is L-Value of Expression ?

  1. L-Value stands for left value
  2. L-Value of Expressions refer to a memory locations
  3. In any assignment statement L-Value of Expression must be a container(i.e. must have ability to hold the data)
  4. Variable is the only container in C programming thus L Value must be any Variable.
  5. L Value Cannot be Constant,Function or any of the available data type in C

Diagram Showing L-Value of Expression :

Example of L-Value of Expression :

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

In the above expression, Constant value 5 is being assigned to a variable ‘num’. Variable ‘num’ is called as storage region’s , ‘num’ can considered as LValue of an expression.

Re-commanded Concepts : Concept of Variables and Constants in C

Important Tips :

Below are some of the tips which are useful to make your concept about L-Value of Expression more clear.

Lvalue cannot be a Constant

int main()
{
int num;
5 = num;  //Error
return(0);
}

You cannot assign the value or any constant value to another constant value because meaning of constant is already defined and it cannot be modified.

Lvalue cannot be a Constant Variable

Even we cannot assign a value to a constant variable. Constant variable should not be used as L Value.

int main()
{
const num;
num = 20;  //Error
return(0);
}

Lvalue cannot be a MACRO

We know that macros gets expanded before processing source code by compiler. All the macros will be replaced by defined value using pre-processor before compiling program.

#define MAX 20
int main()
{
MAX = 20;  //Error
return(0);
}

pre-processor will replace all the occurances of macros and hand over modified source code to compiler. following code will be given to compiler after doing pre-processor task

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

Re-commanded Reading : Preprocessor macro

Lvalue cannot be a Enum Constant

enum {JAN,FEB,MARCH};
int main()
{
JAN = 20;  //Error
return(0);
}

Lvalue cannot be a Data Type

#define<stdio.h>
#define max 125
struct book{
  char *name;
  int  pages;
};
void main()
{
book = {"C Programming",100};
}

L-Value Require error ?

Causes of Error :

  1. Whenever we are trying to assign value to constant value
  2. Whenever we are trying to increment or decrement Constant Expression
  3. Inside if statement , if we try to write comparison operator as “= =” instead of “==”, then we will get this error.

Solution :

Solution to this problem is very simple -

  1. Always try to assign constant value to “Variable“.
  2. If you are using comparison operator then we don’t provide space in between operators.

In the next chapter we will be learning RValue of an expression which is also called as right value of an expression.