C Programming - Expressions - Discussion

Discussion Forum : Expressions - General Questions (Q.No. 3)
3.
Which of the following is the correct usage of conditional operators used in C?
a>b ? c=30 : c=40;
a>b ? c=30;
max = a>b ? a>c?a:c:b>c?b:c
return (a>b)?(a:b)
Answer: Option
Explanation:

Option A: assignment statements are always return in paranthesis in the case of conditional operator. It should be a>b? (c=30):(c=40);

Option B: it is syntatically wrong.

Option D: syntatically wrong, it should be return(a>b ? a:b);

Option C: it uses nested conditional operator, this is logic for finding greatest number out of three numbers.

Discussion:
30 comments Page 2 of 3.

Prit said:   9 years ago
I think both the option A and option C are true.
(1)

Prit said:   9 years ago
I can't understand.

Why not to choose option A?

Krishna said:   9 years ago
I have excuted the first option it is correct only.

Apoorva said:   9 years ago
I think, both the option A and C are correct.

Guddu said:   9 years ago
I think option C is completely right.
Whereas option A having problem.

main.c:6:18: error: lvalue required as left operand of assignment
a>b ? c=30 : c=40; here "c=40" having problem.

Anshul Jain said:   10 years ago
Any one explain me. Why option A is wrong?

Vibhor said:   10 years ago
Please post any example using this statement.

Mahalakshmi said:   10 years ago
I am not understand please explain clearly about it.

AdamK said:   10 years ago
Condition operators does not require strictly the "()"s. And there is a syntax error in option C, just like @Azhar mentioned. Between A and C, personally I tend to choose A.

Merry john said:   1 decade ago
I am agree with @Vishwas and @Azhar. Option A should be correct one.


Post your comments here:

Your comments will be displayed after verification.