The code you show fails to initialize mytime
. In C, an object defined inside a function without static
is not initialized by default to zero (or any other value). Its value is indeterminate, and using it generally results in undefined behavior.
In this case, the code behaved as if mytime.tmp
had a small non-zero value, such as 1e-9. When formatted with %f
, small values are formatted as “0.000000”. And, since the value is not zero, time==0
returns false. (Changing %f
to %g
would likely reveal a small non-zero value.)
Since the code fails to initialize mytime
, the value in mytime.tmp
could also have been very large, or the program could have crashed.
Also, the code as posted fails to compile and has a number of issues:
- The
typedef
definition of MyTime
must be terminated with a semicolon.
- The code should contain
#include <stdio.h>
to declare print
.
main
should be declared int main(void)
or int main(int argc, char *argv[])
.