如何将 int16_t 或 int32_t 与 scanf 等函数一起使用 [英] How to use int16_t or int32_t with functions like scanf
问题描述
我理解 C 中 int16_t
或 int32_t
的方式是它们在您的计算机上分别被定义为 16 位和 32 位数字.我相信当您需要保证数字是 16 位或 32 位时,您会使用这些,因为不同的系统并不总是将 int
表示为 32 位或将 short
表示为 16 位(这个假设正确吗?当我在网上查看时,我发现答案很复杂.
The way that I understand int16_t
or int32_t
in C is that they are typedefed to be 16 and 32 bit numbers respectively on your computer. I believe you would use these when you need to guarentee a number is 16 or 32 bits because different systems do not always represent an int
as 32 bits or a short
as 16 bits (Is this assumption correct? I find mixed answers when I look online.).
我的问题是我将如何使用 scanf
之类的函数从具有 int16_t
或 int32_t
或任何一个的用户那里获取输入当我要求它们实际上是 16 位或 32 位或其他类型时,其他类型定义的数字类型?是否有某种特殊的字符串修饰符?通常,如果我想从用户那里获得 int
而不关心它实际表示的大小,我会写这样的东西
My question is how would I use a function like scanf
to get input from a user with a int16_t
or a int32_t
or any of the other typedefed number types when I require them to actually be 16 bits or 32 bits or whatever? Is there some sort of special string modifier? Normally if I wanted to get an int
from a user without caring about how large it is actually represented as I would write something like this
scanf("%d", &int);
如果我传入一个 int32_t
这会起作用,但我认为这只是因为我系统上的 int
是 32 位并且它没有专门给我一个 32 位number 相反,它只是给了我一个 int
.我将如何获得保证为 32 位的数字?我查看了这个字符串修饰符的页面和其他一些地方,但没有找到提到这些 typedefed 数字类型.
This works if I pass in a int32_t
but I assume it is only because an int
on my system is 32 bits and it does not specifically give me a 32 bit number instead it just gives me an int
. How would I go about getting number that is guarenteed to be 32 bits? I have looked on this page of string modifiers and a few other places but have found no mention of these typedefed number types.
自从收到我的问题的答案后,我做了一些谷歌搜索,发现 这个.我也将其包含在下面以供参考.
Since receiving an answer to my question I have done some Googling and found this. I included it below for reference as well.
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
等
For those you need to use the macros from inttypes.h
such as SCNd64
or SCNu32
etc.
scanf("%" SCNd32, &x);
这篇关于如何将 int16_t 或 int32_t 与 scanf 等函数一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!