它可以完成 - 但是你的函数应该返回一个int。
int foo(void) { return 1; }
int main(void)
{
switch(foo()) {
case 0:
printf("foo\n");
break;
case 1:
printf("bar\n");
break;
default:
break;
}
return 0;
}
至于,可以看出这个编译运行。
[09:36 PM] $ gcc -Wall -Werror switcht.c -o switcht
[09:36 PM] $ ./switcht
bar
[09:36 PM] $
但是,如果你的函数返回int以外的任何东西,那么它不会编译。例如,如果foo()
返回float
:
float foo(void) { return 1.0; }
然后:
[09:44 PM] $ gcc -Wall -Werror switcht.c
switcht.c: In function ‘main’:
switcht.c:7:9: error: switch quantity not an integer
switch(foo()) {
^~~~~~
[09:44 PM] $
来源
2016-07-08 01:39:43
sps
这是为什么downvoted? – vortexman100
它***是可能的。但是,对于switch语句,您似乎使用了无效的语法。 – immibis
@ vortexman100不是我的downvote,但你的'switch'缺少花括号,break和默认值。所以很难猜测你在问什么,而你并不在乎解释什么“不起作用”。 – dxiv