Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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;
The switch case code is as follows

switch( Fav_Car ) {
    case '1' :
        cout<< "That's cool";
        break;
    case '2' :
        cout<< "Even mine!";
        break;
    default :
        cout<< "Oh";
        break;
}

When I ran the program, I noticed that using int returns me the default case and using char works perfectly. Why does it happen so?
And also, what's the difference if I use case '1' : and case "1" :

like image 615
Moltres Avatar asked Mar 13 '16 06:03

Moltres


People also ask

What is the difference between int and Char?

In programming language's integer number referred as int which is actually all real number and character referred as char which is simply A to Z characters. I hope this helps. 'Int' and 'char' are various type of data types used in various types of computer programming languages.

What is a SWITCH CASE statement in C?

What Is A Switch Case Statement? A switch case statement in programming is a statement that tests the value of a variable and compares it with multiple cases. In the process, when the case match has been found a block of statements associated with that given case is executed.

What is the difference between IF-ELSE and switch case in C?

The main difference between if-else and switch case is that the if-else in C programming is a conditional statement that executes a different set of statements based on the condition that is true or false whereas the switch case is a conditional statement used in C programming to check the value of a variable and compare it with all the cases.

What is the difference between a float and a char type?

These are data type Integer being defined as Int , character as char and float as floating type. And float is for decimals. Like 1.01 Originally Answered: What is the difference between float, char, and int? Char is 8 bit and you can't change it. Int is 8 or 16 bit long depends if it is signed or unsigned.


2 Answers

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.

like image 151
cmaster - reinstate monica Avatar answered Oct 06 '22 00:10

cmaster - reinstate monica


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.
like image 43
artm Avatar answered Oct 06 '22 01:10

artm