格式说明符的实现定义的类型,如time_t的 [英] Format specifiers for implementation-defined types like time_t

查看:216
本文介绍了格式说明符的实现定义的类型,如time_t的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的code更与平台/实现无关。我不知道什么是 time_t的将作为平台上实现了code被编译的时候。我怎么知道的牛逼的类型来决定使用何种格式说明?

  ...
time_t的T =时间(NULL);
的printf(%S,T);
...


解决方案

一般情况下,显示的 time_t的是其组件分解到一个值的方式结构TM 使用 gmtime的本地时间键,显示这些或将它们转换根据需要使用的strftime 的ctime 直接从 time_t的以显示当地时间的字符串。

如果你想看到的原始值对于一些目的,C标准规定, time_t的终止的真正的,这意味着它是整数或浮点(C 2011(N1570)6.2.5 17)。因此,你应该能够将其转换为双击并打印。有一些可能性,即 time_t的可以再那个 present值双不能,所以你可能要警惕如果你想利用对异国情调的实现服务。由于 difftime 返回为双击 time_t的对象的差异>,似乎C不真正支持 time_t的更precision比双击

I want to make my code more platform-/implementation-independent. I don't know what a time_t will be implemented as on the platform when the code is being compiled. How do I know the type of t to determine what format specifier to use?

...
time_t t = time(NULL);
printf("%s", t);
...

解决方案

Generally, the way to display the value of a time_t is to break down its components to a struct tm using gmtime or localtime and display those or convert them as desired with strftime, or ctime to go directly from time_t to a string showing local time.

If you want to see the raw value for some purpose, the C standard specifies that time_t is real, which means it is integer or floating-point (C 2011 (N1570) 6.2.5 17). Therefore, you should be able to convert it to double and print that. There is some possibility that time_t can represent values that double cannot, so you might have to guard against that if you want to take care regarding exotic implementations. Since difftime returns the difference of two time_t objects as a double, it seems C does not truly support time_t with more precision than a double.

这篇关于格式说明符的实现定义的类型,如time_t的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆