在C中切换语句 [英] switch statement in C

查看:67
本文介绍了在C中切换语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




{

const int state_black = 1;

const int state_white = 2;


int state;


state = state_black;


switch(state)

{


case state_black:

休息;


case state_white:

break;

}

}


为什么这是不可接受的?

为什么不能宣布变量const使它适合与

一起使用案例标签?


提前致谢

解决方案

ko********@yahoo.com 写道:


>

{

const int state_black = 1;

const int state_white = 2;


int state;


state = state_black;


开关(状态)

{


case state_black:

break;

case state_white:

休息;

}

}


为什么是这是不可接受的?

为什么不能声明变量const使它适合用于

案例标签?



在C中,由于某种原因,const变量不被归类为编译时间

常量。这就是为什么我们不得不求助于enum和臭#defines。


-

Ian Collins。

ko********@yahoo.com 写道:
< blockquote class =post_quotes>
{

const int state_black = 1;

const int state_white = 2;


int state;


州= state_black;


开关(州)

{


案例state_black:

休息;


案例state_white:

休息;

}

}


为什么这是不可接受的?

为什么不能声明变量const make它适合与

的案例标签一起使用?



请参阅comp.lang.c常见问题解答中的问题11.8,< http://www.c-faq.com/>。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


ko ******** @ yahoo.com 写道:


>

{

const int state_black = 1;

const int state_white = 2;


int state;


州= state_black;


开关(州)

{


案例state_black:

休息;


案例state_white:

break;

}

}


为什么这是不可接受的?



state_black和state_white是变量,而不是常量,并且开关

标签必须使用编译时常量。


为什么不能声明变量const使其适合与

案例标签一起使用?



因为变量仍然是变量,而不是编译时常量。




{
const int state_black = 1 ;
const int state_white = 2 ;

int state ;

state = state_black ;

switch(state)
{

case state_black:
break ;

case state_white:
break;
}
}

why is this not acceptable?
why can''t declaring the variable const make it suitable to use with
the case label?

Thanks in advance

解决方案

ko********@yahoo.com wrote:

>
{
const int state_black = 1 ;
const int state_white = 2 ;

int state ;

state = state_black ;

switch(state)
{

case state_black:
break ;

case state_white:
break;
}
}

why is this not acceptable?
why can''t declaring the variable const make it suitable to use with
the case label?

In C, for some reason a const variable isn''t classed as a compile time
constant. Which is why we have to resort to enums and smelly #defines.

--
Ian Collins.


ko********@yahoo.com writes:

{
const int state_black = 1 ;
const int state_white = 2 ;

int state ;

state = state_black ;

switch(state)
{

case state_black:
break ;

case state_white:
break;
}
}

why is this not acceptable?
why can''t declaring the variable const make it suitable to use with
the case label?

See question 11.8 in the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


ko********@yahoo.com wrote:

>
{
const int state_black = 1 ;
const int state_white = 2 ;

int state ;

state = state_black ;

switch(state)
{

case state_black:
break ;

case state_white:
break;
}
}

why is this not acceptable?

state_black and state_white are variables, not constants, and switch
labels must use compile-time constants.

why can''t declaring the variable const make it suitable to use with
the case label?

Because the variable is still a variable, and not a compile-time constant.


这篇关于在C中切换语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆