operator-precedence

order of evaluation of function parameters

烂漫一生 提交于 2019-11-26 18:36:30
问题 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. 回答1: 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

“IF” argument evaluation order?

*爱你&永不变心* 提交于 2019-11-26 18:08:18
问题 if(a && b) { do something; } is there any possibility to evaluate arguments from right to left(b -> a)? if "yes", what influences the evaluation order? (i'm using VS2008) 回答1: The evaluation order is specified by the standard and is left-to-right . The left-most expression will always be evaluated first with the && clause. If you want b to be evaluated first: if(b && a) { //do something } If both arguments are methods and you want both of them to be evaluated regardless of their result: bool

Unexpected order of evaluation (compiler bug?) [duplicate]

只愿长相守 提交于 2019-11-26 17:57:10
Possible Duplicate: Undefined Behavior and Sequence Points I'm not sure if this is a gcc bug or not, so I'll ask: unsigned int n = 0; std::cout << n++ << n << ++n; gcc gives the extremely strange result: "122" which AFAICT is impossible. Because << is left associative, it should be the same as: operator<<(operator<<(operator<<(std::cout, n++), n), ++n) and because there is a sequence point before and after evaluating arguments, n is never modified twice (or even accessed) between two sequence points -- so it shouldn't be undefined behaviour, just the order of evaluation unspecified. So AFAICT

Precedence of && over ||

只愿长相守 提交于 2019-11-26 17:54:28
As I know logical operator && has higher precedence than || . On running the code: #include <stdio.h> int main() { int i = 1, j =1, k = 1; printf("%d\n",++i || ++j && ++k); printf("%d %d %d",i,j,k); return 0; } is giving the output: 1 2 1 1 which is possible only when ++i || ++j && ++k is evaluated like this: (++i) || (++j && ++k) But, according to operator precedence rule it should be evaluated as: (++i || ++j) && (++k) and hence output should be: 1 2 1 2 What is going wrong with this? NOTE: As per my understanding I think an operator of higher precedence evaluated as follows( if it is left

order of evaluation of operands

╄→гoц情女王★ 提交于 2019-11-26 17:54:18
In the expression a + b , is a guaranteed to be evaluated before b , or is the order of evaluation unspecified? I think it is the latter, but I struggle to find a definite answer in the standard. Since I don't know whether C handles this different from C++, or if evaluation order rules were simplified in C++11, I'm gonna tag the question as all three. Peter Alexander In C++, for user-defined types a + b is a function call, and the standard says: §5.2.2.8 - [...] The order of evaluation of function arguments is unspecified . [...] For normal operators, the standard says: §5.4 - Except where

How do we explain the result of the expression (++x)+(++x)+(++x)?

自古美人都是妖i 提交于 2019-11-26 17:33:01
问题 x = 1; std::cout << ((++x)+(++x)+(++x)); I expect the output to be 11 , but it's actually 12 . Why? 回答1: We explain it by expecting undefined behaviour rather than any particular result. As the expression attempts to modify x multiple times without an intervening sequence point its behaviour is undefined. 回答2: As others have said, the C and C++ standards do not define the behaviour that this will produce. But for those people who don't see why the standards would do such a thing, let's go

What's the precedence of ruby's method call

江枫思渺然 提交于 2019-11-26 17:17:03
问题 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

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

纵然是瞬间 提交于 2019-11-26 16:57:04
问题 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++. 回答1: 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

Haskell: Parse error in pattern

送分小仙女□ 提交于 2019-11-26 16:43:12
Who likes to tell me what is wrong with this code (syntactically)? -- merge two sorted lists mergeX [] b res = b ++ res mergeX a [] res = a ++ res mergeX a:as b:bs res | a > b = mergeX as b:bs a:res | otherwise = mergeX a:as bs b:res Interpreter: Parse error in pattern: mergeX You need some parenthesis: mergeX [] b res = b ++ res mergeX a [] res = a ++ res mergeX (a:as) (b:bs) res | a > b = mergeX as (b:bs) (a:res) | otherwise = mergeX (a:as) bs (b:res) The reason is because : has a lower precedence than function application, so mergeX a:as b:bs res will be parsed as: (mergeX a):(as b):(bs res

Java operator precedence guidelines

送分小仙女□ 提交于 2019-11-26 16:37:42
Misunderstanding Java operator precedence is a source of frequently asked questions and subtle errors. I was intrigued to learn that even the Java Language Specification says, "It is recommended that code not rely crucially on this specification." JLS §15.7 Preferring clear to clever , are there any useful guidelines in this area? Here are a number of resources on the topic: JLS Operators JLS Precedence Java Glossary Princeton Oracle Tutorial Conversions and Promotions Java Operator Precedence Evaluation Order and Precedence Usenet discussion Additions or corrections welcome. Neil Coffey As