Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Ternary conditional operator
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===C=== A traditional if-else construct in [[C (programming language)|C]] is written: <syntaxhighlight lang="c"> if (a > b) { result = x; } else { result = y; } </syntaxhighlight> This can be rewritten as the following statement: <syntaxhighlight lang="c"> result = a > b ? x : y; </syntaxhighlight> As in the if-else construct only one of the expressions 'x' and 'y' is evaluated. This is significant if the evaluation of 'x' or 'y' has [[Side effect (computer science)|side effect]]s.<ref name="ISO/IEC 9899:1999">ISO.IEC 9899:1999 (E) 6.5.15.4</ref> The behaviour is undefined if an attempt is made to use the result of the conditional operator as an [[Value (computer science)#lrvalue|lvalue]].<ref name="ISO/IEC 9899:1999"/> A [[GNU Project|GNU]] extension to C allows omitting the second operand, and using implicitly the first operand as the second also: <syntaxhighlight lang="c"> a = x ? : y; </syntaxhighlight> The expression is equivalent to <syntaxhighlight lang="c"> a = x ? x : y; </syntaxhighlight> except that expressions ''a'' and ''x'' are evaluated only once. The difference is significant if evaluating the expression has side effects. This shorthand form is sometimes known as the [[Elvis operator]] in other languages.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)