Avatar of burnedfaceless
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
C

Avatar of undefined
Last Comment
burnedfaceless

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Nitin Sontakke

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Subrat (C++ windows/Linux)

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

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.

burnedfaceless

ASKER
Don't need to look at the other comments you explained it like a boss.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck