小编CK_*_*CK_的帖子

C文字后缀U,UL问题

有人可以向我解释如果我在ANSI C中忘记常量(文字)的后缀(后缀)会发生什么?

例如,我看到了位移操作这样的定义:

#define AAR_INTENSET_NOTRESOLVED_Pos (2UL) /*!< Position of NOTRESOLVED field. */
#define AAR_INTENSET_NOTRESOLVED_Msk (0x1UL << AAR_INTENSET_NOTRESOLVED_Pos) /*!< Bit mask of NOTRESOLVED field. */
#define AAR_INTENSET_NOTRESOLVED_Disabled (0UL) /*!< Interrupt disabled. */
#define AAR_INTENSET_NOTRESOLVED_Enabled (1UL) /*!< Interrupt enabled. */
#define AAR_INTENSET_NOTRESOLVED_Set (1UL) /*!< Enable interrupt on write. */
Run Code Online (Sandbox Code Playgroud)

它用于32位架构.但它可以移植到16位或8位.如果没有使用postfix UL会发生什么,我会将这些宏用于位移操作,因为它应该如此?

我只是假设,例如在8位架构中,(1 << 30)会导致溢出.

编辑:我找到了很好的链接:http://dystopiancode.blogspot.cz/2012/08/constant-suffixes-and-prefixes-in-ansi-c.html

但如果代码应该被移植到各种架构上,那么使用后缀是否安全?

例如,如果后缀U代表unisgned int,那么对于8位架构它通常是16位但是对于32位它是32位变量,所以0xFFFFAAAAU适用于32位编译器但不适用于8位编译器,对吧?

c literals

1
推荐指数
1
解决办法
5879
查看次数

标签 统计

c ×1

literals ×1