C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - General Questions (Q.No. 5)
Which of the following cannot be checked in a switch-case statement?
Answer: Option

The switch/case statement in the c language is defined by the language specification to use an int value, so you can not use a float value.

switch( expression )
    case constant-expression1:    statements 1;
    case constant-expression2:    statements 2;    
    case constant-expression3:    statements3 ;
    default : statements 4;

The value of the 'expression' in a switch-case statement must be an integer, char, short, long. Float and double are not allowed.

17 comments Page 1 of 2.

Jayakrishnan said:   7 years ago
I was performing typecast in functions. I was returning the float value from the function to calling. I am trying to performing typecast but I get the only default value zero. But the address is same I got it. Can anyone explain?

MMM said:   8 years ago
A float constant cannot be used as a case constant in a switch statement.

Gyan said:   8 years ago
But why we can'not use float and double in switch case statement?

Roopesh said:   9 years ago
Enum store integer value only.

Enumerators provides an alternate method of defining a set of integral constants.

Naveen said:   10 years ago
enum example:

void main()
typedef enum a {A,B,C,D} alpha;

alpha m;
case 1:printf("Its enum fool");break;
case A: printf("hi A");break;
//case B:printf("HI B");break;

uncommenting case B give error because 1 and B are same.

Bhoopee said:   10 years ago
How can we use enum in switch statement please explain with an small example?

Aditya Chauhan said:   1 decade ago
Can We use long in switch case?

Priyanka said:   1 decade ago
You declare enum as enum{tamil, english, hindi}.

So sequential number is given for languages namely 0, 1, 2.

As integers are made use you can use enum in switch case statement. :).

Rangasamy said:   1 decade ago
Can you explain the use of enum in switch statement with a program ?

Manojit said:   1 decade ago
The switch/case statement convert the float into an int and use it, so long as the resulting values and functionality meet your functional requirements.

Post your comments here:

Your comments will be displayed after verification.