To "fix" this, you would have to write
switch( marks ) // branch to the label corresponding to the value stored in marks
{
case 100:
case 99:
// repeat with cases 98 through 91.
case 90:
printf( "Excellent!\n" );
break;
case 89:
case 88:
// repeat with cases 87 through 76
case 75:
printf( "very good\n" );
break;
// pattern should be obvious from here
}
case
labels must be integer constant expressions, basically something that can be evaluated at compile time. Relational expressions like marks >= 90
don't count as constant expressions.
A switch
branches to the label corresponding to the value of marks
; IOW, if you want to execute an action when the value of marks
is anywhere from 90
to 100
, then you a separate label for each value as above.
You would normally not use a switch
statement where ranges of values are involved like this; this is better done with an if-else
statement.
Edit
For reference, the following may be used as case
labels:
- An integer literal (decimal, hex, or octal format);
- A character constant (such as
'A'
, '?'
, '\n'
, etc.);
- An enumeration constant;
- An arithmetic expression composed of any of the previous expressions;
- A macro that expands to any of the previous expressions;
A const
-qualified variable is not a constant expression as far as C is concerned, so you can't do something like this:
const int foo = 10;
...
switch( bar )
{
case foo: ... // compiler will yell at you here
You can't branch on strings in a switch
; however, you can compute an integer hash for a string, and branch based on that:
#define FOO_HASH 0xb887389 // result of running hash function on "foo"
#define BAR_HASH 0xb8860ba // result of running hash function on "bar"
/**
* djb2 is a popular hash function
* see http://www.cse.yorku.ca/~oz/hash.html for others
*/
unsigned long hash( const char *text )
{
const unsigned char *str = (const unsigned char *) text;
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
char text[SIZE];
if ( !fgets( text, sizeof text, stdin ) )
// bail on input error
// remove newline from text somehow
switch( hash( text ) )
{
case FOO_HASH:
// do something
break;
case BAR_HASH:
// do something else
break;
}