您的位置:首页 > 编程语言 > C语言/C++

c语言case的使用注意问题

2011-12-16 23:48 127 查看
#include <stdio.h>

2 int main()

3 {

4 int i;

5 printf ("input somenum");

6 scanf ("%d" , &i);

7 switch (i)

8 {

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

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

11 // break;

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

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

14 break;

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

16 printf ("this is a\n");

17 break;

18 case 2:

19 printf ("this is 2\n");

20 break;

21 case 3:

22 printf ("this is 3\n");

23 break;

24 case 4:

25 printf ("this is 4\n");

26 break;

27 default :

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

29 break;

30 }

31 //getchar();

32 //getchar();

33 setbuf(stdin,NULL);

34 char j;

35 scanf ("%c", &j);

36 switch (j)

37 {

38 case 'a':

39 printf ("this is a\n");

40 break;

41 default:

42 printf ("this is default\n");

43 break;

44 }

45 /* getchar();

46 getchar();

47 char k;

48 scanf ("%c", &k);

49 switch (k)

50 {

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

52 printf ("this is a\n");

53 break;

54 default:

55 printf ("this is default\
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: