ulong foo = 0;
ulong bar = 0UL;//this seems redundant and unnecessary. but I see it a lot.我在引用数组的第一个元素时也看到了这一点
blah = arr[0UL];//this seems silly since I don't expect the compiler to magically
//turn '0' into a signed value有人能提供一些见解,为什么我需要“UL”从头到尾明确指出这是一个无符号的长整型?
发布于 2009-08-13 18:26:37
void f(unsigned int x)
{
//
}
void f(int x)
{
//
}
...
f(3); // f(int x)
f(3u); // f(unsigned int x)它只是C++中的另一个工具;如果您不需要它,就不要使用它!
发布于 2009-08-13 18:34:36
在您提供的示例中,它是不需要的。但后缀通常用在表达式中,以防止精度损失。例如:
unsigned long x = 5UL * ...如果您没有使用UL后缀,您可能会得到不同的结果,例如,如果您的系统具有16位整数和32位长整型。
这里是另一个例子,灵感来自Richard Corden的评论:
unsigned long x = 1UL << 17;同样,如果您有16位或32位整数,如果您去掉后缀,您将得到不同的结果。
同样类型的问题也适用于32位与64位整数,以及在表达式中混合long和long long。
发布于 2009-08-13 18:26:46
我想有些编译器可能会发出警告。
作者这样做可能是为了确保代码没有警告?
https://stackoverflow.com/questions/1273687
复制相似问题