operator-precedence

Is short-circuiting logical operators mandated? And evaluation order?

馋奶兔 提交于 2019-11-25 22:55:06
问题 Does the ANSI standard mandate the logical operators to be short-circuited, in either C or C++? I\'m confused for I recall the K&R book saying your code shouldn\'t depend on these operations being short circuited, for they may not. Could someone please point out where in the standard it\'s said logic ops are always short-circuited? I\'m mostly interested on C++, an answer also for C would be great. I also remember reading (can\'t remember where) that evaluation order isn\'t strictly defined,

Why are these constructs using pre and post-increment undefined behavior?

不打扰是莪最后的温柔 提交于 2019-11-25 22:09:44
问题 #include <stdio.h> int main(void) { int i = 0; i = i++ + ++i; printf(\"%d\\n\", i); // 3 i = 1; i = (i++); printf(\"%d\\n\", i); // 2 Should be 1, no ? volatile int u = 0; u = u++ + ++u; printf(\"%d\\n\", u); // 1 u = 1; u = (u++); printf(\"%d\\n\", u); // 2 Should also be one, no ? register int v = 0; v = v++ + ++v; printf(\"%d\\n\", v); // 3 (Should be the same as u ?) int w = 0; printf(\"%d %d\\n\", ++w, w); // shouldn\'t this print 1 1 int x[2] = { 5, 8 }, y = 0; x[y] = y ++; printf(\"%d

What are the rules for evaluation order in Java?

旧巷老猫 提交于 2019-11-25 22:08:28
问题 I am reading some Java text and got the following code: int[] a = {4,4}; int b = 1; a[b] = b = 0; In the text, the author did not give a clear explanation and the effect of the last line is: a[1] = 0; I am not so sure that I understand: how did the evaluation happen? 回答1: Let me say this very clearly, because people misunderstand this all the time: Order of evaluation of subexpressions is independent of both associativity and precedence . Associativity and precedence determine in what order

Why does “++x || ++y && ++z” calculate “++x” first, even though operator “&&” has higher precedence than “||”

独自空忆成欢 提交于 2019-11-25 19:56:55
Why does ++x || ++y && ++z calculate ++x first, even though the precedence of operator && is higher than || ? Unwind , R and others have explained what really happens. So let me just add: The premise of your question is faulty. The fact that the && has higher precedence doesn't mean that operands that surround it must be evaluated before any operands in the expression with lower precedence. Even where the special case short-circuiting of || and && this wouldn't necessarily be so. For example, consider a=b+c+d*e ; * has higher precedence than + , but that doesn't mean that d*e must be evaluated