c 语言switch case遍历,C语言:根据条件遍历Switch中所有case

本文介绍了如何使用C语言编写一个程序,通过检查命令行参数并使用switch语句实现根据不同输入显示不同输出。程序演示了基本的条件判断和case标签的运用,适用于初学者理解控制结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include

#include

int main(int argc, char **argv)

{

if(argc != 2)

{

printf("Please Input One Parameter ! \n");

return -1;

}

int i= atoi(argv[1]);

printf(" ----------------------------------------- \n");

switch(i)

{

case 0:

{

printf("*********** [0] \n");

if(i)

break;

}

case 1:

{

printf("*********** [1] \n");

if(i)

break;

}

case 2:

{

printf("*********** [2] \n");

if(i)

break;

}

case 3:

{

printf("*********** [3] \n");

if(i)

break;

}

case 4:

{

printf("*********** [4] \n");

if(i)

break;

}

default :

{

printf("*********** default \n");

if(i)

break;

}

}

printf(" ----------------------------------------- \n");

return 0;

}

运行测试:

root@ubuntu:/mnt/hgfs/Ubuntu12.04-share/03_test/01_C/4_file# gcc -o test test.c

root@ubuntu:/mnt/hgfs/Ubuntu12.04-share/03_test/01_C/4_file# ./test

Please Input One Parameter !

root@ubuntu:/mnt/hgfs/Ubuntu12.04-share/03_test/01_C/4_file# ./test 3

-----------------------------------------

*********** [3]

-----------------------------------------

root@ubuntu:/mnt/hgfs/Ubuntu12.04-share/03_test/01_C/4_file# ./test 5

-----------------------------------------

*********** default

-----------------------------------------

root@ubuntu:/mnt/hgfs/Ubuntu12.04-share/03_test/01_C/4_file# ./test 6

-----------------------------------------

*********** default

-----------------------------------------

root@ubuntu:/mnt/hgfs/Ubuntu12.04-share/03_test/01_C/4_file# ./test 7

-----------------------------------------

*********** default

-----------------------------------------

root@ubuntu:/mnt/hgfs/Ubuntu12.04-share/03_test/01_C/4_file# ./test 2

-----------------------------------------

*********** [2]

-----------------------------------------

root@ubuntu:/mnt/hgfs/Ubuntu12.04-share/03_test/01_C/4_file# ./test 0

-----------------------------------------

*********** [0]

*********** [1]

*********** [2]

*********** [3]

*********** [4]

*********** default

-----------------------------------------

root@ubuntu:/mnt/hgfs/Ubuntu12.04-share/03_test/01_C/4_file#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值