简体   繁体   中英

Comma in variable initialization/declaration

I've stumbled upon a piece of code that look as follows:

    void check()
    {
        int integer = 7;

        //integer2 is not declared anywhere
        int check = integer, integer2;

        //after running
        //check = 7
        //integer = 7
        //integer2 = 0
    }

what's the purpose of the comma here?

Comma on variable declarations simply allows you to declare a second variable of the same type. It is equivalent to:

int check = integer;
int integer2;

As for:

//integer2 is not declared anywhere

Yes it is; right here! This is the declaration of integer2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM