Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 594 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Are all parameters in ternary operator mandatory or can you do "(exp1 ? : value)"?

#1
I would like to know if in the ternary operator in language C all parameters are mandatory?
e.g.:

(exp1 ? : value2);
or you need to write:

(expr1 ? value1: value2);

I asked that because if you write: `(exp1 ? : value2);` What return if the `expr1` is _TRUE_?
Reply

#2
The statement

(exp1 ? : value2);
is equivalent to

(exp1 ? exp1 : value2);
It is a GCC extension. The only difference in both of them is that `exp1` will be evaluated only once in the former statement.
Reply

#3
It not a standard, but [GCC extension][1] (may be some other compilers do the same):

> 5.7 Conditionals with Omitted Operands
>
> The middle operand in a conditional expression may be omitted. Then if
> the first operand is nonzero, its value is the value of the
> conditional expression.
>
> Therefore, the expression
>
> x ? : y
> has the value of x if that is nonzero; otherwise, the value of y.
>
> This example is perfectly equivalent to
>
> x ? x : y


Edit:

As @MadPhysicist pointed, that shortened form would evaluate the `x` once, while the traditional form would re-evaluate the `x` second time when `x` is non-zero

[1]:

[To see links please register here]

Reply

#4
The C standard (referring to `C11`) does not mention anything about omitting the second operand. It specifies about all the three operands for the form

> _conditional-expression:_ <br>
>                   _logical-OR-expression_ <br>
>                  _logical-OR-expression_ `?` _expression_ `:` _conditional-expression_


You are talking about a [compiler extension](

[To see links please register here]

). The form

(exp1 ? : value2);

actually returns `exp1`, if `exp1` evaluates to TRUE.

Quoting the online documentation for `gcc`,

> The middle operand in a conditional expression may be omitted. [...]
>
> Therefore, the expression
>
> x ? : y
>
> has the value of `x` if that is nonzero; otherwise, the value of `y`.

Just to add a bit of context in ___why___ or ___when___ this can be useful,

> [...] When it becomes useful is when the first operand does, or may (if it is a macro argument), contain a side effect. Then repeating the operand in the middle would perform the side effect twice. Omitting the middle operand uses the value already computed without the undesirable effects of recomputing it.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through