c - 数字后的后缀 L 是否总是与在数字前放置带有 "long"的括号具有相同的效果?

标签 c type-conversion

例如,0xffffffffL

假设 int 是 32 位,0xffffffffL(long)0xffffffff 一样吗?也就是说,0xffffffffL的类型是长的吗?

由于 0xffffffff 的类型是 unsigned int,我认为 0xffffffffLunsigned long 而不是 signed long,虽然我不确定。

这是否取决于 long 是 32 位还是 64 位?

最佳答案

引用自 C99 标准草案 N1256(强调我的):

6.4.4.1 Integer constants

[...]

Description

An integer constant begins with a digit, but has no period or exponent part. It may have a prefix that specifies its base and a suffix that specifies its type.

[...]

Semantics

The type of an integer constant is the first of the corresponding list in which its value can be represented.

[...]


Suffix             Decimal Constant          Octal or Hexadecimal constant
--------------------------------------------------------------------------
none               int                       int
                   long int                  unsigned int
                   long long int             long int
                                             unsigned long int
                                             long long int
                                             unsigned long long int
--------------------------------------------------------------------------
[...]
--------------------------------------------------------------------------
l or L             long int                  long int
                   long long int             unsigned long int
                                             long long int
                                             unsigned long long int

[...]

If an integer constant cannot be represented by any type in its list, it may have an extended integer type, if the extended integer type can represent its value. If all of the types in the list for the constant are signed, the extended integer type shall be signed. If all of the types in the list for the constant are unsigned, the extended integer type shall be unsigned. If the list contains both signed and unsigned types, the extended integer type may be signed or unsigned. If an integer constant cannot be represented by any type in its list and has no extended integer type, then the integer constant has no type.

因此,根据上面的引用:

  • 0xffffffffL 等常量的类型或大小都不是由后缀唯一标识的。它还取决于 long 的实际位宽,这是实现定义的(最少 32 位)。

  • 0xffffffff 不保证是 unsigned intunsigned int 的最小保证位宽为 16,因此如果您在 int 为 16 位的平台上,0xffffffff 很可能是无符号长0xffffffff 也可以是一个 int(如评论中所指出的),即它甚至不能保证是无符号的,如果 int 类型可以用于表示它(例如,int 为 64 位的平台)。

  • 在表达式前添加括号类型是一种类型转换操作,它强制将常量转换为括号中指示的类型。因此,转换 (long)0xffffffff 的结果将具有 long 类型,但该值取决于多种因素,如标准所述:

6.3 Conversions

[...]

6.3.1.3 Signed and unsigned integers

When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.

Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.

关于c - 数字后的后缀 L 是否总是与在数字前放置带有 "long"的括号具有相同的效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18799830/

相关文章:

c - Winsock 接受超时

c - 在字符串中搜索子字符串

c++ - 用 C 或 C++ 为 Android 编写应用程序?

python - 如何将具有字符串值(如 '[title:item][' title2 :item]'. ..etc)的列拆分为带有 pandas 的字典

java - 如何将字符串转换为双[][]

c - 无法理解的VC++6编译错误C2664

c - 不显示计算结果的函数

php - 动态构造 INSERT 语句后类型转换失败?

c# - 将 DataRow 值转换为强类型值

xml - 在 Powershell 中将 xml 转换为 csv 文件