How to read data into time_t variable using scanf ()?

This code gives me warnings:

$ cat test.c
#include<stdio.h>
#include<time.h>

int main() {

    time_t t;
    scanf("%lld", &t);
    printf("%lld\n", t);
    return 0;
}
$ gcc test.c -o test
test.c: In function β€˜main’:
test.c:7: warning: format β€˜%lld’ expects type β€˜long long int *’, but argument 2 has type β€˜time_t *’
test.c:8: warning: format β€˜%lld’ expects type β€˜long long int’, but argument 2 has type β€˜time_t’
$ 

In addition to warnings, the code works as expected.

What should I do to not get compilation warnings (without a compiler?)

+5
source share
4 answers

The exact type time_tdepends on your platform and OS. It is still quite often 32 bits (either int, or long), rather than 64, and some even use float. The right thing is to read an integer of a known size (either intor long long), and then assign the value time_tas the second step.

+8
source

time_t .

gcc test.c -o test --save-temps

grep time_t test.i|grep typedef

, , time_t "long int", "% ld".

+3

-, : , time_t 64- . 32- , .

, , time_t long long , , :

time_t t;
scanf("%lld", (long long *) &t);
printf("%lld\n", (long long) t);
+2

integer scanf(), time_t:

#include <stdio.h>
#include <time.h>

int main()
{
    long long llv;
    time_t t;
    scanf("%lld", &llv);
    t = llv;
    printf("%lld\n", (long long)t);
    return 0;
}

, , , t , .

URL ( POSIX) scanf(), , time_t. , time_t scanf() , . , time_t, , .

+1

All Articles