Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Tiny C error - initializer element is not constant (http://www.velocityreviews.com/forums/t742527-tiny-c-error-initializer-element-is-not-constant.html)

TheFlyingDutchman 01-25-2011 12:03 AM

Tiny C error - initializer element is not constant
 
Compiling this program with Tiny C version 0.9.25 :
__________________________________________________ _______________
const int FALSE = 0;

int Debugging = FALSE;

int main(const int argc, const char *argv[])
{
return 0;
}
__________________________________________________ _______________
I get this error message that I don't understand because FALSE appears
to be a constant value:

d:/c/test_initializer.c:3: initializer element is not constant

Ian Collins 01-25-2011 12:09 AM

Re: Tiny C error - initializer element is not constant
 
On 01/25/11 01:03 PM, TheFlyingDutchman wrote:
> Compiling this program with Tiny C version 0.9.25 :
> __________________________________________________ _______________
> const int FALSE = 0;
>
> int Debugging = FALSE;
>
> int main(const int argc, const char *argv[])
> {
> return 0;
> }
> __________________________________________________ _______________
> I get this error message that I don't understand because FALSE appears
> to be a constant value:
>
> d:/c/test_initializer.c:3: initializer element is not constant


'const' is broken in C. Unfortunately FALSE isn't a compile time
constant. Use

enum { FALSE = 0 };

--
Ian Collins

Keith Thompson 01-25-2011 12:17 AM

Re: Tiny C error - initializer element is not constant
 
TheFlyingDutchman <zzbbaadd@aol.com> writes:
> Compiling this program with Tiny C version 0.9.25 :
> __________________________________________________ _______________
> const int FALSE = 0;
>
> int Debugging = FALSE;
>
> int main(const int argc, const char *argv[])
> {
> return 0;
> }
> __________________________________________________ _______________
> I get this error message that I don't understand because FALSE appears
> to be a constant value:
>
> d:/c/test_initializer.c:3: initializer element is not constant


"const" doesn't mean "constant", it means read-only.

What "const int FALSE = 0;" really means is that FALSE is an object (a
variable) of type int, whose initial value is 0, and whose value you're
not allowed to modify after it's been created. It doesn't mean that
it's a compile time constant. In particular, the name FALSE is not a
constant expression.

To illustrate, the following is a valid C program:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(void)
{
srand(time(NULL));
const int r = rand();
printf("r = %d\n", r);
return 0;
}

The object "r" is const (read-only, so assigning a value to it would
be illegal), but not constant (the value cannot be determined at
compile time). You can make the initializer constant and/or move
the declaration to file scope (as you did with "Debugging"), but
that still doesn't make it constant.

<OT>C++ has a special rule saying, more or less, that if the
initializer is a constant expression, the initialized object is
a constant. C has no such rule. I would argue that it would be
better if it did, but that doens't help you.)</OT>

You can achieve the effect you want either by using the preprocessor:

#define FALSE 0

or by using an enum:

enum { FALSE = 0 };

(since enumerators *are* constant -- but they can only be of
type int).

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Seebs 01-25-2011 12:27 AM

Re: Tiny C error - initializer element is not constant
 
On 2011-01-25, TheFlyingDutchman <zzbbaadd@aol.com> wrote:
> const int FALSE = 0;


This is not a constant.

> I get this error message that I don't understand because FALSE appears
> to be a constant value:


No it doesn't.

The "const" keyword in C means "unmodifiable". There is no way to have
a declared object which is a *constant*. A constant, in C, is something
like a pure numeric literal, not a declared object.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

TheFlyingDutchman 01-25-2011 03:55 AM

Re: Tiny C error - initializer element is not constant
 
Thanks for the replys! The enum style works well.


All times are GMT. The time now is 09:41 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.