ulong foo = 0;
ulong bar = 0UL;//this seems redundant and unnecessary. but I see it a lot.
Run Code Online (Sandbox Code Playgroud)
我在引用数组的第一个元素时也看到了这个数量
blah = arr[0UL];//this seems silly since I don't expect the compiler to magically
//turn '0' into a signed value
Run Code Online (Sandbox Code Playgroud)
有人可以提供一些见解,为什么我需要'UL'来明确指出这是一个无符号长?
Ara*_*raK 24
void f(unsigned int x)
{
//
}
void f(int x)
{
//
}
...
f(3); // f(int x)
f(3u); // f(unsigned int x)
Run Code Online (Sandbox Code Playgroud)
它只是C++中的另一个工具; 如果你不需要它不要使用它!
Bri*_*eal 13
在您提供的示例中,不需要它.但是后缀通常用在表达式中以防止精度损失.例如:
unsigned long x = 5UL * ...
Run Code Online (Sandbox Code Playgroud)
如果你没有留下UL后缀,你可能得到一个不同的答案,比如说你的系统有16位整数和32位长.
这是Richard Corden的评论启发的另一个例子:
unsigned long x = 1UL << 17;
Run Code Online (Sandbox Code Playgroud)
同样,如果您将后缀保留为16或32位整数,则会得到不同的答案.
相同类型的问题将适用于32对64位整数以及混合长长表达式.
对不起,我意识到这是一个相当古老的问题,但我在c ++ 11代码中使用了很多...
ul,d,f都是用于初始化有用的auto变量您预期的类型,例如
auto my_u_long = 0ul;
auto my_float = 0f;
auto my_double = 0d;
Run Code Online (Sandbox Code Playgroud)
查看数字文字的cpp参考:http://www.cplusplus.com/doc/tutorial/constants/