Search code examples
c++11comma-operator

C++ comma operator


I'm trying to run this code from 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;
}

Why I'm seeing this error expected unqualified-id before numeric constant int cats = 17,240; , I don't know, I need a short explanation. Thanks


Solution

  • int cats = 17,240; would be viewed by the compiler as int (cats = 17),240; due to operator precedence. And int 240; makes no sense, so a compiler diagnostic is issued.

    Did you want 17240 cats? If so then drop the comma.