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
An unsigned version of the long long data type. An unsigned long long occupies 8 bytes of memory; it stores an integer from 0 to 2^64-1, which is approximately 1.8×10^19 (18 quintillion, or 18 billion billion). A synonym for the unsigned long long type is uint64 .
Unsigned int is only guaranteed to be able to hold the numbers between 0 and 65535 (inclusive), while unsigned long int is guaranteed to be able to hold the numbers between 0 and 4 294 967 295. Those are just the minimums, though.
9. What is the difference between unsigned int length() and unsigned int size()? Explanation: Both of them will return the length of strings in the same notations. 10.
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.
It is implementation defined as iammilind pointed see How many bytes is unsigned long long? for more details
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With