Reputation: 1063
I have a simple timestamp issue. I am trying to get a timestamp using gettimeofday
and it all works as it should when I print the value to the terminal however when I assign it to a variable it does not. I can not figure out what im doing wrong. The code and output looks as follows:
#include <stdio.h>
#include <sys/time.h>
#include <windows.h>
int main(int argc, char const *argv[])
{
struct timeval t_struct;
while(1){
gettimeofday(&t_struct, NULL);
printf("Print only: %f \n", (float) t_struct.tv_sec + (float) t_struct.tv_usec/1000000);
float timestamp = (float) t_struct.tv_sec + (float) t_struct.tv_usec/1000000;
printf("Assigned : %f \n", timestamp);
Sleep(100);
}
return 0;
}
Output:
Print only: 1598259371.284617
Assigned : 1598259328.000000
Print only: 1598259371.385117
Assigned : 1598259328.000000
Print only: 1598259371.485873
Assigned : 1598259328.000000
Note that I am used to programming in Python and not C, I am aware that the issue might be trivial.
Upvotes: 1
Views: 237
Reputation: 1063
The solution was to change the variable type to double
instead of float
and I also changed the division to be /1000000.0
to avoid integer division.
Upvotes: 1