C89:初始化程序所需的编译时常量? [英] C89: Compile-time constant required for initialiser?

查看:85
本文介绍了C89:初始化程序所需的编译时常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我一直在开发一个C89微控制器应用程序

现在我已经用gcc测试了它的编译。我已经得到零

错误和使用gcc的零警告,但现在我已经转移到

微控制器编译器我得到了各种各样的错误。


我想澄清的一件事是需要(在C89中)变量初始化中的编译 -

时间常数。编译器拒绝

以下源文件:


/ *启动foo.c * /


int const a = 7;


int b = a;


/ *结束foo.c * /

它说b的初始化程序需要是一个编译时间

常量。非const全局对象的初始化器需要是C89中的编译时常量吗?或者这只是编译器的短暂结果?


另一个相关的问题:


/ *开始bar.c * /


void Func(void)

{

int const x = 7;


int const y = x;

}


/ *结束bar.c


在上面的代码片段中,编译器失败,因为y'的
初始化器不是编译时常量。 C89如何看待这个? (如果

y是非const的,那么编译器接受代码,但是我想知道_const_本地自动变量是否是'b $ b'初始化器必须是C89中的b
编译时常量?)

-
$ b $bTomásóhéilidhe

解决方案

Tomásóhéilidhe写道:


有一件事我想澄清的是需要(在C89中)对于变量的初始化中的编译 -

时间常数。编译器拒绝

以下源文件:


/ *启动foo.c * /


int const a = 7;

int b = a;


/ *结束foo.c * /


它说b的初始化程序需要是一个编译时间

常量。非const全局对象的初始化器需要是C89中的编译时常量吗?



是的。 C99也是如此。上面的a的声明在C中具有与C ++不同的含义。


或者这只是编译器的一个短暂的来源?



No.


另一个相关问题:


/ *开始bar.c * /

void Func(无效)

{

int const x = 7;

int const y = x;

}

/ *结束bar.c


在上面的片段中,编译器失败,因为y'的
初始化器不是编译时常量。 C89如何看待这个?



这是一个有效的初始化,假设y未声明为静态。


(如果 y是非const的,然后编译器接受代码,但我是

想知道_const_本地自动变量'的初始值是否必须是

C89中的编译时常量?)



这对我来说看起来像编译错误。


-

Thad


Tomásóhéilidhe写道:


>

我现在一直在开发一个C89微控制器应用程序

我已经用gcc测试了它的编译。我已经得到零

错误和使用gcc的零警告,但现在我已经转移到

微控制器编译器我得到了各种各样的错误。


我想澄清的一件事是需要(在C89中)变量初始化中的编译 -

时间常数。

编译器拒绝以下源文件:


/ *启动foo.c * /


int const a = 7;


int b = a;


/ *结束foo.c * /


它表示b的初始值。需要是一个编译时间

常量。非const全局对象的初始化器需要是C89中的编译时常量吗?



是。

这里是相关的概念:

全局对象有静态持续时间因此

在主要开始执行之前被初始化。


或者这只是一个简短的

来自编译器?



No.


另一个相关问题:


/ *开始bar.c * /


void Func(无效)

{

int const x = 7;


int const y = x;

}


/ *结束bar.c


在上面的代码片段中,编译器失败,因为y'的
初始化器不是编译时常量。 C89如何看待这个?



不应该那样。

Func没用,但是完美无缺。


(如果y是非const的,那么编译器接受代码,

但是我想知道_const_ local是否自动

变量'的初始值必须是C89中的编译时常量?)



编号绝对不行。


char * str_cpy(char * s1,const char * s2)

{

char * const p1 = s1;


while((* s1 ++ = * s2 ++)!=''\ 0''){

;

}

返回p1; < br $>
}


-

pete


"Tomásó héilidhe" < to *@lavabit.comwrites:


我已经开发了一段时间的C89微控制器应用程序

现在和我'我一直在用gcc测试它的编译。我已经得到零

错误和使用gcc的零警告,但现在我已经转移到

微控制器编译器我得到了各种各样的错误。



....


非const全局对象的初始化是否为真

需要是C89中的编译时常量吗?



是的。 (海湾合作委员会也抱怨这一点。你确定你没有把你的程序编译为C ++吗?C ++允许这样做。)


... 。


void Func(void)

{

int const x = 7;


int const y = x;

}



....


在上面的片段中,编译器失败,因为y'的
初始化器不是编译时常量。 C89如何看待这个?



这段代码很好。


(如果y是非const的话,那么编译器接受代码,

,但我想知道_const_本地自动变量'的
初始化器是否必须是C89中的编译时常量?)



不,它不一定是。

-

有些编程实践会产生错误;

这个就像拨打800号码

并将错误发送到你的门口。

- 史蒂夫麦康奈尔



I''ve been developing a C89 microcontroller application for a while
now and I''ve been testing its compilation using gcc. I''ve gotten zero
errors and zero warnings with gcc, but now that I''ve moved over to the
micrcontroller compiler I''m getting all sorts of errors.

One thing I''d like to clarify is the need (in C89) for a compile-
time constant in the initialiser of a variable. The compiler rejects the
following source file:

/* Start foo.c */

int const a = 7;

int b = a;

/* End foo.c */
It says that the initialiser of "b" needs to be a compile-time
constant. Is it true that the initialiser of a non-const global object
needs to be a compile-time constant in C89? Or is this just a short
coming of the compiler?

Another related question:

/* Start bar.c */

void Func(void)
{
int const x = 7;

int const y = x;
}

/* End bar.c

In the snippet immediately above, the compiler fails because y''s
initialiser isn''t a compile-time constant. How does C89 view this? (If
"y" is made non-const, then the compiler accepts the code, but I''m
wondering if a _const_ local automatic variable''s initialiser must be a
compile-time constant in C89?)

--
Tomás ó héilidhe

解决方案

Tomás ó héilidhe wrote:

One thing I''d like to clarify is the need (in C89) for a compile-
time constant in the initialiser of a variable. The compiler rejects the
following source file:

/* Start foo.c */

int const a = 7;
int b = a;

/* End foo.c */

It says that the initialiser of "b" needs to be a compile-time
constant. Is it true that the initialiser of a non-const global object
needs to be a compile-time constant in C89?

Yes. The same is true of C99. The declaration of a, above, has a
different meaning in C than C++.

Or is this just a short coming of the compiler?

No.

Another related question:

/* Start bar.c */
void Func(void)
{
int const x = 7;
int const y = x;
}
/* End bar.c

In the snippet immediately above, the compiler fails because y''s
initialiser isn''t a compile-time constant. How does C89 view this?

That is a valid initialization, assuming that y is not declared static.

(If "y" is made non-const, then the compiler accepts the code, but I''m
wondering if a _const_ local automatic variable''s initialiser must be a
compile-time constant in C89?)

That looks like a compiler bug to me.

--
Thad


Tomás ó héilidhe wrote:

>
I''ve been developing a C89 microcontroller application for a while
now and I''ve been testing its compilation using gcc. I''ve gotten zero
errors and zero warnings with gcc, but now that I''ve moved over to the
micrcontroller compiler I''m getting all sorts of errors.

One thing I''d like to clarify is the need (in C89) for a compile-
time constant in the initialiser of a variable.
The compiler rejects the following source file:

/* Start foo.c */

int const a = 7;

int b = a;

/* End foo.c */

It says that the initialiser of "b" needs to be a compile-time
constant. Is it true that the initialiser of a non-const global object
needs to be a compile-time constant in C89?

Yes.
Here''s the relevant concept:
global objects have static duration and therefore
are initialized before main starts executing.

Or is this just a short
coming of the compiler?

No.

Another related question:

/* Start bar.c */

void Func(void)
{
int const x = 7;

int const y = x;
}

/* End bar.c

In the snippet immediately above, the compiler fails because y''s
initialiser isn''t a compile-time constant. How does C89 view this?

It shouldn''t be that way.
Func is useless, but flawless.

(If "y" is made non-const, then the compiler accepts the code,
but I''m wondering if a _const_ local automatic
variable''s initialiser must be a compile-time constant in C89?)

No. Absolutely not.

char *str_cpy(char *s1, const char *s2)
{
char *const p1 = s1;

while ((*s1++ = *s2++) != ''\0'') {
;
}
return p1;
}

--
pete


"Tomás ó héilidhe" <to*@lavabit.comwrites:

I''ve been developing a C89 microcontroller application for a while
now and I''ve been testing its compilation using gcc. I''ve gotten zero
errors and zero warnings with gcc, but now that I''ve moved over to the
micrcontroller compiler I''m getting all sorts of errors.

....

Is it true that the initialiser of a non-const global object
needs to be a compile-time constant in C89?

Yes. (GCC complains about this too. Are you sure that you
didn''t compile your program as C++? C++ allows this.)

....

void Func(void)
{
int const x = 7;

int const y = x;
}

....

In the snippet immediately above, the compiler fails because y''s
initialiser isn''t a compile-time constant. How does C89 view this?

This code is fine.

(If "y" is made non-const, then the compiler accepts the code,
but I''m wondering if a _const_ local automatic variable''s
initialiser must be a compile-time constant in C89?)

No, it need not be.
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell


这篇关于C89:初始化程序所需的编译时常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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