This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# "Long"s too short?

Justin Porter
Ranch Hand
Posts: 34
Hey, I'm trying to use a number like this:

long n = 109275027171421;

and I believe the max value allowed in a long is 9223372036854775807. Can someone confirm this? I keep getting this error when trying to compile:

integer number too large: 109275027171421

Any help would be appreciated!

marc weber
Sheriff
Posts: 11343
Yes, the max value for a 32-bit int is (2^31)-1 = 2147483647, and the max value for a 64-bit long is (2^63)-1 = 9223372036854775807.

However, a literal integral (whole number) value is implicitly of type int. So, you'll need to explicitly cast this value to type long...

long n = (long)109275027171421;
[ December 01, 2004: Message edited by: marc weber ]

Justin Porter
Ranch Hand
Posts: 34
Thanks for the suggestion, but it didn't fix the problem. Even when casting it like this:

long n = (long)109275027171421;

it still came up with the same error....

Mark Patrick
Ranch Hand
Posts: 51
Justin,

As Marc stated, by default literal numbers are of type int, however, the literal you have in your code '109275027171421' is too big to fit into an int. It has to be able to be able to fit into an int before it can be casted to long. However, you can append 'L' to the end of the literal to tell the compiler that this number should be treated as a long instead of an int. Then it will compile.

marc weber
Sheriff
Posts: 11343
Originally posted by Mark Patrick:
...you can append 'L' to the end of the literal to tell the compiler that this number should be treated as a long instead of an int...

Of course. The literal itself must be designated as a long. Casting won't work because you can't even get the int to cast from. (I must be getting tired. Sorry...)
[ December 01, 2004: Message edited by: marc weber ]

Justin Porter
Ranch Hand
Posts: 34
Many thanks! The second suggestion (appending L) worked!