PDA

View Full Version : time program showing time since epoch?



cus
01-10-2009, 08:49 AM
Hi all, for a tutorial we have been given the task of demonstrating a time program that was discussed in a lecture. However when I convert from the slide to try and compile it, there are many errors - many due to which there are too many spaces in between some of the functions - However there may be other parts missing.



#include <time. h>
#include <stdio. h>
#include <unistd. h>
int main( )
{
int i;
time_ t the_ time;
for( i = 1; i <= 10; i++)
{
the_ time = time(( time_ t *) 0);
printf(" The time is %ld\ n", the_ time);
sleep( 2);
}
exit( 0);
}

Before this code on the same slide there this explanation:

#include<time.h>
time_t time(time_t *tloc);

returns the number of seconds since epoch
also write the returned value to a location pointed to by tloc, if this isnít a null pointer.

Can anybody help with why it wont compile?

cus
01-10-2009, 09:04 AM
i have changed all the obvious spaces to this:



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

int main( )
{
int i;
time_t the_time;
for( i = 1; i <= 10; i++)
{
the_time = time((time_t *) 0);
printf(" The time is %ld\ n", the_time);
sleep(2);
}
exit(0);
}

brewbuck
01-10-2009, 11:05 AM
The spaces in "time_ t" and "the_ time" are causing it. EDIT: I see you fixed those... Now you have an extra space in '\ n' in your printf string.

Other than that, it's badly indented (sorry, have to say it), you should be including stdlib.h, and you don't need unistd.h. Also, exiting by calling exit(0) is unnecessary -- you could just "return 0;"

kermit
01-10-2009, 11:35 AM
unistd.h is necessary for the call to sleep.

brewbuck
01-10-2009, 01:33 PM
unistd.h is necessary for the call to sleep.

Hmm. So it is.

cus
01-10-2009, 01:56 PM
The spaces in "time_ t" and "the_ time" are causing it. EDIT: I see you fixed those... Now you have an extra space in '\ n' in your printf string.

Other than that, it's badly indented (sorry, have to say it), you should be including stdlib.h, and you don't need unistd.h. Also, exiting by calling exit(0) is unnecessary -- you could just "return 0;"

I didn't notice the space on the \n silly me - Thanks for that