【问题标题】:Simple C data types [duplicate]简单的 C 数据类型 [重复]
【发布时间】:2012-12-08 14:03:52
【问题描述】:

可能重复:
The difference of int8_t, int_least8_t and int_fast8_t?

我很困惑。我认为...(如果我错了,请纠正我)

u_int8_t = unsigned short ?
u_int16_t = unsigned int ?
u_int32_t = unsigned long ?
u_int64_t = unsigned long long ?

int8_t = short ?
int16_t = int ?
int32_t = long ?
int64_t = long long ?

那么int_fast8_t 是什么意思呢? int_fastN_t? int_least8_t?

【问题讨论】:

标签: c types primitive-types


【解决方案1】:

这些都在 C99 标准中指定(第 7.18 节)。

[u]intN_t 类型是泛型类型(在 ISO C 标准中),其中N 表示位宽(8、16 等)。 8 位不一定是 shortchar,因为 shorts/ints/longs/etc 被定义为具有 最小 范围(不是位宽)并且可能不是二进制补码。

这些较新的类型二进制补码,无论更常见类型的编码如何,可能是二进制补码或符号/大小(请参阅Representation of negative numbers in C?Why is SCHAR_MIN defined as -127 in C99?)。

fastleast 正是它们听起来的样子,快速最小宽度类型和至少给定宽度的类型。

该标准还详细说明了哪些类型是必需的,哪些是可选的。

【讨论】:

  • 在Linux系统中,我没有找到int_fastN_t。为什么这样?我使用/usr/include$ grep 'int_fastN_t' * -R 搜索。
  • @Grijesh,没有blahN_t 类型,N 是标准中 8、16 等的占位符。而是搜索 int_fast8_t - 它们应该都在一起(并从 inttypes.h 引用)。
  • 是的,int_fast8_t 存在,但在stdint.h...在我的系统声明中在coda.h! - 感谢 paxdiablo :) 投票给你
  • 对不起,我的错,stdint 保存类型,inttypes 保存printf/scanf 的格式说明符。
  • 是的,我看到了..但我得到了答案..谢谢 :)
【解决方案2】:

我写 int 是 16 位的:

u_int8_t = unsigned char  
u_int16_t = unsigned int
u_int32_t = unsigned long int
u_int64_t = unsigned long long int 

int8_t =  char
int16_t = int 
int32_t = long int
int64_t = long long int   

问:“那么 int_fast8_t 是什么意思?int_fastN_t?int_least8_t?”

正如his answer here中的dan04所说:

假设你有一个用于 36 位系统的 C 编译器,char = 9 位,short = 18 位,int = 36 位,long = 72 位。那么

  • int8_t 不存在,因为没有办法满足 恰好 8 个没有填充的值位的约束。
  • int_least8_tchar 的类型定义。不是 shortint,因为标准要求 smallest 类型至少有 8 个 位。
  • int_fast8_t 可以是任何东西。如果“本机”大小被认为是“快速”,则它可能是 int 的 typedef。

如果您在Linux 中,大多数这些都在/usr/include/linux/coda.h 中定义。例如

#ifndef __BIT_TYPES_DEFINED__
#define __BIT_TYPES_DEFINED__
typedef signed char       int8_t;
typedef unsigned char       u_int8_t;
typedef short            int16_t;
typedef unsigned short     u_int16_t;
typedef int          int32_t;
typedef unsigned int       u_int32_t;
#endif  

还有

#if defined(DJGPP) || defined(__CYGWIN32__)
#ifdef KERNEL
typedef unsigned long u_long;
typedef unsigned int u_int;
typedef unsigned short u_short;
typedef u_long ino_t;
typedef u_long dev_t;
typedef void * caddr_t;
#ifdef DOS
typedef unsigned __int64 u_quad_t;
#else 
typedef unsigned long long u_quad_t;
#endif

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-10
    • 2012-02-09
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    相关资源
    最近更新 更多