user5767271
user5767271

Reputation:

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

Upvotes: 0

Views: 95

Answers (1)

Bathsheba
Bathsheba

Reputation: 234875

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.

Upvotes: 2

Related Questions