2017-09-19 25 views
-2
int main() { 
    int marks; 
    printf("enter the marks :"); 
    scanf(" %d", &marks); 

    switch (marks) { 
     case (marks >= 90): 
     printf("Excellent"); 
     break; 
     case (marks < 90 && marks >= 75): 
     printf("very good"); 
     break; 
     case (marks < 40): 
     printf("poor"); 
     break; 
     default: 
     printf("no marks available"); 
    } 
    return 0; 
} 

我最近开始学习C,并在运行switch声明时遇到了这种情况下的标签错误。为什么在这个特定的程序中案例标签不会减少到一个整数常量?

请有人解决这个问题?

+3

因为C的'switch/case'不是'match'?你不能像这样使用它,'case's只是整型常量表达式。 – EOF

+2

您不能将范围放在'switch ... case'中,只能是单个值。你必须链接'if ... else if ... else'语句。 – AntonH

+0

为什么你认为这是一个_constant表达_? – Olaf

回答

0

你不能在条件下提出条件。从https://www.tutorialspoint.com/cprogramming/switch_statement_in_c.htm

案例的常量表达式必须与开关中的变量具有相同的数据类型,并且它必须是常量或文字。

+0

我认为大小写后的值应严格为int或char。 – akshayk07

+0

@ akshayk07需要成为_conditional-expression_。 'char'在这里并不特别。 – chux

+0

@chux Switch case不适用于'float',我认为 – akshayk07

1

该情况必须保持不变。 case (marks>=90):不使用常量。

每个case标签的表达应该是整型常量表达式和没有两个在same switch语句中case常量表达式的应变换后具有相同的值” 3§6.8.4.2

例如:

switch (grade) { 
    case 'A': printf("Excellent"); break; 
    case 'B': printf("very good"); break; 
    case 'C': // fall though (no break) 
    case 'D': printf("poor"); break; 
    default: printf("Hmmm"); 
} 

OP的代码将与得到更好的服务

// switch(marks){ 
// case (marks>=90): 
if (marks>=90) { 
    printf("Excellent"); 
} else if (marks>=75) { 
    printf("very good"); 
} else if (marks < 40){ // Perhaps OP wants (marks > 40) here 
    printf("poor"); 
} else { 
    printf("no marks available"); 
} 
1

“修理”这一点,你会写

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标签必须是整型常量表达式,基本的东西,可以在编译时进行评估。像marks >= 90这样的关系表达式不会被视为常量表达式。

switch分支到相应于值的marks标签; IOW,如果您想在marks的值为90100之间的任何值时执行操作,则您为上述每个值分别标记一个标签。

您通常不会使用switch语句,其中涉及的值范围如下所示;这可以通过if-else声明更好地完成。

编辑

作为参考,下文中可被用作case标签:

  • 一个整数文字(十进制,十六进制或八进制格式);
  • 字符常量(如'A','?','\n'等));
  • 枚举常量;
  • 由任何以前的表达式组成的算术表达式;
  • 一个扩展到任何以前表达式的宏;

一个const -qualified变量不是常量表达式就为C而言,所以你不能做这样的事情:

const int foo = 10; 
... 
switch(bar) 
{ 
    case foo: ... // compiler will yell at you here 

在你不能对字符串分支a switch;然而,你可以计算一个字符串的整数散列,并基于此分支:

#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; 
} 
+0

很好的解释。在研究这个问题时,似乎像'case INT_MAX/2:...'这样的代码也可以。照我看来。 'UINTMAX_MAX:...'也可以,虽然使用更广泛的'int'并不是我在生产代码中完成的。 – chux

相关问题