如何使用像scanf这样的函数使用int16_t或int32_t

时间:2014-10-28 21:03:53

标签: c user-input scanf

我在C中理解int16_tint32_t的方式是在计算机上将它们分别命名为16位和32位数。我相信当你需要保证数字为16或32位时你会使用这些,因为不同的系统并不总是将int表示为32位或short表示为16位(这个假设是否正确?当我在网上看时,我找到了不同的答案。)。

我的问题是,当我需要时,如何使用scanf这样的函数从int16_tint32_t或任何其他类型定义的数字类型的用户那里获取输入实际上是16位或32位或其他什么?有某种特殊的字符串修饰符吗?通常情况下,如果我想从用户那里得到一个int而不关心实际表示的大小,我会写这样的东西

scanf("%d", &int);

如果我传入int32_t,这是有效的,但我认为这只是因为我的系统上的int是32位而且它没有特别给我32位数而是它只给了我int。我如何获得保证为32位的数字?我查看了this page of string modifiers和其他一些地方,但没有发现这些类型定义的数字类型。

修改:自从收到我的问题的答案后,我做了一些谷歌搜索并找到了this。我将它包括在下面以供参考。

uppercase hexadecimal printf format for uintptr_t

#define SCNd16   "d"
decimal scanf format for int16_t

#define SCNd32   "ld"
decimal scanf format for int32_t

#define SCNd8   "hhd"
decimal scanf format for int8_t

#define SCNdFAST16   "d"
decimal scanf format for int_fast16_t

#define SCNdFAST32   "ld"
decimal scanf format for int_fast32_t

#define SCNdFAST8   "hhd"
decimal scanf format for int_fast8_t

#define SCNdLEAST16   "d"
decimal scanf format for int_least16_t

#define SCNdLEAST32   "ld"
decimal scanf format for int_least32_t

#define SCNdLEAST8   "hhd"
decimal scanf format for int_least8_t

#define SCNdPTR   SCNd16
decimal scanf format for intptr_t

#define SCNi16   "i"
generic-integer scanf format for int16_t

#define SCNi32   "li"
generic-integer scanf format for int32_t

#define SCNi8   "hhi"
generic-integer scanf format for int8_t

#define SCNiFAST16   "i"
generic-integer scanf format for int_fast16_t

#define SCNiFAST32   "li"
generic-integer scanf format for int_fast32_t

#define SCNiFAST8   "hhi"
generic-integer scanf format for int_fast8_t

#define SCNiLEAST16   "i"
generic-integer scanf format for int_least16_t

#define SCNiLEAST32   "li"
generic-integer scanf format for int_least32_t

#define SCNiLEAST8   "hhi"
generic-integer scanf format for int_least8_t

#define SCNiPTR   SCNi16
generic-integer scanf format for intptr_t

#define SCNo16   "o"
octal scanf format for uint16_t

#define SCNo32   "lo"
octal scanf format for uint32_t

#define SCNo8   "hho"
octal scanf format for uint8_t

#define SCNoFAST16   "o"
octal scanf format for uint_fast16_t

#define SCNoFAST32   "lo"
octal scanf format for uint_fast32_t

#define SCNoFAST8   "hho"
octal scanf format for uint_fast8_t

#define SCNoLEAST16   "o"
octal scanf format for uint_least16_t

#define SCNoLEAST32   "lo"
octal scanf format for uint_least32_t

#define SCNoLEAST8   "hho"
octal scanf format for uint_least8_t

#define SCNoPTR   SCNo16
octal scanf format for uintptr_t

#define SCNu16   "u"
decimal scanf format for uint16_t

#define SCNu32   "lu"
decimal scanf format for uint32_t

#define SCNu8   "hhu"
decimal scanf format for uint8_t

#define SCNuFAST16   "u"
decimal scanf format for uint_fast16_t

#define SCNuFAST32   "lu"
decimal scanf format for uint_fast32_t

#define SCNuFAST8   "hhu"
decimal scanf format for uint_fast8_t

#define SCNuLEAST16   "u"
decimal scanf format for uint_least16_t

#define SCNuLEAST32   "lu"
decimal scanf format for uint_least32_t

#define SCNuLEAST8   "hhu"
decimal scanf format for uint_least8_t

#define SCNuPTR   SCNu16
decimal scanf format for uintptr_t

#define SCNx16   "x"
hexadecimal scanf format for uint16_t

#define SCNx32   "lx"
hexadecimal scanf format for uint32_t

#define SCNx8   "hhx"
hexadecimal scanf format for uint8_t

#define SCNxFAST16   "x"
hexadecimal scanf format for uint_fast16_t

#define SCNxFAST32   "lx"
hexadecimal scanf format for uint_fast32_t

#define SCNxFAST8   "hhx"
hexadecimal scanf format for uint_fast8_t

#define SCNxLEAST16   "x"
hexadecimal scanf format for uint_least16_t

#define SCNxLEAST32   "lx"
hexadecimal scanf format for uint_least32_t

#define SCNxLEAST8   "hhx"
hexadecimal scanf format for uint_least8_t

#define SCNxPTR   SCNx16
hexadecimal scanf format for uintptr_t

1 个答案:

答案 0 :(得分:17)

对于那些您需要使用inttypes.h中的宏,例如SCNd64SCNu32等。

scanf("%" SCNd32, &x);