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

前端 未结 8 1722
别跟我提以往
别跟我提以往 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条回答
  •  盖世英雄少女心
    2021-01-13 11:32

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

    Using char or int are both ok in a switch statement. It depends on how you input your Fav_Car - as long as the input matches with a case, that case will be executed.

    Note that char is also an integral type - it has a value in range [32, 127] (assume you want a printable char).

    what's the difference if I use case '1' : and case "1"

    switch case only work with integral (ie int, char). So:

    case '1':   // ok.
    
    case "1":   // wrong because "1" is a string - not integral type.
    

提交回复
热议问题