The difference between UINT32_C and uint32_t

As far as I know, the suffix t in uint32_t denotes the name t ype, but I am interested to know what C is in UINT32_C and what are the differences?

+5
source share
5 answers

UINT32_C is a macro that defines an integer constant of type uint_least32_t . For instance:

 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 integer constants of minimum width

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

Thus, it is possible that this constant is more than 32 bits on some rare systems.

But if you are on a system where several 8 types of bits 2 are defined (most modern systems) and uint32_t exists, then this creates a 32-bit constant.

They are all defined in stdint.h and were part of the C standard with C99.

+9
source

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

 #define UINT32_C(value) __CONCAT(value, UL) 

So when you write

 UINT32_C(25) 

he expanded to

 25UL 

UL is the suffix for the 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 uint32_t has a typedef for your purpose, for example. for unsigned long . With other objects, it will be defined differently.

+5
source

These constants are defined something like this:

 #define UINT32_C(value) (value##UL) 

You can only put constant values ​​as an argument to a macro, otherwise it will not compile.

 UINT32_C(10); // compiles uint32_t x = 10; UINT32_C(x); // does not compile 
+3
source

I don't know about keil, but at least in Linux, UINT32_C is a macro for creating the uint32_t literal.

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

+2
source

This is a macro adding a suffix creating a literal, for example #define UINT32_C(c) c##UL

-1
source

Source: https://habr.com/ru/post/1270474/


All Articles