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;
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.
Your problem is that '1'
is not the same thing as 1
.
'1'
means the printable '1' character literal, which on any computer using the ASCII character map is actually the integer 49.
For a demonstration of the difference, try this:
char a = 1;
char b = '1';
int x = a;
int y = b;
cout << "a as int: " << x << "\n";
cout << "b as int: " << y;
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.
Your misunderstanding has nothing to do with the switch()
construct, it's all about the single quotes ''
: If you write 1
, you get an integer of the value 1, when you put it in single quotes '1'
, you get the numeric value of the ASCII character for the digit 1 (this is a bit imprecise, see note below). That ASCII character has the numeric value of 0x31
, or 49
in decimal. Now imagine the difference between
switch( Fav_Car ) {
case 1 :
cout<< "That's cool";
break;
case 2 :
cout<< "Even mine!";
break;
default :
cout<< "Oh";
break;
}
and
switch( Fav_Car ) {
case 49 :
cout<< "That's cool";
break;
case 50 :
cout<< "Even mine!";
break;
default :
cout<< "Oh";
break;
}
The second one is equivalent to the version that you posted, and I think it's clear why it behaves very differently from the first version.
Note:
While '1'
yields an ASCII character value in most C++ implementations, that does not need to be the case. The implementation is free to use some other character code, so that the value of '1'
is actually implementation defined. It could be virtually anything except zero (because that's used for the terminating null-byte in strings). However, most implementations do use ASCII encoding, which is why I assumed ASCII in the text above.
Why does it happen? Because in your switch case, you use a char, not int.
What is the difference between them?
1 //int
'1' // char 1
"1" // string 1
To use int
int a;
switch(a){
case 1 :
}
because '1' is a char and 1 is an integer. If you want to make integers work , remove the single quotes. Using double quotes makes it a string.