case是什么语句啊?

如题所述

case 是开关语句的一个组成部分。

用法:

case 下面必须是整型常数。

冒号和break;之间是满足这个case时要执行的语句。

例如:

switch (整型表达式)
{

case 1: printf("case 1\n"); break;

case 2: case 5: printf("case 2 or 5\n"); break;
...

default: ...break;

}

当整型表达式得1,打印字符串 "case 1"

当整型表达式得2或5,打印字符串 "case 2 or 5"

扩展资料:

c语言case的使用注意问题

#include <stdio.h>

int main()

{

int i;

printf ("input somenum");

scanf ("%d" , &i);

switch (i)

{

//case (0.1+0.9)://这样是不行的case后面必须是一个整数

//      printf ("this is 1\n");

//      break;

case -1://这样是可以的,,,可以看出case后面应是一个有符号的整数

printf ("this is -1\n");

break;

case 'a'://这是可行的,,,后面跟字符是可以的

printf ("this is a\n");

break;

case 2:

printf ("this is 2\n");

break;

case 3:

printf ("this is 3\n");

break;

case 4:

printf ("this is 4\n");

break;

default :

printf ("this is not 1234\n");

break;

}

//getchar();

//getchar();

setbuf(stdin,NULL);

char j;

scanf ("%c", &j);

switch (j)

{

case 'a':

printf ("this is a\n");

break;

default:

printf ("this is default\n");

break;

}

/*      getchar();

getchar();

char k;

scanf ("%c", &k);

switch (k)

{

case "a":这里是错误的也就是说case后面只能跟整形和与整形通用的字符型并且只能是字符而不能是字符串

printf ("this is a\n");

break;

default:

printf ("this is default\

温馨提示:答案为网友推荐,仅供参考
相似回答