[英]Difference between UINT32_C and uint32_t
据我所知, uint32_t中的后缀t表示t ype名称,但我想知道UINT32_C中的C是什么 ,有什么区别?
UINT32_C
is a macro which defines integer constant of type uint_least32_t
. UINT32_C
是一个宏,它定义了uint_least32_t
类型的整数常量。 For example: 例如:
UINT32_C(123) // Might expand to 123UL on system where uint_least32_t is unsigned long
// or just 123U, if uint_least32_t is unsigned int.
7.20.4.1 Macros for minimum-width integer constants
7.20.4.1最小宽度整数常量的宏
- The macro INT N _C( value ) shall expand to an integer constant expression corresponding to the type int_least N _t .
宏INT N _C( 值 )将扩展为对应于int_least N _t类型的整数常量表达式。 The macro UINT N _C( value ) shall expand to an integer constant expression corresponding to the type uint_least N _t .
宏UINT N _C( 值 )将扩展为对应于类型uint_least N _t的整数常量表达式。 For example, if
uint_least64_t
is a name for the typeunsigned long long int
, thenUINT64_C(0x123)
might expand to the integer constant0x123ULL
.例如,如果
uint_least64_t
是unsigned long long int
类型的名称,则UINT64_C(0x123)
可能会扩展为整数常量0x123ULL
。
It is thus possible that this constant is more than 32 bits on some rare systems. 因此,在某些稀有系统上,该常数可能超过32位。
But if you are on a system where multiple of 8-bits 2's complement types are defined (most modern systems), and uint32_t
exists, then this creates 32-bit constant. 但是如果你所在的系统中定义了多个8位2的补码类型(大多数现代系统),并且存在
uint32_t
,则会产生32位常数。
They all are defined in stdint.h
, and have been part of the C standard since C99. 它们都在
stdint.h
中定义,并且自C99以来一直是C标准的一部分。
UINT32_C
is a macro for writing a constant of type uint_least32_t
. UINT32_C
是一个用于写入uint_least32_t
类型常量的uint_least32_t
。 Such a constant is suitable eg for initializing an uint32_t
variable. 这种常量适用于例如初始化
uint32_t
变量。 I found for example the following definition in avr-libc (this is for the AVR target, just as an example): 我在avr-libc中找到了以下定义(这是针对AVR目标,仅作为示例):
#define UINT32_C(value) __CONCAT(value, UL)
So, when you write 所以,当你写作
UINT32_C(25)
it's expanded to 它扩展到了
25UL
UL
is the suffix for an unsigned long
integer constant. UL
是unsigned long
整数常量的后缀。 The macro is useful because there is no standard suffix for uint32_t
, so you can use it without knowing that on your target, uint32_t
is a typedef
eg for unsigned long
. 该宏非常有用,因为
uint32_t
没有标准后缀,因此您可以在不知道目标的情况下使用它, uint32_t
是一个typedef
例如unsigned long
。 With other targets, it will be defined in a different way. 对于其他目标,它将以不同的方式定义。
These constants are defined something like this: 这些常量的定义如下:
#define UINT32_C(value) (value##UL)
You can only put constant values as macro argument, otherwise it wont compile. 您只能将常量值作为宏参数,否则不会编译。
UINT32_C(10); // compiles
uint32_t x = 10;
UINT32_C(x); // does not compile
Don't know about keil, but at least in Linux UINT32_C is a macro to create a uint32_t literal. 不知道keil,但至少在Linux中UINT32_C是一个创建uint32_t文字的宏。
And as mentioned by others, uint32_t is a type defined as of C99 in stdint.h. 正如其他人所提到的,uint32_t是一个在stdint.h中定义为C99的类型。
它是附加后缀的宏,用于创建文字,例如#define UINT32_C(c) c##UL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.