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;
}
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.
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!
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)
Navi said:
1 decade ago
/* 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;
}
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;
}
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).
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).
Anonymous said:
10 years ago
@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;
}
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;
}
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.
Franco said:
1 decade ago
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.
R value always must be a constant to assign for that variable.
(1)
Sameer said:
1 decade ago
@sundar,
as you mention at above explanation of if's valid condition example 4th one is false but not true..
as you mention at above explanation of if's valid condition example 4th one is false but not true..
Ravindar said:
1 decade ago
I can't understand your explanation. Please clearly explanation. Lvalue means?
Siva said:
1 decade ago
I can't understand I want clear explanation. Why we use L value.
Vinita Jain said:
1 decade ago
I want to know what is Rvalue. Please explain.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers