#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 …Run Code Online (Sandbox Code Playgroud) c increment operator-precedence undefined-behavior sequence-points
请考虑以下示例:
class Quirky {
public static void main(String[] args) {
int x = 1;
int y = 3;
System.out.println(x == (x = y)); // false
x = 1; // reset
System.out.println((x = y) == x); // true
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定Java语言规范中是否有一个项目要求加载变量的先前值以与右侧(x = y)进行比较,右侧()按括号隐含的顺序首先计算.
为什么第一个表达式评估false,但第二个表达式评估为true?我本来期望(x = y)先评估,然后它会x与自己(3)进行比较并返回true.
这个问题与Java表达式中子表达式的评估顺序不同,这x绝对不是这里的"子表达式".需要加载它以进行比较而不是"评估".这个问题是特定于Java的,而且这个表达式x == (x = y)不同于通常为棘手的面试问题精心设计的不切实际的结构,而是来自一个真实的项目.它应该是比较和替换成语的单行替代品
int oldX = x;
x = y;
return …Run Code Online (Sandbox Code Playgroud) 以下for循环产生相同的结果,即使一个使用后增量和另一个预增量.
这是代码:
for(i=0; i<5; i++) {
printf("%d", i);
}
for(i=0; i<5; ++i) {
printf("%d", i);
}
Run Code Online (Sandbox Code Playgroud)
我为两个'for'循环得到了相同的输出.我错过了什么吗?
以下两个陈述是否相同?
SELECT [...]
FROM [...]
WHERE some_col in (1,2,3,4,5) AND some_other_expr
Run Code Online (Sandbox Code Playgroud)
和
SELECT [...]
FROM [...]
WHERE some_col in (1,2,3) or some_col in (4,5) AND some_other_expr
Run Code Online (Sandbox Code Playgroud)
我可以使用某种真值表来验证这一点吗?
当我看到这个问题的答案时,我发现我不明白自己的答案.
我真的不明白这是如何被解析的.为什么第二个示例返回False?
>>> 1 in [1,0] # This is expected
True
>>> 1 in [1,0] == True # This is strange
False
>>> (1 in [1,0]) == True # This is what I wanted it to be
True
>>> 1 in ([1,0] == True) # But it's not just a precedence issue!
# It did not raise an exception on the second example.
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
1 in ([1,0] == …Run Code Online (Sandbox Code Playgroud) ANSI标准是否要求逻辑运算符在C或C++中被短路?
我很困惑,因为我记得K&R的书说你的代码不应该依赖于这些操作被短路,因为它们可能没有.有人可以指出标准中的哪个位置逻辑操作始终是短路的吗?我最感兴趣的是C++,C的答案也很棒.
我还记得读(不记得在哪里)评估顺序没有严格定义,所以你的代码不应该依赖或假设表达式中的函数将按特定的顺序执行:在语句的末尾所有引用的函数将被调用,但编译器可以自由选择最有效的顺序.
标准是否表明该表达式的评估顺序?
if( functionA() && functionB() && functionC() ) cout<<"Hello world";
Run Code Online (Sandbox Code Playgroud) c c++ operator-precedence short-circuiting logical-operators
我目前正在学习使用C++ Primer一书的C++,本书的其中一个练习是:
解释以下表达式的作用:
someValue ? ++x, ++y : --x, --y
我们知道什么?我们知道三元运算符的优先级高于逗号运算符.使用二元运算符这很容易理解,但是对于三元运算符,我有点挣扎.使用二元运算符"具有更高的优先级"意味着我们可以使用具有更高优先级的表达式周围的括号,并且它不会更改执行.
对于三元运算符,我会这样做:
(someValue ? ++x, ++y : --x, --y)
Run Code Online (Sandbox Code Playgroud)
有效地产生相同的代码,这无法帮助我理解编译器如何对代码进行分组.
但是,通过使用C++编译器进行测试,我知道表达式编译并且我不知道:运算符本身可以代表什么.所以编译器似乎正确地解释了三元运算符.
然后我以两种方式执行程序:
#include <iostream>
int main()
{
bool someValue = true;
int x = 10, y = 10;
someValue ? ++x, ++y : --x, --y;
std::cout << x << " " << y << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果是:
11 10
Run Code Online (Sandbox Code Playgroud)
而另一方面someValue = false它与它打印:
9 9
Run Code Online (Sandbox Code Playgroud)
为什么C++编译器生成的代码只能为三元运算符的真分支递增x,而对于三元的假分支,它会减少x和y?
我甚至把括号括在真正的分支周围,就像这样:
someValue ? …Run Code Online (Sandbox Code Playgroud) c++ conditional-operator operator-precedence language-lawyer
我似乎无法将这个代码的第一部分(+ =)与三元运算符结合起来.
h.className += h.className ? ' error' : 'error'
Run Code Online (Sandbox Code Playgroud)
我认为这段代码的工作方式如下:
h.className = h.className + h.className ? ' error' : 'error'
Run Code Online (Sandbox Code Playgroud)
但这不正确,因为这会在我的控制台中出错.
所以我的问题是我应该如何正确地插入这段代码?
javascript variable-assignment conditional-operator operator-precedence compound-assignment
在Python中(我只使用Python 3.6进行了检查,但我相信它应该适用于许多以前的版本):
(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
Run Code Online (Sandbox Code Playgroud)
但:
a = 0, 0
b = (0, 0)
a == b # results in a boolean True
Run Code Online (Sandbox Code Playgroud)
为什么两种方法的结果不同?相等运算符是否以不同方式处理元组?
该程序的输出:
#include <iostream>
class c1
{
public:
c1& meth1(int* ar) {
std::cout << "method 1" << std::endl;
*ar = 1;
return *this;
}
void meth2(int ar)
{
std::cout << "method 2:"<< ar << std::endl;
}
};
int main()
{
c1 c;
int nu = 0;
c.meth1(&nu).meth2(nu);
}
Run Code Online (Sandbox Code Playgroud)
方法是:
method 1
method 2:0
Run Code Online (Sandbox Code Playgroud)
开始nu时为什么不是1 meth2()?