• C

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nitin SontakkeDeveloperCommented:
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;
  minMultiple = n2;

Open in new window

Doesn't the one with ternary operator looks nicer? I love it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Subrat (C++ windows/Linux)Software EngineerCommented:
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


                  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.

burnedfacelessAuthor Commented:
Don't need to look at the other comments you explained it like a boss.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.