operator-precedence

Is this program having any sequence point issues?

爱⌒轻易说出口 提交于 2019-11-30 20:54:33
问题 #include<stdio.h> int main() { int i=7,j; j=(i++,++i,j*i); return 0; } j=(i++,++i,j*i);Is this well defined ? Let me clear my doubt. 回答1: This expression is OK because the comma operator is a sequence point: (i++, ++i, j*i) However do not confuse it with the following where the comma is not acting as a sequence point: somefunction(i++, ++i, j*i) What about j = i++ * ++i The multiplication operator is not a sequence point. ( Excuse me hijacking your answer ) From §3.4 of ISO 9899:1999 (C

Different results in Java and C++ using += in recursion

为君一笑 提交于 2019-11-30 20:45:38
The very simple Java code as follows has the weird output, but the same logic code in C and C++ has the right output. I try with the JDK 1.7 and JDK 1.3 (relative JRE), the weird output is always there. public class Test { public static int sum=0; public static int fun(int n) { if (n == 1) return 1; else sum += fun(n - 1); // this statement leads to weird output // { // the following block has right output // int tmp = fun(n - 1); // sum += tmp; // } return sum; } public static void main(String[] arg) { System.out.print(fun(5)); } } The output is 1 which should be 8. Relative C/C++ code is as

Chaining Bool values give opposite result to expected

陌路散爱 提交于 2019-11-30 17:29:42
Unthinkingly I wrote some code to check that all the values of a struct were set to 0. To accomplish this I used: bool IsValid() { return !(0 == year == month == day == hour == minute == second); } where all struct members were of type unsigned short. I used the code as part of a larger test but noticed that it was returning false for values differing from zero, and true for values that were all equal to zero - the opposite of what I expected. I changed the code to read: bool IsValid() { return (0 != year) || (0 != month) || (0 != day) || (0 != hour) || (0 != minute) || (0 != second); } But

Explanation of ++val++ and ++*p++ in C

﹥>﹥吖頭↗ 提交于 2019-11-30 15:32:35
问题 int val = 5; printf("%d",++val++); //gives compilation error : '++' needs l-value int *p = &val; printf("%d",++*p++); //no error Could someone explain these 2 cases? Thanks. 回答1: ++val++ is the same as ++(val++) . Since the result of val++ is not an lvalue, this is illegal. And as Stephen Canon pointed out, if the result of val++ were an lvalue, ++(val++) would be undefined behavior as there is no sequence point between the ++ s. ++*p++ is the same as ++(*(p++)) . Since the result of *(p++)

Order of execution in SQL Server variable assignment using SELECT

时光毁灭记忆、已成空白 提交于 2019-11-30 13:43:54
Given the following example: declare @i int select @i = 1, @i = 2 select @i Will @i always be 2? This is about the most trivial example I can think of, but I am considering using this for swapping values in variables. I also believe this method of assignment (select) is not ANSI compliant (however useful), but don't really care to have portable code in this case. UPDATE Thanks to @MichaelFredrickson, we have @MartinSmith's answer and reference to MSDN on this. I am now struggling with what the second sentence in this documentation means, exactly (emphasis added): If there are multiple

Oracle SQL clause evaluation order

≡放荡痞女 提交于 2019-11-30 12:22:36
问题 In Oracle, which clause types get evaluated first? If I had the following ( pretend .... represent valid expressions and relation names ), what would the order of evaluation be? SELECT ... FROM ..... WHERE ........ GROUP BY ........... HAVING ............. ORDER BY ................ I am under the impression that the SELECT clause is evaluated last, but other than that I'm clueless. 回答1: The select list cannot always be evaluated last because the ORDER BY can use aliases that are defined in

PHP7 method_exists Uncaught Error: Function name must be a string

穿精又带淫゛_ 提交于 2019-11-30 11:38:14
I am getting this error: Fatal error: Uncaught Error: Function name must be a string in For this code: if (function_exists($item['function'])) { $item['function']($item, $default); } elseif (method_exists($this, $item['function'])) { $this->$item['function']($item, $default); } I know that changing the code to if (function_exists($item['function'])) { $item['function']($item, $default); } elseif (method_exists($this,$item['function'])) { $this->{$item['function']}($item, $default); } Solved that error, but my question is, should this line $item['function']($item, $default); also be converted

Why in Python does “0, 0 == (0, 0)” equal “(0, False)”?

ぃ、小莉子 提交于 2019-11-30 10:13:59
问题 In Python (I checked only with Python 3.6 but I believe it should hold for many of the previous versions as well): (0, 0) == 0, 0 # results in a two element tuple: (False, 0) 0, 0 == (0, 0) # results in a two element tuple: (0, False) (0, 0) == (0, 0) # results in a boolean True But: a = 0, 0 b = (0, 0) a == b # results in a boolean True Why does the result differ between the two approaches? Does the equality operator handle tuples differently? 回答1: The first two expressions both parse as

How can I add parentheses as the highest level of precedence in a simple grammar?

别等时光非礼了梦想. 提交于 2019-11-30 09:34:18
问题 I'm trying to add 2 things to my grammar: Unary minus sign, i.e. '-', and Parentheses Here's my grammar so far: <comp> ::= <expr> | <comp> <op0> <expr> <expr> ::= <term> | <expr> <op1> <term> <term> ::= <darg> | <term> <op2> <darg> <darg> ::= <digit> | <darg> <digit> <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 <op0> ::= > | < | =< | => | = <op1> ::= + | - <op2> ::= * | / I've tried everything and can't figure this out. How can I make the unary minus sign be at the highest level of

What is the Operator Precedence of Await?

允我心安 提交于 2019-11-30 08:09:56
In Javascript certain operators are processed before others: 1 + 2 * 3 // 1 + (2 * 3) // 7 because * has higher precedence than + 1 === 0 + 1 // 1 === (0 + 1) // true because + has a higher precedence than === The MDN has a full breakdown of all operators and their precedence ... except await . await getFoo() * 2; // await (getFoo() * 2) or (await getFoo()) * 2? await getFoo() === 5; // await (getFoo() === 5) or (await getFoo()) === 5? Can anyone explain which operators are processed before/after await? Right now I feel like I have to add a bunch of parenthesis that are probably unnecessary