admin管理员组文章数量:1125307
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]
double difftime()
int difftime()
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]
double difftime()
int difftime()
1 Answer
Reset to default 3In 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.
本文标签: visual studiodifftime() return different data type in c on vs2022Stack Overflow
版权声明:本文标题:visual studio - difftime() return different data type in c on vs2022 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736657316a1946288.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Go to definition
ofdouble difftime()
and opentime.h
. But I cant open theint difftime()
source file in vs2022. – Lex Commented 2 days ago-Wall
or from the IDE. ... I just realise it does. – Yunnosch Commented 2 days agoint difftime()
. However, I can not find any post to testify it.difftime
,_difftime32
,_difftime64
– Lex Commented 2 days ago