operator-precedence

Who defines C operator precedence and associativity?

為{幸葍}努か 提交于 2019-11-27 19:22:08
Introduction In every textbook on C/C++, you'll find an operator precedence and associativity table such as the following: http://en.cppreference.com/w/cpp/language/operator_precedence One of the questions on StackOverflow asked something like this: What order do the following functions execute: f1() * f2() + f3(); f1() + f2() * f3(); Referring to the previous chart I confidently replied that functions have left-to-right associativity so in the previous statements the are evaluated like this in both cases: f1() -> f2() -> f3() After the functions are evaluated you finish the evaluation like

Why is $a + ++$a == 2?

不想你离开。 提交于 2019-11-27 17:40:18
If I try this: $a = 0; echo $a + ++$a, PHP_EOL; echo $a; I get this output: 2 1 Demo: http://codepad.org/ncVuJtJu Why is that? I expect to get this as an output: 1 1 My understanding: $a = 0; // a === 0 echo $a + ++$a, PHP_EOL; // (0) + (0+1) === 1 echo $a; // a === 1 But why isn't that the output? All the answers explaining why you get 2 and not 1 are actually wrong. According to the PHP documentation, mixing + and ++ in this manner is undefined behavior, so you could get either 1 or 2. Switching to a different version of PHP may change the result you get, and it would be just as valid. See

Problem with operator precedence [duplicate]

谁都会走 提交于 2019-11-27 16:29:06
This question already has an answer here: Why does “++x || ++y && ++z” calculate “++x” first, even though operator “&&” has higher precedence than “||” 11 answers The O/p comes out to be x=2,y=1,z=1 which doesnt agree with the operator precedence. I was running this on Turbo c++ compiler: void main() { int x,y,z,q; x=y=z=1; q=++x || ++y && ++z; printf("x=%d y=%d z=%d",x,y,z); } Operator precedence does not in any way determine the order in which the operators are executed. Operator precedence only defines the grouping between operators and their operands. In your case, operator precedence says

order of evaluation of function parameters

柔情痞子 提交于 2019-11-27 16:19:43
What will be printed as the result of the operation below: x=5; printf("%d,%d,%d\n",x,x<<2,x>>2); Answer: 5,20,1 I thought order is undefined yet I found above as interview question on many sites. From the C++ standard: The order of evaluation of arguments is unspecified. All side effects of argument expression evaluations take effect before the function is entered. The order of evaluation of the postfix expression and the argument expression list is unspecified. However, your example would only have undefined behavior if the arguments were x>>=2 and x<<=2 , such that x were being modified.

Do parentheses force order of evaluation and make an undefined expression defined?

≯℡__Kan透↙ 提交于 2019-11-27 16:05:36
问题 I was just going though my text book when I came across this question What would be the value of a after the following expression ? Assume the initial value of a = 5.Mention the steps a+=(a++)+(++a) At first I thought this is undefined behaviour because a has been modified more than once. So then I read the question and it said Mention the steps so I probably thought this question is right. So my question is : Does applying parentheses make an undefined behaviour defined ? Is a sequence point

What's the precedence of ruby's method call

爷,独闯天下 提交于 2019-11-27 15:51:06
http://phrogz.net/programmingruby/language.html#table_18.4 The table provided by the link above only gives the precedence of ruby's operators. What's the precedence of a method(or should I say: a message/ function) ? For example, when I input something as below in irb Math.sqrt 2 + 2 I got 2.0 as the result. Without the definite rules of the precedence, I just can't decide where to use the parens and where to omit them. So, someone please help me get rid of this uncertainty. Thanks in advance! Any operator has precedence over the method call. It is highly recommended to use () for method calls

Multiple assignment confusion

爱⌒轻易说出口 提交于 2019-11-27 14:57:14
问题 I understand that the assignment operator is right associative. So for example x = y = z = 2 is equivalent to (x = (y = (z = 2))) That being the case, I tried the following: foo.x = foo = {a:1} I expected that the object foo would be created with value {a:1} and then the property x will be created on foo which will just be a reference to the foo object. (This is actually what happens if I was to separate the multiple assignment statement into two separate statements foo = {a:1};foo.x = foo; )

Is the right-hand side of an assignment always evaluated before the assignment?

不想你离开。 提交于 2019-11-27 14:53:58
Here is a code snippet. x = {} x[1] = len(x) print x {1: 0} Is this well defined? That is, could x == {1: 1} instead? Because I remember that an equivalent program in C++ '98 (if we use std::map ) has undefined behaviour. The output of the program was different when compiled with VS compiler and G++. As I mentioned in a comment, this test case can be reduced to: x = {} x[1] = len(x) The question then becomes, is x[1] == 0 , or is x[1] == 1 ? Let's look at the relevant 2.x documentation and 3.x documentation : Python evaluates expressions from left to right. Notice that while evaluating an

Potential Problem in “Swapping values of two variables without using a third variable”

China☆狼群 提交于 2019-11-27 14:38:21
I recently came along this method for swapping the values of two variables without using a third variable. a^=b^=a^=b But when I tried the above code on different compilers, I got different results, some gave correct results, some didn't. Is anything terribly wrong with the code? Prasoon Saurav Is anything terribly wrong with the code? Yes! a^=b^=a^=b in fact invokes Undefined Behaviour in C and in C++ because you are trying to change the value of a more than once between two sequence points. Try writing (although not foolproof ) a ^= b; b ^= a; a ^= b; instead of a^=b^=a^=b . P.S : Never try

Why is the order of evaluation for function parameters unspecified in c++?

允我心安 提交于 2019-11-27 14:07:13
The standard doesn't specify the order of evaluation of arguments with this line: The order of evaluation of arguments is unspecified. What does Better code can be generated in the absence of restrictions on expression evaluation order imply? What is the drawback in asking all the compilers to evaluate the function arguments Left to Right for example? What kinds of optimizations do compilers perform because of this unspecified spec? Allowing the compiler to re-order the evaluation of the operands adds more room for optimization. Here's a completely made up example for illustration purposes.