(a)
#include<stdio.h>
int main()
{
int suite = 1 ;
switch ( suite ) ;
{
case 0 ;
printf ( "\nClub" ) ;
case 1 ;
printf ( "\nDiamond" ) ;
}
return 0;
}
Error:
1. case statements do not belong to any switch, because there's a semicolon after a switch
statement.
2. Expected :
before ;
in case statements.
(b)
#include<stdio.h>
int main()
{
int temp ;
scanf ( "%d", &temp ) ;
switch ( temp )
{
case (temp <= 20):
printf ( "\nOoooooohhhh! Damn cool!" ) ;
case ( temp > 20 && temp <= 30 ) :
printf ( "\nRain rain here again!" ) ;
case ( temp > 30 && temp <= 40 ) :
printf ( "\nWish I am on Everest" ) ;
default :
printf ( "\nGood old nagpur weather" ) ;
}
return 0;
}
Error: temp cannot appear in a constant expression. We can never have a variable in case
statement, but the temp is there.
(c)
#include<stdio.h>
int main()
{
float a = 3.5 ;
switch ( a )
{
case 0.5 :
printf ( "\nThe art of C" ) ; break ;
case 1.5 :
printf ( "\nThe spirit of C" ) ; break ;
case 2.5 :
printf ( "\nSee through C" ) ; break ;
case 3.5 :
printf ( "\nSimply c" ) ;
}
return 0;
}
Error: a
is not an integer in switch
condition. We cannot test floats in switch
statements.
(d)
#include<stdio.h>
int main()
{
int a = 3, b = 4, c;
c = b – a;
switch ( c )
{
case 1 || 2 :
printf ( "God give me an opportunity to change things" ) ;
break ;
case a || b :
printf ( "God give me an opportunity to run my show" ) ;
break ;
}
return 0;
}
Error: a
and b
are not constants used in case
statement.