什么是显式声明L或UL对于长值的原因 [英] what is the reason for explicitly declaring L or UL for long values

查看:282
本文介绍了什么是显式声明L或UL对于长值的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从为例

unsigned long x = 12345678UL

我们一直了解到,编译器需要看到在上面的例子中只有长,设置4字节(32位)的存储器。问题是,为什么要我们甚至宣称它是一个长期的使用后,L / UL长常量。

We have always learnt that the compiler needs to see only "long" in the above example to set 4 bytes (in 32 bit) of memory. The question is why is should we use L/UL in long constants even after declaring it to be a long.

推荐答案

在一个后缀 UL 是不使用时,编译器使用,它可以包含常数从列表中的第一个类型(参见C99标准的细节,6.4.4条:5为十进制常量,列表是 INT 长整型得到long long int )。

When a suffix L or UL is not used, the compiler uses the first type that can contain the constant from a list (see details in C99 standard, clause 6.4.4:5. For a decimal constant, the list is int, long int, long long int).

其结果是,大部分的时间,这是没有必要使用的后缀。它不改变程序的含义。它不会改变的你的榜样初始化的意义X 对于大多数的架构,尽管它会,如果你选择了一个数字,无法重新presented为长长。另见codebauer的回答为例,其中后缀的 U 部分是必要的。

As a consequence, most of the times, it is not necessary to use the suffix. It does not change the meaning of the program. It does not change the meaning of your example initialization of x for most architectures, although it would if you had chosen a number that could not be represented as a long long. See also codebauer's answer for an example where the U part of the suffix is necessary.

有一对夫妇的情况下,当程序员可能要明确设置常量的类型。一个例子使用的是可变参数函数的时候是:

There are a couple of circumstances when the programmer may want to set the type of the constant explicitly. One example is when using a variadic function:

printf("%lld", 1LL); // correct
printf("%lld", 1);   // undefined behavior


要使用一个后缀的常见原因是保证运算结果不会溢出。两个例子是:


A common reason to use a suffix is ensuring that the result of a computation doesn't overflow. Two examples are:

long x = 10000L * 4096L;
unsigned long long y = 1ULL << 36;

在这两个例子中,没有后缀的常量将有键入 INT 和计算将被制成 INT 。在每一个示例中,这会带来溢出的风险。使用后缀意味着计算将在较大型代替,其具有足够的范围为结果来进行。

In both examples, without suffixes, the constants would have type int and the computation would be made as int. In each example this incurs a risk of overflow. Using the suffixes means that the computation will be done in a larger type instead, which has sufficient range for the result.

由于亮度种族在轨道所说的那样,在litteral的后缀来的之前的分配。在上面的两个例子,只是声明 X 无符号长长是不够的,prevent在分配给他们的前pressions的计算溢出。

As Lightness Races in Orbit puts it, the litteral's suffix comes before the assignment. In the two examples above, simply declaring x as long and y as unsigned long long is not enough to prevent the overflow in the computation of the expressions assigned to them.

另一个例子是比较 X&LT; 12U 其中变量 X 的类型为 INT 。如果没有 U 后缀,编译器类型不变 12 INT ,比较有符号整数的比较。

Another example is the comparison x < 12U where variable x has type int. Without the U suffix, the compiler types the constant 12 as an int, and the comparison is therefore a comparison of signed ints.

int x = -3;
printf("%d\n", x < 12); // prints 1 because it's true that -3 < 12

随着 U 为后缀的比较就变得无符号整数的比较。 通常的算术转换意味着-3转化为一个大的无符号整型:

With the U suffix, the comparison becomes a comparison of unsigned ints. "Usual arithmetic conversions" mean that -3 is converted to a large unsigned int:

printf("%d\n", x < 12U); // prints 0 because (unsigned int)-3 is large

事实上,一个常量的类型甚至可能因为道路通常的算术转换工作变动的算术运算的结果,再次。

In fact, the type of a constant may even change the result of an arithmetic computation, again because of the way "usual arithmetic conversions" work.

需要注意的是,为十进制的常量,通过C99建议类型列表中不包含无符号长长。在C90,名单与当时最大的标准化的无符号整型结束(这是无符号长)。一个后果是,一些项目的意义是通过将标准型<$ ​​C $ C>长长来C99改变:这是类型为 unsigned long类型相同的常在C90现在可以输入一个签署长长来代替。我相信这就是为什么在C99,它决定不具有无符号长长在类型的常数列表中的原因。
请参见并的this~~V 博客文章中的例子。

Note that, for decimal constants, the list of types suggested by C99 does not contain unsigned long long. In C90, the list ended with the largest standardized unsigned integer type at the time (which was unsigned long). A consequence was that the meaning of some programs was changed by adding the standard type long long to C99: the same constant that was typed as unsigned long in C90 could now be typed as a signed long long instead. I believe this is the reason why in C99, it was decided not to have unsigned long long in the list of types for decimal constants. See this and this blog posts for an example.

这篇关于什么是显式声明L或UL对于长值的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆