Link to home
Start Free TrialLog in
Avatar of burnedfaceless

asked on

Can someone explain the following statement in C?

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
Avatar of Nitin Sontakke
Nitin Sontakke
Flag of India image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of phoffric

>> 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


                  logical-OR-expression ?  expression :  conditional-expression


   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 .


   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.

Avatar of burnedfaceless


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