如何使用int16_t或int32_t与scanf

如何使用int16_t或int32_t与scanf,第1张

概述参见英文答案 > printf format specifiers for uint32_t and size_t                                    4个 我在C中了解int16_t或int32_t的方式是在计算机上分别键入16位和32位数字.我相信你会使用这些,当你需要保证一个数字是16或32位,因为不同的系统并不总是表示一个int 32位或短16位(这是假设 参见英文答案 > 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   SCNd16decimal 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   SCNi16generic-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   SCNo16octal 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   SCNu16decimal 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   SCNx16hexadecimal scanf format for uintptr_t
解决方法 对于那些您需要使用inttypes.h宏,如SCNd64或SCNu32等.
scanf("%" SCNd32,&x);
总结

以上是内存溢出为你收集整理的如何使用int16_t或int32_t与scanf全部内容,希望文章能够帮你解决如何使用int16_t或int32_t与scanf所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1246536.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存