Why do we use ternary operator in C?

Status
Not open for further replies.

vimal2010

Banned
Joined
Sep 14, 2005
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
0
why do we use ternary operator in C?whats its significance?whats the difference between = and == in C?
 

Re: ternary operator?

This conditional operator is a little stranged and it's the only one operator with operate three expresions.
Ternary operator is used to substitute code below for example:

int max, a, b;

if (a > b) // if condition is true do it
{
max = a;
}
else // if condition is false
{
max = b;
}

with ternary
max = (a > b) ? a : b;
in this case if a > b are true then max = a, if not max = b

it has the format:
exp1 ? exp2 : exp3;
if exp1 is true the result is exp2, if false the result is exp3



= is used to attribute values
== is used to compare

leomecma
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…