Difference between UINT32_C and uint32_t

北城余情 提交于 2020-01-14 08:04:17

问题


As far as I know the suffix t in uint32_t denote type name but I wonder to know what is the C in UINT32_C and what is the differences?


回答1:


UINT32_C is a macro which defines integer constant of type 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

  1. The macro INTN_C(value) shall expand to an integer constant expression corresponding to the type int_leastN_t. The macro UINTN_C(value) shall expand to an integer constant expression corresponding to the type uint_leastN_t. For example, if uint_least64_t is a name for the type unsigned long long int, then UINT64_C(0x123) might expand to the integer constant 0x123ULL.

It is thus possible that this constant is more than 32 bits on some rare systems.

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.

They all are defined in stdint.h, and have been part of the C standard since C99.




回答2:


UINT32_C is a macro for writing a constant of type uint_least32_t. Such a constant is suitable e.g. for initializing an uint32_t variable. I found for example the following definition in avr-libc (this is for the AVR target, just as an example):

#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. 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 e.g. for unsigned long. With other targets, it will be defined in a different way.




回答3:


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



回答4:


Don't know about keil, but at least in Linux UINT32_C is a macro to create a uint32_t literal.

And as mentioned by others, uint32_t is a type defined as of C99 in stdint.h.




回答5:


It is the macro appending the suffix creating the literal for example #define UINT32_C(c) c##UL



来源:https://stackoverflow.com/questions/45453583/difference-between-uint32-c-and-uint32-t

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