Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

error C2099: initializer is not a constant

218 views
Skip to first unread message

unicorn

unread,
Jul 1, 2005, 4:58:02 PM7/1/05
to
in file main.c there is a code.
here is the code:
short a=5;
short b=a;
void main(){}

why the compiler doesn't let me init the b with a ?

Barry Schwarz

unread,
Jul 2, 2005, 12:01:37 AM7/2/05
to

The initialization value must be a compile time constant. a is a
variable. Even if you defined a as const short, it is still not a
compile time constant.


<<Remove the del for email>>

Mattice@discussions.microsoft.com Andy Mattice

unread,
Aug 5, 2008, 10:30:00 PM8/5/08
to

"Barry Schwarz" wrote:

Your options for compile time constant initialization goes beyond hard-coded
values: You may initialize it to some macro's value (#define BOOOO_HISSSS)
or a specific enum value. The enum hackonly works for int.

With variables you can of course get around it by simply assigning after
declaring(instead of all-in-one initialization). The real problem rears it's
ugly head when you want to inizialize a const with somevalue based another
const.

Are there any other ways besides setting them to a macro or enum?

i.e.

const int i = 1;
const int j = i + 100 // error C2099

enum Name_Does_Not_Matter
{
bar = 5
};
const int foo = bar + 10 // Success!! foo is initialized to 15

Andy

0 new messages