How to use int16_t or int32_t with functions like scanf [duplicate]

…衆ロ難τιáo~ 提交于 2019-12-17 19:32:54

问题


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.).

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);

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.

Edit: 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

回答1:


For those you need to use the macros from inttypes.h such as SCNd64 or SCNu32 etc.

scanf("%" SCNd32, &x);


来源:https://stackoverflow.com/questions/26618443/how-to-use-int16-t-or-int32-t-with-functions-like-scanf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!