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

前端 未结 8 1723
别跟我提以往
别跟我提以往 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:53

    Characters in switch cases are eventually converted to ASCII equivalent decimal i.e

    char '1' - int 49 
    char '2' - int 50
    

    For example, if input is integer int 1 , switch case will switch to default because 1 doesn't satisfy any case.

    1 != 49
    1 != 50
    

    However, in case input is character char '1', output will be the first case as your desire.

提交回复
热议问题