How many bytes is unsigned long long? - c++

How many bytes is unsigned long long?
Is it the same as unsigned long long int ?

Executive summary: it's 64 bits, or larger.
unsigned long long is the same as unsigned long long int. Its size is platform-dependent, but guaranteed by the C standard (ISO C99) to be at least 64 bits. There was no long long in C89, but apparently even MSVC supports it, so it's quite portable.
In the current C++ standard (issued in 2003), there is no long long, though many compilers support it as an extension. The upcoming C++0x standard will support it and its size will be the same as in C, so at least 64 bits.
You can get the exact size, in bytes (8 bits on typical platforms) with the expression sizeof(unsigned long long). If you want exactly 64 bits, use uint64_t, which is defined in the header <stdint.h> along with a bunch of related types (available in C99, C++11 and some current C++ compilers).

The beauty of C++, like C, is that the sized of these things are implementation-defined, so there's no correct answer without your specifying the compiler you're using. Are those two the same? Yes. "long long" is a synonym for "long long int", for any compiler that will accept both.

It must be at least 64 bits. Other than that it's implementation defined.
Strictly speaking, unsigned long long isn't standard in C++ until the C++0x standard. unsigned long long is a 'simple-type-specifier' for the type unsigned long long int (so they're synonyms).
The long long set of types is also in C99 and was a common extension to C++ compilers even before being standardized.

Use the operator sizeof, it will give you the size of a type expressed in byte. One byte is eight bits. See the following program:
#include <iostream>
int main(int,char**)
{
std::cout << "unsigned long long " << sizeof(unsigned long long) << "\n";
std::cout << "unsigned long long int " << sizeof(unsigned long long int) << "\n";
return 0;
}

Related

What is the difference between "long", "long long", "long int", and "long long int" in C++?

I am transitioning from Java to C++ and have some questions about the long data type. In Java, to hold an integer greater than 232, you would simply write long x;. However, in C++, it seems that long is both a data type and a modifier.
There seems to be several ways to use long:
long x;
long long x;
long int x;
long long int x;
Also, it seems there are things such as:
long double x;
and so on.
What is the difference between all of these various data types, and do they all have the same purpose?
long and long int are identical. So are long long and long long int. In both cases, the int is optional.
As to the difference between the two sets, the C++ standard mandates minimum ranges for each, and that long long is at least as wide as long.
The controlling parts of the standard (C++11, but this has been around for a long time) are, for one, 3.9.1 Fundamental types, section 2 (a later section gives similar rules for the unsigned integral types):
There are five standard signed integer types : signed char, short int, int, long int, and long long int. In this list, each type provides at least as much storage as those preceding it in the list.
There's also a table 9 in 7.1.6.2 Simple type specifiers, which shows the "mappings" of the specifiers to actual types (showing that the int is optional), a section of which is shown below:
Specifier(s) Type
------------- -------------
long long int long long int
long long long long int
long int long int
long long int
Note the distinction there between the specifier and the type. The specifier is how you tell the compiler what the type is but you can use different specifiers to end up at the same type.
Hence long on its own is neither a type nor a modifier as your question posits, it's simply a specifier for the long int type. Ditto for long long being a specifier for the long long int type.
Although the C++ standard itself doesn't specify the minimum ranges of integral types, it does cite C99, in 1.2 Normative references, as applying. Hence the minimal ranges as set out in C99 5.2.4.2.1 Sizes of integer types <limits.h> are applicable.
In terms of long double, that's actually a floating point value rather than an integer. Similarly to the integral types, it's required to have at least as much precision as a double and to provide a superset of values over that type (meaning at least those values, not necessarily more values).
Long and long int are at least 32 bits.
long long and long long int are at least 64 bits. You must be using a c99 compiler or better.
long doubles are a bit odd. Look them up on Wikipedia for details.
long is equivalent to long int, just as short is equivalent to short int. A long int is a signed integral type that is at least 32 bits, while a long long or long long int is a signed integral type is at least 64 bits.
This doesn't necessarily mean that a long long is wider than a long. Many platforms / ABIs use the LP64 model - where long (and pointers) are 64 bits wide. Win64 uses the LLP64, where long is still 32 bits, and long long (and pointers) are 64 bits wide.
There's a good summary of 64-bit data models here.
long double doesn't guarantee much other than it will be at least as wide as a double.
While in Java a long is always 64 bits, in C++ this depends on computer architecture and operating system. For example, a long is 64 bits on Linux and 32 bits on Windows (this was done to keep backwards-compatability, allowing 32-bit programs to compile on 64-bit Windows without any changes). long int is a synonym for long.
Later on, long long was introduced to mean "long (64 bits) on Windows for real this time". long long int is a synonym for this.
It is considered good C++ style to avoid short, int, long etc. and instead use:
std::int8_t # exactly 8 bits
std::int16_t # exactly 16 bits
std::int32_t # exactly 32 bits
std::int64_t # exactly 64 bits
std::size_t # can hold all possible object sizes, used for indexing
You can use these int*_t types by including the <cstdint> header. size_t is in <stdlib.h>.
This looks confusing because you are taking long as a datatype itself.
long is nothing but just the shorthand for long int when you are using it alone.
long is a modifier, you can use it with double also as long double.
long == long int.
Both of them take 4 bytes.
Historically, in early C times, when processors had 8 or 16 bit wordlength,intwas identical to todays short(16 bit). In a certain sense, int is a more abstract data type thanchar,short,longorlong long, as you cannot be sure about the bitwidth.
When definingint n;you could translate this with "give me the best compromise of bitwidth and speed on this machine for n". Maybe in the future you should expect compilers to translateintto be 64 bit. So when you want your variable to have 32 bits and not more, better use an explicitlongas data type.
[Edit: #include <stdint.h> seems to be the proper way to ensure bitwidths using the int##_t types, though it's not yet part of the standard.]
There is no deffirence, (long long x ) is equivalent to (long long int x ), but the second confirms that variable x is integer

Int types in C/C++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why are C++ int and long types both 4 bytes?
In C/C++, what is the difference between:
u_int64 myNum;
and:
unsigned long myNum;
As far as I can tell, both ate just unsigned integers, with 64bits of memory.
unsigned long does not have to be 64 bits, whereas uint64_t does. There is a kind of hierarchy of integer types where each type has to be at least large as the preceding type: signed char, short, int, long, long long, and similarly for their unsigned counterparts. There are some anchor points, stating that char is one byte (a byte does not have to be 8 bits, as far as I can recall short is at least 2 bytes 16 bits. In C++11, long long is at least 64 bits. But none of these types is exactly a given number of bits.
See fixed width integer types for more information (thanks to #chris for the link).
unsigned long is dependent on the machine like every int in C/C++. Many library especially libraries that allow for two machines to interact will type def most int like numbers to make sure both are the same size. u_int64 is basically type def to an unsigned integer of 64 bits, to allow for use on any machine. In theory unsigned long could be 128, 64, 256 or pretty much any size.
The C language itself doesn't specify how big an int is
long only has to be at least as long as int
long long only has to be at least as long as long
also u_int64_t is a c99 type, and wouldn't be available in ANSI c89.
also among 64 bit architectures that are differences LP64 indicates long's and pointers are 64 bit, LLP64 means that long long's and pointers are 64 bit.

What's the difference between long long and long

What's the difference between long long and long? And they both don't work with 12 digit numbers (600851475143), am I forgetting something?
#include <iostream>
using namespace std;
int main(){
long long a = 600851475143;
}
Going by the standard, all that's guaranteed is:
int must be at least 16 bits
long must be at least 32 bits
long long must be at least 64 bits
On major 32-bit platforms:
int is 32 bits
long is 32 bits as well
long long is 64 bits
On major 64-bit platforms:
int is 32 bits
long is either 32 or 64 bits
long long is 64 bits as well
If you need a specific integer size for a particular application, rather than trusting the compiler to pick the size you want, #include <stdint.h> (or <cstdint>) so you can use these types:
int8_t and uint8_t
int16_t and uint16_t
int32_t and uint32_t
int64_t and uint64_t
You may also be interested in #include <stddef.h> (or <cstddef>):
size_t
ptrdiff_t
long long does not exist in C++98/C++03, but does exist in C99 and c++0x.
long is guaranteed at least 32 bits.
long long is guaranteed at least 64 bits.
To elaborate on #ildjarn's comment:
And they both don't work with 12 digit numbers (600851475143), am I forgetting something?
The compiler looks at the literal value 600851475143 without considering the variable that you're assigning it to/initializing it with. You've written it as an int typed literal, and it won't fit in an int.
Use 600851475143LL to get a long long typed literal.
Your C++ compiler supports long long, that is guaranteed to be at least 64-bits in the C99 standard (that's a C standard, not a C++ standard). See Visual C++ header file to get the ranges on your system.
Recommendation
For new programs, it is recommended that one use only bool, char, int, and double, until circumstance arises that one of the other types is needed.
http://www.somacon.com/p111.php
Depends on your compiler.long long is 64 bits and should handle 12 digits.Looks like in your case it is just considering it long and hence not handling 12 digits.

What's the 'long' data type used for?

I've been programming in C++ for quite a while now and I am pretty familiar with most of the stuff. One thing that I've never understood though is the 'long' data type.
I googled it but I still don't know what it is for. I've found pages that say it is the same size and has the same range as an int. So what would be the point in using it?
I found another stack overflow question regarding this here:
Difference between long and int data types
And it seems that the only difference between the two is that sometimes the size is different on different systems. Does that mean that an application that uses long on a 64bit machine won't work on a 32bit machine? If so then wouldn't it be better to not use them at all?
Also I noticed stuff called "long int" or even "long long"! Is it a data type or a modifier?
It is compiler dependent. My standards-fu is a bit rusty but I believe it is defined as follows:
char <= short <= int <= long <= long long
where:
char >= 8 bits
short >= 16 bits
int >= 16 bits
long >= 32 bits
long long >= 64 bits
Which means that it is perfectly valid to have char = short = int = long = long long = 64bits and in fact compilers of some DSPs are designed that way.
This underscores the importance of actually reading your compiler documentation.
I noticed stuff called "long int" or even "long long"! Is it a data type or a modifier?
long int is the same as long (just as short int is the same as short).
long long is a distinct data type introduced by several compilers and adopted by C++0x.
Note that there is no such thing as long long long:
error: 'long long long' is too long for GCC
From one of the answers in the question you linked:
The long must be at least the same size as an int, and possibly, but not necessarily, longer.
I can't think of a better way to explain it.
long is guaranteed (at least) 32 bits. int is only guaranteed (at least) 16 bits. on 16- and 8-bit systems long provided range at a cost of efficiency.
cheers & hth.,
This is what the C++03 standard says (3.9.1/2) :
There are four signed integer types:
“signed char”, “short int”, “int”, and
“long int.” In this list, each type
provides at least as much storage as
those preceding it in the list.
So : sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
This is what the C++0x (3.9.1/2) and C99 (6.2.5/4) standards say :
There are five standard signed integer
types, designated as signed char,
short int, int, long int, and long
long int.
long is synonym of long int
long long doesn't exist in C++03, but will in C++0x.
I googled it but I still don't know
what its for. I've found pages that
say its the same size and has the same
range as an int. So what would be the
point in using it?
I've wondered the same thing. And concluded that long is now useless.
Prior to the rise of 64-bit systems, the de facto standard for C integer types was:
char = (u)int8_t (Note that C predates Unicode.)
short = int16_t
int = intptr_t [until 64-bit], int_fast16_t
long = int32_t [until 64-bit], intmax_t [until 1999]
long long = int64_t or intmax_t
Today, however, long has no consistent semantics.
If you want an integer that is guaranteed to be 32 bit or 64 bit, there are such types, e.g. int64_t. If you really want to ensure your int are of such a size, use these types instead of long, long long, etc. You'll need to include cstdint for these (stdint.h in C).

Does GCC support long long int?

Does GCC support:
long long int
which would be a 64-bit integer?
Also, is long long int part of the standard?
Yes GCC does support long long int, which is a part of C99 standard.
The standard does not mandate its size in bits, but required values of LLONG_MIN and LLONG_MAX in <limits.h> imply it's at least 64-bit (exact 64-bit wide integer types are int64_t/uint64_t from <stdint.h>).
LLONG_MIN must be at most -9223372036854775807
LLONG_MAX must be at least 9223372036854775807
long long int is a part of the C99 standard, and I know GCC supports it. And now I can prove it.
On my 32-bit machine,
int main()
{
printf("%d\n", sizeof(long long int));
return 0;
}
compiled with gcc prints 8 (8 bytes * 8 bits/byte = 64 bits).
Yes, long long is part of C99, as well as long long constants (10222333444555LL) and a few support elements. (LLONG_MAX, llrint(d), llround(d), some others.) And gcc has implemented it for some time now.
In order to print long long int variables:
long long int lli = 100000000;
printf("%lld\n", lli);
long longs are well supported, and have been for a long long time [sorry]. As I understand it, this should have been 128 bit on 64-bit platforms, but for compatibility/portability reasons in GCC, has standardised on a 64-bit width.
See also: (u)int128_t, and this discussion on GCC's 128-bit integer support
I believe that usually an unsigned long long is the traditional representation of a 64-bit integer. I'm assuming long long int would work too, but I've never personally seen any 64-bit vars declared that way before.