What's the difference between char and int in a switch case?

前端 未结 8 1644
别跟我提以往
别跟我提以往 2021-01-13 11:21

I started C++ recently and while learning switch case, I got this doubt.
What\'s the difference if I use int or char in the following code :
int Fav_Car;

8条回答
  •  -上瘾入骨i
    2021-01-13 11:55

    Are you sure that INT is not working?

    The following code works well:

    #include 
    
    int main() {
        int fav_car = 2;
    switch(fav_car) {
            case 1 :
                std::cout<< "That's cool";
            break;
            case 2 :
                std::cout<< "Even mine!";
            break;
            default :
                std::cout<< "Oh";
            break;
        }
    }
    

    case '1' - it is a symbol

    case "1" - it is a string constant

提交回复
热议问题