c语言switch case调用函数
时间: 2024-06-04 22:05:12 浏览: 173
在C语言中,switch case语句可以用来根据某个变量的不同取值来执行不同的操作。我们可以在每个case分支中调用不同的函数,以实现对不同情况的处理。例如:
```
#include <stdio.h>
void func1() {
printf("This is function 1.\n");
}
void func2() {
printf("This is function 2.\n");
}
void func3() {
printf("This is function 3.\n");
}
int main() {
int x = 2;
switch(x) {
case 1:
func1();
break;
case 2:
func2();
break;
case 3:
func3();
break;
default:
printf("Invalid value.\n");
}
return 0;
}
```
在这个例子中,我们定义了三个函数func1、func2和func3,分别输出不同的信息。在main函数中,我们声明了一个整型变量x,并根据其取值调用不同的函数。当x为1时,调用func1;当x为2时,调用func2;当x为3时,调用func3。如果x的取值不在1、2、3之间,则输出“Invalid value.”。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)