timeval end_time;
timeval Dbg_timer;
The above time structures where initialized with gettimeofday() at some point.
Below is how I calculated the time difference:
long int elaspsed_time_s = end_time.tv_sec - Dbg_timer.tv_sec;
printf("elaspsed_time_s =%d -> end_time_tv_sec=%d Dbg_timer_tv_sec=%d \n",
elaspsed_time_s, end_time.tv_sec, Dbg_timer.tv_sec);
The Output:
elaspsed_time_s =3 -> end_time_tv_sec=1631446699 Dbg_timer_tv_sec=1631446699
How is the above possible? What is really going on?
%dis not a proper conversion to use for thelong intvalueelaspsed_time_s. (By the way, the correct spelling is “elapsed”.) Use%ldforelaspsed_time_s.%dis also not a proper conversion to use for thetime_tvalues in thetv_secmember. On POSIX systems,time_tis an integer type, but it is not necessarily anint, as far as I know. To print atime_tvalue, you might convert it to along intand print it with%ld:Alternatively, you could include
<stdint.h>and<inttypes.h>and print it viauintmax_t: