I have stumbled over an interesting problem in C. I have to implement a timer function in my organization in Simulink (which is a modeling software). However, since it does not have timer function, I am using a custom block in simulink to write the C code
In the code below time_t is of long data type, which I have to convert to float to be able to use it in Simulink. However, when I convert long to float, the seconds do not update as frequenty when using the float variable, and the result also looks different
I tried to search over the internet regarding to how to typecast long to float in "C", and I found out that one has to just add (float) infront of the variable that needs to be typecasted
Can anyone help provide suggestions on what could be going wrong, and why the two print statements give different results and how can I fix it
Please take note that I have never programmed in "C" (this is my first code), so it would really help me if the explanations are simple
#include <stdio.h>
#include <time.h>
int main () {
time_t seconds;
float seconds_f;
seconds = time(NULL);
seconds_f = (float)seconds;
printf("Hours since January 1, 1970 = %f\n", seconds_f);
printf("Hours since January 1, 1970 = %.ld\n", seconds);
return(0);
}
Result snippet
$gcc -o main *.c
$main
Hours since January 1, 1970 = 1625470848.000000
Hours since January 1, 1970 = 1625470904