tags:

views:

138

answers:

4

Hi, when I'm trying to compile my c program it gives me this error warning: integer constant is too large for 'long' type

which refers to these lines

int barcode, a, b, c;
scanf("%d", &barcode);
a = barcode / 1000000000000;
b = barcode / 100000000000 % 10;
c = barcode / 10000000000 % 10;

and the rest is fine. I know I'm not supposed to use int for such a large number, any suggestions on what I should use? if I replace int with double what should the '%d' part be replaced with then?

A: 

You should replace your int with long int, and prefix the constants with L

Like this:

long long unsigned int barcode;
barcode = 100000000LL;
printf("Barcode is %li", barcode);
Henri
So it would be:long int barcode, a, b, c;scanf("%il", a = barcode / 1000000000000L;b = barcode / 100000000000L % 10;c = barcode / 10000000000L % 10;right?13 digits is pretty big, what about long long instead?long long barcode, a, b, c; what do I replace %d with?
dydx
nope, still no luck, i've added the L at the end of a = barcode / 1000000000000L;and it still gives me integer constant is too large for 'long' type
dydx
Im sorry my bad, i forgot that in c/c++ long int has the same size as int. So it should be long long int. Ill update my post
Henri
The printf in your example will work, because the literal value you've given it is within the constraints of a 32-bit integer, but if the value stored in the long long exceeds MAX_INT, you will need to specify %lld to the printf specifier to make it output correctly on most compilers.
Dan Story
@Dan Story, you're right!
Henri
+1  A: 
tommieb75
Not `long` enough on many 32-bit targets.
KennyTM
+4  A: 

Use long longs instead of ints for integer values of that size, with the LL literal suffix:

long long barcode, a, b, c;
scanf("%lld", &barcode);
a = barcode / 1000000000000LL;
b = barcode / 100000000000LL % 10;
c = barcode / 10000000000LL % 10;
Dan Story
shouldn't it bescanf("%lld", since barcodes are 13 digits?
dydx
Good catch. Fixed.
Dan Story
A: 

(if you like an answer, perhaps you could select one?)

Also, you might consider placing the barcode in a string instead. This way you can access the first 3 digits that you need more easily (if that is what you roughly want - note: you do want c to be the first two digits after the first digit, correct?)

char barcode[14];
int a, b, c;
scanf("%s", &barcode);
a = (int) (barcode[0] - '0');
b = (int) (barcode[1] - '0');
c = b * 10 + (int)(barcode[2] - '0');
philcolbourn