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()

Share Improve this question edited 2 days ago Jonathan Leffler 753k145 gold badges946 silver badges1.3k bronze badges asked 2 days ago LexLex 3351 silver badge9 bronze badges 10
  • Are you sure that the called difftime is returning int and not only be presented as int because your (non-identical) format specifier forces that? Also, calling a function without having seen the prototype makes the compiler assume - and assume int. So in short, what makes you think that the return value type is different, when your experimental code has more than one way of causing that impression, even if not. (Please do not see this as cheeky phrasing, I realise that these things could have slipped you, let me know and I turn it into an answer. Otherwise please elaborate your reasons.) – Yunnosch Commented 2 days ago
  • Yes, I am sure. I have post two pics. The result of these two example also testify it. I can Go to definition of double difftime() and open time.h. But I cant open the int difftime() source file in vs2022. – Lex Commented 2 days ago
  • 1 Thanks for the edit. It seems to confirm my suspicion, that without the include the IDE and the compiler are guessing and defaulting (to int-returning and unknown paramer list). Do you observe anything that contradicts this interpretation? – Yunnosch Commented 2 days ago
  • My interpretation would probably mean that there should be some kind of "implicitly declared function called" warning, either when compiling with -Wall or from the IDE. ... I just realise it does. – Yunnosch Commented 2 days ago
  • Vs2022 provide its own version of int difftime(). However, I can not find any post to testify it. difftime, _difftime32, _difftime64 – Lex Commented 2 days ago
 |  Show 5 more comments

1 Answer 1

Reset to default 3

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.

本文标签: visual studiodifftime() return different data type in c on vs2022Stack Overflow