LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-01-2004, 04:24 PM   #1
mr.neil
LQ Newbie
 
Registered: Jun 2004
Posts: 5

Rep: Reputation: 0
c difftime returns zero


Hello,

Can anyone explain why difftime return zero seconds difference between two different times?

Thank you,
Neil

#include <sys/time.h>

int main(void)
{
time_t s, f;

time(&s);
sleep(10);
time(&f);
printf("%s\n", ctime(&s));
printf("%s\n", ctime(&f));
printf("%f\n", difftime(f, s));
return 0;
}
 
Old 06-01-2004, 05:17 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
If you compile the program with optimalization (gcc -O3 prog.c) you'll get the right result. It's strange, don't know why it works this way.
 
Old 06-01-2004, 09:00 PM   #3
seeLnd
LQ Newbie
 
Registered: May 2004
Location: Beijing, China
Distribution: Debian
Posts: 16

Rep: Reputation: 0
you should include time.h not sys/time.h, then you'll get the right result
 
Old 06-02-2004, 12:43 PM   #4
mr.neil
LQ Newbie
 
Registered: Jun 2004
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you both for your replies.

Including <time.h> instead of <sys/time.h> has fixed the problem. I guess I should have been following my
Kernighan and Ritchie more closely.

Compiling with -O3 and <sys/time.h> gives a non-zero, but incorrect answer of 7000 odd seconds
for an actual delay of 10 seconds! I've no idea what's going on here either. (Compiling with -O3 and
<time.h> works properly.)

So problem solved. Thank you very much for your help.

However, I need to use gettimeofday() from <sys/time.h>. (I need it as it gives access to the microsecond
accuracy timer.) I've included both <time.h> and <sys/time.h>, and the program compiles and runs properly.
However, I was worried that difftime() would be defined twice.

Looking at <sys/time.h> I see it includes <time.h> itself and does not separately define difftime() so
I don't think my including both header files is a problem. (In fact, according to "grep -r difftime *" run in
/usr/include, difftime only occurs in time.h) But that still leaves the puzzle of why difftime does
not work properly when only <sys/time.h> is included.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fscanf returns -1 trutnev Programming 2 06-23-2005 08:50 AM
Carriage Returns Thorkyl Linux - Software 7 06-28-2004 05:42 PM
setup returns after doing nothing?? dfong63 Slackware - Installation 5 06-04-2004 01:40 AM
Bind returns -1 slackwarefan Programming 18 05-31-2004 12:36 AM
mktime() returns -1 nodger Programming 2 01-27-2004 04:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration