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)
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.
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.