time_t: initializer element is not constant

A

arnuld

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]$
 
H

Heinrich Wolf

arnuld said:
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]$

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));
}
 
J

James Kuyper

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

August Karlstrom

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
 
K

Keith Thompson

August Karlstrom said:
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.
 
K

Kenny McCormack

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


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
 
K

Keith Thompson

Tim Rentsch said:
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:}
 
T

Tim Rentsch

Keith Thompson said:
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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top