在标准C声明固定大小的整数的typedef [英] Declaring fixed-size integer typedef in Standard C

查看:125
本文介绍了在标准C声明固定大小的整数的typedef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种可靠的方法来声明整数类型的固定8,16,32类型定义,和64位长度的ISO标准C?

Is there a reliable way to declare typedefs for integer types of fixed 8,16,32, and 64 bit length in ISO Standard C?

当我说的ISO标准C,我的意思是严格的:

When I say ISO Standard C, I mean that strictly:


  • ISO C89 / C90,C99没有。

  • 无ISO标准没有定义的头。

  • 无preprocessor符号ISO标准没有定义。

  • 在ISO标准中没有指定任何类型大小的假设。

  • 无专有供应商的符号。

我看到了计算器与此类似的其他问题,但没有解答不违反上述制约因素之一。我不知道这是可能的,而不诉诸平台符号。

I see other questions similar to this in StackOverflow, but no answers yet that do not violate one of the above constraints. I'm not sure it's possible without resorting to platform symbols.

推荐答案

即可。

头文件 limits.h中应该是C90的一部分。然后,我将测试通过preprocessor指令 SHRT_MAX INT_MAX LONG_MAX <值/ code>和 LLONG_MAX 并设置相应的typedef。

The header file limits.h should be part of C90. Then I would test through preprocessor directives values of SHRT_MAX, INT_MAX, LONG_MAX, and LLONG_MAX and set typedefs accordingly.

例如:

#include <limits.h>

#if SHRT_MAX == 2147483647
typedef unsigned short int uint32_t;
#elif INT_MAX == 2147483647
typedef unsigned int uint32_t;
#elif LONG_MAX == 2147483647
typedef unsigned long uint32_t ;
#elif LLONG_MAX == 2147483647
typedef unsigned long long uint32_t;
#else
#error "Cannot find 32bit integer."
#endif

这篇关于在标准C声明固定大小的整数的typedef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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