error C2099: initializer is not a constant

本文探讨了C语言中变量初始化的问题,特别是常量初始化时遇到的错误C2099,并提供了几种解决方法,包括使用宏定义、枚举类型等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

in file main.c there is a code.
here is the code:

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.
解决方案

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.

------------------msdn上的样例和解释---------------------

C2099 can also occur because the compiler is not able to perform constant folding on an expression under /fp:strict because the floating point precision environment settings (see _controlfp_s for more information) may differ from compile to run time.

When constant folding fails, the compiler invokes dynamic initialization, which is not allowed in C.

To resolve this error, compile the module as a .cpp file or simplify the expression.

For more information, see /fp (Specify Floating-Point Behavior).

或者

This error is issued only by the C compiler and occurs only for non-automatic variables. The compiler initializes non-automatic variables at the start of the program and the values they are initialized with must be constant.

或者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值