如何使用int16_t或int32_t与scanf

前端之家收集整理的这篇文章主要介绍了如何使用int16_t或int32_t与scanf前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > printf format specifiers for uint32_t and size_t4个
我在C中了解int16_t或int32_t的方式是在计算机上分别键入16位和32位数字.我相信你会使用这些,当你需要保证一个数字是16或32位,因为不同的系统并不总是表示一个int 32位或短16位(这是假设正确吗? ).

我的问题是,当我要求它们实际上是16位或32位或者什么时候,我将如何使用像scanf这样的函数从具有int16_t或int32_t的用户或任何其他类型定义的数字类型中获取输入?是否有某种特殊的字符串修饰符?通常,如果我想从用户那里得到一个int,而不用担心实际表现的大小,我会写这样的东西

scanf("%d",&int);

如果我传入一个int32_t,这可以工作,但是我认为这只是因为我的系统上的一个int是32位,并没有给我一个32位的数字,而只是给我一个int.如何获得32位的数字呢?我已经看过this page of string modifiers和其他几个地方,但没有发现没有提到这些typedefed数字类型.

编辑:自从收到我的问题的答案后,我已经做了一些谷歌搜索,发现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

解决方法

对于那些您需要使用inttypes.h宏,如SCNd64或SCNu32等.
scanf("%" SCNd32,&x);
原文链接:https://www.f2er.com/c/114922.html

猜你在找的C&C++相关文章