Can someone explain the following statement in C?

burnedfaceless
burnedfaceless used Ask the Experts™
on
I came across a statement I've seen before but I've never learned.

Can someone explain it?

//maximum number between n1 and n2 is stored in minmultiple
minMultiple = (n1>n2) ? n1 : n2;

Open in new window


edit: this statement in general a good answer would be the statement and how it could be expanded - that would really help me out
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
This is called a ternary operator. For details you may wish to google the term.

Syntax is: variable = condition ? true part : false part;

It can be rewritten as follows:

 
if(n1 > n2)
  minMultiple = n1;
else
  minMultiple = n2;

Open in new window


Doesn't the one with ternary operator looks nicer? I love it.
adding to the above comment.

ternary operator is an operator which operates upon 3 operands. It is represented by ? :


condition ? statement1  : statement2;
here condition, statement1,statement2 are 3 operands.


statement1 is executed if your condition is true.
statement2 executes, if your condition is false.

it's one of the drawback is debugging.
during debugging, you can't find which statement is executing as it is written in a single line.
using watch window or output window, you can guess which one is execute.

As said above, it is representing single line if else instructions.
>> condition ? statement1  : statement2;
"statement1" and "statement2" is not correct. Here is the correct definition from the ANSI C specification. Saying "expression" instead of "statement" would have been much closer to the specification of ?:.

3.3.15 Conditional operator

Syntax

          conditional-expression:
                  logical-OR-expression
                  logical-OR-expression ?  expression :  conditional-expression

Constraints

   The first operand shall have scalar type.

   One of the following shall hold for the second and third operands:

 * both operands have arithmetic type;

 * both operands have compatible structure or union types;

 * both operands have void type;

 * both operands are pointers to qualified or unqualified versions of
   compatible types;

 * one operand is a pointer and the other is a null pointer constant; or

 * one operand is a pointer to an object or incomplete type and the
   other is a pointer to a qualified or unqualified version of void .

Semantics

   The first operand is evaluated; there is a sequence point after its
evaluation.  The second operand is evaluated only if the first
compares unequal to 0; the third operand is evaluated only if the
first compares equal to 0; the value of the second or third operand
(whichever is evaluated) is the result./41/

   If both the second and third operands have arithmetic type, the
usual arithmetic conversions are performed to bring them to a common
type and the result has that type.  If both the operands have
structure or union type, the result has that type.  If both operands
have void type, the result has void type.

   If both the second and third operands are pointers or one is a null
pointer constant and the other is a pointer, the result type is a
pointer to a type qualified with all the type qualifiers of the types
pointed-to by both operands.  Furthermore, if both operands are
pointers to compatible types or differently qualified versions of a
compatible type, the result has the composite type; if one operand is
a null pointer constant, the result has the type of the other operand;
otherwise, one operand is a pointer to void or a qualified version of
void, in which case the other operand is converted to type pointer to
void, and the result has that type.

Author

Commented:
Don't need to look at the other comments you explained it like a boss.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial