Xavier Roche wrote: > Is there a C standard expert who can confirm what shall return > 0.0 ? 1 : 0 > > With gcc 4.3/Linux, the result is 0. With Visual C++2010, the result is.. 1. > > Is the result undefined by the standard, by chance ? Lemme see... 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. [...] 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]. [41] A conditional expression does not yield an lvalue. 0.0 is a 'floating-constant' which is a kind of 'primary-expression' which is a kind of 'logical-OR-expression'. (Scalar type means integral or floating or pointer.) Since 0.0 (the floating constant) compares equal to 0 (the integral constant) (0.0 ? 1 : 0) has value 0, as far as I can tell. Regards.

0 |

7/20/2012 1:58:50 PM

Xavier Roche wrote: > On 07/20/2012 03:58 PM, Noob wrote: >> Since 0.0 (the floating constant) compares equal to 0 (the integral constant) >> (0.0 ? 1 : 0) has value 0, as far as I can tell. > > Thanks a lot to both of you. It appears to be a bug in Visual C++ 2010 > (*) then. > > This impression is confirmed when trying: > static double zero = 0.0; > .. > zero ? 0 : 1 > which returns the correct value (0). NB: You swapped 0 and 1 there. ( 0.0 ? 42 : 666) evaluates to 666 (zero ? 42 : 666) evaluates to 666

0 |

7/20/2012 2:22:40 PM