C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - Point Out Errors (Q.No. 9)
9.
Point out the error, if any in the program.
#include<stdio.h>
int main()
{
int a = 10, b;
a >=5 ? b=100: b=200;
printf("%d\n", b);
return 0;
}
Answer: Option
Explanation:
Variable b is not assigned.
It should be like:
b = a >= 5 ? 100 : 200;
Discussion:
34 comments Page 2 of 4.
Aloke said:
1 decade ago
#include<stdio.h>
int main()
{
int a = 10, b;
a >=5 ? (b=100): (b=200);
printf("%d\n", b);
return 0;
}
gives the output 100 on the online compilar which is provided by this site
check it my friends
int main()
{
int a = 10, b;
a >=5 ? (b=100): (b=200);
printf("%d\n", b);
return 0;
}
gives the output 100 on the online compilar which is provided by this site
check it my friends
Praveen said:
1 decade ago
Aloke would you please explain your program ? How does it work ?
Praveen said:
1 decade ago
@Mani
The program which Aloke has posted works even though the expression a >=5 ? (b=100): (b=200); is not assigned to any value .. Would you please explain us in detail if you are clear with the concept ?
The program which Aloke has posted works even though the expression a >=5 ? (b=100): (b=200); is not assigned to any value .. Would you please explain us in detail if you are clear with the concept ?
Maikal kumar said:
1 decade ago
Answer must be 100; because a=10 thats are >a. But please help me about lvalue value.
Sarang said:
1 decade ago
It gives output b = 200 on DOSBox c++ when the expression is
a >=5 ? b=100: b=200;
In above expression.if you change a>=11/12 or anything.
Ouptut is b = 200. any condition doesn't make any difference to this answer.
When expression is a >=5 ? (b=100): (b=200);
It gives output b = 100;
Nowhere error is coming, please somebody explain me
a >=5 ? b=100: b=200;
In above expression.if you change a>=11/12 or anything.
Ouptut is b = 200. any condition doesn't make any difference to this answer.
When expression is a >=5 ? (b=100): (b=200);
It gives output b = 100;
Nowhere error is coming, please somebody explain me
Umesh said:
1 decade ago
Actually when we use conditional operators we should store the condition result in any variable. see below example.
#include<stdio.h>
{
int a=10, b;
b= a>=5?100:200;
printf("%d", b);
}
RESULT:
100
The above given question should be like i have stated here.
#include<stdio.h>
{
int a=10, b;
a>=5?100:200;
printf("%d", b);
}
RESULT:
ERROR
According to the given question answer should be 100 not error because b value will be assigned if condition is true or false.
#include<stdio.h>
{
int a=10, b;
b= a>=5?100:200;
printf("%d", b);
}
RESULT:
100
The above given question should be like i have stated here.
#include<stdio.h>
{
int a=10, b;
a>=5?100:200;
printf("%d", b);
}
RESULT:
ERROR
According to the given question answer should be 100 not error because b value will be assigned if condition is true or false.
Subbu said:
1 decade ago
What is the meaning of ? in between the a>=5 and b=100.
Ajeet said:
1 decade ago
The conditional operator (?:) always return either true part or false part. Lvalue means legal left value. There no need of Lvalue in conditional operator so the program executes successfully and gives output 100;.
Priyanka K. said:
1 decade ago
Condition is satisfying, hence b=100 will be assigned to b and it becomes print 100?
Parveen Soni said:
1 decade ago
L value error is found because if you are writing a>=5?b=100:b=200;
Then in last assignment b=200,the value 200 is assigned to left side,which is not correct as per C-language Rule.
Hence,the above problem can be removed by using parenthesis on b=100 and b=200 [like (b=100) and (b=200)].
On other side,if you use expression as:
b=a>=5 ? b=100: b=200;
Then again it will show the same error because of same reason.
But expression is correct if you use:
b=a>=5 ? (b=100): (b=200);
Output is 100 when you use parenthesis.
Compiler used : Dev C++ version 5.4.1 Portable.
Then in last assignment b=200,the value 200 is assigned to left side,which is not correct as per C-language Rule.
Hence,the above problem can be removed by using parenthesis on b=100 and b=200 [like (b=100) and (b=200)].
On other side,if you use expression as:
b=a>=5 ? b=100: b=200;
Then again it will show the same error because of same reason.
But expression is correct if you use:
b=a>=5 ? (b=100): (b=200);
Output is 100 when you use parenthesis.
Compiler used : Dev C++ version 5.4.1 Portable.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers