unsigned long和unsigned long long之间的区别是什么? [英] What is the difference between unsigned long and unsigned long long?
问题描述
我预计尺寸会有所不同。但两者都显示为8bytes。
#include< iostream>
using namespace std;
int main()
{
cout<<Size of long:<<< sizeof(unsigned long)<<\\\
cout<<Long Long长度:<< sizeof(unsigned long long)<<\\\
;
}
输出:
长度的大小:8
长长的尺寸:8
它们是两种不同的类型,即使它们在某些特定实现中具有相同的大小和表示。
unsigned long
必须至少为32位。 unsigned long long
必须至少为64位。 (实际上,要求以它们可以表示的值的范围来表示。)
正如你所看到的,这与它们都是相同的大小是一致的,只要该大小至少为64位。
在大多数情况下,它们是不同类型的事实并不重要(除非你不能取决于它们都具有相同的值范围)。例如,您可以将 unsigned long long
分配给 unsigned long
对象,该值将被隐式转换,可能有一些信息丢失。类似地,您可以将 unsigned long long
参数传递给期望使用 unsigned long
的函数> variadic ,例如 printf
;然后需要显式转换)。
它的事情是当你有指针。类型 unsigned long *
和 unsigned long long *
不仅仅是不同的,它们不是赋值兼容的,因为没有从一个到另一个的隐式转换。例如,此程序:
int main()
{
unsigned long * ulp = 0;
unsigned long long * ullp = 0;
ulp = ullp; //非法
}
在使用g ++编译时会产生以下结果:
c.cpp:在函数'int main()':
c.cpp:5:11:错误:在赋值语句中的long long unsigned int *'to'long unsigned int *'
C ++标准在2011年之前没有添加 long long
和 unsigned long long
类型。 ,并且对于C ++前编译器(和C99前)编译器提供它们作为扩展名并不罕见。
I expected that the size will be different. But both are showing 8bytes.
#include <iostream>
using namespace std;
int main()
{
cout<<"Size of long:"<<sizeof(unsigned long)<<"\n";
cout<<"Size of Long Long:"<< sizeof(unsigned long long)<<"\n";
}
Output:
Size of long:8
Size of Long Long:8
They're two distinct types, even if they happen to have the same size and representation in some particular implementation.
unsigned long
is required to be at least 32 bits. unsigned long long
is required to be at least 64 bits. (Actually the requirements are stated in terms of the ranges of values they can represent.)
As you've seen, this is consistent with them both being the same size, as long as that size is at least 64 bits.
In most cases, the fact that they're distinct types doesn't matter much (except that you can't depend on them both having the same range of values). For example, you can assign an unsigned long long
to an unsigned long
object, and the value will be converted implicitly, possibly with some loss of information. Similarly, you can pass an unsigned long long
argument to a function expecting an unsigned long
(unless the function is variadic, like printf
; then an explicit conversion is needed).
But one case where it does matter is when you have pointers. The types unsigned long*
and unsigned long long*
are not just distinct, they're not assignment-compatible, because there is no implicit conversion from one to the other. For example, this program:
int main()
{
unsigned long* ulp = 0;
unsigned long long* ullp = 0;
ulp = ullp; // illegal
}
produces the following when I compile it with g++:
c.cpp: In function ‘int main()’:
c.cpp:5:11: error: cannot convert ‘long long unsigned int*’ to ‘long unsigned int*’ in assignment
One more difference: the C++ standard didn't add the long long
and unsigned long long
types until 2011. C added them with the 1999 standard, and it's not uncommon for pre-C++2011 (and pre-C99) compilers to provide them as an extension.
这篇关于unsigned long和unsigned long long之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!