time_t: initializer element is not constant

Discussion in 'C Programming' started by arnuld, Dec 15, 2011.

  1. arnuld

    arnuld Guest

    WANTED: To initialize the time_t type only once when programs runs for
    first time. Later I want to assign values myself.

    GOT: compile time error


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

    int main(void)
    {
    static time_t t0 = time(NULL);
    time_t t1;

    t1 = time(NULL);
    printf("diff = %f\n", difftime(t1,t0));

    return 0;
    }
    ====================== OUTPUT ======================
    [arnuld@dune C]$ gcc -ansi -pedantic -Wall -Wextra time.c
    time.c: In function ‘main’:
    time.c:6: error: initializer element is not constant
    [arnuld@dune C]$



    --
    arnuld
    http://LispMachine.Wordpress.com
    arnuld, Dec 15, 2011
    #1
    1. Advertising

  2. "arnuld" <> schrieb im Newsbeitrag
    news:4ee9de7f$0$292$...
    > WANTED: To initialize the time_t type only once when programs runs for
    > first time. Later I want to assign values myself.
    >
    > GOT: compile time error
    >
    >
    > #include <stdio.h>
    > #include <time.h>
    >
    > int main(void)
    > {
    > static time_t t0 = time(NULL);
    > time_t t1;
    >
    > t1 = time(NULL);
    > printf("diff = %f\n", difftime(t1,t0));
    >
    > return 0;
    > }
    > ====================== OUTPUT ======================
    > [arnuld@dune C]$ gcc -ansi -pedantic -Wall -Wextra time.c
    > time.c: In function ‘main’:
    > time.c:6: error: initializer element is not constant
    > [arnuld@dune C]$
    >
    >
    >
    > --
    > arnuld
    > http://LispMachine.Wordpress.com


    Hi,

    try this:

    void ElapsedTime(void)
    {
    static int FirstTime = 1;
    static time_t t0;
    time_t t1;

    if (FirstTime)
    {
    FirstTime = 0;
    t0 = time(NULL);
    }

    t1 = time(NULL);
    printf("diff = %f\n", difftime(t1,t0));
    }
    Heinrich Wolf, Dec 15, 2011
    #2
    1. Advertising

  3. arnuld

    James Kuyper Guest

    On 12/15/2011 06:48 AM, arnuld wrote:
    > WANTED: To initialize the time_t type only once when programs runs for
    > first time. Later I want to assign values myself.
    >
    > GOT: compile time error
    >
    >
    > #include <stdio.h>
    > #include <time.h>
    >
    > int main(void)
    > {
    > static time_t t0 = time(NULL);
    > time_t t1;
    >
    > t1 = time(NULL);
    > printf("diff = %f\n", difftime(t1,t0));
    >
    > return 0;
    > }
    > ====================== OUTPUT ======================
    > [arnuld@dune C]$ gcc -ansi -pedantic -Wall -Wextra time.c
    > time.c: In function ‘main’:
    > time.c:6: error: initializer element is not constant
    > [arnuld@dune C]$



    "All the expressions in an initializer for an object that has static
    storage duration shall be constant expressions or string literals."
    (6.7.8p4). "Constant expressions shall not contain assignment,
    increment, decrement, function-call, or comma operators, except when
    they are contained within a subexpression that is not evaluated."
    (6.6p3). Since time(NULL) is a function call, and both of those
    citations occur in Constraints sections, that code is constraint violation.

    Now, if you remove the 'static' keyword, then t0 will be initialized by
    a call to time(NULL) as the very first event in the execution of your
    program. If you're merely trying to define an initial time for the run
    of your program, there's not much difference between initialization
    prior to program startup, and initialization at the very start of your
    program. If you are trying to measure that difference (which is what
    your program seems to be trying to do), you're out of luck - C won't let
    you find out.

    The only other issue with giving t0 automatic storage duration is that
    if your code contains a recursive call to main(), a new instance of t0
    will be created for that call, and initialized by a new call to
    time(NULL). In the unlikely event that your code does recursively call
    main(), that might be precisely what you want it to do. However, if
    that's a problem for you, one alternative is the following:

    static time_t t0;
    bool first_time = true;

    if(first_time)
    {
    t0 = time(NULL);
    first_time = false;
    }
    --
    James Kuyper
    James Kuyper, Dec 15, 2011
    #3
  4. "James Kuyper" <> schrieb im Newsbeitrag
    news:jccrg0$kn0$...
    ....
    >
    > static time_t t0;
    > bool first_time = true;


    I think you mean static bool

    >
    > if(first_time)
    > {
    > t0 = time(NULL);
    > first_time = false;
    > }
    > --
    > James Kuyper
    Heinrich Wolf, Dec 15, 2011
    #4
  5. arnuld

    James Kuyper Guest

    On 12/15/2011 11:56 AM, Heinrich Wolf wrote:
    >
    > "James Kuyper" <> schrieb im Newsbeitrag
    > news:jccrg0$kn0$...
    > ...
    >>
    >> static time_t t0;
    >> bool first_time = true;

    >
    > I think you mean static bool


    You're right. I'ts pointless without the 'static'.

    >> if(first_time)
    >> {
    >> t0 = time(NULL);
    >> first_time = false;
    >> }
    James Kuyper, Dec 15, 2011
    #5
  6. On 2011-12-15 12:48, arnuld wrote:
    > WANTED: To initialize the time_t type only once when programs runs for
    > first time. Later I want to assign values myself.


    You cannot initialize types, only variables. If you want your program to
    "remember" something between invocations you need to write this
    information to a file.

    August
    August Karlstrom, Dec 16, 2011
    #6
  7. August Karlstrom <> writes:
    > On 2011-12-15 12:48, arnuld wrote:
    >> WANTED: To initialize the time_t type only once when programs runs for
    >> first time. Later I want to assign values myself.

    >
    > You cannot initialize types, only variables.


    Yes, that was misstated, but it was obvious from the rest of the article
    that he wants to initialize a variable.

    > If you want your program to
    > "remember" something between invocations you need to write this
    > information to a file.


    It was equally obvious that he's only trying to remember the value
    within a single execution of the program.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Dec 16, 2011
    #7
  8. In article <>,
    Keith Thompson <> wrote:
    >August Karlstrom <> writes:
    >> On 2011-12-15 12:48, arnuld wrote:
    >>> WANTED: To initialize the time_t type only once when programs runs for
    >>> first time. Later I want to assign values myself.

    >>
    >> You cannot initialize types, only variables.

    >
    >Yes, that was misstated, but it was obvious from the rest of the article
    >that he wants to initialize a variable.
    >
    >> If you want your program to
    >> "remember" something between invocations you need to write this
    >> information to a file.

    >
    >It was equally obvious that he's only trying to remember the value
    >within a single execution of the program.


    This is priceless! (And a good candidate for one of those MasterCard
    commercial...)

    Kiki lecturing *other* people about being too specific/literal-minded.

    Priceless!

    --
    Is God willing to prevent evil, but not able? Then he is not omnipotent.
    Is he able, but not willing? Then he is malevolent.
    Is he both able and willing? Then whence cometh evil?
    Is he neither able nor willing? Then why call him God?
    ~ Epicurus
    Kenny McCormack, Dec 16, 2011
    #8
  9. arnuld

    Tim Rentsch Guest

    "Heinrich Wolf" <> writes:

    > "James Kuyper" <> schrieb im Newsbeitrag
    > news:jccrg0$kn0$...
    > ...
    >>
    >> static time_t t0;
    >> bool first_time = true;

    >
    > I think you mean static bool
    >
    >>
    >> if(first_time)
    >> {
    >> t0 = time(NULL);
    >> first_time = false;
    >> }


    Why not just

    static time_t t0;
    if( t0 == 0 ) t0 = time( NULL );
    Tim Rentsch, Jan 25, 2012
    #9
  10. Tim Rentsch <> writes:
    > "Heinrich Wolf" <> writes:
    >> "James Kuyper" <> schrieb im Newsbeitrag
    >> news:jccrg0$kn0$...
    >> ...
    >>>
    >>> static time_t t0;
    >>> bool first_time = true;

    >>
    >> I think you mean static bool
    >>
    >>>
    >>> if(first_time)
    >>> {
    >>> t0 = time(NULL);
    >>> first_time = false;
    >>> }

    >
    > Why not just
    >
    > static time_t t0;
    > if( t0 == 0 ) t0 = time( NULL );


    Obviously because there's a redundant call to time() if you run the
    program at time 0. :cool:}

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 26, 2012
    #10
  11. arnuld

    Tim Rentsch Guest

    Keith Thompson <> writes:

    > Tim Rentsch <> writes:
    >> "Heinrich Wolf" <> writes:
    >>> "James Kuyper" <> schrieb im Newsbeitrag
    >>> news:jccrg0$kn0$...
    >>> ...
    >>>>
    >>>> static time_t t0;
    >>>> bool first_time = true;
    >>>
    >>> I think you mean static bool
    >>>
    >>>>
    >>>> if(first_time)
    >>>> {
    >>>> t0 = time(NULL);
    >>>> first_time = false;
    >>>> }

    >>
    >> Why not just
    >>
    >> static time_t t0;
    >> if( t0 == 0 ) t0 = time( NULL );

    >
    > Obviously because there's a redundant call to time() if you run the
    > program at time 0. :cool:}


    Yes, and not just one, but potentially millions.
    Tim Rentsch, Feb 1, 2012
    #11
    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,329
    Kevin Easton
    Jul 30, 2003
  2. Replies:
    1
    Views:
    503
    Scott David Daniels
    May 30, 2006
  3. Scott

    initializer element is not constant

    Scott, Jan 17, 2005, in forum: C Programming
    Replies:
    15
    Views:
    1,438
    E. Robert Tisdale
    Jan 24, 2005
  4. grid

    Initializer element not constant

    grid, Jul 30, 2005, in forum: C Programming
    Replies:
    8
    Views:
    810
    Jasen Betts
    Aug 2, 2005
  5. Levi Campbell
    Replies:
    3
    Views:
    401
    Flash Gordon
    Feb 11, 2006
Loading...

Share This Page