|
|
|
Show HN: I always wanted to learn C and make some app in āCā
|
|
1 points
by thomasdd
1512 days ago
|
|
I always wanted to learn "C" but never found an real-world application of what I cound make. On last Sunday, I needed to measure time of some tasks. So I wrote an terminal "Stopwatch" app.
It's my first application in C programming language, probably it is stupid and not efficient. If you mind, let me know, so I can learn more about C in real world. https://github.com/ggtd/cstopwatch |
|
If you want to try to sleep for the exact right amount of time, this is a great opportunity to learn about some more system calls because that's something you often come in contact with when programming in C. Here is an example of how it can be done on Linux...
Before the loop, get the current time in nanosecond precision by using `clock_gettime(CLOCK_REALTIME, &t)`, where t is a `struct timespec` that needs to be declared before the call. Then in the loop (in the end), increment t.tv_sec by one and sleep until that new absolute time by calling `clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &t, NULL)`.
Another thing you can try is to print the exact time at sub-second precision when the user presses ctrl+c. Here you can use clock_gettime again to get the exact time, and to catch the ctrl+c you need to use the `signal(SIGINT, your_ctrl_c_handler_function)` system call.