UINT32_C 和 uint32_t 之间的区别

Difference between UINT32_C and uint32_t

据我所知tuint32_t中表示t输入名称,但我想知道 UINT32_C 中的 C 是什么,有什么区别?

不知道 keil,但至少在 Linux UINT32_C 是一个创建 uint32_t 文字的宏。

正如其他人所提到的,uint32_t 是在 stdint.h 中定义为 C99 的类型。

UINT32_C 是一个宏,它定义了 uint_least32_t 类型的整型常量。例如:

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.

因此在一些罕见的系统上这个常量可能超过 32 位。

但是,如果您使用的系统定义了多个 8 位 2 的补码类型(大多数现代系统),并且 uint32_t 存在,那么这将创建 32 位常量。

它们都在 stdint.h 中定义,并且自 C99 以来一直是 C 标准的一部分。

是创建字面量后缀的宏,例如#define UINT32_C(c) c##UL

UINT32_C是写uint_least32_t类型常量的宏。这样的常数是合适的,例如用于初始化 uint32_t 变量。例如,我在 avr-libc 中找到了以下定义(这是针对 AVR 目标的,仅作为示例):

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

所以,当你写

UINT32_C(25)

扩展到

25UL

ULunsigned long 整数常量的后缀。该宏很有用,因为 uint32_t 没有标准后缀,因此您可以在不知道目标上 uint32_ttypedef 的情况下使用它,例如unsigned long。对于其他目标,它将以不同的方式定义。

这些常量的定义如下:

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

只能将常量值作为宏参数,否则无法编译。

UINT32_C(10); // compiles

uint32_t x = 10;
UINT32_C(x); // does not compile