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.

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.

Hemant said:   8 years ago
If they are being so specific about parenthesis. They should also include a semicolon in the end of their result mentioned. Because I believe the first option will also work.

Deepa said:   8 years ago
I did not understand the meaning of the operator ":" between the variables.

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.

Souravb said:   2 decades ago
I cant understand it. Please help.

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.


Post your comments here:

Your comments will be displayed after verification.