Bru*_*rio 4 c++ compiler-construction operators operator-precedence
根据C/C++语言中运算符的优先级表(参见Wikipedia),增量运算符(++)优先于赋值运算符(=).
有人可以解释为什么编译器首先分配值(bill [x]中的1),然后在这个简单的程序中增加索引值(i ++).我认为它应该是相反的(先增加再分配):
#include <iostream>
using namespace std;
int bill[] = {16, 17, 18, 19, 20};
int main ()
{
int i = 3;
bill[(i++)] = 1; // I think it should be bill[4] = 1;
cout << bill[0] << endl;
cout << bill[1] << endl;
cout << bill[2] << endl;
cout << bill[3] << endl;
cout << bill[4] << endl;
cout << "Index value:" << i << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
16
17
18
1
20
Index value:4
Run Code Online (Sandbox Code Playgroud)
我做错了什么?