96

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

Dushant
  • 969
  • 1
  • 6
  • 3
  • 56
    Do sizeof(unsigned long long). – PrettyPrincessKitty FS Apr 29 '11 at 19:12
  • 2
    Related, sort of. http://stackoverflow.com/questions/4329777/is-long-guaranteed-to-be-at-least-32-bits – John Dibling Apr 29 '11 at 19:56
  • 2
    The C and C++ standards should adopt stuff with the number in them, to override the way the compiler automatically chooses. Something like float32, float64, int32, etc up to whatever limit we need, what about int256 or float1024 ?... this would really remove pains... – jokoon Apr 29 '11 at 20:45
  • 2
    @gokoon: as I explained in my answer, the C standard already has `int32_t`, `uint64_t`, etc. C++ will receive them with the upcoming standard. They're in the header ``. – Fred Foo Apr 30 '11 at 19:48
  • 1
    what about float and double ? – jokoon May 02 '11 at 12:25

4 Answers4

97

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).

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • 2
    @lars: `sizeof(char)` [is guaranteed to return exactly one](http://stackoverflow.com/questions/4329777/is-long-guaranteed-to-be-at-least-32-bits). It returns how big the operand is in number of bytes, not the number of "char units". Therefore, its not bytes "on most platforms" its bytes on every platform. – John Dibling Apr 29 '11 at 19:55
  • 2
    @John: that depends on the definition of *byte*. If you equate `char` and byte, you're right. If you equate `char` and *octet*, then it varies. – Fred Foo Apr 29 '11 at 19:58
  • 4
    @lars: No, it doesn't. A char is always exactly one byte on every platform. The Standard doesn't specify how many bits are in a byte, but that's a different thing. An implementation could use 64 bit bytes, which would result in `sizeof(long long) == 1` and that would be conformant. – John Dibling Apr 29 '11 at 20:01
  • 1
    @lars: I already upvoted you anyway, but here's a nitpick: "guaranteed by the C standard (ISO C99) to be at least 64 bits == 8 bytes" I can't imagine a non-embedded world in which 64 bits != 8 bytes, but the Standard doesn't guarantee it. – John Dibling Apr 29 '11 at 20:07
  • @John: right again. Removed the 64 bits == 8 bytes since I'm in language lawyer mode already. And I've heard there actually are embedded platforms where a byte is 16 bits. – Fred Foo Apr 29 '11 at 20:08
  • I know it's a common misconception. But the last Standard that was issued is from 2003. It wasn't merely a technical corrigendum. – Johannes Schaub - litb Apr 30 '11 at 22:02
19

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.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • 3
    While what you say is technically true, the standard requires that long long be *at least* 64 bits long, and I'm not aware of any implementation that defines any size other than 64-bit. That may change when 128-bit compilers come out, but for current compilers its not a stretch to assume 64 bits. – Boaz Yaniv Apr 29 '11 at 19:19
  • 3
    +1, countering the downvote. The answer depends on the meaning of 'is'. – Andy Finkenstadt Apr 29 '11 at 19:52
  • 3
    @Andy: Bill Clinton? Is that you? ;) – John Dibling Apr 29 '11 at 20:10
  • 7
    This is absolutely not "The beauty of C++", it is a complete pain :p – asimes Oct 18 '15 at 05:45
  • @MichaelBurr 8 years later, I'm still questioning what I'm doing with my life, trying to figure out how to get as close as possible to what Rust does with it's forced type sizes at least from perspective of programmer who wrote out the literals, at compile time. Still no such luck. Things like this are nothing but pure agony. –  Dec 05 '19 at 17:45
16

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.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 1
    "Recently ratified?" Last time I checked they were considering postponing it to 2012. – Fred Foo Apr 30 '11 at 00:24
  • 1
    @larsmans: ["... the ISO C++ committee approved the final technical changes to the C++0x standard. The new International Standard for Programming Language C++ is expected to be published in summer 2011."](http://herbsutter.com/2011/03/25/we-have-fdis-trip-report-march-2011-c-standards-meeting/) – In silico May 01 '11 at 00:04
  • 1
    @In silico: I read that. I also read somewhere about a possible delay, but I honestly can't remember where. – Fred Foo May 01 '11 at 00:07
  • 1
    @larsmans: Unless I see an official source about the FDIS being delayed (a search engine query for "C++0x FDIS delay" gives me no relevant hits), I fully expect the standard to be published in 2011, serious issues within the standard itself notwithstanding. – In silico May 01 '11 at 02:16
  • 1
    @In silico: I haven't found the reference either, so I guess it was either a rumor or a misunderstanding on my part. Still, I don't think FDIS status is the same as full ratification, it it? – Fred Foo May 01 '11 at 11:08
  • @larsmans: It's basically the final draft of the standard. It still needs to undergo an international ballot before it becomes the official C++0x standard, but the only thing that's really going to stop the ratification is if a serious issue comes up within the FDIS. I don't think that's likely. – In silico May 01 '11 at 12:52
  • Sorry for my mistake - I was over enthusiastic in describing FDIS as ratification. – Michael Burr May 01 '11 at 17:43
2

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;
}
Alessandro Jacopson
  • 18,047
  • 15
  • 98
  • 153