我有一些代码是在 Windows 和 Linux 上构建的。此时的 Linux 始终是 32 位,但 Windows 是 32 位和 64 位。 Windows 希望 time_t 是 64 位的,而 Linux 仍然是 32 位的。我对此很好,除了在某些地方 time_t 值被转换为字符串。所以当 time_T 是 32 位时,它应该用 %d 来完成,而当它是 64 位时,它应该用 %lld 来完成……这样做的聪明方法是什么?另外:关于如何找到将 time_t 传递给 printf 样式函数的所有位置以解决此问题的任何想法?
编辑: 我想到了将 TT_FMT 声明为“%d”或“%lld”,然后将我的 printfs 更改为 printf("time: %d, register: blah") 变成 printf("time: "TT_FMT ", register: blah") 有没有更好的办法?我如何找到它们?
最佳答案
根据C标准,time_t
是算术类型,“能够表示时间”。因此,例如,它可以是 double
。 (Posix mentions this more explicitly ,并且还保证 time()
返回自纪元以来的秒数——C 标准不保证后者。)
也许最干净的解决方案是将值转换为您想要的任何类型。您可能需要 unsigned long long
或 unsigned long
之一:
printf("%llu\n", (unsigned long long)t);
关于c - 处理 64/32 位 time_t 的便携方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2467418/