By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,473 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

why error C2051?

P: n/a
const int c=9;

void main()
{
int a=9;
switch (a)
{
case c:
a=c+1;
break;
}
}
-----------
compile result:
c.c(8) : error C2051: case expression not constant
-----------
But [c] is a const int....why?

Nov 17 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
clino said:
const int c=9;

void main()
int main(void)
{
int a=9;
switch (a)
{
case c:
a=c+1;
break;
}
}
-----------
compile result:
c.c(8) : error C2051: case expression not constant
-----------
But [c] is a const int....why?
It's a const int all right, but it isn't a constant integer expression,
which is what you need in a case expression.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 17 '06 #2

P: n/a
"clino" <zh**********@gmail.comwrote:
const int c=9;

void main()
<http://c-faq.com/ansi/maindecl.html>
<http://c-faq.com/ansi/voidmain.html>
<http://c-faq.com/ansi/voidmain3.html>
<http://c-faq.com/ansi/voidmainbooks.html>
{
int a=9;
switch (a)
{
case c:
a=c+1;
break;
}
}
-----------
compile result:
c.c(8) : error C2051: case expression not constant
-----------
But [c] is a const int....why?
<http://c-faq.com/cpp/constvsdefine.html>
<http://c-faq.com/ansi/constasconst.html>

HTH; HAND; RTFFAQ.

Richard
Nov 17 '06 #3

P: n/a
very clearly~
Thank u very much~

On 11月17日, 下午4时08分, r...@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
"clino" <zhangchun...@gmail.comwrote:
const int c=9;
void main()<http://c-faq.com/ansi/maindecl.html>
<http://c-faq.com/ansi/voidmain.html>
<http://c-faq.com/ansi/voidmain3.html>
<http://c-faq.com/ansi/voidmainbooks.html>
{
int a=9;
switch (a)
{
case c:
a=c+1;
break;
}
}
-----------
compile result:
c.c(8) : error C2051: case expression not constant
-----------
But [c] is a const int....why?<http://c-faq.com/cpp/constvsdefine.html>
<http://c-faq.com/ansi/constasconst.html>

HTH; HAND; RTFFAQ.

Richard
Nov 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.