Paul Kar.
Paul Kar.

Reputation: 1324

Time subtraction in C

I would like to subtract current_time with previously_saved_time and check if it's bigger then wait_time. wait time should be an int. Can someone give me a simple example? In other words:

if ((current_time - previously_saved_time) > wait_time) {
    do something;
}

Upvotes: 1

Views: 10182

Answers (2)

Rohit Vipin Mathews
Rohit Vipin Mathews

Reputation: 11787

check out time.h you will find the needed function to get current system time.

Or the Wiki Page: http://en.wikipedia.org/wiki/System_time

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881223

What you have is exactly what you need, other than replacing do something with some actual code.

For example, this program waits three seconds:

#include <time.h>

int main(void) {
    time_t base = time (0);
    time_t now = base;
    while (now - base < 3)
        now = time (0);
    return 0;
}

It simply loops in the while statement until the difference between the base time and the current time is three or more (there are almost certainly better ways to wait for N seconds, this is just meant to illustrate the calculation).

Upvotes: 4

Related Questions