hello, i’m new to programming in i’m trying to solve this exercise in C, basically it’s the amount of passed hours between the start of a game and it’s end, if the game started at 16 and ended at 2 the result is a game with 10 hours(in different days) i know i can to it more manually, but i wanted to somehow use the <time.h> to learn how to use a header etc, can someone help me?, thank you all

    • Vilian@lemmy.caOP
      link
      fedilink
      arrow-up
      1
      ·
      2 years ago

      tom scott descends into madness lmao, but was just for a simple exercise, i just wanted to complicate it to learn a little more :b

  • 🇨🇦 tunetardis@lemmy.ca
    link
    fedilink
    arrow-up
    2
    ·
    2 years ago

    The thing is to take 2 time stamps: one at the beginning and one at the end of the game. Then you subtract the beginning value from the end to get the elapsed time.

    In terms of <time.h>, you probably want to call clock() to get these time stamps.

    clock_t beginTime = clock();
    

    And later:

    clock_t endTime = clock();
    double elapsed = (double)(endTime - beginTime) / CLOCKS_PER_SEC;
    

    That would give you the time in seconds, which you could make into hours by dividing it further by 3600.

    (Note: It’s been a long time since I’ve programmed straight C so I may be off on something here? In C++, you would want to use std::chrono::steady_clock::now() to get your time stamps, even though clock() still exists.)