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

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

    In this case: none; there is no difference in the way your program will execute its switch statement. Both char and int are Integral Types: they represent integers. char is typically unsigned and at least 8 bits (0-255) and int is signed and typically 32-bits (-2 billion to + 2 billion).

    Note that char represents a single character, not a string; and that you cannot use a string value in a switch statement in the way you can in C#, Java and Swift, as switch compiles-down to an in-memory hashtable for ultra-fast performance, that optimization cannot be done with string types currently.

提交回复
热议问题