2

I try to define a 64 bits width integer using C language on Ubnutu 9.10. 9223372036854775808 is 2^23

long long max=9223372036854775808
long max=9223372036854775808

When I compile it, the compiler gave the warning message:

binary.c:79:19: warning: integer constant is so large that it is unsigned
binary.c: In function ‘bitReversal’:
binary.c:79: warning: this decimal constant is unsigned only in ISO C90
binary.c:79: warning: integer constant is too large for ‘long’ type

Is the long type 64 bits width?

Best Regards,

Yongwei Xing
  • 12,983
  • 24
  • 70
  • 90

4 Answers4

11
   long long max=9223372036854775808LL; // Note the LL
// long max=9223372036854775808L; // Note the L

A long long type is at least 64 bit, a long type is at least 32 bit. Actual width depends on the compiler and targeting platform.

Use int64_t and int32_t to ensure the 64-/32-bit integer can fit into the variable.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • if I use the int64_t, what header file I need include, or I just include the stdlib.h? – Yongwei Xing Feb 27 '10 at 15:49
  • You can find int64_t in 'sys/types.h' or 'stdint.h' – aks Feb 27 '10 at 15:51
  • I have another question,If I want to printf a int64_t variable, what parameter I should pass like %d – Yongwei Xing Feb 27 '10 at 15:59
  • No, not `%lld` if you use `int64_t`, %lld is for `long long`. You should use the defined constants of `stdint.h` which is in this case, `PRi64`. `printf("my var i64 is %" PRi64 "\n", i64);` – Patrick Schlüter Feb 27 '10 at 16:23
  • 8
    Not true. An integer constant which is too large for int is automatically promoted to long, and if it is too large for long it is promoted to long long. L and LL are only needed to force smaller values to a larger type, for example 9600 * 32 will overflow with 16-bit int, so you need 9600L * 32 instead. – Philip Potter Feb 27 '10 at 16:24
6

The problem you are having is that the number you're using (9223372036854775808) is 2**63, and the maximum value that your long long can hold (as a 64 bit signed 2s complement type) is one less than that - 2**63 - 1, or 9223372036854775807 (that's 63 binary 1s in a row).

Constants that are too large for long long (as in this case) are given type unsigned long long, which is why you get the warning integer constant is so large that it is unsigned.

The long type is at least 32 bits, and the long long type at least 64 bits (both including the sign bit).

caf
  • 233,326
  • 40
  • 323
  • 462
  • Yep or to use hex - max value of a signed 64-bit integer is 7FFF FFFF FFFF FFFF and OP's value is CCCC CCCC CCCC CCCC. As you say, it's too big to fit ( the compiler was even telling the OP that ) – zebrabox Feb 28 '10 at 11:55
  • The OP's value is actually `0x8000 0000 0000 0000`, but yes. – caf Feb 28 '10 at 12:06
  • Argh - Sorry yes. I hate cut and paste :( – zebrabox Feb 28 '10 at 12:43
1

I'm not sure it's will fix your problem (The LL solution looks good). But here is a recomandation :

You should use hexadecimal notation to write the maximal value of something.

unsigned char max = 0xFF;
unsigned short max = 0xFFFF;
uint32_t max = 0xFFFFFFFF;
uint64_t max = 0xFFFFFFFFFFFFFFFF;

As you can see it's readable.

To display the value :

printf("%lld\n", value_of_64b_int);

Nicolas Guillaume
  • 8,160
  • 6
  • 35
  • 44
0

I think it depends on what platform you use. If you need to know exactly what kind of integer type you use you should use types from Stdint.h if you have this include file on you system.