What's the difference between long long and long

Hikari Iwasaki picture Hikari Iwasaki · Jun 24, 2011 · Viewed 34.4k times · Source

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;
}

Answer

Joey Adams picture Joey Adams · Jun 24, 2011

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