C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Point Out Correct Statements (Q.No. 2)
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;
}
Error: Expression syntax
Error: Lvalue required
Error: Rvalue required
The Code runs successfully
Answer: Option
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).

Discussion:
20 comments Page 1 of 2.

Cynthia Newton said:   1 decade ago
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:   1 decade ago
@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!
(1)

Ravindar said:   1 decade ago
I can't understand your explanation. Please clearly explanation. Lvalue means?

Rajat Rahul said:   1 decade ago
@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:   1 decade ago
@sundar ...thanks..

Praveen said:   1 decade ago
@Sundar.. Good explanation :)

Siva said:   1 decade ago
I can't understand I want clear explanation. Why we use L value.

Priyanka.s said:   1 decade ago
@sundar good explanation thank you.

Priyanka.s said:   1 decade ago
Can any one explain what is meant by Rvalue?

Vinita Jain said:   1 decade ago
I want to know what is Rvalue. Please explain.


Post your comments here:

Your comments will be displayed after verification.