Tiny C error - initializer element is not constant

Discussion in 'C Programming' started by TheFlyingDutchman, Jan 25, 2011.

  1. 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
     
    TheFlyingDutchman, Jan 25, 2011
    #1
    1. Advertisements

  2. TheFlyingDutchman

    Ian Collins Guest

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

    enum { FALSE = 0 };
     
    Ian Collins, Jan 25, 2011
    #2
    1. Advertisements

  3. "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, Jan 25, 2011
    #3
  4. TheFlyingDutchman

    Seebs Guest

    This is not a constant.
    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
     
    Seebs, Jan 25, 2011
    #4
  5. Thanks for the replys! The enum style works well.
     
    TheFlyingDutchman, Jan 25, 2011
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.