initializer is not a constant error ??

Discussion in 'C Programming' started by David Green, Jan 4, 2004.

  1. David Green

    David Green Guest

    Ok, i'm a real c n00b but i needed a piece of code for some work i was
    doing. Initially i was running the stuff under linux and using gcc to
    compile the c code and it worked fine but now i need to port it to
    windows and when i try to compile the same code with the visual studio
    command line tool "cl" i get this error:

    const.c(92) : error C2099: initializer is not a constant
    const.c(97) : error C2099: initializer is not a constant

    with the relevant lines of the const.c code being:

    ln 92 double INFINITY = 1.0/0.0; /* 99e999; */
    ln 97 double NAN = 1.0/0.0 - 1.0/0.0;

    Now i have no real idea what this is doing so i was wondering why i
    compiles fine uner gcc but not under windows, oh the command i am
    using is:

    cl /c const.c

    Is there a compile option i need to set or will i have to change the
    code?

    Any help would be great, thanx.

    Dave.
     
    David Green, Jan 4, 2004
    #1
    1. Advertising

  2. David Green

    Ben Pfaff Guest

    (David Green) writes:

    > const.c(92) : error C2099: initializer is not a constant
    > const.c(97) : error C2099: initializer is not a constant
    >
    > with the relevant lines of the const.c code being:
    >
    > ln 92 double INFINITY = 1.0/0.0; /* 99e999; */
    > ln 97 double NAN = 1.0/0.0 - 1.0/0.0;


    Both of these invoke undefined behavior due to division by zero.
    Maybe that's why the compiler regards them as not being constant
    expressions. I can't think of another reason right now.

    If your implementation has a C99-compliant <math.h>, you can just
    #include <math.h> to get ready-made macros for these.
    --
    "I don't have C&V for that handy, but I've got Dan Pop."
    --E. Gibbons
     
    Ben Pfaff, Jan 4, 2004
    #2
    1. Advertising

  3. Ben Pfaff wrote:
    > (David Green) writes:
    >
    >
    >>const.c(92) : error C2099: initializer is not a constant
    >>const.c(97) : error C2099: initializer is not a constant
    >>
    >>with the relevant lines of the const.c code being:
    >>
    >>ln 92 double INFINITY = 1.0/0.0; /* 99e999; */
    >>ln 97 double NAN = 1.0/0.0 - 1.0/0.0;

    >
    >
    > Both of these invoke undefined behavior due to division by zero.
    > Maybe that's why the compiler regards them as not being constant
    > expressions. I can't think of another reason right now.
    >
    > If your implementation has a C99-compliant <math.h>, you can just
    > #include <math.h> to get ready-made macros for these.


    He did mention that he's using Visual C, which does not (to the best of
    my knowledge) support C99 (though it apparently supports it in some
    ways, and it's certainly possible that this is one of them).

    To the best of my knowledge, outside of C99 there is not a portable way
    to generate INF and NAN. But there's probably a system-specific way. I'd
    recommend checking the documentation for a function or a predefined
    constant.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Jan 4, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Todd Nathan

    Error: "initializer element is not constant"

    Todd Nathan, Jul 30, 2003, in forum: C Programming
    Replies:
    2
    Views:
    19,432
    Kevin Easton
    Jul 30, 2003
  2. Levi Campbell
    Replies:
    3
    Views:
    422
    Flash Gordon
    Feb 11, 2006
  3. Replies:
    3
    Views:
    642
    Wade Ward
    Oct 21, 2007
  4. Gowtham
    Replies:
    16
    Views:
    817
    Szabolcs Borsanyi
    May 9, 2008
  5. skyworld
    Replies:
    0
    Views:
    541
    skyworld
    May 28, 2008
Loading...

Share This Page