Stopwatch with negative time in C

Practicing my use of time() in making a stopwatch. But it seems that I have misused it as the call returns a negative value. What is going wrong?
(structs also may not be the most efficient variable here, but I wanted practice)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//time() testing

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

struct Timer {
    double time1;
    double time2;
};

void stopwatch(struct Timer* t) {
    char c;
    printf("press enter to start >> ");
    scanf("%c", &c);
    t->time1 = getTime();
    printf("press enter to end >> ");
    scanf("%c", &c);
    t->time2 = getTime();
}

int getTime(void) {
    return time(0);
}

void printTime(struct Timer* t) {
    printf("%d\n", t->time1);
    putc('-', stdout);
    printf("\n%d\n\n", t->time2);
    printf("Time elapsed was ", (t->time2 - t->time1));
}

int main(void) {
    struct Timer t;
    struct tm time;
    stopwatch(&t);
    printTime(&t);
    printf("\n%d", time.tm_sec);
    return EXIT_SUCCESS;
}
Not sure what is wrong, getTime is not anything I know ??

if you want the seconds elapsed, at a low resolution, clock() is a pretty good pick. (its like what you have, start = clock(); now = (clock()-start)/(double)CLOCKS_PER_SEC;
if you want high resolution, I don't know what C uses today...

First, I would get a better compiler, or put more errors/warnings enabled, because I don't think your code should compile as you don't have a function prototype ("declaration?) for getTime().

I mean, you need to put
int getTime(void); before your stopwatch function definition.

Second, %d is meant for integers, but you are passing a double into printf.
%f should be the format for floating-point types.

Third, this isn't a correctly formed printf statement
printf("Time elapsed was ", (t->time2 - t->time1));
you probably want to add a %f somewhere in there as well.
Another problem is that time() doesn't return an int but a time_t. On my system an int is 4 bytes and a time_t is 8 bytes.
@jonnin
I added it as a function for a strange reason. Finding no purpose for it, it was removed

What do you mean by resolution?

@ganado
That seems to be a common theme here. How well does the one from codelite work?

That was my primary error. I need to remember command characters better...

Yeah, I forgot that one too.

Thanks for the help all
resolution... you can get the time in seconds, milliseconds, nanoseconds... this is resolution. I think the default clock() on most systems is more or less ms, but it varies. I think it also behaves a little differently across platforms.
I missed that YOU defined getTime, ... my mistake.




Seems like Codelite uses clang, which is a well-maintained compiler. I would google how to update your compiler in codelite.

Are you coding in C or C++? I believe C will actually treat a function without a prototype as by default returning int, which is what you have. So maybe C treats functions without prototypes/declarations differently than C++, so it might just be how it is.

Regardless, it's good practice to put prototypes.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <time.h>

void stop_watch()
{
    puts( "press enter to start" ) ;
    getchar() ;
    const time_t start = time(NULL) ;
    printf( "start at %s\n", ctime(&start) ) ;

    puts( "press enter to end" ) ;
    getchar() ;
    const time_t end = time(NULL) ;

    // use difftime to compute the elapsed time in seconds
    // http://en.cppreference.com/w/c/chrono/difftime
    printf( "  end at %s\nelapsed about %f seconds\n", ctime(&end), difftime( end, start ) ) ;
}

int main()
{
    stop_watch() ;
}
Topic archived. No new replies allowed.