c语言类型探索:unsigned long
0x00 缘起
目前在做mib相关的工作,oid节点的类型是counter64,支持私有mib的类型使用的是unsigned long,在考虑是不是够用,组内给出的结论是我们的平台上unsigned long和unsigned long long是相同位宽,即都为8字节。感觉自己基本功不扎实,故才有了这次探索
0x01 测试
事实胜于雄辩,我还是在设备上做下测试吧。结果显示的却是如此,unsigned long和unsigned long long是相同位宽,即都为8字节。
printf("unsigned long size = %ld\n", sizeof(unsigned long);
printf("unsigned long size = %ld\n", sizeof(unsigned long long);
0x02 查看规范
§5.2.4.2.1节给出了相关范围说明。
5.2.4.2.1 Sizes of integer types <limits.h>
The values given below shall be replaced by constant expressions suitable for use in #if preprocessing directives. Moreover, except for CHAR_BIT and MB_LEN_MAX, the
following shall be replaced by expressions that have the same type as would an
expression that is an object of the corresponding type converted according to the integer promoti

本文讲述了作者在进行MIB工作时,对C语言中unsigned long类型的位宽产生疑问,通过测试和查阅规范发现,在其工作平台上unsigned long和unsigned long long都是8字节。通过man limits.h进一步确认了这一情况,并引发了对64位编程模型的思考。


被折叠的 条评论
为什么被折叠?



