Tiny C error - initializer element is not constant

T

TheFlyingDutchman

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
 
I

Ian Collins

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 };
 
K

Keith Thompson

TheFlyingDutchman said:
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).
 
S

Seebs

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top