I'm trying to use long
for 12 digit number but it's saying "integer constant is too large for "long" type", and I tried it with C++ and Processing (similar to Java). What's happening and what should I use for it?
Asked
Active
Viewed 7,017 times
2

Evan Teran
- 87,561
- 32
- 179
- 238

Hikari Iwasaki
- 871
- 2
- 9
- 11
-
Can you provide a code sample so that we can see specific details? – Josh Peterson Jun 21 '11 at 00:03
-
@Blindy, Sorry, I thought it might be useful. Obviously it is unnecessary though. – Josh Peterson Jun 21 '11 at 00:14
-
will you be using this number for calculations? It's strange that you asking for a "12 digit number". Normally people are interested in a range and they specify if it is signed or not. If you are just dealing with a number like a credit card number or some phone number then it would be better to store as a string. – glowworms Jun 21 '11 at 00:30
-
I didn't think it was strange. But then, I've had to write code to interoperate with COBOL. – dan04 Jun 21 '11 at 01:37
-
well the number is 600851475143 and I have to find out the largest prime factor. – Hikari Iwasaki Jun 24 '11 at 01:27
4 Answers
5
In C and C++ (unlike in Java), the size of long
is implementation-defined. Sometimes it's 64 bits, sometimes it's 32. In the latter case, you only have enough room for 9 decimal digits.
To guarantee 64 bits, you can use either the long long
type, or a fixed-width type like int64_t
.

dan04
- 87,747
- 23
- 163
- 198
-
2+1 for specific-width types, like those achievable through `cstdint` if your platform has it, or [`boost/cstdint.hpp`](http://www.boost.org/doc/libs/1_36_0/libs/integer/cstdint.htm) if it doesn't. – Wyatt Anderson Jun 21 '11 at 00:13
-
Just FYI - on some compilers (e.g. older GCC), `cstdint` is missing but the platform may have C99's `stdint.h`.... – Tony Delroy Jun 21 '11 at 01:15
3
If you are specifying a literal constant, you must use the appropriate type specifier:
int i = 5;
unsigned i = 6U;
long int i = 12L;
unsigned long int i = 13UL;
long long int i = 143LL;
unsigned long long int i = 144ULL;
long double q = 0.33L;
wchar_t a = L'a';

Kerrek SB
- 464,522
- 92
- 875
- 1,084
-
I really think that in his case was the problem of a long int being 32bits – Vinicius Kamakura Jun 21 '11 at 01:26
-
@hexa: You're probably right. OP didn't say which platform/compiler/settings, I'm not sure which combinations of types and literal constants would cause which sort of warnings. – Kerrek SB Jun 21 '11 at 01:30
1
I don't know in C++, but in C, there is a header file called <stdint.h>
that will portably have the integer types with the number of bits you desire.
int8_t
int16_t
int32_t
int64_t
and their unsigned counterpart (uint8_t and etc).
Update: the header is called <cstdint>
in C++

Vinicius Kamakura
- 7,665
- 1
- 29
- 43