C Programming - Expressions - Discussion


Which of the following is the correct usage of conditional operators used in C?

[A]. a>b ? c=30 : c=40;
[B]. a>b ? c=30;
[C]. max = a>b ? a>c?a:c:b>c?b:c
[D]. return (a>b)?(a:b)

Answer: Option C


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.

Souravb said: (Jul 11, 2010)  
I cant understand it. Please help.

Afsheen said: (Jul 19, 2010)  
If b>c then b is true else c is true, so 1st b>c is evaluated and stored then a>c is evaluated and stored. now the whole expression is considered.

Let us assume

(i) in b>c case this stmnt is true so b is saved.
(ii)in a>c case this stmnt is false so c is saved.
now the expression is in the form

Now finally this is evaluated.
you just assume the given stmnt as a>b?(a>c?a:c):(b>c?b:c).

I hope you understood.

Durga said: (May 11, 2011)  
I am not under stand please clear about it. In detailed because I don't know how to use ? opearator. So plese clear with a example how to use this operator.

Ramu said: (Aug 1, 2011)  
Simply we have to think in general if..else format

like if(a>b)
max is a
max is c
else if(b>c)
max is b
max is c

This is implemented , thinking in this we could easily analyse for greatest of 3 or 4 or 5 and so on easily I think so.

Vishwas Pathak said: (Aug 28, 2011)  
Option A is also right i did it in the dev-c++ and i got first one as the answer.

Vinutha said: (Sep 18, 2013)  
Can any one tell the correct syntax of all operators?

Heena said: (Oct 18, 2013)  
max = a>b ? (a>C ? a : c) : (b>c ? b :c);

Now I guess this statement is more clear.

Shashank said: (Nov 19, 2013)  
What if in 'max = a>b ? (a>C ? a : c) : (b>c ? b :c)'

c is greater than both a and b, then the equation will be

max = a>b?c:c

Then how would the statement be evaluated? though the answer is C, I want the steps compiler takes to evaluate it.

DEVIPRIYA said: (Jun 9, 2014)  
Max = a>b?(a>c?a:c):(b<c?b:c).

First it checks a>b if condition is true then Max = (a>c?a:c).

Else Max = (b<c?b:c).

Again it checks the condition like this.

Azhar said: (Jan 13, 2015)  
It is not terminated with ";" (option C).

Merry John said: (Apr 15, 2015)  
I am agree with @Vishwas and @Azhar. Option A should be correct one.

AdamK said: (Sep 6, 2015)  
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.

Mahalakshmi said: (Sep 13, 2015)  
I am not understand please explain clearly about it.

Vibhor said: (Oct 23, 2015)  
Please post any example using this statement.

Anshul Jain said: (Jan 8, 2016)  
Any one explain me. Why option A is wrong?

Guddu said: (Apr 23, 2016)  
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.

Apoorva said: (Aug 18, 2016)  
I think, both the option A and C are correct.

Krishna said: (Sep 7, 2016)  
I have excuted the first option it is correct only.

Prit said: (Sep 11, 2016)  
I can't understand.

Why not to choose option A?

Prit said: (Sep 11, 2016)  
I think both the option A and option C are true.

Deepa said: (Mar 14, 2017)  
I did not understand the meaning of the operator ":" between the variables.

Hemant said: (Jul 29, 2017)  
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.

Mona said: (Aug 18, 2017)  
Option C is also not correct because there is no semi colon in the end of the statement.

Sankar said: (Aug 28, 2017)  
Here the expression is evaluated from right to left.

Reddy said: (Nov 27, 2017)  
C. Also a syntax error because at the semicolon has missed.

Daniel said: (Dec 30, 2017)  
; is missing in the C option.

Sivarenjini said: (Jan 27, 2019)  
C option is also wrong.

There must be semicolon at the end. Then only it can be considered as valid.

Husain said: (Apr 2, 2020)  
A is not the correct answer. It will always generate the error of lvalue required
instead righteous way to write statement A is;

Of course, C, the option can also be termed wrong in syntax for not terminating it with a semicolon.

Laya said: (Jul 15, 2020)  
Can you explain clearly about this?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.