Reputation: 1579
I Came across a problem in a book, when i ran the code in MSDOSBox TurboC++ For Windows 7, it showed me unexpected behaviour :-
the code was as follows :-
#include <stdio.h>
#include <conio.h>
void main()
{
char s = 3;
switch(s)
{
case 1 :
printf("\nDiamond");
break;
case 2 :
printf("\nSpades");
break;
default :
printf("\nHeart");
break;
}
printf("\nI thought something like this");
getch();
}
now the problem was…
the statement in default case was never got printed. it only printed "I thought something like this"
I gathered that in switch-case char is treated as its value in ASCII code
Well… here, the value to char s has been given 3 not '3' but again at least default case should be printed even if the value is nothing like any of the cases
well… if this helps..
i have tried to put
case 3 :
instead of
default :
now the statement i.e printf("\nHeart"); got executed and printed "Heart" on the screen
Upvotes: 0
Views: 221
Reputation: 17956
Did you cut/paste this code from your source, or retype it? If you retyped it, go back and make sure you spelled default
correctly in the original.
A common error that usually elicits no warning from the compiler is to type something like defualt
instead of default
for that last label.
Upvotes: 3