c++ - Is there a variable type for the constant in a "case" in the switch-case? -



c++ - Is there a variable type for the constant in a "case" in the switch-case? -

this question has reply here:

how switch-case handle value of constant in “case”? [duplicate] 4 answers

lets have :

unsigned char test; test=0xff; switch (test) { case -1: cout<<"hit 1"; break; case 255: cout<<"hit 2"; break; }

why gives out "hit 2" while -1 represent 0xff in memory unsigned char. there type constant (here -1 , 255) in "case" in switch-case? far know, without variable types, cannot compare 2 variables.

the standard says:

6.4.2 switch statement [stmt.switch]

the switch statement causes command transferred 1 of several statements depending on value of condition.

the status shall of integral type, enumeration type, or class type. if of class type, status contextually implicitly converted (clause 4) integral or enumeration type. integral promotions performed. statement within switch statement can labeled 1 or more case labels follows:

case constant-expression :

where constant-expression shall converted constant look (5.19) of promoted type of switch condition. no 2 of case constants in same switch shall have same value after conversion promoted type of switch condition.

the info type int, because smaller integral types promote int.

c++ switch-statement

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -