简体   繁体   中英

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 . 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 INT N _C( value ) shall expand to an integer constant expression corresponding to the type 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 . 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.

UINT32_C is a macro for writing a constant of type uint_least32_t . Such a constant is suitable eg 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 eg for 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.

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

它是附加后缀的宏,用于创建文字,例如#define UINT32_C(c) c##UL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM