C ++逗号运算符

时间:2017-02-21 13:52:54

标签: c++11 comma-operator

我正在尝试从C ++ Primer plus

运行此代码
#include <iostream>
using namespace std;

int main() {
    int i = 20, j= 2*i;
    cout << "i = " << i << endl;
    int cats = 17,240;  //No, I don't want the number 17240
    return 0;
}

为什么我在数字常量 int cats = 17,240;之前看到此错误期望的非限定ID,我不知道,我需要一个简短的解释。感谢

1 个答案:

答案 0 :(得分:2)

由于运算符优先级

int cats = 17,240;将被编译器视为int (cats = 17),240;。并且int 240;没有意义,因此发出了编译器诊断。

你想要猫17240吗?如果是,则删除逗号。