c - 处理 64/32 位 time_t 的便携方式

标签 c portability 32bit-64bit

我有一些代码是在 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 longunsigned long 之一:

printf("%llu\n", (unsigned long long)t);

关于c - 处理 64/32 位 time_t 的便携方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2467418/

相关文章:

C# 可移植应用程序与 .NET

windows - 开始 64 位 Windows 应用程序开发

android - 如何使 Android 的 aapt 和 adb 在没有 ia32-libs 的 64 位 Ubuntu 上工作(适用于版本 12、13 和 14)

c - 将数组传递给 scanf 函数和变量

c - 相同的代码,不同 C 编译器中 printf() 和 write() 的打印顺序不同

c - 你将如何在 C 中声明一个二维指针数组?

c - C 中的可移植线程安全?

c - SQLite3 : non-ascii unicode chars' update not working?

c++ - 如何检测难以捉摸的 64 位可移植性问题?

c# - .net c# 异常处理在 64 位模式下失败