473,657 Members | 2,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C89: Compile-time constant required for initialiser?


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
Dec 9 '07 #1
22 3600
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
Dec 9 '07 #2
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
Dec 9 '07 #3
"Tomás Ó hÉilidhe" <to*@lavabit.co mwrites:
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
Dec 9 '07 #4
Thad Smith <Th*******@acm. orgwrote in
news:47******** *************** @auth.newsreade r.octanews.com:
> 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.

Is that to say that static variables within functions get exactly the same
treatment as global variables when it comes to initialisation? That is to
say, should I C89 compiler reject the following?:
void Func(void)
{
static int a = (0,2);
}

(I use the comma operator so as not to yield a compile-time constant).

--
Tomás Ó hÉilidhe
Dec 10 '07 #5
Tomás Ó hÉilidhe wrote:
Is that to say that static variables within
functions get exactly the same
treatment as global variables when it comes to initialisation?
Yes.
Here's the relevant concept:
static variables within functions and global objects,
have static duration and therefore
are initialized before main starts executing.
That is to
say, should I C89 compiler reject the following?:
Yes.
void Func(void)
{
static int a = (0,2);
}

(I use the comma operator so as not to yield a compile-time constant).
--
pete
Dec 10 '07 #6
pete wrote:
>
Tomás Ó hÉilidhe wrote:
Is that to say that static variables within
functions get exactly the same
treatment as global variables when it comes to initialisation?

Yes.
Here's the relevant concept:
static variables within functions and global objects,
have static duration and therefore
are initialized before main starts executing.
That is to
say, should I C89 compiler reject the following?:

Yes.
void Func(void)
{
static int a = (0,2);
}

(I use the comma operator
so as not to yield a compile-time constant).
Though,
the relevant concept doesn't explain what's wrong with (0,2),
so I'll stop repeating it.

--
pete
Dec 10 '07 #7
pete <pf*****@mindsp ring.comwrote in news:47******** ***@mindspring. com:
Though,
the relevant concept doesn't explain what's wrong with (0,2),
so I'll stop repeating it.

What's wrong with (0,2)? It evaluates to an R-value whose type is int and
whose value is 2. Fair enough it's not a compile-time constant... but what
exactly are you saying is wrong with it?
--
Tomás Ó hÉilidhe
Dec 10 '07 #8
Tomás Ó hÉilidhe wrote:
>
pete <pf*****@mindsp ring.comwrote in news:47******** ***@mindspring. com:
Though,
the relevant concept doesn't explain what's wrong with (0,2),
so I'll stop repeating it.

What's wrong with (0,2)?
It evaluates to an R-value whose type is int and whose value is 2.
Fair enough it's not a compile-time constant...
but what exactly are you saying is wrong with it?
It's not a "constant expression".
What I was getting at,
was that since objects with static duration
are initialised before main starts executing,
they need to be initialised with compile time constants.
However, the value of (0,2) is capable of being evaluated
at a glance, even before compile time,
and the only reason that it is not considered a
constant expression is because of the rules banning comma
operators from evaluated parts of constant expressions.

The problem is that there is no such term as
"compile-time constant" in the standard.
Where a "compile-time constant" is needed,
a "constant expression" is called for.

ISO/IEC 9899: 1990
6.5.7 Initialization
All the expressions in an initializer for an object that has static
storage duration or in an initializer list for an object that has
aggregate or union type shall be constant expressions.

But I think you knew that already.

--
pete
Dec 10 '07 #9
Tomás Ó hÉilidhe wrote, On 09/12/07 22:14:
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.
<snip>

Something that others have not mentioned which may help you is that by
default gcc permits all sorts of extensions some of which are
incompatible with the C standard. To get it to follow the standard you
should use "gcc -ansi -pedantic" to get a lot more useful warnings try
"gcc -ansi -pedantic -Wall -Wextra".
--
Flash Gordon
Dec 10 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
476
by: jrefactors | last post by:
When people say C programming language, they mean ISO C89? The latest C is ISO C99, but I heard this is not commonly used. What's the differences between ISO C89 and ISO C99? Please advise. Thanks
3
4417
by: sugaray | last post by:
I have seen so many threads discussions refered to C89 and C99 every so often, but I've been confused about what are the differences between those specifications ? and what are the C89-conforming C compilers, and what are the C99-conforming ones ? (let's say, Visual C, Borland C, GNU C, Watcom C, LCC,etc) Can somebody point that out for me, or provide me some pages to visit on the net about that topic ? thanx.
1
1724
by: G Patel | last post by:
Hi, Does anyone know if there are any meaningful differences between the final pre-C89 draft (http://dev.unicals.com/papers/c89-draft.html) and the actual C89 standard? For a non-implementor, are there any traps/pitfalls in using the final draft? It's a shame that the C99 standard and the changes that are coming up ("safe" library functions) are implemented in different degrees by
66
11777
by: Jason Curl | last post by:
I've seen the document N869.txt and the copy I could find talks about C99. Is there anything that comprehensively describes the difference between C89 and C99? I'd like to write code to be as portable as possible. Thanks, Jason.
23
2485
by: napi | last post by:
Does anybody know if there's any C99 to C89 translator out there, either free or commercial. I searched on Google but couldn't see it so far. Thanks for any tips. Napi
5
8838
by: alsor.zhou | last post by:
hi all, I'm searching the `ANSI C89 specification' and the related. Since I tried with `ansi c89 standard' and `ansi c89 specificatioin' in google, there was none useful links return. anybody here could do me a fever to show me a link or send me a copy? Thanks and best regards, alsor
8
2737
by: Michal Nazarewicz | last post by:
Hi, What does returning 0 from main() mean according to C89/C90 standard? I've found that in C99 it means successful termination (7.20.4.3p5) however as I'm editing book on C at Polish Wikibooks I'd like to know what C89/C90 says about it - quotation from C89 or C90 would be nice. Moreover, in C99 main() function is somewhat special because lack of return statement is equivalent with returning zero (5.1.2.2.3p1). Is it also the case...
73
7405
by: Yevgen Muntyan | last post by:
Hey, I was reading C99 Rationale, and it has the following two QUIET CHANGE paragraphs: 6.5.3.4: "With the introduction of the long long and extended integer types, the sizeof operator may yield a value that exceeds the range of an unsigned long." 6.5.6: "With the introduction of the long long and extended integer
18
2425
by: Tomás Ó hÉilidhe | last post by:
(SHA-1 is a cryptographic hash function. For info on what SHA-1 is: http://en.wikipedia.org/wiki/SHA-1) I'm writing fullportable C89 code that needs to make use of the SHA-1 algorithm. Does anyone know if there's been a fully-portable C89 implementation of the SHA-1 algorithm? If not, could someone please point me to a very good implementation of the algorithm, an implementation which is very portable (perhaps portable to machines...
2
2756
by: unauthorized | last post by:
Hi guys. I've been trying to compile the pcre library for use with the MSVC compiler, but the code wouldn't compile because it needed the func strtoq which isn't part of my C(++) library. Anyway, I figured I may as well get down and dirty with it and write a quick replacement (and it worked fine) but once I tried compiling it as a .c source, bad things happened: Compiler log: ------ Build started: Project: pcre, Configuration: Debug Win32...
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.