tags:

views:

212

answers:

4

Hi all

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,

+9  A: 
   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
if I use the int64_t, what header file I need include, or I just include the stdlib.h?
Yongwei Xing
@Yongwei Xing: stdint.h
tur1ng
You can find int64_t in 'sys/types.h' or 'stdint.h'
muteW
I have another question,If I want to printf a int64_t variable, what parameter I should pass like %d
Yongwei Xing
Use %ld, not %d.
tur1ng
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);`
tristopia
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
A: 

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.

char max = 0xFF;
short max = 0xFFFF;
int32_t max = 0xFFFFFFFF;
int64_t max = 0xFFFFFFFFFFFFFFFF;

As you can see it's readable.

To display the value :

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

Niklaos
I have another question,If I want to printf a int64_t variable, what parameter I should pass like %d
Yongwei Xing
%lld or some macro defined in inttypes.h
el.pescado
%llu for unsigned if I recall correctly
Ronny
@ronnybrendel if it's an unsigned long long int ;)
Niklaos
A: 

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.

skwllsp
+1  A: 

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

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

caf
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
The OP's value is actually `0x8000 0000 0000 0000`, but yes.
caf
Argh - Sorry yes. I hate cut and paste :(
zebrabox