I found a weird thing in my program. Here are two examples
#include <stdio.h>
int main() {
time_t t1, t2;
t1 = 1735689614;
t2 = 1735689600;
printf("difftime =%d \n", difftime(t1, t2)); // difftime() return int
}
#include <stdio.h>
#include <time.h>
int main() {
time_t t1, t2;
t1 = 1735689614.0;
t2 = 1735689600.0;
printf("difftime =%f \n", difftime(t1, t2)); // difftime() return double
}
you may notice that #include<time.h>
and the return value of difftime()
The difftime()
of two examples are different functions.
I know the double difftime()
is the standard function of time.h (C11)
. But where the int difftime()
come from?
It is a very weird and subtle problem when c programming in vs2022. I have change the c standard to C11
in vs2022. And The program compiled OK. I thought it would use the double difftime()
without #inlcude<time.h>
.
While this problem can be clearly seen when using gcc
implicit declaration of function 'difftime' [-Wimplicit-function-declaration]
In the first version, you don't include <time.h>
, and therefore difftime
is implicitly declared as returning int
whereas it actually returns a double
.
BTW the compiler warns you with this message: warning C4013: 'difftime' undefined; assuming extern returning int
.
So basically the first version of your code is broken.
Consider all warnings containing the words "undefined" and "assuming" as errors.