Why does C not allow concatenating strings when using the conditional operator?

As per the C11 standard, chapter §5.1.1.2, concatenation of adjacent string literals: Adjacent string literal tokens are concatenated. happens in translation phase. On the other hand: printf(“Hi” (test ? “Bye” : “Goodbye”)); involves the conditional operator, which is evaluated at run-time. So, at compile time, during the translation phase, there are no adjacent string literals … Read more

What does the question mark character (‘?’) mean in C++?

This is commonly referred to as the conditional operator, and when used like this: condition ? result_if_true : result_if_false … if the condition evaluates to true, the expression evaluates to result_if_true, otherwise it evaluates to result_if_false. It is syntactic sugar, and in this case, it can be replaced with int qempty() { if(f == r) … Read more

Operator precedence with JavaScript’s ternary operator

Use: h.className = h.className + (h.className ? ‘ error’ : ‘error’) You want the operator to work for h.className. Better be specific about it. Of course, no harm should come from h.className += ‘ error’, but that’s another matter. Also, note that + has precedence over the ternary operator: JavaScript Operator Precedence

Why does the ternary operator with commas evaluate only one expression in the true case?

As @Rakete said in their excellent answer, this is tricky. I’d like to add on to that a little. The ternary operator must have the form: logical-or-expression ? expression : assignment-expression So we have the following mappings: someValue : logical-or-expression ++x, ++y : expression ??? is assignment-expression –x, –y or only –x? In fact it … Read more

Booleans, conditional operators and autoboxing

The difference is that the explicit type of the returnsNull() method affects the static typing of the expressions at compile time: E1: `true ? returnsNull() : false` – boolean (auto-unboxing 2nd operand to boolean) E2: `true ? null : false` – Boolean (autoboxing of 3rd operand to Boolean) See Java Language Specification, section 15.25 Conditional … Read more