Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > time_t: initializer element is not constant

Reply
Thread Tools

time_t: initializer element is not constant

 
 
arnuld
Guest
Posts: n/a
 
      12-15-2011
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
 
Reply With Quote
 
 
 
 
Heinrich Wolf
Guest
Posts: n/a
 
      12-15-2011

"arnuld" <(E-Mail Removed)> schrieb im Newsbeitrag
news:4ee9de7f$0$292$(E-Mail Removed)...
> 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));
}

 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      12-15-2011
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
 
Reply With Quote
 
Heinrich Wolf
Guest
Posts: n/a
 
      12-15-2011

"James Kuyper" <(E-Mail Removed)> schrieb im Newsbeitrag
news:jccrg0$kn0$(E-Mail Removed)...
....
>
> 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


 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      12-15-2011
On 12/15/2011 11:56 AM, Heinrich Wolf wrote:
>
> "James Kuyper" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:jccrg0$kn0$(E-Mail Removed)...
> ...
>>
>> 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;
>> }


 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      12-16-2011
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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-16-2011
August Karlstrom <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      12-16-2011
In article <(E-Mail Removed)>,
Keith Thompson <(E-Mail Removed)> wrote:
>August Karlstrom <(E-Mail Removed)> 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

 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      01-25-2012
"Heinrich Wolf" <(E-Mail Removed)> writes:

> "James Kuyper" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:jccrg0$kn0$(E-Mail Removed)...
> ...
>>
>> 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 );
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-26-2012
Tim Rentsch <(E-Mail Removed)> writes:
> "Heinrich Wolf" <(E-Mail Removed)> writes:
>> "James Kuyper" <(E-Mail Removed)> schrieb im Newsbeitrag
>> news:jccrg0$kn0$(E-Mail Removed)...
>> ...
>>>
>>> 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. }

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"initializer element is not constant" emin.martinian@gmail.com Python 1 05-30-2006 10:15 PM
playfunc.c:524: error: initializer element is not constant Levi Campbell C Programming 3 02-11-2006 10:34 PM
Initializer element not constant grid C Programming 8 08-02-2005 11:56 AM
initializer element is not constant Scott C Programming 15 01-24-2005 04:33 AM
Error: "initializer element is not constant" Todd Nathan C Programming 2 07-30-2003 04:29 PM



Advertisments