小编Bru*_*rio的帖子

为什么C++编译器在这个简单的程序中没有给出优先级(赋值下的递增运算符)?

根据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 …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-construction operators operator-precedence

4
推荐指数
1
解决办法
738
查看次数