1

Possible Duplicate:
Why would you use the ternary operator without assigning a value for the “true” condition (x = x ?: 1)

In one's book I saw the vague (for me) syntax of ternary operator usage:

int nr = nr ? : 1; /* allowed shortcut, same as "nr ? nr : 1" */

What exactly this mean? Somewhere in the code the 'nr' variable is declared and it's initial value is based on the comparison result whether the 'nr' (which has a junk inside it, I guess O_o) is not equal to zero... And if so then what value it would get?

Community
  • 1
  • 1
mesmerizingr
  • 1,417
  • 1
  • 18
  • 25
  • That line is also declaring nr, so it is being used before it is initialized, which means that line could result in either branch being taken with no predictability. Doesn't seem like a particularly useful line of code to me. – Ed S. Oct 11 '12 at 21:10
  • not a dupe" this one is declaring a variable and inits it with itself! that one does not... – Johannes Schaub - litb Oct 11 '12 at 21:11
  • 2
    @JohannesSchaub-litb: I think the point of question is in regards to what happens if `nr` evaluates to `true`, i.e., what value is assigned. That is answered by the spec for the extension. – Ed S. Oct 11 '12 at 21:12
  • @EdS that is highly questionable since *that* answer is given in this question already, as a c comment in the example code.. – Johannes Schaub - litb Oct 11 '12 at 21:18
  • @JohannesSchaub-litb: I don't think so. Quote: *"And if so then what value it would get?"*. The OP wants to know what value will be assigned to `nr` if the expression `nr` evaluates to true. The OP recognizes that `nr` is uninitialized, he wants to know what happens in the true branch. – Ed S. Oct 11 '12 at 21:19
  • also please dont close questions as duplicates merely because of a feeling that a question might be after something or might be not. please ask the questioner to clarify instead. – Johannes Schaub - litb Oct 11 '12 at 21:20
  • @JohannesSchaub-litb: I didn't actually vote to close, but it seems obvious to me that the linked duplicate answers this question perfectly. `nr` will retain its value if the expression `nr` evaluates to true. – Ed S. Oct 11 '12 at 21:21
  • @EdS since the true branch is nr and nr is uninitialized, that question is entirely sensible to ask. i dont understand your argumemt at all. – Johannes Schaub - litb Oct 11 '12 at 21:22
  • @JohannesSchaub-litb: I'm not sure what you don't understand. Whether or not `nr` is initialized is irrelevant. The extension states that, if the branch is omitted, the current value is returned. So yes, `nr` may have an indeterminate value, but it does *have* a value, and that will be retained should that branch be taken. – Ed S. Oct 11 '12 at 21:24
  • @ed.s yes but the question was *what* value it will have. an indeterminate value is an indeterminate answer to that question. and AFAIK an indeterminated value isnt necessarily a value. it may also be a trap representation. – Johannes Schaub - litb Oct 11 '12 at 21:31
  • @JohannesSchaub-litb: Indeterminate just means it could be anything. That anything will be a legal (for the type), discrete value however. – Ed S. Oct 11 '12 at 21:51
  • 1
    @EdS If `int` has trap representations, the bits in that location may indeed constitute one, "indeterminate value" is defined as "either an unspecified value or a trap representation" in 3.19.2 (n1570). Anyway, it's UB per Annex J.2 "Undefined behavior": "The value of an object with automatic storage duration is used while it is indeterminate (6.2.4, 6.7.9, 6.8)". (Yes, Annex J is informative only, but it's pretty unambiguous here.) – Daniel Fischer Oct 11 '12 at 21:54
  • @DanielFischer: I was just looking into the same thing after Johannes last comment. Thanks for that (both of you), I didn't appreciate "trap representations" at all. This is why I comment on seemingly trivial matters, never know when you will learn something :) – Ed S. Oct 11 '12 at 22:03
  • @EdS. On Stack Overflow, approximately every two-and-a-half hours is my experience ;) – Daniel Fischer Oct 11 '12 at 22:04

1 Answers1

2

This is an extension to the ternaray operator that allows the second operand to be omitted, as noted in the comment.

This:

int nr = nr ? : 1;

Is equivalent to:

int nr = nr ? nr : 1;

I believe that this is a GCC-specific extension, here's the GCC extension page for it.

As others have pointed out in the comments, since nr is being declared and it's value used in the declaration, the result of this line is unpredictable.

pb2q
  • 58,613
  • 19
  • 146
  • 147