Search code examples
clong-integerc99ctimelong-long

C - How to actually use a long long datatype with the ctime function


I tried asking this before but I forgot to include a question (since I just joined the site), so I didn't actually have my problem solved, people just told me why the second bit of code was wrong instead of how to make it work. This is part of a homework assignment. The purpose of which is to eventually put INT_MAX+1 into ctime to prove the point that in a 32bit machine the date cannot pass ~2038 because it runs out of bits for the number of seconds since 1970. My question simply is how can I put a long long into ctime? How can I make the second bit of code work?

All is compiled in C99, if that matters.

Works:

    long x = INT_MAX-1;
    printf("Time: %s",ctime(&x));

Doesn't Work:

    long long x = INT_MAX+1;
    printf("Time: %s",ctime(&x));

Error:

incompatible pointer type: "Expected 'const time_t *' but argument is of type 'long long int *'"

Solution

  • You can't. You are on a system that uses a 32 bit time_t value. Passing a 64 bit long long will gives you an error because it doesn't work.