Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

difftime() return different data type in c on vs2022

I found a wired 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I know the double difftime() is the standard function of time.h (C11). But where the int difftime() come from?

It is a very wired 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()
enter image description here
int difftime()
enter image description here

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading