2

Possible Duplicate:
What primitive data type is time_t?

Basically, all I want to do is produce the current UNIX time (result from time(NULL)) and print it to a file I have open.

I've tried code such as:

fprintf(f, "%i", time(NULL));

But I get these annoying compiler warnings:

src/database-createtbl.c:140: warning: int format, time_t arg (arg 3)

I'm trying to compile using -Wall - this really shouldn't be an issue but it's driving me nuts.

Community
  • 1
  • 1
Jonathon
  • 778
  • 8
  • 21

1 Answers1

5

Simply cast to a known type:

printf("%ld", (long)time(NULL));

The type returned by time depends on the OS (it's aliased to time_t, but you don't know if this is long or int or something else), so you need to cast it to a known type before passing it to printf, in order for the format to match the argument type.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • 1
    This is the most portable (to pre-C99), but using `long long` or `intmax_t` (or the unsigned versions thereof) and the corresponding C99 format specifiers may be safer, e.g. on a hypothetical post-Y2038 32-bit machine where `long` is 32 bits but `time_t` is 64 bits. – R.. GitHub STOP HELPING ICE Jul 24 '11 at 02:40