C Programming - Control Instructions - Discussion

2. 

Which of the following statements are correct about the below program?

#include<stdio.h>
int main()
{
    int i = 10, j = 15;
    if(i % 2 = j % 3)
        printf("IndiaBIX\n");
    return 0;
}

[A]. Error: Expression syntax
[B]. Error: Lvalue required
[C]. Error: Rvalue required
[D]. The Code runs successfully

Answer: Option B

Explanation:

if(i % 2 = j % 3) This statement generates "LValue required error". There is no variable on the left side of the expression to assign (j % 3).


Cynthia Newton said: (Feb 17, 2011)  
Equality can be checked with == symbol .

The condition should be - if(i%2 == j%3) , where even then it will be a error due to the lack of value for j.

Hence it is an Expression syntax (option A).

Sundar said: (Feb 18, 2011)  
@Cynthia Newton

You are wrong. The given answer is correct only.

Because we can use '=' and '==' in if conditions.

Eg: The followings are valid if conditions.

1. if(0 == 0) // true
2. if(1 == 2) // false
3. if(i = 0) // false
4. if(i = 1) // true

Eg: Some invalid if conditions.

5. if(0 = 0) // Invalid. LValue required
6. if(1 = 2) // Invalid. LValue required

But, the actual reason for the error "LValue required error" is due to the following reason:

The assignment operator '=' always requires an operand immediate left.

LValue --> LEFT side operand required.

In the above 4 examples, 3 and 4 uses an operand immediate LEFT side to the '=' operator. So, it is a valid statement. But in the case 5 and 6 it fails due to 'LValue required'.

I hope this may help you. Have a nice day!

Ravindar said: (Jun 4, 2011)  
I can't understand your explanation. Please clearly explanation. Lvalue means?

Rajat Rahul said: (Jun 6, 2011)  
@Ravinder: In 5 and 6 there are no any identifier immediet to the left which should be mendatory to have the result of that expression.

Keerthi Kumar said: (Jul 20, 2011)  
@sundar ...thanks..

Praveen said: (Aug 28, 2011)  
@Sundar.. Good explanation :)

Siva said: (Sep 23, 2011)  
I can't understand I want clear explanation. Why we use L value.

Priyanka.S said: (Oct 5, 2011)  
@sundar good explanation thank you.

Priyanka.S said: (Oct 5, 2011)  
Can any one explain what is meant by Rvalue?

Vinita Jain said: (Oct 13, 2011)  
I want to know what is Rvalue. Please explain.

Sameer said: (Dec 17, 2011)  
@sundar,
as you mention at above explanation of if's valid condition example 4th one is false but not true..

Franco said: (Oct 9, 2012)  
L value always must be a variable and should not be a constant.

R value always must be a constant to assign for that variable.

Navi said: (May 12, 2015)  
/* Note: GCC Compiler (32 Bit Linux Platform). */

Then this is also not working why?

#include<stdio.h>
int main()
{
int i = 10, j = 15, a, b;
if((a=i % 2) = (b= j % 3))
printf("IndiaBIX\n");
return 0;
}

Anonymous said: (Aug 11, 2015)  
@Navi.

It should be:

#include<stdio.h>
int main()
{
int i = 10, j = 15, a, b;
if((a=i % 2) == (b= j % 3))
printf("IndiaBIX\n");
return 0;
}

Anjali said: (Jan 7, 2017)  
Nice explanation, thanks @Sundar.

Prashant3393 said: (Jun 1, 2017)  
Thanks @Sundar.

Ravi Mishra said: (Jun 4, 2017)  
Please tell me the meaning of Lvalue.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.